7 "github.com/anacrolix/chansync"
8 "github.com/anacrolix/missinggo/v2/bitmap"
10 "github.com/anacrolix/torrent/metainfo"
11 pp "github.com/anacrolix/torrent/peer_protocol"
12 "github.com/anacrolix/torrent/storage"
16 // The completed piece SHA1 hash, from the metainfo "pieces" field.
22 readerCond chansync.BroadcastCond
27 storageCompletionOk bool
29 publicPieceState PieceState
30 priority piecePriority
31 // Availability adjustment for this piece relative to len(Torrent.connsWithAllPieces). This is
32 // incremented for any piece a peer has when a peer has a piece, Torrent.haveInfo is true, and
33 // the Peer isn't recorded in Torrent.connsWithAllPieces.
34 relativeAvailability int
36 // This can be locked when the Client lock is taken, but probably not vice versa.
37 pendingWritesMutex sync.Mutex
39 noPendingWrites sync.Cond
41 // Connections that have written data to this piece since its last check.
42 // This can include connections that have closed.
43 dirtiers map[*Peer]struct{}
46 func (p *Piece) String() string {
47 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
50 func (p *Piece) Info() metainfo.Piece {
51 return p.t.info.Piece(int(p.index))
54 func (p *Piece) Storage() storage.Piece {
55 return p.t.storage.Piece(p.Info())
58 func (p *Piece) Flush() {
59 if p.t.storage.Flush != nil {
60 _ = p.t.storage.Flush()
64 func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
65 return !p.chunkIndexDirty(chunkIndex)
68 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
69 return p.pendingChunkIndex(chunkIndexFromChunkSpec(cs, chunkSize))
72 func (p *Piece) hasDirtyChunks() bool {
73 return p.numDirtyChunks() != 0
76 func (p *Piece) numDirtyChunks() chunkIndexType {
77 return chunkIndexType(roaringBitmapRangeCardinality[RequestIndex](
79 p.requestIndexOffset(),
80 p.t.pieceRequestIndexOffset(p.index+1)))
83 func (p *Piece) unpendChunkIndex(i chunkIndexType) {
84 p.t.dirtyChunks.Add(p.requestIndexOffset() + i)
85 p.t.updatePieceRequestOrder(p.index)
86 p.readerCond.Broadcast()
89 func (p *Piece) pendChunkIndex(i RequestIndex) {
90 p.t.dirtyChunks.Remove(p.requestIndexOffset() + i)
91 p.t.updatePieceRequestOrder(p.index)
94 func (p *Piece) numChunks() chunkIndexType {
95 return p.t.pieceNumChunks(p.index)
98 func (p *Piece) incrementPendingWrites() {
99 p.pendingWritesMutex.Lock()
101 p.pendingWritesMutex.Unlock()
104 func (p *Piece) decrementPendingWrites() {
105 p.pendingWritesMutex.Lock()
106 if p.pendingWrites == 0 {
110 if p.pendingWrites == 0 {
111 p.noPendingWrites.Broadcast()
113 p.pendingWritesMutex.Unlock()
116 func (p *Piece) waitNoPendingWrites() {
117 p.pendingWritesMutex.Lock()
118 for p.pendingWrites != 0 {
119 p.noPendingWrites.Wait()
121 p.pendingWritesMutex.Unlock()
124 func (p *Piece) chunkIndexDirty(chunk chunkIndexType) bool {
125 return p.t.dirtyChunks.Contains(p.requestIndexOffset() + chunk)
128 func (p *Piece) chunkIndexSpec(chunk chunkIndexType) ChunkSpec {
129 return chunkIndexSpec(pp.Integer(chunk), p.length(), p.chunkSize())
132 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
134 // if ret > p.length() {
135 // panic("too many dirty bytes")
138 numRegularDirtyChunks := p.numDirtyChunks()
139 if p.chunkIndexDirty(p.numChunks() - 1) {
140 numRegularDirtyChunks--
141 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
143 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
147 func (p *Piece) length() pp.Integer {
148 return p.t.pieceLength(p.index)
151 func (p *Piece) chunkSize() pp.Integer {
155 func (p *Piece) lastChunkIndex() chunkIndexType {
156 return p.numChunks() - 1
159 func (p *Piece) bytesLeft() (ret pp.Integer) {
160 if p.t.pieceComplete(p.index) {
163 return p.length() - p.numDirtyBytes()
166 // Forces the piece data to be rehashed.
167 func (p *Piece) VerifyData() {
169 defer p.t.cl.unlock()
170 target := p.numVerifies + 1
174 // log.Printf("target: %d", target)
175 p.t.queuePieceCheck(p.index)
177 // log.Printf("got %d verifies", p.numVerifies)
178 if p.numVerifies >= target {
186 func (p *Piece) queuedForHash() bool {
187 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
190 func (p *Piece) torrentBeginOffset() int64 {
191 return int64(p.index) * p.t.info.PieceLength
194 func (p *Piece) torrentEndOffset() int64 {
195 return p.torrentBeginOffset() + int64(p.length())
198 func (p *Piece) SetPriority(prio piecePriority) {
200 defer p.t.cl.unlock()
202 p.t.updatePiecePriority(p.index, "Piece.SetPriority")
205 func (p *Piece) purePriority() (ret piecePriority) {
206 for _, f := range p.files {
209 if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
210 ret.Raise(PiecePriorityNow)
212 // if t._readerNowPieces.Contains(piece - 1) {
213 // return PiecePriorityNext
215 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
216 ret.Raise(PiecePriorityReadahead)
218 ret.Raise(p.priority)
222 func (p *Piece) uncachedPriority() (ret piecePriority) {
223 if p.hashing || p.marking || p.t.pieceComplete(p.index) || p.queuedForHash() {
224 return PiecePriorityNone
226 return p.purePriority()
229 // Tells the Client to refetch the completion status from storage, updating priority etc. if
230 // necessary. Might be useful if you know the state of the piece data has changed externally.
231 func (p *Piece) UpdateCompletion() {
233 defer p.t.cl.unlock()
234 p.t.updatePieceCompletion(p.index)
237 func (p *Piece) completion() (ret storage.Completion) {
238 ret.Complete = p.t.pieceComplete(p.index)
239 ret.Ok = p.storageCompletionOk
243 func (p *Piece) allChunksDirty() bool {
244 return p.numDirtyChunks() == p.numChunks()
247 func (p *Piece) State() PieceState {
248 return p.t.PieceState(p.index)
251 func (p *Piece) requestIndexOffset() RequestIndex {
252 return p.t.pieceRequestIndexOffset(p.index)
255 func (p *Piece) availability() int {
256 return len(p.t.connsWithAllPieces) + p.relativeAvailability