8 "github.com/anacrolix/chansync"
9 "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{}
45 undirtiedChunksIter undirtiedChunksIter
48 func (p *Piece) String() string {
49 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
52 func (p *Piece) Info() metainfo.Piece {
53 return p.t.info.Piece(int(p.index))
56 func (p *Piece) Storage() storage.Piece {
57 return p.t.storage.Piece(p.Info())
60 func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
61 return !p.chunkIndexDirty(chunkIndex)
64 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
65 return p.pendingChunkIndex(chunkIndexFromChunkSpec(cs, chunkSize))
68 func (p *Piece) hasDirtyChunks() bool {
69 return p.numDirtyChunks() != 0
72 func (p *Piece) numDirtyChunks() chunkIndexType {
73 return chunkIndexType(roaringBitmapRangeCardinality[RequestIndex](
75 p.requestIndexOffset(),
76 p.t.pieceRequestIndexOffset(p.index+1)))
79 func (p *Piece) unpendChunkIndex(i chunkIndexType) {
80 p.t.dirtyChunks.Add(p.requestIndexOffset() + i)
81 p.t.updatePieceRequestOrder(p.index)
82 p.readerCond.Broadcast()
85 func (p *Piece) pendChunkIndex(i RequestIndex) {
86 p.t.dirtyChunks.Remove(p.requestIndexOffset() + i)
87 p.t.updatePieceRequestOrder(p.index)
90 func (p *Piece) numChunks() chunkIndexType {
91 return p.t.pieceNumChunks(p.index)
94 func (p *Piece) incrementPendingWrites() {
95 p.pendingWritesMutex.Lock()
97 p.pendingWritesMutex.Unlock()
100 func (p *Piece) decrementPendingWrites() {
101 p.pendingWritesMutex.Lock()
102 if p.pendingWrites == 0 {
106 if p.pendingWrites == 0 {
107 p.noPendingWrites.Broadcast()
109 p.pendingWritesMutex.Unlock()
112 func (p *Piece) waitNoPendingWrites() {
113 p.pendingWritesMutex.Lock()
114 for p.pendingWrites != 0 {
115 p.noPendingWrites.Wait()
117 p.pendingWritesMutex.Unlock()
120 func (p *Piece) chunkIndexDirty(chunk chunkIndexType) bool {
121 return p.t.dirtyChunks.Contains(p.requestIndexOffset() + chunk)
124 func (p *Piece) chunkIndexSpec(chunk chunkIndexType) ChunkSpec {
125 return chunkIndexSpec(pp.Integer(chunk), p.length(), p.chunkSize())
128 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
130 // if ret > p.length() {
131 // panic("too many dirty bytes")
134 numRegularDirtyChunks := p.numDirtyChunks()
135 if p.chunkIndexDirty(p.numChunks() - 1) {
136 numRegularDirtyChunks--
137 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
139 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
143 func (p *Piece) length() pp.Integer {
144 return p.t.pieceLength(p.index)
147 func (p *Piece) chunkSize() pp.Integer {
151 func (p *Piece) lastChunkIndex() chunkIndexType {
152 return p.numChunks() - 1
155 func (p *Piece) bytesLeft() (ret pp.Integer) {
156 if p.t.pieceComplete(p.index) {
159 return p.length() - p.numDirtyBytes()
162 // Forces the piece data to be rehashed.
163 func (p *Piece) VerifyData() {
165 defer p.t.cl.unlock()
166 target := p.numVerifies + 1
170 // log.Printf("target: %d", target)
171 p.t.queuePieceCheck(p.index)
173 // log.Printf("got %d verifies", p.numVerifies)
174 if p.numVerifies >= target {
182 func (p *Piece) queuedForHash() bool {
183 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
186 func (p *Piece) torrentBeginOffset() int64 {
187 return int64(p.index) * p.t.info.PieceLength
190 func (p *Piece) torrentEndOffset() int64 {
191 return p.torrentBeginOffset() + int64(p.length())
194 func (p *Piece) SetPriority(prio piecePriority) {
196 defer p.t.cl.unlock()
198 p.t.updatePiecePriority(p.index, "Piece.SetPriority")
201 func (p *Piece) purePriority() (ret piecePriority) {
202 for _, f := range p.files {
205 if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
206 ret.Raise(PiecePriorityNow)
208 // if t._readerNowPieces.Contains(piece - 1) {
209 // return PiecePriorityNext
211 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
212 ret.Raise(PiecePriorityReadahead)
214 ret.Raise(p.priority)
218 func (p *Piece) uncachedPriority() (ret piecePriority) {
219 if p.hashing || p.marking || p.t.pieceComplete(p.index) || p.queuedForHash() {
220 return PiecePriorityNone
222 return p.purePriority()
225 // Tells the Client to refetch the completion status from storage, updating priority etc. if
226 // necessary. Might be useful if you know the state of the piece data has changed externally.
227 func (p *Piece) UpdateCompletion() {
229 defer p.t.cl.unlock()
230 p.t.updatePieceCompletion(p.index)
233 func (p *Piece) completion() (ret storage.Completion) {
234 ret.Complete = p.t.pieceComplete(p.index)
235 ret.Ok = p.storageCompletionOk
239 func (p *Piece) allChunksDirty() bool {
240 return p.numDirtyChunks() == p.numChunks()
243 func (p *Piece) State() PieceState {
244 return p.t.PieceState(p.index)
248 gob.Register(undirtiedChunksIter{})
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