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) numDirtyBytes() (ret pp.Integer) {
120 // if ret > p.length() {
121 // panic("too many dirty bytes")
124 numRegularDirtyChunks := p.numDirtyChunks()
125 if p.chunkIndexDirty(p.numChunks() - 1) {
126 numRegularDirtyChunks--
127 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
129 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
133 func (p *Piece) length() pp.Integer {
134 return p.t.pieceLength(p.index)
137 func (p *Piece) chunkSize() pp.Integer {
141 func (p *Piece) lastChunkIndex() pp.Integer {
142 return p.numChunks() - 1
145 func (p *Piece) bytesLeft() (ret pp.Integer) {
146 if p.t.pieceComplete(p.index) {
149 return p.length() - p.numDirtyBytes()
152 // Forces the piece data to be rehashed.
153 func (p *Piece) VerifyData() {
155 defer p.t.cl.unlock()
156 target := p.numVerifies + 1
160 //log.Printf("target: %d", target)
161 p.t.queuePieceCheck(p.index)
163 //log.Printf("got %d verifies", p.numVerifies)
164 if p.numVerifies >= target {
172 func (p *Piece) queuedForHash() bool {
173 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
176 func (p *Piece) torrentBeginOffset() int64 {
177 return int64(p.index) * p.t.info.PieceLength
180 func (p *Piece) torrentEndOffset() int64 {
181 return p.torrentBeginOffset() + int64(p.length())
184 func (p *Piece) SetPriority(prio piecePriority) {
186 defer p.t.cl.unlock()
188 p.t.updatePiecePriority(p.index)
191 func (p *Piece) purePriority() (ret piecePriority) {
192 for _, f := range p.files {
195 if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
196 ret.Raise(PiecePriorityNow)
198 // if t._readerNowPieces.Contains(piece - 1) {
199 // return PiecePriorityNext
201 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
202 ret.Raise(PiecePriorityReadahead)
204 ret.Raise(p.priority)
208 func (p *Piece) uncachedPriority() (ret piecePriority) {
209 if p.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) {
210 return PiecePriorityNone
212 return p.purePriority()
215 // Tells the Client to refetch the completion status from storage, updating priority etc. if
216 // necessary. Might be useful if you know the state of the piece data has changed externally.
217 func (p *Piece) UpdateCompletion() {
219 defer p.t.cl.unlock()
220 p.t.updatePieceCompletion(p.index)
223 func (p *Piece) completion() (ret storage.Completion) {
224 ret.Complete = p.t.pieceComplete(p.index)
225 ret.Ok = p.storageCompletionOk
229 func (p *Piece) allChunksDirty() bool {
230 return p._dirtyChunks.Len() == bitmap.BitRange(p.numChunks())
233 func (p *Piece) State() PieceState {
234 return p.t.PieceState(p.index)
237 func (p *Piece) iterUndirtiedChunks(f func(cs ChunkSpec) bool) bool {
238 for i := pp.Integer(0); i < p.numChunks(); i++ {
239 if p.chunkIndexDirty(i) {
242 if !f(p.chunkIndexSpec(i)) {