2021-07-30 06:38:09 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
2022-01-25 15:29:02 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-06-15 10:06:22 +00:00
|
|
|
"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) {
|
2022-02-18 10:27:46 +00:00
|
|
|
if isUnitaryObject(c) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 06:38:09 +00:00
|
|
|
return v.set.Visit(c), nil
|
|
|
|
}
|
2021-07-30 06:51:09 +00:00
|
|
|
|
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) {
|
2022-02-18 10:27:46 +00:00
|
|
|
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
|
2021-07-30 06:51:09 +00:00
|
|
|
}
|