forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
orbitdb_datastore_cache.go
52 lines (41 loc) · 1.24 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
48
49
50
51
52
package weshnet
import (
"context"
datastore "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"berty.tech/go-orbit-db/address"
"berty.tech/go-orbit-db/cache"
"berty.tech/weshnet/v2/internal/datastoreutil"
)
type datastoreCache struct {
ds datastore.Batching
}
//nolint:revive
func (d *datastoreCache) Load(directory string, dbAddress address.Address) (datastore.Datastore, error) {
return datastoreutil.NewNamespacedDatastore(d.ds, datastore.NewKey(dbAddress.String())), nil
}
func (d *datastoreCache) Close() error {
return nil
}
//nolint:revive
func (d *datastoreCache) Destroy(directory string, dbAddress address.Address) error {
keys, err := datastoreutil.NewNamespacedDatastore(d.ds, datastore.NewKey(dbAddress.String())).Query(context.TODO(), query.Query{KeysOnly: true})
if err != nil {
return nil
}
for {
val, hasValue := keys.NextSync()
if !hasValue {
return nil
}
if err := d.ds.Delete(context.TODO(), datastore.NewKey(val.Key)); err != nil {
return err
}
}
}
func NewOrbitDatastoreCache(ds datastore.Batching) cache.Interface {
return &datastoreCache{
ds: datastoreutil.NewNamespacedDatastore(ds, datastore.NewKey(NamespaceOrbitDBDatastore)),
}
}
var _ cache.Interface = (*datastoreCache)(nil)