}
cn.peerSentHaveAll = true
cn._peerPieces.Clear()
- if cn.t._pendingPieces.Len() != 0 {
- cn.updateRequests("have all")
+ if !cn.t._pendingPieces.IsEmpty() {
+ cn.updateRequests("Peer.onPeerHasAllPieces")
}
cn.peerPiecesChanged()
}
return !c.t.haveAllPieces()
}
if !c.t.haveInfo() {
- return c._peerPieces.GetCardinality() != 0
+ return !c._peerPieces.IsEmpty()
}
- return c._peerPieces.Intersects(
- roaring.FlipInt(&c.t._completedPieces, 0, c.t.numPieces()),
- )
+ return c._peerPieces.Intersects(&c.t._pendingPieces)
}
func (c *Peer) deleteRequest(r RequestIndex) bool {
"github.com/anacrolix/missinggo/slices"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/missinggo/v2/bitmap"
- "github.com/anacrolix/missinggo/v2/prioritybitmap"
"github.com/anacrolix/multiless"
"github.com/anacrolix/sync"
"github.com/davecgh/go-spew/spew"
// A cache of pieces we need to get. Calculated from various piece and
// file priorities and completion states elsewhere.
- _pendingPieces prioritybitmap.PriorityBitmap
+ _pendingPieces roaring.Bitmap
// A cache of completed piece indices.
_completedPieces roaring.Bitmap
// Pieces that need to be hashed.
return !t.wantPieceIndex(i)
}
-func (t *Torrent) pendingPieces() *prioritybitmap.PriorityBitmap {
- return &t._pendingPieces
-}
-
// Returns a channel that is closed when the Torrent is closed.
func (t *Torrent) Closed() events.Done {
return t.closed.Done()
}
func (t *Torrent) wantPieceIndex(index pieceIndex) bool {
- // TODO: Are these overly conservative, should we be guarding this here?
- {
- if !t.haveInfo() {
- return false
- }
- if index < 0 || index >= t.numPieces() {
- return false
- }
- }
- p := &t.pieces[index]
- if p.queuedForHash() {
- return false
- }
- if p.hashing {
- return false
- }
- if t.pieceComplete(index) {
- return false
- }
- if t._pendingPieces.Contains(int(index)) {
- return true
- }
- // t.logger.Printf("piece %d not pending", index)
- return !t.forReaderOffsetPieces(func(begin, end pieceIndex) bool {
- return index < begin || index >= end
- })
+ return t._pendingPieces.Contains(uint32(index))
}
// A pool of []*PeerConn, to reduce allocations in functions that need to index or sort Torrent
}
func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) {
- if t._pendingPieces.Contains(piece) {
+ if t._pendingPieces.Contains(uint32(piece)) {
t.iterPeers(func(c *Peer) {
if c.actualRequestState.Interested {
return
newPrio := p.uncachedPriority()
// t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio)
if newPrio == PiecePriorityNone {
- if !t._pendingPieces.Remove(int(piece)) {
+ if !t._pendingPieces.CheckedRemove(uint32(piece)) {
return
}
} else {
- if !t._pendingPieces.Set(int(piece), newPrio.BitmapPriority()) {
+ if !t._pendingPieces.CheckedAdd(uint32(piece)) {
return
}
}
}
func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
- prio, ok := t._pendingPieces.GetPriority(piece)
- if !ok {
- return PiecePriorityNone
- }
- if prio > 0 {
- panic(prio)
- }
- ret := piecePriority(-prio)
- if ret == PiecePriorityNone {
- panic(piece)
- }
- return ret
+ return t.piece(piece).uncachedPriority()
}
func (t *Torrent) pendRequest(req RequestIndex) {
if !t.haveInfo() {
return true
}
- return t._pendingPieces.Len() != 0
+ return !t._pendingPieces.IsEmpty()
}
func appendMissingStrings(old, new []string) (ret []string) {