forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbitdb_datastore_cache.go
47 lines (38 loc) · 1.08 KB
/
orbitdb_datastore_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package bertyprotocol
import (
"berty.tech/berty/v2/go/internal/ipfsutil"
"berty.tech/go-orbit-db/address"
"berty.tech/go-orbit-db/cache"
datastore "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
type datastoreCache struct {
ds datastore.Batching
}
func (d *datastoreCache) Load(directory string, dbAddress address.Address) (datastore.Datastore, error) {
return ipfsutil.NewNamespacedDatastore(d.ds, datastore.NewKey(dbAddress.String())), nil
}
func (d *datastoreCache) Close() error {
return nil
}
func (d *datastoreCache) Destroy(directory string, dbAddress address.Address) error {
keys, err := ipfsutil.NewNamespacedDatastore(d.ds, datastore.NewKey(dbAddress.String())).Query(query.Query{KeysOnly: true})
if err != nil {
return nil
}
for {
val, hasValue := keys.NextSync()
if !hasValue {
return nil
}
if err := d.ds.Delete(datastore.NewKey(val.Key)); err != nil {
return err
}
}
}
func NewOrbitDatastoreCache(ds datastore.Batching) cache.Interface {
return &datastoreCache{
ds: ds,
}
}
var _ cache.Interface = (*datastoreCache)(nil)