fix remove pieces

This commit is contained in:
yourok
2019-10-11 12:17:46 +03:00
parent e36fc05f0a
commit 358de66eff
3 changed files with 30 additions and 25 deletions

View File

@@ -90,21 +90,21 @@ func (b *BufferPool) ReleaseBuffer(index int) {
} }
} }
func (b *BufferPool) Used() map[int]struct{} { //func (b *BufferPool) Used() map[int]struct{} {
b.mu.Lock() // b.mu.Lock()
defer b.mu.Unlock() // defer b.mu.Unlock()
if len(b.buffs) == 0 { // if len(b.buffs) == 0 {
b.mkBuffs() // b.mkBuffs()
} // }
used := make(map[int]struct{}) // used := make(map[int]struct{})
for _, b := range b.buffs { // for _, b := range b.buffs {
if b.used { // if b.used {
used[b.pieceId] = struct{}{} // used[b.pieceId] = struct{}{}
} // }
} // }
return used // return used
} //}
//
func (b *BufferPool) Len() int { //func (b *BufferPool) Len() int {
return b.frees // return b.frees
} //}

View File

@@ -32,17 +32,19 @@ type Cache struct {
pieces map[int]*Piece pieces map[int]*Piece
bufferPull *BufferPool bufferPull *BufferPool
usedPieces map[int]struct{}
readers map[*reader.Reader]struct{} readers map[*reader.Reader]struct{}
} }
func NewCache(capacity int64, storage *Storage) *Cache { func NewCache(capacity int64, storage *Storage) *Cache {
ret := &Cache{ ret := &Cache{
capacity: capacity, capacity: capacity,
filled: 0, filled: 0,
pieces: make(map[int]*Piece), pieces: make(map[int]*Piece),
readers: make(map[*reader.Reader]struct{}), readers: make(map[*reader.Reader]struct{}),
s: storage, usedPieces: make(map[int]struct{}),
s: storage,
} }
return ret return ret
@@ -159,13 +161,12 @@ func (c *Cache) cleanPieces() {
func (c *Cache) getBufferedPieces() map[int]*Piece { func (c *Cache) getBufferedPieces() map[int]*Piece {
pieces := make(map[int]*Piece) pieces := make(map[int]*Piece)
fill := int64(0) fill := int64(0)
used := c.bufferPull.Used() used := c.usedPieces
for u := range used { for u := range used {
piece := c.pieces[u] piece := c.pieces[u]
if piece.Size > 0 { if piece.Size > 0 {
if piece.Id > 0 { if piece.Id > 0 {
pieces[piece.Id] = piece pieces[piece.Id] = piece
//pieces = append(pieces, piece)
} }
fill += piece.Size fill += piece.Size
} }

View File

@@ -39,6 +39,7 @@ func (p *Piece) WriteAt(b []byte, off int64) (n int, err error) {
if p.buffer == nil { if p.buffer == nil {
return 0, errors.New("Can't get buffer write") return 0, errors.New("Can't get buffer write")
} }
p.cache.usedPieces[p.Id] = struct{}{}
} }
n = copy(p.buffer[off:], b[:]) n = copy(p.buffer[off:], b[:])
p.Size += int64(n) p.Size += int64(n)
@@ -101,6 +102,9 @@ func (p *Piece) Release() {
} }
p.Size = 0 p.Size = 0
p.complete = false p.complete = false
if _, ok := p.cache.usedPieces[p.Id]; ok {
delete(p.cache.usedPieces, p.Id)
}
} }
func (p *Piece) Stat() state.ItemState { func (p *Piece) Stat() state.ItemState {