-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtxn.go
164 lines (139 loc) · 3.45 KB
/
txn.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mongods
import (
"context"
"errors"
"fmt"
"sync"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
dsextensions "github.com/textileio/go-datastore-extensions"
"go.mongodb.org/mongo-driver/mongo"
)
var (
ErrTxnFinalized = errors.New("txn was already finalized")
)
type mongoTxn struct {
// lock serializes all API access since the
// mongo session isn't goroutine-safe as mentioned
// in the docs.
lock sync.Mutex
finalized bool
m *MongoDS
session mongo.Session
ctx mongo.SessionContext
}
var _ dsextensions.TxnExt = (*mongoTxn)(nil)
func (m *MongoDS) NewTransaction(readOnly bool) (datastore.Txn, error) {
return m.newTransaction(readOnly)
}
func (m *MongoDS) NewTransactionExtended(readOnly bool) (dsextensions.TxnExt, error) {
return m.newTransaction(readOnly)
}
func (m *MongoDS) newTransaction(bool) (dsextensions.TxnExt, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if m.closed {
return nil, ErrClosed
}
session, err := m.m.StartSession()
if err != nil {
return nil, fmt.Errorf("starting mongo session: %s", err)
}
if err := session.StartTransaction(); err != nil {
return nil, fmt.Errorf("starting session txn: %s", err)
}
return &mongoTxn{
session: session,
m: m,
ctx: mongo.NewSessionContext(context.Background(), session),
}, nil
}
func (t *mongoTxn) Commit() error {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return ErrTxnFinalized
}
ctx, cls := context.WithTimeout(context.Background(), t.m.txnTimeout)
defer cls()
if err := t.session.CommitTransaction(ctx); err != nil {
return fmt.Errorf("commiting session txn: %s", err)
}
t.finalized = true
ctx, cls = context.WithTimeout(context.Background(), t.m.opTimeout)
defer cls()
t.session.EndSession(ctx)
return nil
}
func (t *mongoTxn) Discard() {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return
}
ctx, cls := context.WithTimeout(context.Background(), t.m.txnTimeout)
defer cls()
if err := t.session.AbortTransaction(ctx); err != nil {
log.Errorf("aborting transaction: %s", err)
}
ctx, cls = context.WithTimeout(context.Background(), t.m.opTimeout)
defer cls()
t.session.EndSession(ctx)
}
func (t *mongoTxn) Get(key datastore.Key) ([]byte, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return nil, ErrTxnFinalized
}
return t.m.get(t.ctx, key)
}
func (t *mongoTxn) Has(key datastore.Key) (bool, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return false, ErrTxnFinalized
}
return t.m.has(t.ctx, key)
}
func (t *mongoTxn) GetSize(key datastore.Key) (int, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return 0, ErrTxnFinalized
}
return t.m.getSize(t.ctx, key)
}
func (t *mongoTxn) Query(q query.Query) (query.Results, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return nil, ErrTxnFinalized
}
qe := dsextensions.QueryExt{Query: q}
return t.m.query(t.ctx, qe)
}
func (t *mongoTxn) QueryExtended(q dsextensions.QueryExt) (query.Results, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return nil, ErrTxnFinalized
}
return t.m.query(t.ctx, q)
}
func (t *mongoTxn) Delete(key datastore.Key) error {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return ErrClosed
}
return t.m.delete(t.ctx, key)
}
func (t *mongoTxn) Put(key datastore.Key, val []byte) error {
t.lock.Lock()
defer t.lock.Unlock()
if t.finalized {
return ErrClosed
}
return t.m.put(t.ctx, key, val)
}