Skip to content

Instantly share code, notes, and snippets.

@starius
Created June 27, 2017 00:21
Show Gist options
  • Select an option

  • Save starius/89b53e5cc252523e35566ea45fe7bfde to your computer and use it in GitHub Desktop.

Select an option

Save starius/89b53e5cc252523e35566ea45fe7bfde to your computer and use it in GitHub Desktop.
diff --git a/modules/host/contractmanager/sector.go b/modules/host/contractmanager/sector.go
index f8ba8ba..cdfb532 100644
--- a/modules/host/contractmanager/sector.go
+++ b/modules/host/contractmanager/sector.go
@@ -63,17 +63,22 @@ type (
}
)
-// readSector will read the sector in the file, starting from the provided
-// location.
-func readSector(f file, sectorIndex uint32) ([]byte, error) {
- b := make([]byte, modules.SectorSize)
- _, err := f.ReadAt(b, int64(uint64(sectorIndex)*modules.SectorSize))
+// readSectorPart will read the part of sector in the file.
+func readSectorPart(f file, sectorIndex, offset, length uint32) ([]byte, error) {
+ b := make([]byte, length)
+ _, err := f.ReadAt(b, int64(uint64(sectorIndex)*modules.SectorSize+uint64(offset)))
if err != nil {
return nil, build.ExtendErr("unable to read within storage folder", err)
}
return b, nil
}
+// readSector will read the sector in the file, starting from the provided
+// location.
+func readSector(f file, sectorIndex uint32) ([]byte, error) {
+ return readSectorPart(f, sectorIndex, 0, uint32(modules.SectorSize))
+}
+
// readFullMetadata will read a full sector metadata file into memory.
func readFullMetadata(f file, numSectors int) ([]byte, error) {
sectorLookupBytes := make([]byte, numSectors*sectorMetadataDiskSize)
@@ -123,9 +128,8 @@ func (cm *ContractManager) managedSectorID(sectorRoot crypto.Hash) (id sectorID)
return id
}
-// ReadSector will read a sector from the storage manager, returning the bytes
-// that match the input sector root.
-func (cm *ContractManager) ReadSector(root crypto.Hash) ([]byte, error) {
+// ReadSectorPart will read a part of a sector from the storage manager.
+func (cm *ContractManager) ReadSectorPart(root crypto.Hash, offset, length uint32) ([]byte, error) {
err := cm.tg.Add()
if err != nil {
return nil, err
@@ -153,7 +157,7 @@ func (cm *ContractManager) ReadSector(root crypto.Hash) ([]byte, error) {
}
// Read the sector.
- sectorData, err := readSector(sf.sectorFile, sl.index)
+ sectorData, err := readSectorPart(sf.sectorFile, sl.index, offset, length)
if err != nil {
atomic.AddUint64(&sf.atomicFailedReads, 1)
return nil, build.ExtendErr("unable to fetch sector", err)
@@ -162,6 +166,12 @@ func (cm *ContractManager) ReadSector(root crypto.Hash) ([]byte, error) {
return sectorData, nil
}
+// ReadSector will read a sector from the storage manager, returning the bytes
+// that match the input sector root.
+func (cm *ContractManager) ReadSector(root crypto.Hash) ([]byte, error) {
+ return cm.ReadSectorPart(root, 0, uint32(modules.SectorSize))
+}
+
// managedLockSector grabs a sector lock.
func (wal *writeAheadLog) managedLockSector(id sectorID) {
wal.mu.Lock()
diff --git a/modules/host/negotiatedownload.go b/modules/host/negotiatedownload.go
index c5c0e4e..3a1c6a6 100644
--- a/modules/host/negotiatedownload.go
+++ b/modules/host/negotiatedownload.go
@@ -89,11 +89,11 @@ func (h *Host) managedDownloadIteration(conn net.Conn, so *storageObligation) er
// Load the sectors and build the data payload.
for _, request := range requests {
- sectorData, err := h.ReadSector(request.MerkleRoot)
+ sectorData, err := h.ReadSectorPart(request.MerkleRoot, uint32(request.Offset), uint32(request.Length))
if err != nil {
return extendErr("failed to load sector: ", ErrorInternal(err.Error()))
}
- payload = append(payload, sectorData[request.Offset:request.Offset+request.Length])
+ payload = append(payload, sectorData)
}
return nil
}()
diff --git a/modules/storagemanager.go b/modules/storagemanager.go
index 6e3bf1f..d06bb5e 100644
--- a/modules/storagemanager.go
+++ b/modules/storagemanager.go
@@ -84,6 +84,9 @@ type (
// bytes that match the input sector root.
ReadSector(sectorRoot crypto.Hash) ([]byte, error)
+ // ReadSectorPart will read part of a sector from the storage manager.
+ ReadSectorPart(sectorRoot crypto.Hash, offset, length uint32) ([]byte, error)
+
// RemoveSector will remove a sector from the storage manager. The
// height at which the sector expires should be provided, so that the
// auto-expiry information for that sector can be properly updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment