7 "github.com/anacrolix/chansync"
8 "github.com/anacrolix/missinggo/v2/bitmap"
9 "github.com/anacrolix/torrent/metainfo"
10 pp "github.com/anacrolix/torrent/peer_protocol"
11 "github.com/anacrolix/torrent/storage"
15 // The completed piece SHA1 hash, from the metainfo "pieces" field.
21 readerCond chansync.BroadcastCond
26 storageCompletionOk bool
28 publicPieceState PieceState
29 priority piecePriority
30 // Availability adjustment for this piece relative to len(Torrent.connsWithAllPieces). This is
31 // incremented for any piece a peer has when a peer has a piece, Torrent.haveInfo is true, and
32 // the Peer isn't recorded in Torrent.connsWithAllPieces.
33 relativeAvailability int
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{}
45 func (p *Piece) String() string {
46 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
49 func (p *Piece) Info() metainfo.Piece {
50 return p.t.info.Piece(int(p.index))
53 func (p *Piece) Storage() storage.Piece {
54 return p.t.storage.Piece(p.Info())
57 func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
58 return !p.chunkIndexDirty(chunkIndex)
61 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
62 return p.pendingChunkIndex(chunkIndexFromChunkSpec(cs, chunkSize))
65 func (p *Piece) hasDirtyChunks() bool {
66 return p.numDirtyChunks() != 0
69 func (p *Piece) numDirtyChunks() chunkIndexType {
70 return chunkIndexType(roaringBitmapRangeCardinality[RequestIndex](
72 p.requestIndexOffset(),
73 p.t.pieceRequestIndexOffset(p.index+1)))
76 func (p *Piece) unpendChunkIndex(i chunkIndexType) {
77 p.t.dirtyChunks.Add(p.requestIndexOffset() + i)
78 p.t.updatePieceRequestOrder(p.index)
79 p.readerCond.Broadcast()
82 func (p *Piece) pendChunkIndex(i RequestIndex) {
83 p.t.dirtyChunks.Remove(p.requestIndexOffset() + i)
84 p.t.updatePieceRequestOrder(p.index)
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)
244 func (p *Piece) requestIndexOffset() RequestIndex {
245 return p.t.pieceRequestIndexOffset(p.index)
248 func (p *Piece) availability() int {
249 return len(p.t.connsWithAllPieces) + p.relativeAvailability