lotus/blockstore/splitstore/visitor.go

61 lines
1.0 KiB
Go
Raw Permalink Normal View History

2021-07-30 06:38:09 +00:00
package splitstore
import (
2022-01-25 15:29:02 +00:00
"sync"
"github.com/ipfs/go-cid"
2021-07-30 06:38:09 +00:00
)
// ObjectVisitor is an interface for deduplicating objects during walks
type ObjectVisitor interface {
Visit(cid.Cid) (bool, error)
}
type noopVisitor struct{}
var _ ObjectVisitor = (*noopVisitor)(nil)
func (v *noopVisitor) Visit(_ cid.Cid) (bool, error) {
return true, nil
}
2022-01-25 15:29:02 +00:00
type tmpVisitor struct {
2021-07-30 06:38:09 +00:00
set *cid.Set
}
2022-01-25 15:29:02 +00:00
var _ ObjectVisitor = (*tmpVisitor)(nil)
2021-07-30 06:38:09 +00:00
2022-01-25 15:29:02 +00:00
func (v *tmpVisitor) Visit(c cid.Cid) (bool, error) {
if isUnitaryObject(c) {
return false, nil
}
2021-07-30 06:38:09 +00:00
return v.set.Visit(c), nil
}
2022-01-25 15:29:02 +00:00
func newTmpVisitor() ObjectVisitor {
return &tmpVisitor{set: cid.NewSet()}
}
type concurrentVisitor struct {
mx sync.Mutex
set *cid.Set
}
var _ ObjectVisitor = (*concurrentVisitor)(nil)
func newConcurrentVisitor() *concurrentVisitor {
return &concurrentVisitor{set: cid.NewSet()}
}
func (v *concurrentVisitor) Visit(c cid.Cid) (bool, error) {
if isUnitaryObject(c) {
return false, nil
}
2022-01-25 15:29:02 +00:00
v.mx.Lock()
defer v.mx.Unlock()
return v.set.Visit(c), nil
}