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"
14 // Describes the importance of obtaining a particular piece.
15 type piecePriority byte
17 func (pp *piecePriority) Raise(maybe piecePriority) bool {
25 // Priority for use in PriorityBitmap
26 func (me piecePriority) BitmapPriority() int {
31 PiecePriorityNone piecePriority = iota // Not wanted. Must be the zero value.
32 PiecePriorityNormal // Wanted.
33 PiecePriorityHigh // Wanted a lot.
34 PiecePriorityReadahead // May be required soon.
35 // Succeeds a piece where a read occurred. Currently the same as Now,
36 // apparently due to issues with caching.
38 PiecePriorityNow // A Reader is reading in this piece. Highest urgency.
42 // The completed piece SHA1 hash, from the metainfo "pieces" field.
47 // Chunks we've written to since the last check. The chunk offset and
48 // length can be determined by the request chunkSize in use.
49 _dirtyChunks bitmap.Bitmap
54 storageCompletionOk bool
56 publicPieceState PieceState
57 priority piecePriority
60 // This can be locked when the Client lock is taken, but probably not vice versa.
61 pendingWritesMutex sync.Mutex
63 noPendingWrites sync.Cond
65 // Connections that have written data to this piece since its last check.
66 // This can include connections that have closed.
67 dirtiers map[*Peer]struct{}
70 func (p *Piece) String() string {
71 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
74 func (p *Piece) Info() metainfo.Piece {
75 return p.t.info.Piece(int(p.index))
78 func (p *Piece) Storage() storage.Piece {
79 return p.t.storage.Piece(p.Info())
82 func (p *Piece) pendingChunkIndex(chunkIndex int) bool {
83 return !p._dirtyChunks.Contains(chunkIndex)
86 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
87 return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
90 func (p *Piece) hasDirtyChunks() bool {
91 return p._dirtyChunks.Len() != 0
94 func (p *Piece) numDirtyChunks() pp.Integer {
95 return pp.Integer(p._dirtyChunks.Len())
98 func (p *Piece) unpendChunkIndex(i int) {
103 func (p *Piece) pendChunkIndex(i int) {
104 p._dirtyChunks.Remove(i)
107 func (p *Piece) numChunks() pp.Integer {
108 return p.t.pieceNumChunks(p.index)
111 func (p *Piece) incrementPendingWrites() {
112 p.pendingWritesMutex.Lock()
114 p.pendingWritesMutex.Unlock()
117 func (p *Piece) decrementPendingWrites() {
118 p.pendingWritesMutex.Lock()
119 if p.pendingWrites == 0 {
123 if p.pendingWrites == 0 {
124 p.noPendingWrites.Broadcast()
126 p.pendingWritesMutex.Unlock()
129 func (p *Piece) waitNoPendingWrites() {
130 p.pendingWritesMutex.Lock()
131 for p.pendingWrites != 0 {
132 p.noPendingWrites.Wait()
134 p.pendingWritesMutex.Unlock()
137 func (p *Piece) chunkIndexDirty(chunk pp.Integer) bool {
138 return p._dirtyChunks.Contains(bitmap.BitIndex(chunk))
141 func (p *Piece) chunkIndexSpec(chunk pp.Integer) ChunkSpec {
142 return chunkIndexSpec(chunk, p.length(), p.chunkSize())
145 func (p *Piece) chunkIndexRequest(chunkIndex pp.Integer) Request {
148 p.chunkIndexSpec(chunkIndex),
152 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
154 // if ret > p.length() {
155 // panic("too many dirty bytes")
158 numRegularDirtyChunks := p.numDirtyChunks()
159 if p.chunkIndexDirty(p.numChunks() - 1) {
160 numRegularDirtyChunks--
161 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
163 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
167 func (p *Piece) length() pp.Integer {
168 return p.t.pieceLength(p.index)
171 func (p *Piece) chunkSize() pp.Integer {
175 func (p *Piece) lastChunkIndex() pp.Integer {
176 return p.numChunks() - 1
179 func (p *Piece) bytesLeft() (ret pp.Integer) {
180 if p.t.pieceComplete(p.index) {
183 return p.length() - p.numDirtyBytes()
186 // Forces the piece data to be rehashed.
187 func (p *Piece) VerifyData() {
189 defer p.t.cl.unlock()
190 target := p.numVerifies + 1
194 //log.Printf("target: %d", target)
195 p.t.queuePieceCheck(p.index)
197 //log.Printf("got %d verifies", p.numVerifies)
198 if p.numVerifies >= target {
206 func (p *Piece) queuedForHash() bool {
207 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
210 func (p *Piece) torrentBeginOffset() int64 {
211 return int64(p.index) * p.t.info.PieceLength
214 func (p *Piece) torrentEndOffset() int64 {
215 return p.torrentBeginOffset() + int64(p.length())
218 func (p *Piece) SetPriority(prio piecePriority) {
220 defer p.t.cl.unlock()
222 p.t.updatePiecePriority(p.index)
225 func (p *Piece) purePriority() (ret piecePriority) {
226 for _, f := range p.files {
229 if p.t.readerNowPieces().Contains(int(p.index)) {
230 ret.Raise(PiecePriorityNow)
232 // if t._readerNowPieces.Contains(piece - 1) {
233 // return PiecePriorityNext
235 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
236 ret.Raise(PiecePriorityReadahead)
238 ret.Raise(p.priority)
242 func (p *Piece) uncachedPriority() (ret piecePriority) {
243 if p.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) {
244 return PiecePriorityNone
246 return p.purePriority()
249 // Tells the Client to refetch the completion status from storage, updating priority etc. if
250 // necessary. Might be useful if you know the state of the piece data has changed externally.
251 func (p *Piece) UpdateCompletion() {
253 defer p.t.cl.unlock()
254 p.t.updatePieceCompletion(p.index)
257 func (p *Piece) completion() (ret storage.Completion) {
258 ret.Complete = p.t.pieceComplete(p.index)
259 ret.Ok = p.storageCompletionOk
263 func (p *Piece) allChunksDirty() bool {
264 return p._dirtyChunks.Len() == int(p.numChunks())
267 func (p *Piece) dirtyChunks() bitmap.Bitmap {
268 return p._dirtyChunks
271 func (p *Piece) State() PieceState {
272 return p.t.PieceState(p.index)
275 func (p *Piece) iterUndirtiedChunks(f func(ChunkSpec) bool) bool {
276 for i := pp.Integer(0); i < p.numChunks(); i++ {
277 if p.chunkIndexDirty(i) {
280 if !f(p.chunkIndexSpec(i)) {