6 "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 // Piece priority describes the importance of obtaining a particular piece.
15 type piecePriority byte
17 func (pp *piecePriority) Raise(maybe piecePriority) {
24 PiecePriorityNone piecePriority = iota // Not wanted.
25 PiecePriorityNormal // Wanted.
26 PiecePriorityReadahead // May be required soon.
27 PiecePriorityNext // Succeeds a piece where a read occurred.
28 PiecePriorityNow // A read occurred in this piece.
32 // The completed piece SHA1 hash, from the metainfo "pieces" field.
36 // Chunks we've written to since the last check. The chunk offset and
37 // length can be determined by the request chunkSize in use.
38 DirtyChunks bitmap.Bitmap
42 PublicPieceState PieceState
43 priority piecePriority
45 pendingWritesMutex sync.Mutex
47 noPendingWrites sync.Cond
50 func (p *piece) Info() metainfo.Piece {
51 return p.t.info.Piece(p.index)
54 func (p *piece) Storage() storage.Piece {
55 return p.t.storage.Piece(p.Info())
58 func (p *piece) pendingChunkIndex(chunkIndex int) bool {
59 return !p.DirtyChunks.Contains(chunkIndex)
62 func (p *piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {
63 return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
66 func (p *piece) hasDirtyChunks() bool {
67 return p.DirtyChunks.Len() != 0
70 func (p *piece) numDirtyChunks() (ret int) {
71 return p.DirtyChunks.Len()
74 func (p *piece) unpendChunkIndex(i int) {
78 func (p *piece) pendChunkIndex(i int) {
79 p.DirtyChunks.Remove(i)
82 func (p *piece) numChunks() int {
83 return p.t.pieceNumChunks(p.index)
86 func (p *piece) undirtiedChunkIndices() (ret bitmap.Bitmap) {
87 ret = p.DirtyChunks.Copy()
88 ret.FlipRange(0, p.numChunks())
92 func (p *piece) incrementPendingWrites() {
93 p.pendingWritesMutex.Lock()
95 p.pendingWritesMutex.Unlock()
98 func (p *piece) decrementPendingWrites() {
99 p.pendingWritesMutex.Lock()
100 if p.pendingWrites == 0 {
104 if p.pendingWrites == 0 {
105 p.noPendingWrites.Broadcast()
107 p.pendingWritesMutex.Unlock()
110 func (p *piece) waitNoPendingWrites() {
111 p.pendingWritesMutex.Lock()
112 for p.pendingWrites != 0 {
113 p.noPendingWrites.Wait()
115 p.pendingWritesMutex.Unlock()
118 func (p *piece) chunkIndexDirty(chunk int) bool {
119 return p.DirtyChunks.Contains(chunk)
122 func (p *piece) chunkIndexSpec(chunk int) chunkSpec {
123 return chunkIndexSpec(chunk, p.length(), p.chunkSize())
126 func (p *piece) numDirtyBytes() (ret pp.Integer) {
128 if ret > p.length() {
129 panic("too many dirty bytes")
132 numRegularDirtyChunks := p.numDirtyChunks()
133 if p.chunkIndexDirty(p.numChunks() - 1) {
134 numRegularDirtyChunks--
135 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
137 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
141 func (p *piece) length() pp.Integer {
142 return p.t.pieceLength(p.index)
145 func (p *piece) chunkSize() pp.Integer {
149 func (p *piece) lastChunkIndex() int {
150 return p.numChunks() - 1
153 func (p *piece) bytesLeft() (ret pp.Integer) {
154 if p.t.pieceComplete(p.index) {
157 return p.length() - p.numDirtyBytes()