X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=piece.go;h=e08b2609690e385663c4df716d22d05ab3c7808b;hb=HEAD;hp=7424bba403476c278ff6c558e18a21f9127b3afa;hpb=63b3d2d211ca13d7f7aaa17dc2595ba23c103970;p=btrtrc.git diff --git a/piece.go b/piece.go index 7424bba4..e08b2609 100644 --- a/piece.go +++ b/piece.go @@ -28,7 +28,10 @@ type Piece struct { publicPieceState PieceState priority piecePriority - availability int64 + // Availability adjustment for this piece relative to len(Torrent.connsWithAllPieces). This is + // incremented for any piece a peer has when a peer has a piece, Torrent.haveInfo is true, and + // the Peer isn't recorded in Torrent.connsWithAllPieces. + relativeAvailability int // This can be locked when the Client lock is taken, but probably not vice versa. pendingWritesMutex sync.Mutex @@ -52,6 +55,12 @@ func (p *Piece) Storage() storage.Piece { return p.t.storage.Piece(p.Info()) } +func (p *Piece) Flush() { + if p.t.storage.Flush != nil { + _ = p.t.storage.Flush() + } +} + func (p *Piece) pendingChunkIndex(chunkIndex chunkIndexType) bool { return !p.chunkIndexDirty(chunkIndex) } @@ -65,7 +74,7 @@ func (p *Piece) hasDirtyChunks() bool { } func (p *Piece) numDirtyChunks() chunkIndexType { - return chunkIndexType(roaringBitmapRangeCardinality( + return chunkIndexType(roaringBitmapRangeCardinality[RequestIndex]( &p.t.dirtyChunks, p.requestIndexOffset(), p.t.pieceRequestIndexOffset(p.index+1))) @@ -73,11 +82,13 @@ func (p *Piece) numDirtyChunks() chunkIndexType { func (p *Piece) unpendChunkIndex(i chunkIndexType) { p.t.dirtyChunks.Add(p.requestIndexOffset() + i) + p.t.updatePieceRequestOrder(p.index) p.readerCond.Broadcast() } func (p *Piece) pendChunkIndex(i RequestIndex) { p.t.dirtyChunks.Remove(p.requestIndexOffset() + i) + p.t.updatePieceRequestOrder(p.index) } func (p *Piece) numChunks() chunkIndexType { @@ -160,10 +171,10 @@ func (p *Piece) VerifyData() { if p.hashing { target++ } - //log.Printf("target: %d", target) + // log.Printf("target: %d", target) p.t.queuePieceCheck(p.index) for { - //log.Printf("got %d verifies", p.numVerifies) + // log.Printf("got %d verifies", p.numVerifies) if p.numVerifies >= target { break } @@ -188,7 +199,7 @@ func (p *Piece) SetPriority(prio piecePriority) { p.t.cl.lock() defer p.t.cl.unlock() p.priority = prio - p.t.updatePiecePriority(p.index) + p.t.updatePiecePriority(p.index, "Piece.SetPriority") } func (p *Piece) purePriority() (ret piecePriority) { @@ -209,7 +220,7 @@ func (p *Piece) purePriority() (ret piecePriority) { } func (p *Piece) uncachedPriority() (ret piecePriority) { - if p.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) { + if p.hashing || p.marking || p.t.pieceComplete(p.index) || p.queuedForHash() { return PiecePriorityNone } return p.purePriority() @@ -237,15 +248,10 @@ func (p *Piece) State() PieceState { return p.t.PieceState(p.index) } -func (p *Piece) iterUndirtiedChunks(f func(cs chunkIndexType)) { - for i := chunkIndexType(0); i < p.numChunks(); i++ { - if p.chunkIndexDirty(i) { - continue - } - f(i) - } -} - func (p *Piece) requestIndexOffset() RequestIndex { return p.t.pieceRequestIndexOffset(p.index) } + +func (p *Piece) availability() int { + return len(p.t.connsWithAllPieces) + p.relativeAvailability +}