2016-06-20 15:51:05 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-03-30 18:38:44 +08:00
|
|
|
"sync"
|
|
|
|
|
2016-06-20 15:51:05 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mapPieceCompletion struct {
|
2017-03-30 18:38:44 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
m map[metainfo.PieceKey]struct{}
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|
|
|
|
|
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 }
|
2016-06-20 15:51:05 +08:00
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func (me *mapPieceCompletion) Get(pk metainfo.PieceKey) (bool, error) {
|
2017-03-30 18:38:44 +08:00
|
|
|
me.mu.Lock()
|
2016-08-26 18:29:05 +08:00
|
|
|
_, ok := me.m[pk]
|
2017-03-30 18:38:44 +08:00
|
|
|
me.mu.Unlock()
|
2016-07-12 14:45:22 +08:00
|
|
|
return ok, nil
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func (me *mapPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
|
2017-03-30 18:38:44 +08:00
|
|
|
me.mu.Lock()
|
2016-06-20 15:51:05 +08:00
|
|
|
if b {
|
|
|
|
if me.m == nil {
|
|
|
|
me.m = make(map[metainfo.PieceKey]struct{})
|
|
|
|
}
|
2016-08-26 18:29:05 +08:00
|
|
|
me.m[pk] = struct{}{}
|
2016-06-20 15:51:05 +08:00
|
|
|
} else {
|
2016-08-26 18:29:05 +08:00
|
|
|
delete(me.m, pk)
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|
2017-03-30 18:38:44 +08:00
|
|
|
me.mu.Unlock()
|
2016-07-12 14:45:22 +08:00
|
|
|
return nil
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|