skip.Union(cn.t.completedPieces)
// Return an iterator over the different priority classes, minus the skip
// pieces.
- return iterBitmapsDistinct(skip, now, readahead, cn.t.pendingPieces)
+ return iter.Chain(
+ iterBitmapsDistinct(skip, now, readahead),
+ func(cb iter.Callback) {
+ cn.t.pendingPieces.IterTyped(func(piece int) bool {
+ if skip.Contains(piece) {
+ return true
+ }
+ more := cb(piece)
+ skip.Add(piece)
+ return more
+ })
+ },
+ )
}
// The connection should download highest priority pieces first, without any
}
t.setChunkSize(defaultChunkSize)
t.makePieces()
- t.pendingPieces.Add(0)
+ t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
r, w := io.Pipe()
cn := &connection{
t: t,
}
}
+// Priority for use in PriorityBitmap
+func (me piecePriority) BitmapPriority() int {
+ return -int(me)
+}
+
const (
PiecePriorityNone piecePriority = iota // Not wanted.
PiecePriorityNormal // Wanted.
+ PiecePriorityHigh // Wanted a lot.
PiecePriorityReadahead // May be required soon.
- // Succeeds a piece where a read occurred. Currently the same as Now, apparently due to issues with caching.
+ // Succeeds a piece where a read occurred. Currently the same as Now,
+ // apparently due to issues with caching.
PiecePriorityNext
- PiecePriorityNow // A Reader is reading in this piece.
+ PiecePriorityNow // A Reader is reading in this piece. Highest urgency.
)
type Piece struct {
"text/tabwriter"
"time"
+ "github.com/anacrolix/missinggo/prioritybitmap"
+
"github.com/anacrolix/dht"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/bitmap"
// The indexes of pieces we want with normal priority, that aren't
// currently available.
- pendingPieces bitmap.Bitmap
+ pendingPieces prioritybitmap.PriorityBitmap
// A cache of completed piece indices.
completedPieces bitmap.Bitmap
// Pieces that need to be hashed.
if t.havePiece(piece) {
return
}
- t.pendingPieces.Add(piece)
+ t.pendingPieces.Set(piece, PiecePriorityNormal.BitmapPriority())
t.updatePiecePriority(piece)
}
func (t *Torrent) unpendPieces(unpend bitmap.Bitmap) {
- t.pendingPieces.Sub(unpend)
- unpend.IterTyped(func(piece int) (again bool) {
+ unpend.IterTyped(func(piece int) (more bool) {
+ t.pendingPieces.Remove(piece)
t.updatePiecePriority(piece)
return true
})