Skip to content

Commit

Permalink
Example on how to wrap a transaction datastore in a key-transform dat…
Browse files Browse the repository at this point in the history
…astore
  • Loading branch information
hsanjuan committed Oct 25, 2023
1 parent 04c7b6f commit 6c4f847
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions keytransform/txn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package keytransform

import (
"context"

ds "github.com/ipfs/go-datastore"
)

var _ ds.TxnDatastore = (*TxnKeyTransformDatastore)(nil)

// All methods are provided by embedded *Datastore except NewTransaction.
type TxnKeyTransformDatastore struct {
*Datastore
txnChild ds.TxnDatastore
t KeyTransform
}

func WrapTxn(child ds.TxnDatastore, t KeyTransform) ds.TxnDatastore {
kt := Wrap(child, t)

return &TxnKeyTransformDatastore{
Datastore: kt,
txnChild: child,
t: t,
}

Check warning on line 25 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L18-L25

Added lines #L18 - L25 were not covered by tests
}

// NewTransaction returns a Txn that runs all Read/Write operations through a key transform datastore
// backed by the original txn.
func (txnktds *TxnKeyTransformDatastore) NewTransaction(ctx context.Context, readOnly bool) (ds.Txn, error) {
// New transaction - normal
txn, err := txnktds.txnChild.NewTransaction(ctx, readOnly)
if err != nil {
return nil, err
}

Check warning on line 35 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L30-L35

Added lines #L30 - L35 were not covered by tests

// wrap it in an object that augments it to be a Datastore
txnKt := &wrappedTxn{
Txn: txn,
}

// Make that object a key-transform datastore with the original KeyTransform.
kt := Wrap(txnKt, txnktds.t)

// Finally, ensure that this datastore provides Commit and Discard() so that it can be
// a transaction.
return &wrappedKt{
Datastore: kt,
txn: txnKt,
}, nil

Check warning on line 50 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L38-L50

Added lines #L38 - L50 were not covered by tests
}

// makes a txn a datastore
type wrappedTxn struct {
ds.Txn
}

func (wtxn *wrappedTxn) Close() error {
return nil

Check warning on line 59 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}

func (wtxn *wrappedTxn) Sync(context.Context, ds.Key) error {
return nil

Check warning on line 63 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}

// makes a datastore a tnx
type wrappedKt struct {
*Datastore
txn ds.Txn
}

func (wkt *wrappedKt) Commit(ctx context.Context) error {
return wkt.txn.Commit(ctx)

Check warning on line 73 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L72-L73

Added lines #L72 - L73 were not covered by tests
}

func (wkt *wrappedKt) Discard(ctx context.Context) {
wkt.txn.Discard(ctx)

Check warning on line 77 in keytransform/txn.go

View check run for this annotation

Codecov / codecov/patch

keytransform/txn.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

0 comments on commit 6c4f847

Please sign in to comment.