return p.t.dirtyChunks.Contains(p.requestIndexBegin() + chunk)
}
+func (p *Piece) firstCleanChunk() (_ g.Option[chunkIndexType]) {
+ it := p.t.dirtyChunks.Iterator()
+ begin := uint32(p.requestIndexBegin())
+ end := uint32(p.requestIndexMaxEnd())
+ it.AdvanceIfNeeded(begin)
+ for next := begin; next < end; next++ {
+ if !it.HasNext() || it.Next() != next {
+ return g.Some(chunkIndexType(next - begin))
+ }
+ }
+ return
+}
+
func (p *Piece) chunkIndexSpec(chunk chunkIndexType) ChunkSpec {
return chunkIndexSpec(pp.Integer(chunk), p.length(), p.chunkSize())
}
}
}
-func (p *Piece) fileExtents() iter.Seq2[int, segments.Extent] {
+func (p *Piece) fileExtents(offsetIntoPiece int64) iter.Seq2[int, segments.Extent] {
return p.t.info.FileSegmentsIndex().LocateIter(segments.Extent{
- p.torrentBeginOffset(),
- segments.Int(p.length()),
+ p.torrentBeginOffset() + offsetIntoPiece,
+ int64(p.length()) - offsetIntoPiece,
})
}
value.pieces,
func(ih metainfo.Hash, pieceIndex int, orderState requestStrategy.PieceRequestOrderState) bool {
t := cl.torrentsByShortHash[ih]
- for i, e := range t.piece(pieceIndex).fileExtents() {
+ p := t.piece(pieceIndex)
+ cleanOpt := p.firstCleanChunk()
+ if !cleanOpt.Ok {
+ // Could almost return true here, as clearly something is going on with the piece.
+ return false
+ }
+ // Pretty sure we want this and not the order state priority. That one is for
+ // client piece request order and ignores other states like hashing, marking
+ // etc. Order state priority would be faster otherwise.
+ priority := p.effectivePriority()
+ for i, e := range p.fileExtents(int64(cleanOpt.Value) * int64(t.chunkSize)) {
for url, ws := range t.webSeeds {
- // Return value from this function doesn't terminate, so don't pretend
- // it does here either.
+ // Return value from this function (RequestPieceFunc) doesn't terminate
+ // iteration, so propagate that to not handling the yield return value.
yield(
webseedUniqueRequestKey{
aprioriWebseedRequestKey{
e.Start,
},
webseedRequestOrderValue{
- priority: orderState.Priority,
+ priority: priority,
costKey: ws.hostKey,
},
)