Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions keytransform/keytransform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keytransform

import (
"context"
"fmt"

ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
Expand Down Expand Up @@ -31,6 +32,7 @@ type Datastore struct {

var _ ds.Datastore = (*Datastore)(nil)
var _ ds.Batching = (*Datastore)(nil)
var _ ds.TxnDatastore = (*Datastore)(nil)
var _ ds.Shim = (*Datastore)(nil)
var _ ds.PersistentDatastore = (*Datastore)(nil)
var _ ds.CheckedDatastore = (*Datastore)(nil)
Expand Down Expand Up @@ -224,6 +226,23 @@ func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error) {
}, nil
}

func (d *Datastore) NewTransaction(ctx context.Context, readOnly bool) (ds.Txn, error) {
tds, ok := d.child.(ds.TxnDatastore)
if !ok {
return nil, fmt.Errorf("txn not supported")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer errors.New when no formatting. Also want to avoid abbreviations in message.

Suggested change
return nil, fmt.Errorf("txn not supported")
return nil, errors.New("transaction not supported")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

childTxn, err := tds.NewTransaction(ctx, readOnly)
if err != nil {
return nil, err
}

return &transformTxn{
dst: childTxn,
f: d.ConvertKey,
}, nil
}

type transformBatch struct {
dst ds.Batch

Expand All @@ -244,6 +263,47 @@ func (t *transformBatch) Commit(ctx context.Context) error {
return t.dst.Commit(ctx)
}

type transformTxn struct {
ds *Datastore
dst ds.Txn

f KeyMapping
}

var _ ds.Txn = (*transformTxn)(nil)

func (t *transformTxn) Put(ctx context.Context, key ds.Key, val []byte) error {
return t.dst.Put(ctx, t.f(key), val)
}

func (t *transformTxn) Delete(ctx context.Context, key ds.Key) error {
return t.dst.Delete(ctx, t.f(key))
}

func (t *transformTxn) Commit(ctx context.Context) error {
return t.dst.Commit(ctx)
}

func (t *transformTxn) Get(ctx context.Context, key ds.Key) (value []byte, err error) {
return t.dst.Get(ctx, t.f(key))
}

func (t *transformTxn) Has(ctx context.Context, key ds.Key) (exists bool, err error) {
return t.dst.Has(ctx, t.f(key))
}

func (t *transformTxn) GetSize(ctx context.Context, key ds.Key) (size int, err error) {
return t.dst.GetSize(ctx, t.f(key))
}

func (t *transformTxn) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
return t.ds.Query(ctx, q)
}

func (t *transformTxn) Discard(ctx context.Context) {
t.dst.Discard(ctx)
}

func (d *Datastore) Check(ctx context.Context) error {
if c, ok := d.child.(ds.CheckedDatastore); ok {
return c.Check(ctx)
Expand Down
Loading