]> Sergey Matveev's repositories - btrtrc.git/blob - piece.go
Remove unused code
[btrtrc.git] / piece.go
1 package torrent
2
3 import (
4         "fmt"
5         "sync"
6
7         "github.com/anacrolix/missinggo/v2/bitmap"
8
9         "github.com/anacrolix/torrent/metainfo"
10         pp "github.com/anacrolix/torrent/peer_protocol"
11         "github.com/anacrolix/torrent/storage"
12 )
13
14 type Piece struct {
15         // The completed piece SHA1 hash, from the metainfo "pieces" field.
16         hash  *metainfo.Hash
17         t     *Torrent
18         index pieceIndex
19         files []*File
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
23
24         numVerifies         int64
25         hashing             bool
26         marking             bool
27         storageCompletionOk bool
28
29         publicPieceState PieceState
30         priority         piecePriority
31         availability     int64
32
33         // This can be locked when the Client lock is taken, but probably not vice versa.
34         pendingWritesMutex sync.Mutex
35         pendingWrites      int
36         noPendingWrites    sync.Cond
37
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{}
41 }
42
43 func (p *Piece) String() string {
44         return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
45 }
46
47 func (p *Piece) Info() metainfo.Piece {
48         return p.t.info.Piece(int(p.index))
49 }
50
51 func (p *Piece) Storage() storage.Piece {
52         return p.t.storage.Piece(p.Info())
53 }
54
55 func (p *Piece) pendingChunkIndex(chunkIndex int) bool {
56         return !p._dirtyChunks.Contains(bitmap.BitIndex(chunkIndex))
57 }
58
59 func (p *Piece) pendingChunk(cs ChunkSpec, chunkSize pp.Integer) bool {
60         return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
61 }
62
63 func (p *Piece) hasDirtyChunks() bool {
64         return p._dirtyChunks.Len() != 0
65 }
66
67 func (p *Piece) numDirtyChunks() pp.Integer {
68         return pp.Integer(p._dirtyChunks.Len())
69 }
70
71 func (p *Piece) unpendChunkIndex(i int) {
72         p._dirtyChunks.Add(bitmap.BitIndex(i))
73         p.t.tickleReaders()
74 }
75
76 func (p *Piece) pendChunkIndex(i int) {
77         p._dirtyChunks.Remove(bitmap.BitIndex(i))
78 }
79
80 func (p *Piece) numChunks() pp.Integer {
81         return p.t.pieceNumChunks(p.index)
82 }
83
84 func (p *Piece) incrementPendingWrites() {
85         p.pendingWritesMutex.Lock()
86         p.pendingWrites++
87         p.pendingWritesMutex.Unlock()
88 }
89
90 func (p *Piece) decrementPendingWrites() {
91         p.pendingWritesMutex.Lock()
92         if p.pendingWrites == 0 {
93                 panic("assertion")
94         }
95         p.pendingWrites--
96         if p.pendingWrites == 0 {
97                 p.noPendingWrites.Broadcast()
98         }
99         p.pendingWritesMutex.Unlock()
100 }
101
102 func (p *Piece) waitNoPendingWrites() {
103         p.pendingWritesMutex.Lock()
104         for p.pendingWrites != 0 {
105                 p.noPendingWrites.Wait()
106         }
107         p.pendingWritesMutex.Unlock()
108 }
109
110 func (p *Piece) chunkIndexDirty(chunk pp.Integer) bool {
111         return p._dirtyChunks.Contains(bitmap.BitIndex(chunk))
112 }
113
114 func (p *Piece) chunkIndexSpec(chunk pp.Integer) ChunkSpec {
115         return chunkIndexSpec(chunk, p.length(), p.chunkSize())
116 }
117
118 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
119         // defer func() {
120         //      if ret > p.length() {
121         //              panic("too many dirty bytes")
122         //      }
123         // }()
124         numRegularDirtyChunks := p.numDirtyChunks()
125         if p.chunkIndexDirty(p.numChunks() - 1) {
126                 numRegularDirtyChunks--
127                 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
128         }
129         ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
130         return
131 }
132
133 func (p *Piece) length() pp.Integer {
134         return p.t.pieceLength(p.index)
135 }
136
137 func (p *Piece) chunkSize() pp.Integer {
138         return p.t.chunkSize
139 }
140
141 func (p *Piece) lastChunkIndex() pp.Integer {
142         return p.numChunks() - 1
143 }
144
145 func (p *Piece) bytesLeft() (ret pp.Integer) {
146         if p.t.pieceComplete(p.index) {
147                 return 0
148         }
149         return p.length() - p.numDirtyBytes()
150 }
151
152 // Forces the piece data to be rehashed.
153 func (p *Piece) VerifyData() {
154         p.t.cl.lock()
155         defer p.t.cl.unlock()
156         target := p.numVerifies + 1
157         if p.hashing {
158                 target++
159         }
160         //log.Printf("target: %d", target)
161         p.t.queuePieceCheck(p.index)
162         for {
163                 //log.Printf("got %d verifies", p.numVerifies)
164                 if p.numVerifies >= target {
165                         break
166                 }
167                 p.t.cl.event.Wait()
168         }
169         // log.Print("done")
170 }
171
172 func (p *Piece) queuedForHash() bool {
173         return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
174 }
175
176 func (p *Piece) torrentBeginOffset() int64 {
177         return int64(p.index) * p.t.info.PieceLength
178 }
179
180 func (p *Piece) torrentEndOffset() int64 {
181         return p.torrentBeginOffset() + int64(p.length())
182 }
183
184 func (p *Piece) SetPriority(prio piecePriority) {
185         p.t.cl.lock()
186         defer p.t.cl.unlock()
187         p.priority = prio
188         p.t.updatePiecePriority(p.index)
189 }
190
191 func (p *Piece) purePriority() (ret piecePriority) {
192         for _, f := range p.files {
193                 ret.Raise(f.prio)
194         }
195         if p.t.readerNowPieces().Contains(bitmap.BitIndex(p.index)) {
196                 ret.Raise(PiecePriorityNow)
197         }
198         // if t._readerNowPieces.Contains(piece - 1) {
199         //      return PiecePriorityNext
200         // }
201         if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
202                 ret.Raise(PiecePriorityReadahead)
203         }
204         ret.Raise(p.priority)
205         return
206 }
207
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
211         }
212         return p.purePriority()
213 }
214
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() {
218         p.t.cl.lock()
219         defer p.t.cl.unlock()
220         p.t.updatePieceCompletion(p.index)
221 }
222
223 func (p *Piece) completion() (ret storage.Completion) {
224         ret.Complete = p.t.pieceComplete(p.index)
225         ret.Ok = p.storageCompletionOk
226         return
227 }
228
229 func (p *Piece) allChunksDirty() bool {
230         return p._dirtyChunks.Len() == bitmap.BitRange(p.numChunks())
231 }
232
233 func (p *Piece) State() PieceState {
234         return p.t.PieceState(p.index)
235 }
236
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) {
240                         continue
241                 }
242                 if !f(p.chunkIndexSpec(i)) {
243                         return false
244                 }
245         }
246         return true
247 }