]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Read from more than a single piece in each read to Torrent storage
authorMatt Joiner <anacrolix@gmail.com>
Tue, 27 Oct 2020 01:24:43 +0000 (12:24 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 27 Oct 2020 01:24:43 +0000 (12:24 +1100)
reader.go
torrent.go

index 7e00d4ead228331e4bc5a1d43f78f0311a6bb6b1..8a74873b8afec4eb77095e47676ed7a077c4a54f 100644 (file)
--- a/reader.go
+++ b/reader.go
@@ -204,6 +204,8 @@ func (r *reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64, e
        }
 }
 
+// Adds the reader's torrent offset to the reader object offset (for example the reader might be
+// constrainted to a particular file within the torrent).
 func (r *reader) torrentOffset(readerPos int64) int64 {
        return r.offset + readerPos
 }
@@ -220,10 +222,9 @@ func (r *reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err erro
                if avail == 0 {
                        return
                }
-               pi := pieceIndex(r.torrentOffset(pos) / r.t.info.PieceLength)
-               ip := r.t.info.Piece(pi)
-               po := r.torrentOffset(pos) % r.t.info.PieceLength
-               b1 := missinggo.LimitLen(b, ip.Length()-po, avail)
+               firstPieceIndex := pieceIndex(r.torrentOffset(pos) / r.t.info.PieceLength)
+               firstPieceOffset := r.torrentOffset(pos) % r.t.info.PieceLength
+               b1 := missinggo.LimitLen(b, avail)
                n, err = r.t.readAt(b1, r.torrentOffset(pos))
                if n != 0 {
                        err = nil
@@ -233,9 +234,9 @@ func (r *reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err erro
                // TODO: Just reset pieces in the readahead window. This might help
                // prevent thrashing with small caches and file and piece priorities.
                r.log(log.Fstr("error reading torrent %s piece %d offset %d, %d bytes: %v",
-                       r.t.infoHash.HexString(), pi, po, len(b1), err))
-               if !r.t.updatePieceCompletion(pi) {
-                       r.log(log.Fstr("piece %d completion unchanged", pi))
+                       r.t.infoHash.HexString(), firstPieceIndex, firstPieceOffset, len(b1), err))
+               if !r.t.updatePieceCompletion(firstPieceIndex) {
+                       r.log(log.Fstr("piece %d completion unchanged", firstPieceIndex))
                }
                r.t.cl.unlock()
        }
index e034933fba17bd9f1c18ec0f8f8afb85515155d6..ace386eb3fce410cde0a57bf9742fea57e21ee0e 100644 (file)
@@ -1147,9 +1147,19 @@ func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
 
 // Non-blocking read. Client lock is not required.
 func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
-       p := &t.pieces[off/t.info.PieceLength]
-       p.waitNoPendingWrites()
-       return p.Storage().ReadAt(b, off-p.Info().Offset())
+       for len(b) != 0 {
+               p := &t.pieces[off/t.info.PieceLength]
+               p.waitNoPendingWrites()
+               var n1 int
+               n1, err = p.Storage().ReadAt(b, off-p.Info().Offset())
+               if n1 == 0 {
+                       break
+               }
+               off += int64(n1)
+               n += n1
+               b = b[n1:]
+       }
+       return
 }
 
 // Returns an error if the metadata was completed, but couldn't be set for