-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
167 lines (130 loc) · 2.49 KB
/
tx.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
165
166
167
package mossdb
import (
"errors"
"github.com/qingwave/mossdb/pkg/wal"
)
type Tx struct {
db *DB
commits []*Record
undos []*Record
}
func (tx *Tx) lock() {
tx.db.mu.Lock()
}
func (tx *Tx) unlock() {
tx.db.mu.Unlock()
}
func (db *DB) begin() (*Tx, error) {
if db.closed {
return nil, errors.New("mossDB has closed")
}
tx := &Tx{
db: db,
commits: make([]*Record, 0),
undos: make([]*Record, 0),
}
tx.lock()
return tx, nil
}
func (tx *Tx) commit() error {
if len(tx.commits) == 0 {
return nil
}
batch := new(wal.Batch)
for _, commit := range tx.commits {
data, err := commit.Encode()
if err != nil {
return err
}
batch.Write(data)
}
if n, err := tx.db.wal.WriteBatch(batch, wal.WithSync, wal.WithAtomic); err != nil {
if n > 0 {
tx.db.wal.Truncate(n)
}
tx.commits = nil
return err
}
// notify all commit
for _, commit := range tx.commits {
tx.db.notify(&Op{
key: string(commit.Key),
val: commit.Val,
op: OpType(commit.Op),
})
}
tx.commits = nil
tx.undos = nil
tx.unlock()
return nil
}
func (tx *Tx) rollBack() error {
tx.commits = nil
for _, undo := range tx.undos {
if err := tx.db.loadRecord(undo); err != nil {
return err
}
}
tx.unlock()
tx.undos = nil
tx.db = nil
return nil
}
func (tx *Tx) Get(key string, opts ...Option) Val {
val, _ := tx.db.store.Get(key)
return val
}
func (tx *Tx) List(opts ...Option) map[string][]byte {
op := listOption(opts...)
if op.all {
return tx.db.store.Dump()
}
if op.prefix && op.key != "" {
return tx.db.store.Prefix(op.key)
}
return make(map[string][]byte)
}
func (tx *Tx) Set(key string, val Val, opts ...Option) {
op := setOption(key, val, opts...)
undo := &Op{key: key}
old, ok := tx.db.store.Get(key)
if ok {
undo.op = ModifyOp
undo.val = old
} else {
undo.op = DeleteOp
}
tx.undos = append(tx.undos, NewRecord(undo))
tx.commits = append(tx.commits, NewRecord(op))
tx.db.set(op)
}
func (tx *Tx) Delete(key string, opts ...Option) {
op := deleteOption(key, opts...)
undo := &Op{key: key}
old, ok := tx.db.store.Get(key)
if ok {
undo.op = ModifyOp
undo.val = old
tx.undos = append(tx.undos, NewRecord(undo))
} else {
return
}
tx.commits = append(tx.commits, NewRecord(op))
tx.db.delete(op)
}
func (db *DB) Tx(f func(tx *Tx) error) error {
tx, err := db.begin()
if err != nil {
return err
}
defer func() {
if err != nil {
tx.rollBack()
}
}()
if err = f(tx); err != nil {
return err
}
err = tx.commit()
return err
}