-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtxn.go
More file actions
78 lines (62 loc) · 1.66 KB
/
txn.go
File metadata and controls
78 lines (62 loc) · 1.66 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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,
}
}
// 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
}
// 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
}
// makes a txn a datastore
type wrappedTxn struct {
ds.Txn
}
func (wtxn *wrappedTxn) Close() error {
return nil
}
func (wtxn *wrappedTxn) Sync(context.Context, ds.Key) error {
return nil
}
// 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)
}
func (wkt *wrappedKt) Discard(ctx context.Context) {
wkt.txn.Discard(ctx)
}