@@ -2,6 +2,7 @@ package keytransform
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
6
7
ds "github.com/ipfs/go-datastore"
7
8
dsq "github.com/ipfs/go-datastore/query"
@@ -31,6 +32,7 @@ type Datastore struct {
31
32
32
33
var _ ds.Datastore = (* Datastore )(nil )
33
34
var _ ds.Batching = (* Datastore )(nil )
35
+ var _ ds.TxnDatastore = (* Datastore )(nil )
34
36
var _ ds.Shim = (* Datastore )(nil )
35
37
var _ ds.PersistentDatastore = (* Datastore )(nil )
36
38
var _ ds.CheckedDatastore = (* Datastore )(nil )
@@ -224,6 +226,23 @@ func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error) {
224
226
}, nil
225
227
}
226
228
229
+ func (d * Datastore ) NewTransaction (ctx context.Context , readOnly bool ) (ds.Txn , error ) {
230
+ tds , ok := d .child .(ds.TxnDatastore )
231
+ if ! ok {
232
+ return nil , fmt .Errorf ("txn not supported" )
233
+ }
234
+
235
+ childTxn , err := tds .NewTransaction (ctx , readOnly )
236
+ if err != nil {
237
+ return nil , err
238
+ }
239
+
240
+ return & transformTxn {
241
+ dst : childTxn ,
242
+ f : d .ConvertKey ,
243
+ }, nil
244
+ }
245
+
227
246
type transformBatch struct {
228
247
dst ds.Batch
229
248
@@ -244,6 +263,47 @@ func (t *transformBatch) Commit(ctx context.Context) error {
244
263
return t .dst .Commit (ctx )
245
264
}
246
265
266
+ type transformTxn struct {
267
+ ds * Datastore
268
+ dst ds.Txn
269
+
270
+ f KeyMapping
271
+ }
272
+
273
+ var _ ds.Txn = (* transformTxn )(nil )
274
+
275
+ func (t * transformTxn ) Put (ctx context.Context , key ds.Key , val []byte ) error {
276
+ return t .dst .Put (ctx , t .f (key ), val )
277
+ }
278
+
279
+ func (t * transformTxn ) Delete (ctx context.Context , key ds.Key ) error {
280
+ return t .dst .Delete (ctx , t .f (key ))
281
+ }
282
+
283
+ func (t * transformTxn ) Commit (ctx context.Context ) error {
284
+ return t .dst .Commit (ctx )
285
+ }
286
+
287
+ func (t * transformTxn ) Get (ctx context.Context , key ds.Key ) (value []byte , err error ) {
288
+ return t .dst .Get (ctx , t .f (key ))
289
+ }
290
+
291
+ func (t * transformTxn ) Has (ctx context.Context , key ds.Key ) (exists bool , err error ) {
292
+ return t .dst .Has (ctx , t .f (key ))
293
+ }
294
+
295
+ func (t * transformTxn ) GetSize (ctx context.Context , key ds.Key ) (size int , err error ) {
296
+ return t .dst .GetSize (ctx , t .f (key ))
297
+ }
298
+
299
+ func (t * transformTxn ) Query (ctx context.Context , q dsq.Query ) (dsq.Results , error ) {
300
+ return t .ds .Query (ctx , q )
301
+ }
302
+
303
+ func (t * transformTxn ) Discard (ctx context.Context ) {
304
+ t .dst .Discard (ctx )
305
+ }
306
+
247
307
func (d * Datastore ) Check (ctx context.Context ) error {
248
308
if c , ok := d .child .(ds.CheckedDatastore ); ok {
249
309
return c .Check (ctx )
0 commit comments