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
|
2017-10-12 13:09:32 +08:00
|
|
|
m map[metainfo.PieceKey]bool
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
var _ PieceCompletion = (*mapPieceCompletion)(nil)
|
|
|
|
|
2017-05-22 10:15:48 +08:00
|
|
|
func NewMapPieceCompletion() PieceCompletion {
|
2017-10-12 13:09:32 +08:00
|
|
|
return &mapPieceCompletion{m: make(map[metainfo.PieceKey]bool)}
|
2017-05-22 10:15:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*mapPieceCompletion) Close() error { return nil }
|
2016-06-20 15:51:05 +08:00
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
func (me *mapPieceCompletion) Get(pk metainfo.PieceKey) (c Completion, err error) {
|
2017-03-30 18:38:44 +08:00
|
|
|
me.mu.Lock()
|
2017-10-12 13:09:32 +08:00
|
|
|
defer me.mu.Unlock()
|
|
|
|
c.Complete, c.Ok = me.m[pk]
|
|
|
|
return
|
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()
|
2017-10-12 13:09:32 +08:00
|
|
|
defer me.mu.Unlock()
|
|
|
|
if me.m == nil {
|
|
|
|
me.m = make(map[metainfo.PieceKey]bool)
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|
2017-10-12 13:09:32 +08:00
|
|
|
me.m[pk] = b
|
2016-07-12 14:45:22 +08:00
|
|
|
return nil
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|