8 "github.com/RoaringBitmap/roaring"
9 "github.com/anacrolix/chansync"
10 "github.com/anacrolix/missinggo/v2/bitmap"
12 "github.com/anacrolix/torrent/metainfo"
13 pp "github.com/anacrolix/torrent/peer_protocol"
14 "github.com/anacrolix/torrent/storage"
18 // The completed piece SHA1 hash, from the metainfo "pieces" field.
24 readerCond chansync.BroadcastCond
29 storageCompletionOk bool
31 publicPieceState PieceState
32 priority piecePriority
35 // This can be locked when the Client lock is taken, but probably not vice versa.
36 pendingWritesMutex sync.Mutex
38 noPendingWrites sync.Cond
40 // Connections that have written data to this piece since its last check.
41 // This can include connections that have closed.
42 dirtiers map[*Peer]struct{}
44 undirtiedChunksIter undirtiedChunksIter
47 func (p *Piece) String() string {
48 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
51 func (p *Piece) Info() metainfo.Piece {
52 return p.t.info.Piece(int(p.index))
55 func (p *Piece) Storage() storage.Piece {
56 return p.t.storage.Piece(p.Info())
59 func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
60 return !p.chunkIndexDirty(chunkIndex)
63 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
64 return p.pendingChunkIndex(chunkIndexFromChunkSpec(cs, chunkSize))
67 func (p *Piece) hasDirtyChunks() bool {
68 return p.numDirtyChunks() != 0
71 func (p *Piece) numDirtyChunks() chunkIndexType {
72 return chunkIndexType(roaringBitmapRangeCardinality(
74 p.requestIndexOffset(),
75 p.t.pieceRequestIndexOffset(p.index+1)))
78 func (p *Piece) unpendChunkIndex(i chunkIndexType) {
79 p.t.dirtyChunks.Add(p.requestIndexOffset() + i)
80 p.readerCond.Broadcast()
83 func (p *Piece) pendChunkIndex(i RequestIndex) {
84 p.t.dirtyChunks.Remove(p.requestIndexOffset() + i)
87 func (p *Piece) numChunks() chunkIndexType {
88 return p.t.pieceNumChunks(p.index)
91 func (p *Piece) incrementPendingWrites() {
92 p.pendingWritesMutex.Lock()
94 p.pendingWritesMutex.Unlock()
97 func (p *Piece) decrementPendingWrites() {
98 p.pendingWritesMutex.Lock()
99 if p.pendingWrites == 0 {
103 if p.pendingWrites == 0 {
104 p.noPendingWrites.Broadcast()
106 p.pendingWritesMutex.Unlock()
109 func (p *Piece) waitNoPendingWrites() {
110 p.pendingWritesMutex.Lock()
111 for p.pendingWrites != 0 {
112 p.noPendingWrites.Wait()
114 p.pendingWritesMutex.Unlock()
117 func (p *Piece) chunkIndexDirty(chunk chunkIndexType) bool {
118 return p.t.dirtyChunks.Contains(p.requestIndexOffset() + chunk)
121 func (p *Piece) chunkIndexSpec(chunk chunkIndexType) ChunkSpec {
122 return chunkIndexSpec(pp.Integer(chunk), p.length(), p.chunkSize())
125 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
127 // if ret > p.length() {
128 // panic("too many dirty bytes")
131 numRegularDirtyChunks := p.numDirtyChunks()
132 if p.chunkIndexDirty(p.numChunks() - 1) {
133 numRegularDirtyChunks--
134 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
136 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
140 func (p *Piece) length() pp.Integer {
141 return p.t.pieceLength(p.index)
144 func (p *Piece) chunkSize() pp.Integer {
148 func (p *Piece) lastChunkIndex() chunkIndexType {
149 return p.numChunks() - 1
152 func (p *Piece) bytesLeft() (ret pp.Integer) {
153 if p.t.pieceComplete(p.index) {
156 return p.length() - p.numDirtyBytes()
159 // Forces the piece data to be rehashed.
160 func (p *Piece) VerifyData() {
162 defer p.t.cl.unlock()
163 target := p.numVerifies + 1
167 // log.Printf("target: %d", target)
168 p.t.queuePieceCheck(p.index)
170 // log.Printf("got %d verifies", p.numVerifies)
171 if p.numVerifies >= target {
179 func (p *Piece) queuedForHash() bool {
180 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
183 func (p *Piece) torrentBeginOffset() int64 {
184 return int64(p.index) * p.t.info.PieceLength
187 func (p *Piece) torrentEndOffset() int64 {
188 return p.torrentBeginOffset() + int64(p.length())
191 func (p *Piece) SetPriority(prio piecePriority) {
193 defer p.t.cl.unlock()
195 p.t.updatePiecePriority(p.index, "Piece.SetPriority")
198 func (p *Piece) purePriority() (ret piecePriority) {
199 for _, f := range p.files {
202 if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
203 ret.Raise(PiecePriorityNow)
205 // if t._readerNowPieces.Contains(piece - 1) {
206 // return PiecePriorityNext
208 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
209 ret.Raise(PiecePriorityReadahead)
211 ret.Raise(p.priority)
215 func (p *Piece) uncachedPriority() (ret piecePriority) {
216 if p.hashing || p.marking || p.t.pieceComplete(p.index) || p.queuedForHash() {
217 return PiecePriorityNone
219 return p.purePriority()
222 // Tells the Client to refetch the completion status from storage, updating priority etc. if
223 // necessary. Might be useful if you know the state of the piece data has changed externally.
224 func (p *Piece) UpdateCompletion() {
226 defer p.t.cl.unlock()
227 p.t.updatePieceCompletion(p.index)
230 func (p *Piece) completion() (ret storage.Completion) {
231 ret.Complete = p.t.pieceComplete(p.index)
232 ret.Ok = p.storageCompletionOk
236 func (p *Piece) allChunksDirty() bool {
237 return p.numDirtyChunks() == p.numChunks()
240 func (p *Piece) State() PieceState {
241 return p.t.PieceState(p.index)
245 gob.Register(undirtiedChunksIter{})
248 // Use an iterator to jump between dirty bits.
249 type undirtiedChunksIter struct {
250 TorrentDirtyChunks *roaring.Bitmap
251 StartRequestIndex RequestIndex
252 EndRequestIndex RequestIndex
255 func (me *undirtiedChunksIter) Iter(f func(chunkIndexType)) {
256 it := me.TorrentDirtyChunks.Iterator()
257 startIndex := me.StartRequestIndex
258 endIndex := me.EndRequestIndex
259 it.AdvanceIfNeeded(startIndex)
260 lastDirty := startIndex - 1
263 if next >= endIndex {
266 for index := lastDirty + 1; index < next; index++ {
267 f(index - startIndex)
271 for index := lastDirty + 1; index < endIndex; index++ {
272 f(index - startIndex)
277 func (p *Piece) requestIndexOffset() RequestIndex {
278 return p.t.pieceRequestIndexOffset(p.index)