7 "github.com/anacrolix/missinggo/bitmap"
8 "github.com/anacrolix/torrent/metainfo"
9 pp "github.com/anacrolix/torrent/peer_protocol"
10 "github.com/anacrolix/torrent/storage"
13 // Describes the importance of obtaining a particular piece.
14 type piecePriority byte
16 func (pp *piecePriority) Raise(maybe piecePriority) bool {
24 // Priority for use in PriorityBitmap
25 func (me piecePriority) BitmapPriority() int {
30 PiecePriorityNone piecePriority = iota // Not wanted. Must be the zero value.
31 PiecePriorityNormal // Wanted.
32 PiecePriorityHigh // Wanted a lot.
33 PiecePriorityReadahead // May be required soon.
34 // Succeeds a piece where a read occurred. Currently the same as Now,
35 // apparently due to issues with caching.
37 PiecePriorityNow // A Reader is reading in this piece. Highest urgency.
41 // The completed piece SHA1 hash, from the metainfo "pieces" field.
46 // Chunks we've written to since the last check. The chunk offset and
47 // length can be determined by the request chunkSize in use.
48 dirtyChunks bitmap.Bitmap
52 storageCompletionOk bool
54 publicPieceState PieceState
55 priority piecePriority
57 pendingWritesMutex sync.Mutex
59 noPendingWrites sync.Cond
61 // Connections that have written data to this piece since its last check.
62 // This can include connections that have closed.
63 dirtiers map[*connection]struct{}
66 func (p *Piece) String() string {
67 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
70 func (p *Piece) Info() metainfo.Piece {
71 return p.t.info.Piece(int(p.index))
74 func (p *Piece) Storage() storage.Piece {
75 return p.t.storage.Piece(p.Info())
78 func (p *Piece) pendingChunkIndex(chunkIndex int) bool {
79 return !p.dirtyChunks.Contains(chunkIndex)
82 func (p *Piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {
83 return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
86 func (p *Piece) hasDirtyChunks() bool {
87 return p.dirtyChunks.Len() != 0
90 func (p *Piece) numDirtyChunks() pp.Integer {
91 return pp.Integer(p.dirtyChunks.Len())
94 func (p *Piece) unpendChunkIndex(i int) {
99 func (p *Piece) pendChunkIndex(i int) {
100 p.dirtyChunks.Remove(i)
103 func (p *Piece) numChunks() pp.Integer {
104 return p.t.pieceNumChunks(p.index)
107 func (p *Piece) undirtiedChunkIndices() (ret bitmap.Bitmap) {
108 ret = p.dirtyChunks.Copy()
109 ret.FlipRange(0, bitmap.BitIndex(p.numChunks()))
113 func (p *Piece) incrementPendingWrites() {
114 p.pendingWritesMutex.Lock()
116 p.pendingWritesMutex.Unlock()
119 func (p *Piece) decrementPendingWrites() {
120 p.pendingWritesMutex.Lock()
121 if p.pendingWrites == 0 {
125 if p.pendingWrites == 0 {
126 p.noPendingWrites.Broadcast()
128 p.pendingWritesMutex.Unlock()
131 func (p *Piece) waitNoPendingWrites() {
132 p.pendingWritesMutex.Lock()
133 for p.pendingWrites != 0 {
134 p.noPendingWrites.Wait()
136 p.pendingWritesMutex.Unlock()
139 func (p *Piece) chunkIndexDirty(chunk pp.Integer) bool {
140 return p.dirtyChunks.Contains(bitmap.BitIndex(chunk))
143 func (p *Piece) chunkIndexSpec(chunk pp.Integer) chunkSpec {
144 return chunkIndexSpec(chunk, p.length(), p.chunkSize())
147 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
149 // if ret > p.length() {
150 // panic("too many dirty bytes")
153 numRegularDirtyChunks := p.numDirtyChunks()
154 if p.chunkIndexDirty(p.numChunks() - 1) {
155 numRegularDirtyChunks--
156 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
158 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
162 func (p *Piece) length() pp.Integer {
163 return p.t.pieceLength(p.index)
166 func (p *Piece) chunkSize() pp.Integer {
170 func (p *Piece) lastChunkIndex() pp.Integer {
171 return p.numChunks() - 1
174 func (p *Piece) bytesLeft() (ret pp.Integer) {
175 if p.t.pieceComplete(p.index) {
178 return p.length() - p.numDirtyBytes()
181 func (p *Piece) VerifyData() {
183 defer p.t.cl.unlock()
184 target := p.numVerifies + 1
188 // log.Printf("target: %d", target)
189 p.t.queuePieceCheck(p.index)
190 for p.numVerifies < target {
191 // log.Printf("got %d verifies", p.numVerifies)
197 func (p *Piece) queuedForHash() bool {
198 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
201 func (p *Piece) torrentBeginOffset() int64 {
202 return int64(p.index) * p.t.info.PieceLength
205 func (p *Piece) torrentEndOffset() int64 {
206 return p.torrentBeginOffset() + int64(p.length())
209 func (p *Piece) SetPriority(prio piecePriority) {
211 defer p.t.cl.unlock()
213 p.t.updatePiecePriority(p.index)
216 func (p *Piece) uncachedPriority() (ret piecePriority) {
217 if p.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) {
218 return PiecePriorityNone
220 for _, f := range p.files {
223 if p.t.readerNowPieces.Contains(int(p.index)) {
224 ret.Raise(PiecePriorityNow)
226 // if t.readerNowPieces.Contains(piece - 1) {
227 // return PiecePriorityNext
229 if p.t.readerReadaheadPieces.Contains(bitmap.BitIndex(p.index)) {
230 ret.Raise(PiecePriorityReadahead)
232 ret.Raise(p.priority)
236 func (p *Piece) completion() (ret storage.Completion) {
237 ret.Complete = p.t.pieceComplete(p.index)
238 ret.Ok = p.storageCompletionOk