]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move a few things around to save a bit of memory
authorMatt Joiner <anacrolix@gmail.com>
Wed, 7 May 2025 09:37:57 +0000 (19:37 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 7 May 2025 09:37:57 +0000 (19:37 +1000)
piecestate.go
storage/interface.go
storage/piece-resource.go
torrent.go

index d41f38d9d229bd7487ee2acbf1651a1669048cd6..52d4002aabce1729808656ee86c6f8fd05c746ef 100644 (file)
@@ -6,8 +6,8 @@ import (
 
 // The current state of a piece.
 type PieceState struct {
-       Priority PiecePriority
        storage.Completion
+       Priority PiecePriority
        // The piece is being hashed, or is queued for hash. Deprecated: Use those fields instead.
        Checking bool
 
index ddae026d7eb87a9b893d7231c0e03592cbc1196f..edc2e9f32082b90e1cdfbe8ea5489c4a73976037 100644 (file)
@@ -54,15 +54,19 @@ type PieceImpl interface {
        // fit.
        MarkComplete() error
        MarkNotComplete() error
-       // Returns true if the piece is complete.
+       // Returns the state of a piece. Typically, this is implemented in some kind of storage to avoid
+       // rehashing, and cheap checks are performed here. (The implementation maintains a cache in
+       // Torrent).
        Completion() Completion
 }
 
-// TODO: Yo where the fuck is the documentation.
+// Completion state of a piece.
 type Completion struct {
+       Err error
+       // The state is known or cached.
+       Ok bool
+       // If Ok, whether the data is correct.
        Complete bool
-       Ok       bool
-       Err      error
 }
 
 // Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
index 4d703b3541174e4859f38f001c3d771705faba2e..4ab8c07eddae01f132eeadf564b2d21b768b7008 100644 (file)
@@ -23,12 +23,12 @@ type piecePerResource struct {
 }
 
 type ResourcePiecesOpts struct {
+       Capacity TorrentCapacity
        // After marking a piece complete, don't bother deleting its incomplete blobs.
        LeaveIncompleteChunks bool
        // Sized puts require being able to stream from a statement executed on another connection.
        // Without them, we buffer the entire read and then put that.
        NoSizedPuts bool
-       Capacity    TorrentCapacity
 }
 
 func NewResourcePieces(p PieceProvider) ClientImpl {
@@ -269,8 +269,8 @@ func (s piecePerResourcePiece) WriteAt(b []byte, off int64) (n int, err error) {
 }
 
 type chunk struct {
-       offset   int64
        instance resource.Instance
+       offset   int64
 }
 
 type chunks []chunk
@@ -327,7 +327,7 @@ func (s piecePerResourcePiece) getChunks(dir string) (chunks chunks) {
                if err != nil {
                        panic(err)
                }
-               chunks = append(chunks, chunk{offset, i})
+               chunks = append(chunks, chunk{i, offset})
        }
        sort.Slice(chunks, func(i, j int) bool {
                return chunks[i].offset < chunks[j].offset
index b8ac02484c8a95e87a41b9bd08dcc7402e48016e..69b2e72f46ed6d2b0c2e118a7835fa33c91d0675 100644 (file)
@@ -519,14 +519,14 @@ func (t *Torrent) cacheLength() {
 // separately.
 func (t *Torrent) setInfo(info *metainfo.Info) error {
        if err := validateInfo(info); err != nil {
-               return fmt.Errorf("bad info: %s", err)
+               return fmt.Errorf("bad info: %w", err)
        }
        if t.storageOpener != nil {
                var err error
                ctx := log.ContextWithLogger(context.Background(), t.logger)
                t.storage, err = t.storageOpener.OpenTorrent(ctx, info, *t.canonicalShortInfohash())
                if err != nil {
-                       return fmt.Errorf("error opening torrent storage: %s", err)
+                       return fmt.Errorf("error opening torrent storage: %w", err)
                }
        }
        t.nameMu.Lock()
@@ -637,8 +637,9 @@ func (t *Torrent) setInfoBytesLocked(b []byte) (err error) {
        if t.info != nil {
                return nil
        }
-       if err := t.setInfo(&info); err != nil {
-               return err
+       err = t.setInfo(&info)
+       if err != nil {
+               return
        }
        t.onSetInfo()
        return nil