-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
72 lines (62 loc) · 1.69 KB
/
transactions.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
package gostore
// Transaction is an atomic operation or set of operations on the store.
type Transaction struct {
tid TransactionID
}
// New Transaction creates a new transaction and returns it.
func NewTransaction() Transaction {
t := Transaction{lmInstance.nextTransactionID()}
lmInstance.beginTransaction(t.tid)
return t
}
// Commit commits and ends Transaction.
func (t Transaction) Commit() (err error) {
return lmInstance.commitTransaction(t.tid)
}
// Commit aborts and ends Transaction.
func (t Transaction) Abort() (err error) {
return lmInstance.abortTransaction(t.tid)
}
// Get retrieves the value of a key in Transaction.
func (t Transaction) Get(key Key) (value Value, err error) {
return lmInstance.getValue(t.tid, key)
}
// Set sets the value of a key in Transaction.
func (t Transaction) Set(key Key, value Value) (err error) {
return lmInstance.setValue(t.tid, key, value)
}
// Delete deletes a key in Transaction.
func (t Transaction) Delete(key Key) (err error) {
return lmInstance.deleteValue(t.tid, key)
}
// Get retrieves the value of a key in a new single-operation transaction.
func Get(key Key) (value Value, err error) {
t := NewTransaction()
value, err = t.Get(key)
if err != nil {
t.Abort()
return
}
err = t.Commit()
return
}
// Set sets the value of a key in a new single-operation transaction.
func Set(key Key, value Value) (err error) {
t := NewTransaction()
if err = t.Set(key, value); err != nil {
t.Abort()
return
}
err = t.Commit()
return
}
// Delete deletes a key in a new single-operation transaction.
func Delete(key Key) (err error) {
t := NewTransaction()
if err = t.Delete(key); err != nil {
t.Abort()
return
}
err = t.Commit()
return
}