package torrent
import (
- "encoding/gob"
"fmt"
"sync"
"github.com/anacrolix/chansync"
"github.com/anacrolix/missinggo/v2/bitmap"
+
"github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/storage"
- "github.com/anacrolix/torrent/typed-roaring"
)
type Piece struct {
// Connections that have written data to this piece since its last check.
// This can include connections that have closed.
dirtiers map[*Peer]struct{}
-
- undirtiedChunksIter undirtiedChunksIter
}
func (p *Piece) String() string {
return p.t.storage.Piece(p.Info())
}
+func (p *Piece) Flush() {
+ if p.t.storage.Flush != nil {
+ _ = p.t.storage.Flush()
+ }
+}
+
func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
return !p.chunkIndexDirty(chunkIndex)
}
return p.t.PieceState(p.index)
}
-func init() {
- gob.Register(undirtiedChunksIter{})
-}
-
-// Use an iterator to jump between dirty bits.
-type undirtiedChunksIter struct {
- TorrentDirtyChunks *typedRoaring.Bitmap[RequestIndex]
- StartRequestIndex RequestIndex
- EndRequestIndex RequestIndex
-}
-
-func (me *undirtiedChunksIter) Iter(f func(chunkIndexType)) {
- it := me.TorrentDirtyChunks.Iterator()
- startIndex := me.StartRequestIndex
- endIndex := me.EndRequestIndex
- it.AdvanceIfNeeded(startIndex)
- lastDirty := startIndex - 1
- for it.HasNext() {
- next := it.Next()
- if next >= endIndex {
- break
- }
- for index := lastDirty + 1; index < next; index++ {
- f(index - startIndex)
- }
- lastDirty = next
- }
- for index := lastDirty + 1; index < endIndex; index++ {
- f(index - startIndex)
- }
- return
-}
-
func (p *Piece) requestIndexOffset() RequestIndex {
return p.t.pieceRequestIndexOffset(p.index)
}