From: Matt Joiner Date: Wed, 7 May 2025 09:37:57 +0000 (+1000) Subject: Move a few things around to save a bit of memory X-Git-Tag: v1.59.0~167 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=d6ea7b019457b93259758fab2a61ca327f3d9b8d;p=btrtrc.git Move a few things around to save a bit of memory --- diff --git a/piecestate.go b/piecestate.go index d41f38d9..52d4002a 100644 --- a/piecestate.go +++ b/piecestate.go @@ -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 diff --git a/storage/interface.go b/storage/interface.go index ddae026d..edc2e9f3 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -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) diff --git a/storage/piece-resource.go b/storage/piece-resource.go index 4d703b35..4ab8c07e 100644 --- a/storage/piece-resource.go +++ b/storage/piece-resource.go @@ -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 diff --git a/torrent.go b/torrent.go index b8ac0248..69b2e72f 100644 --- a/torrent.go +++ b/torrent.go @@ -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