Skip to content

Commit d1138db

Browse files
authored
feat(keytransform): support transaction feature (#239)
* feat(keytransform): support transction feature * avoid conversion if already converted
1 parent 1ce683b commit d1138db

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

keytransform/keytransform.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keytransform
22

33
import (
44
"context"
5+
"errors"
56

67
ds "github.com/ipfs/go-datastore"
78
dsq "github.com/ipfs/go-datastore/query"
@@ -31,6 +32,7 @@ type Datastore struct {
3132

3233
var _ ds.Datastore = (*Datastore)(nil)
3334
var _ ds.Batching = (*Datastore)(nil)
35+
var _ ds.TxnDatastore = (*Datastore)(nil)
3436
var _ ds.Shim = (*Datastore)(nil)
3537
var _ ds.PersistentDatastore = (*Datastore)(nil)
3638
var _ ds.CheckedDatastore = (*Datastore)(nil)
@@ -223,6 +225,23 @@ func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error) {
223225
}, nil
224226
}
225227

228+
func (d *Datastore) NewTransaction(ctx context.Context, readOnly bool) (ds.Txn, error) {
229+
tds, ok := d.child.(ds.TxnDatastore)
230+
if !ok {
231+
return nil, errors.New("keytransform: transaction feature not supported")
232+
}
233+
234+
childTxn, err := tds.NewTransaction(ctx, readOnly)
235+
if err != nil {
236+
return nil, err
237+
}
238+
239+
return &transformTxn{
240+
dst: childTxn,
241+
f: d.ConvertKey,
242+
}, nil
243+
}
244+
226245
type transformBatch struct {
227246
dst ds.Batch
228247

@@ -243,6 +262,47 @@ func (t *transformBatch) Commit(ctx context.Context) error {
243262
return t.dst.Commit(ctx)
244263
}
245264

265+
type transformTxn struct {
266+
ds *Datastore
267+
dst ds.Txn
268+
269+
f KeyMapping
270+
}
271+
272+
var _ ds.Txn = (*transformTxn)(nil)
273+
274+
func (t *transformTxn) Put(ctx context.Context, key ds.Key, val []byte) error {
275+
return t.dst.Put(ctx, t.f(key), val)
276+
}
277+
278+
func (t *transformTxn) Delete(ctx context.Context, key ds.Key) error {
279+
return t.dst.Delete(ctx, t.f(key))
280+
}
281+
282+
func (t *transformTxn) Commit(ctx context.Context) error {
283+
return t.dst.Commit(ctx)
284+
}
285+
286+
func (t *transformTxn) Get(ctx context.Context, key ds.Key) (value []byte, err error) {
287+
return t.dst.Get(ctx, t.f(key))
288+
}
289+
290+
func (t *transformTxn) Has(ctx context.Context, key ds.Key) (exists bool, err error) {
291+
return t.dst.Has(ctx, t.f(key))
292+
}
293+
294+
func (t *transformTxn) GetSize(ctx context.Context, key ds.Key) (size int, err error) {
295+
return t.dst.GetSize(ctx, t.f(key))
296+
}
297+
298+
func (t *transformTxn) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
299+
return t.ds.Query(ctx, q)
300+
}
301+
302+
func (t *transformTxn) Discard(ctx context.Context) {
303+
t.dst.Discard(ctx)
304+
}
305+
246306
func (d *Datastore) Check(ctx context.Context) error {
247307
if c, ok := d.child.(ds.CheckedDatastore); ok {
248308
return c.Check(ctx)

keytransform/transforms.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ type PrefixTransform struct {
2929

3030
// ConvertKey adds the prefix.
3131
func (p PrefixTransform) ConvertKey(k ds.Key) ds.Key {
32+
if p.Prefix.IsAncestorOf(k) {
33+
return k
34+
}
35+
3236
return p.Prefix.Child(k)
3337
}
3438

0 commit comments

Comments
 (0)