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 {
|
2021-09-02 08:21:56 +08:00
|
|
|
// TODO: Generics
|
2021-05-05 10:38:26 +08:00
|
|
|
m sync.Map
|
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 {
|
2021-05-05 10:38:26 +08:00
|
|
|
return &mapPieceCompletion{}
|
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) {
|
2021-05-05 10:38:26 +08:00
|
|
|
v, ok := me.m.Load(pk)
|
|
|
|
if ok {
|
|
|
|
c.Complete = v.(bool)
|
|
|
|
}
|
|
|
|
c.Ok = ok
|
2017-10-12 13:09:32 +08:00
|
|
|
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 {
|
2021-05-05 10:38:26 +08:00
|
|
|
me.m.Store(pk, b)
|
2016-07-12 14:45:22 +08:00
|
|
|
return nil
|
2016-06-20 15:51:05 +08:00
|
|
|
}
|