2021-07-06 11:44:40 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
2021-07-09 01:26:36 +00:00
|
|
|
type MapMarkSetEnv struct{}
|
2021-07-06 11:44:40 +00:00
|
|
|
|
|
|
|
var _ MarkSetEnv = (*MapMarkSetEnv)(nil)
|
|
|
|
|
|
|
|
type MapMarkSet struct {
|
2021-07-08 07:20:29 +00:00
|
|
|
mx sync.RWMutex
|
2021-07-06 11:44:40 +00:00
|
|
|
set map[string]struct{}
|
|
|
|
|
|
|
|
ts bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ MarkSet = (*MapMarkSet)(nil)
|
2021-07-30 19:07:45 +00:00
|
|
|
var _ MarkSetVisitor = (*MapMarkSet)(nil)
|
2021-07-30 06:42:20 +00:00
|
|
|
|
2021-07-09 01:26:36 +00:00
|
|
|
func NewMapMarkSetEnv() (*MapMarkSetEnv, error) {
|
|
|
|
return &MapMarkSetEnv{}, nil
|
2021-07-06 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 19:07:45 +00:00
|
|
|
func (e *MapMarkSetEnv) create(name string, sizeHint int64) (*MapMarkSet, error) {
|
2021-07-06 11:44:40 +00:00
|
|
|
return &MapMarkSet{
|
|
|
|
set: make(map[string]struct{}, sizeHint),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:07:45 +00:00
|
|
|
func (e *MapMarkSetEnv) Create(name string, sizeHint int64) (MarkSet, error) {
|
|
|
|
return e.create(name, sizeHint)
|
|
|
|
}
|
|
|
|
|
2021-07-30 06:42:20 +00:00
|
|
|
func (e *MapMarkSetEnv) CreateVisitor(name string, sizeHint int64) (MarkSetVisitor, error) {
|
2021-07-30 19:07:45 +00:00
|
|
|
return e.create(name, sizeHint)
|
2021-07-30 06:42:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 07:27:32 +00:00
|
|
|
func (e *MapMarkSetEnv) SupportsVisitor() bool { return true }
|
|
|
|
|
2021-07-06 11:44:40 +00:00
|
|
|
func (e *MapMarkSetEnv) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MapMarkSet) Mark(cid cid.Cid) error {
|
|
|
|
if s.ts {
|
|
|
|
s.mx.Lock()
|
|
|
|
defer s.mx.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:18:43 +00:00
|
|
|
if s.set == nil {
|
|
|
|
return errMarkSetClosed
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:44:40 +00:00
|
|
|
s.set[string(cid.Hash())] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MapMarkSet) Has(cid cid.Cid) (bool, error) {
|
|
|
|
if s.ts {
|
2021-07-08 07:20:29 +00:00
|
|
|
s.mx.RLock()
|
|
|
|
defer s.mx.RUnlock()
|
2021-07-06 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 07:18:43 +00:00
|
|
|
if s.set == nil {
|
|
|
|
return false, errMarkSetClosed
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:44:40 +00:00
|
|
|
_, 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) {
|
2021-07-08 07:18:43 +00:00
|
|
|
if s.ts {
|
|
|
|
s.mx.Lock()
|
|
|
|
defer s.mx.Unlock()
|
|
|
|
}
|
2021-07-09 01:26:36 +00:00
|
|
|
|
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 {
|
|
|
|
if s.ts {
|
|
|
|
s.mx.Lock()
|
|
|
|
defer s.mx.Unlock()
|
|
|
|
}
|
|
|
|
s.set = nil
|
2021-07-30 06:42:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-30 19:07:45 +00:00
|
|
|
|
|
|
|
func (s *MapMarkSet) SetConcurrent() {
|
|
|
|
s.ts = true
|
|
|
|
}
|