7 "github.com/anacrolix/chansync"
8 g "github.com/anacrolix/generics"
9 "github.com/anacrolix/missinggo/v2/bitmap"
11 "github.com/anacrolix/torrent/metainfo"
12 pp "github.com/anacrolix/torrent/peer_protocol"
13 "github.com/anacrolix/torrent/storage"
14 infohash_v2 "github.com/anacrolix/torrent/types/infohash-v2"
18 // The completed piece SHA1 hash, from the metainfo "pieces" field. Nil if the info is not V1
21 hashV2 g.Option[infohash_v2.T]
26 readerCond chansync.BroadcastCond
31 storageCompletionOk bool
33 publicPieceState PieceState
34 priority piecePriority
35 // Availability adjustment for this piece relative to len(Torrent.connsWithAllPieces). This is
36 // incremented for any piece a peer has when a peer has a piece, Torrent.haveInfo is true, and
37 // the Peer isn't recorded in Torrent.connsWithAllPieces.
38 relativeAvailability int
40 // This can be locked when the Client lock is taken, but probably not vice versa.
41 pendingWritesMutex sync.Mutex
43 noPendingWrites sync.Cond
45 // Connections that have written data to this piece since its last check.
46 // This can include connections that have closed.
47 dirtiers map[*Peer]struct{}
50 func (p *Piece) String() string {
51 return fmt.Sprintf("%s/%d", p.t.canonicalShortInfohash().HexString(), p.index)
54 func (p *Piece) Info() metainfo.Piece {
55 return p.t.info.Piece(int(p.index))
58 func (p *Piece) Storage() storage.Piece {
59 return p.t.storage.Piece(p.Info())
62 func (p *Piece) Flush() {
63 if p.t.storage.Flush != nil {
64 _ = p.t.storage.Flush()
68 func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool {
69 return !p.chunkIndexDirty(chunkIndex)
72 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
73 return p.pendingChunkIndex(chunkIndexFromChunkSpec(cs, chunkSize))
76 func (p *Piece) hasDirtyChunks() bool {
77 return p.numDirtyChunks() != 0
80 func (p *Piece) numDirtyChunks() chunkIndexType {
81 return chunkIndexType(roaringBitmapRangeCardinality[RequestIndex](
83 p.requestIndexOffset(),
84 p.t.pieceRequestIndexOffset(p.index+1)))
87 func (p *Piece) unpendChunkIndex(i chunkIndexType) {
88 p.t.dirtyChunks.Add(p.requestIndexOffset() + i)
89 p.t.updatePieceRequestOrderPiece(p.index)
90 p.readerCond.Broadcast()
93 func (p *Piece) pendChunkIndex(i RequestIndex) {
94 p.t.dirtyChunks.Remove(p.requestIndexOffset() + i)
95 p.t.updatePieceRequestOrderPiece(p.index)
98 func (p *Piece) numChunks() chunkIndexType {
99 return p.t.pieceNumChunks(p.index)
102 func (p *Piece) incrementPendingWrites() {
103 p.pendingWritesMutex.Lock()
105 p.pendingWritesMutex.Unlock()
108 func (p *Piece) decrementPendingWrites() {
109 p.pendingWritesMutex.Lock()
110 if p.pendingWrites == 0 {
114 if p.pendingWrites == 0 {
115 p.noPendingWrites.Broadcast()
117 p.pendingWritesMutex.Unlock()
120 func (p *Piece) waitNoPendingWrites() {
121 p.pendingWritesMutex.Lock()
122 for p.pendingWrites != 0 {
123 p.noPendingWrites.Wait()
125 p.pendingWritesMutex.Unlock()
128 func (p *Piece) chunkIndexDirty(chunk chunkIndexType) bool {
129 return p.t.dirtyChunks.Contains(p.requestIndexOffset() + chunk)
132 func (p *Piece) chunkIndexSpec(chunk chunkIndexType) ChunkSpec {
133 return chunkIndexSpec(pp.Integer(chunk), p.length(), p.chunkSize())
136 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
138 // if ret > p.length() {
139 // panic("too many dirty bytes")
142 numRegularDirtyChunks := p.numDirtyChunks()
143 if p.chunkIndexDirty(p.numChunks() - 1) {
144 numRegularDirtyChunks--
145 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
147 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
151 func (p *Piece) length() pp.Integer {
152 return p.t.pieceLength(p.index)
155 func (p *Piece) chunkSize() pp.Integer {
159 func (p *Piece) lastChunkIndex() chunkIndexType {
160 return p.numChunks() - 1
163 func (p *Piece) bytesLeft() (ret pp.Integer) {
164 if p.t.pieceComplete(p.index) {
167 return p.length() - p.numDirtyBytes()
170 // Forces the piece data to be rehashed.
171 func (p *Piece) VerifyData() {
173 defer p.t.cl.unlock()
174 target := p.numVerifies + 1
178 // log.Printf("target: %d", target)
179 p.t.queuePieceCheck(p.index)
181 // log.Printf("got %d verifies", p.numVerifies)
182 if p.numVerifies >= target {
190 func (p *Piece) queuedForHash() bool {
191 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
194 func (p *Piece) torrentBeginOffset() int64 {
195 return int64(p.index) * p.t.info.PieceLength
198 func (p *Piece) torrentEndOffset() int64 {
199 return p.torrentBeginOffset() + int64(p.t.usualPieceSize())
202 func (p *Piece) SetPriority(prio piecePriority) {
204 defer p.t.cl.unlock()
206 p.t.updatePiecePriority(p.index, "Piece.SetPriority")
209 func (p *Piece) purePriority() (ret piecePriority) {
210 for _, f := range p.files {
213 if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
214 ret.Raise(PiecePriorityNow)
216 // if t._readerNowPieces.Contains(piece - 1) {
217 // return PiecePriorityNext
219 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
220 ret.Raise(PiecePriorityReadahead)
222 ret.Raise(p.priority)
226 func (p *Piece) uncachedPriority() (ret piecePriority) {
227 if p.hashing || p.marking || p.t.pieceComplete(p.index) || p.queuedForHash() {
228 return PiecePriorityNone
230 return p.purePriority()
233 // Tells the Client to refetch the completion status from storage, updating priority etc. if
234 // necessary. Might be useful if you know the state of the piece data has changed externally.
235 func (p *Piece) UpdateCompletion() {
237 defer p.t.cl.unlock()
238 p.t.updatePieceCompletion(p.index)
241 func (p *Piece) completion() (ret storage.Completion) {
242 ret.Complete = p.t.pieceComplete(p.index)
243 ret.Ok = p.storageCompletionOk
247 func (p *Piece) allChunksDirty() bool {
248 return p.numDirtyChunks() == p.numChunks()
251 func (p *Piece) State() PieceState {
252 return p.t.PieceState(p.index)
255 func (p *Piece) requestIndexOffset() RequestIndex {
256 return p.t.pieceRequestIndexOffset(p.index)
259 func (p *Piece) availability() int {
260 return len(p.t.connsWithAllPieces) + p.relativeAvailability
263 // For v2 torrents, files are aligned to pieces so there should always only be a single file for a
265 func (p *Piece) mustGetOnlyFile() *File {
266 if len(p.files) != 1 {