lotus/blockstore/splitstore/markset_map.go

82 lines
1.3 KiB
Go
Raw Normal View History

package splitstore
import (
"sync"
cid "github.com/ipfs/go-cid"
)
type MapMarkSetEnv struct{}
var _ MarkSetEnv = (*MapMarkSetEnv)(nil)
type MapMarkSet struct {
2021-07-08 07:20:29 +00:00
mx sync.RWMutex
set map[string]struct{}
}
var _ MarkSet = (*MapMarkSet)(nil)
2021-07-30 06:42:20 +00:00
func NewMapMarkSetEnv() (*MapMarkSetEnv, error) {
return &MapMarkSetEnv{}, nil
}
func (e *MapMarkSetEnv) Create(name string, sizeHint int64) (MarkSet, error) {
return &MapMarkSet{
set: make(map[string]struct{}, sizeHint),
}, nil
}
func (e *MapMarkSetEnv) Close() error {
return nil
}
func (s *MapMarkSet) Mark(cid cid.Cid) error {
s.mx.Lock()
defer s.mx.Unlock()
if s.set == nil {
return errMarkSetClosed
}
s.set[string(cid.Hash())] = struct{}{}
return nil
}
func (s *MapMarkSet) Has(cid cid.Cid) (bool, error) {
s.mx.RLock()
defer s.mx.RUnlock()
if s.set == nil {
return false, errMarkSetClosed
}
_, ok := s.set[string(cid.Hash())]
return ok, nil
}
2021-07-30 19:07:45 +00:00
func (s *MapMarkSet) Visit(c cid.Cid) (bool, error) {
s.mx.Lock()
defer s.mx.Unlock()
2021-07-30 19:07:45 +00:00
if s.set == nil {
2021-07-30 06:42:20 +00:00
return false, errMarkSetClosed
}
key := string(c.Hash())
2021-07-30 19:07:45 +00:00
if _, ok := s.set[key]; ok {
2021-07-30 06:42:20 +00:00
return false, nil
}
2021-07-30 19:07:45 +00:00
s.set[key] = struct{}{}
2021-07-30 06:42:20 +00:00
return true, nil
}
2021-07-30 19:07:45 +00:00
func (s *MapMarkSet) Close() error {
s.mx.Lock()
defer s.mx.Unlock()
2021-07-30 19:07:45 +00:00
s.set = nil
2021-07-30 06:42:20 +00:00
return nil
}