7 "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.
20 // Chunks we've written to since the last check. The chunk offset and
21 // length can be determined by the request chunkSize in use.
22 _dirtyChunks bitmap.Bitmap
27 storageCompletionOk bool
29 publicPieceState PieceState
30 priority piecePriority
33 // This can be locked when the Client lock is taken, but probably not vice versa.
34 pendingWritesMutex sync.Mutex
36 noPendingWrites sync.Cond
38 // Connections that have written data to this piece since its last check.
39 // This can include connections that have closed.
40 dirtiers map[*Peer]struct{}
43 func (p *Piece) String() string {
44 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
47 func (p *Piece) Info() metainfo.Piece {
48 return p.t.info.Piece(int(p.index))
51 func (p *Piece) Storage() storage.Piece {
52 return p.t.storage.Piece(p.Info())
55 func (p *Piece) pendingChunkIndex(chunkIndex int) bool {
56 return !p._dirtyChunks.Contains(bitmap.BitIndex(chunkIndex))
59 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
60 return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
63 func (p *Piece) hasDirtyChunks() bool {
64 return p._dirtyChunks.Len() != 0
67 func (p *Piece) numDirtyChunks() pp.Integer {
68 return pp.Integer(p._dirtyChunks.Len())
71 func (p *Piece) unpendChunkIndex(i int) {
72 p._dirtyChunks.Add(bitmap.BitIndex(i))
76 func (p *Piece) pendChunkIndex(i int) {
77 p._dirtyChunks.Remove(bitmap.BitIndex(i))
80 func (p *Piece) numChunks() pp.Integer {
81 return p.t.pieceNumChunks(p.index)
84 func (p *Piece) incrementPendingWrites() {
85 p.pendingWritesMutex.Lock()
87 p.pendingWritesMutex.Unlock()
90 func (p *Piece) decrementPendingWrites() {
91 p.pendingWritesMutex.Lock()
92 if p.pendingWrites == 0 {
96 if p.pendingWrites == 0 {
97 p.noPendingWrites.Broadcast()
99 p.pendingWritesMutex.Unlock()
102 func (p *Piece) waitNoPendingWrites() {
103 p.pendingWritesMutex.Lock()
104 for p.pendingWrites != 0 {
105 p.noPendingWrites.Wait()
107 p.pendingWritesMutex.Unlock()
110 func (p *Piece) chunkIndexDirty(chunk pp.Integer) bool {
111 return p._dirtyChunks.Contains(bitmap.BitIndex(chunk))
114 func (p *Piece) chunkIndexSpec(chunk pp.Integer) ChunkSpec {
115 return chunkIndexSpec(chunk, p.length(), p.chunkSize())
118 func (p *Piece) chunkIndexRequest(chunkIndex pp.Integer) Request {
121 p.chunkIndexSpec(chunkIndex),
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() pp.Integer {
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)
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.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) {
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._dirtyChunks.Len() == bitmap.BitRange(p.numChunks())
240 func (p *Piece) dirtyChunks() bitmap.Bitmap {
241 return p._dirtyChunks
244 func (p *Piece) State() PieceState {
245 return p.t.PieceState(p.index)
248 func (p *Piece) iterUndirtiedChunks(f func(cs ChunkSpec) bool) bool {
249 for i := pp.Integer(0); i < p.numChunks(); i++ {
250 if p.chunkIndexDirty(i) {
253 if !f(p.chunkIndexSpec(i)) {