FedP2P/storage/completion_piece_map.go

40 lines
731 B
Go
Raw Normal View History

package storage
import (
"sync"
"github.com/anacrolix/torrent/metainfo"
)
type mapPieceCompletion struct {
mu sync.Mutex
m map[metainfo.PieceKey]struct{}
}
2017-05-22 10:15:48 +08:00
func NewMapPieceCompletion() PieceCompletion {
return &mapPieceCompletion{m: make(map[metainfo.PieceKey]struct{})}
}
func (*mapPieceCompletion) Close() error { return nil }
func (me *mapPieceCompletion) Get(pk metainfo.PieceKey) (bool, error) {
me.mu.Lock()
_, ok := me.m[pk]
me.mu.Unlock()
2016-07-12 14:45:22 +08:00
return ok, nil
}
func (me *mapPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
me.mu.Lock()
if b {
if me.m == nil {
me.m = make(map[metainfo.PieceKey]struct{})
}
me.m[pk] = struct{}{}
} else {
delete(me.m, pk)
}
me.mu.Unlock()
2016-07-12 14:45:22 +08:00
return nil
}