-
Notifications
You must be signed in to change notification settings - Fork 7
/
transaction.go
236 lines (193 loc) · 4.95 KB
/
transaction.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package keydb
import (
"errors"
"sync/atomic"
"time"
)
var txID uint64
// Transaction for keydb operations
type Transaction struct {
table string
open bool
db *Database
id uint64
multi *multiSegment
memory segment
}
type transactionLookup struct {
LookupIterator
}
// skip removed records
func (tl *transactionLookup) Next() (key, value []byte, err error) {
for {
key, value, err = tl.LookupIterator.Next()
if value == nil && err == nil {
continue
}
return
}
}
// GetID returns the internal transaction identifier
func (tx *Transaction) GetID() uint64 {
return tx.id
}
// BeginTX starts a transaction for a database table.
// a Transaction can only be used by a single Go routine.
// each transaction should be completed with either Commit, or Rollback
func (db *Database) BeginTX(table string) (*Transaction, error) {
db.Lock()
defer db.Unlock()
if db.err != nil {
return nil, db.err
}
if db.closing {
return nil, DatabaseClosed
}
it, ok := db.tables[table]
if !ok {
it = &internalTable{name: table, segments: loadDiskSegments(db.path, table)}
db.tables[table] = it
}
for { // wait to start transaction if table has too many segments
if len(it.segments) > maxSegments*10 {
db.Unlock()
time.Sleep(100 * time.Millisecond)
db.Lock()
} else {
break
}
}
it.Lock()
defer it.Unlock()
it.transactions++
tx := &Transaction{db: db, table: table, open: true}
tx.id = atomic.AddUint64(&txID, 1)
tx.memory = newMemorySegment()
tx.multi = newMultiSegment(append(it.segments, tx.memory))
db.transactions[tx.id] = tx
return tx, nil
}
// Get a value for a key, error is non-nil if the key was not found or an error occurred
func (tx *Transaction) Get(key []byte) (value []byte, err error) {
if !tx.open {
return nil, TransactionClosed
}
if len(key) > 1024 {
return nil, KeyTooLong
}
value, err = tx.multi.Get(key)
if err != nil {
return nil, err
}
if value == nil {
return nil, KeyNotFound
}
return
}
// Put a key/value pair into the table, overwriting any existing entry. empty keys are not supported.
func (tx *Transaction) Put(key []byte, value []byte) error {
if !tx.open {
return TransactionClosed
}
if len(key) > 1024 {
return KeyTooLong
}
if len(key) == 0 {
return EmptyKey
}
return tx.memory.Put(key, value)
}
// Remove a key and its value from the table. empty keys are not supported.
func (tx *Transaction) Remove(key []byte) ([]byte, error) {
if !tx.open {
return nil, TransactionClosed
}
if len(key) > 1024 {
return nil, KeyTooLong
}
value, err := tx.Get(key)
if err != nil {
return nil, err
}
tx.memory.Remove(key)
return value, nil
}
// Lookup finds matching record between lower and upper inclusive. lower or upper can be nil and
// and then the range is unbounded on that side. Using the iterator after the transaction has
// been Commit/Rollback is not supported.
func (tx *Transaction) Lookup(lower []byte, upper []byte) (LookupIterator, error) {
if !tx.open {
return nil, TransactionClosed
}
itr, err := tx.multi.Lookup(lower, upper)
if err != nil {
return nil, err
}
return &transactionLookup{itr}, nil
}
// Commit persists any changes to the table. after Commit the transaction can no longer be used
func (tx *Transaction) Commit() error {
if !tx.open {
return TransactionClosed
}
tx.db.Lock()
delete(tx.db.transactions, tx.id)
table := tx.db.tables[tx.table]
tx.open = false
tx.db.Unlock()
table.Lock()
defer table.Unlock()
table.transactions--
table.segments = append(table.segments, tx.memory)
tx.db.wg.Add(1)
go func() {
defer tx.db.wg.Done() // allows database to close with no writers pending
err := writeSegmentToDisk(tx.db, tx.table, tx.memory)
if err != nil {
tx.db.Lock()
tx.db.err = errors.New("transaction failed: " + err.Error())
tx.db.Unlock()
}
}()
return nil
}
// CommitSync persists any changes to the table, waiting for disk segment to be written. note that synchronous writes are not used,
// so that a hard OS failure could leave the database in a corrupted state. after Commit the transaction can no longer be used
func (tx *Transaction) CommitSync() error {
if !tx.open {
return TransactionClosed
}
tx.db.Lock()
delete(tx.db.transactions, tx.id)
table := tx.db.tables[tx.table]
tx.open = false
err := tx.db.err
tx.db.Unlock()
if err != nil {
return err
}
table.Lock()
table.transactions--
table.segments = append(table.segments, tx.memory)
tx.db.wg.Add(1)
table.Unlock()
err = writeSegmentToDisk(tx.db, tx.table, tx.memory)
tx.db.wg.Done() // allows database to close with no writers pending
return err
}
// Rollback discards any changes to the table. after Rollback the transaction can no longer be used
func (tx *Transaction) Rollback() error {
if !tx.open {
return TransactionClosed
}
tx.db.Lock()
defer tx.db.Unlock()
table := tx.db.tables[tx.table]
table.Lock()
tx.multi = nil
tx.open = false
delete(tx.db.transactions, tx.id)
defer table.Unlock()
table.transactions--
return nil
}