forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
386 lines (316 loc) · 9.53 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2019 The nutsdb Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"errors"
"strings"
"time"
"github.com/bwmarrin/snowflake"
"github.com/xujiajun/nutsdb/ds/list"
"github.com/xujiajun/nutsdb/ds/set"
"github.com/xujiajun/nutsdb/ds/zset"
"github.com/xujiajun/utils/strconv2"
)
var (
// ErrKeyAndValSize is returned when given key and value size is too big.
ErrKeyAndValSize = errors.New("key and value size too big")
// ErrTxClosed is returned when committing or rolling back a transaction
// that has already been committed or rolled back.
ErrTxClosed = errors.New("tx is closed")
// ErrTxNotWritable is returned when performing a write operation on
// a read-only transaction.
ErrTxNotWritable = errors.New("tx not writable")
// ErrKeyEmpty is returned if an empty key is passed on an update function.
ErrKeyEmpty = errors.New("key cannot be empty")
// ErrRangeScan is returned when range scanning not found the result
ErrRangeScan = errors.New("range scans not found")
// ErrPrefixScan is returned when prefix scanning not found the result
ErrPrefixScan = errors.New("prefix scans not found")
// ErrNotFoundKey is returned when key not found int the bucket on an view function.
ErrNotFoundKey = errors.New("key not found in the bucket")
)
// Tx represents a transaction.
type Tx struct {
id uint64
db *DB
writable bool
pendingWrites []*Entry
}
// Begin opens a new transaction.
// Multiple read-only transactions can be opened at the same time but there can
// only be one read/write transaction at a time. Attempting to open a read/write
// transactions while another one is in progress will result in blocking until
// the current read/write transaction is completed.
// All transactions must be closed by calling Commit() or Rollback() when done.
func (db *DB) Begin(writable bool) (tx *Tx, err error) {
tx, err = newTx(db, writable)
if err != nil {
return nil, err
}
tx.lock()
if db.closed {
tx.unlock()
return nil, ErrDBClosed
}
return
}
// newTx returns a newly initialized Tx object at given writable.
func newTx(db *DB, writable bool) (tx *Tx, err error) {
var txID uint64
tx = &Tx{
db: db,
writable: writable,
pendingWrites: []*Entry{},
}
txID, err = tx.getTxID()
if err != nil {
return nil, err
}
tx.id = txID
return
}
// getTxID returns the tx id.
func (tx *Tx) getTxID() (id uint64, err error) {
node, err := snowflake.NewNode(tx.db.opt.NodeNum)
if err != nil {
return 0, err
}
id = uint64(node.Generate().Int64())
return
}
// Commit commits the transaction, following these steps:
//
// 1. check the length of pendingWrites.If there are no writes, return immediately.
//
// 2. check if the ActiveFile has not enough space to store entry. if not, call rotateActiveFile function.
//
// 3. write pendingWrites to disk, if a non-nil error,return the error.
//
// 4. build Hint index.
//
// 5. Unlock the database and clear the db field.
func (tx *Tx) Commit() error {
var e *Entry
if tx.db == nil {
return ErrDBClosed
}
writesLen := len(tx.pendingWrites)
if writesLen == 0 {
tx.unlock()
tx.db = nil
return nil
}
for i := 0; i < writesLen; i++ {
entry := tx.pendingWrites[i]
entrySize := entry.Size()
if entrySize > tx.db.opt.SegmentSize {
return ErrKeyAndValSize
}
if tx.db.ActiveFile.ActualSize+entrySize > tx.db.opt.SegmentSize {
if err := tx.rotateActiveFile(); err != nil {
return err
}
}
if i == writesLen-1 {
entry.Meta.status = Committed
}
off := tx.db.ActiveFile.writeOff
if _, err := tx.db.ActiveFile.WriteAt(entry.Encode(), off); err != nil {
return err
}
tx.db.ActiveFile.ActualSize += entrySize
tx.db.ActiveFile.writeOff += entrySize
e = nil
if tx.db.opt.EntryIdxMode == HintAndRAMIdxMode {
entry.Meta.status = Committed
e = entry
}
countFlag := CountFlagEnabled
if tx.db.isMerging {
countFlag = CountFlagDisabled
}
bucket := string(entry.Meta.bucket)
if entry.Meta.ds == DataStructureBPTree {
tx.buildBPTreeIdx(bucket, entry, e, off, countFlag)
}
if entry.Meta.ds == DataStructureSet {
tx.buildSetIdx(bucket, entry)
}
if entry.Meta.ds == DataStructureSortedSet {
tx.buildSortedSetIdx(bucket, entry)
}
if entry.Meta.ds == DataStructureList {
tx.buildListIdx(bucket, entry)
}
tx.db.KeyCount++
}
tx.unlock()
tx.db = nil
return nil
}
func (tx *Tx) buildBPTreeIdx(bucket string, entry, e *Entry, off int64, countFlag bool) {
if _, ok := tx.db.BPTreeIdx[bucket]; !ok {
tx.db.BPTreeIdx[bucket] = NewTree()
}
_ = tx.db.BPTreeIdx[bucket].Insert(entry.Key, e, &Hint{
fileID: tx.db.ActiveFile.fileID,
key: entry.Key,
meta: entry.Meta,
dataPos: uint64(off),
}, countFlag)
}
func (tx *Tx) buildSetIdx(bucket string, entry *Entry) {
if _, ok := tx.db.SetIdx[bucket]; !ok {
tx.db.SetIdx[bucket] = set.New()
}
if entry.Meta.Flag == DataDeleteFlag {
_ = tx.db.SetIdx[bucket].SRem(string(entry.Key), entry.Value)
}
if entry.Meta.Flag == DataSetFlag {
_ = tx.db.SetIdx[bucket].SAdd(string(entry.Key), entry.Value)
}
}
func (tx *Tx) buildSortedSetIdx(bucket string, entry *Entry) {
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
tx.db.SortedSetIdx[bucket] = zset.New()
}
switch entry.Meta.Flag {
case DataZAddFlag:
keyAndScore := strings.Split(string(entry.Key), SeparatorForZSetKey)
key := keyAndScore[0]
score, _ := strconv2.StrToFloat64(keyAndScore[1])
_ = tx.db.SortedSetIdx[bucket].Put(key, zset.SCORE(score), entry.Value)
case DataZRemFlag:
_ = tx.db.SortedSetIdx[bucket].Remove(string(entry.Key))
case DataZRemRangeByRankFlag:
start, _ := strconv2.StrToInt(string(entry.Key))
end, _ := strconv2.StrToInt(string(entry.Value))
_ = tx.db.SortedSetIdx[bucket].GetByRankRange(start, end, true)
case DataZPopMaxFlag:
_ = tx.db.SortedSetIdx[bucket].PopMax()
case DataZPopMinFlag:
_ = tx.db.SortedSetIdx[bucket].PopMin()
}
}
func (tx *Tx) buildListIdx(bucket string, entry *Entry) {
if _, ok := tx.db.ListIdx[bucket]; !ok {
tx.db.ListIdx[bucket] = list.New()
}
key, value := entry.Key, entry.Value
switch entry.Meta.Flag {
case DataLPushFlag:
_, _ = tx.db.ListIdx[bucket].LPush(string(key), value)
case DataRPushFlag:
_, _ = tx.db.ListIdx[bucket].RPush(string(key), value)
case DataLRemFlag:
count, _ := strconv2.StrToInt(string(value))
_, _ = tx.db.ListIdx[bucket].LRem(string(key), count)
case DataLPopFlag:
_, _ = tx.db.ListIdx[bucket].LPop(string(key))
case DataRPopFlag:
_, _ = tx.db.ListIdx[bucket].RPop(string(key))
case DataLSetFlag:
keyAndIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndIndex[0]
index, _ := strconv2.StrToInt(keyAndIndex[1])
_ = tx.db.ListIdx[bucket].LSet(newKey, index, value)
case DataLTrimFlag:
keyAndStartIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndStartIndex[0]
start, _ := strconv2.StrToInt(keyAndStartIndex[1])
end, _ := strconv2.StrToInt(string(value))
_ = tx.db.ListIdx[bucket].Ltrim(newKey, start, end)
}
}
// rotateActiveFile rotates log file when active file is not enough space to store the entry.
func (tx *Tx) rotateActiveFile() error {
var err error
tx.db.MaxFileID++
if err := tx.db.ActiveFile.m.Unmap(); err != nil {
return err
}
path := tx.db.getDataPath(tx.db.MaxFileID)
tx.db.ActiveFile, err = NewDataFile(path, tx.db.opt.SegmentSize)
if err != nil {
return err
}
tx.db.ActiveFile.fileID = tx.db.MaxFileID
return nil
}
// Rollback closes the transaction.
func (tx *Tx) Rollback() error {
if tx.db == nil {
return ErrDBClosed
}
tx.unlock()
tx.db = nil
return nil
}
// lock locks the database based on the transaction type.
func (tx *Tx) lock() {
if tx.writable {
tx.db.mu.Lock()
} else {
tx.db.mu.RLock()
}
}
// unlock unlocks the database based on the transaction type.
func (tx *Tx) unlock() {
if tx.writable {
tx.db.mu.Unlock()
} else {
tx.db.mu.RUnlock()
}
}
// Put sets the value for a key in the bucket.
// a wrapper of the function put.
func (tx *Tx) Put(bucket string, key, value []byte, ttl uint32) error {
return tx.put(bucket, key, value, ttl, DataSetFlag, uint64(time.Now().Unix()), DataStructureBPTree)
}
func (tx *Tx) checkTxIsClosed() error {
if tx.db == nil {
return ErrTxClosed
}
return nil
}
// put sets the value for a key in the bucket.
// Returns an error if tx is closed, if performing a write operation on a read-only transaction, if the key is empty.
func (tx *Tx) put(bucket string, key, value []byte, ttl uint32, flag uint16, timestamp uint64, ds uint16) error {
if err := tx.checkTxIsClosed(); err != nil {
return err
}
if !tx.writable {
return ErrTxNotWritable
}
if len(key) == 0 {
return ErrKeyEmpty
}
tx.pendingWrites = append(tx.pendingWrites, &Entry{
Key: key,
Value: value,
Meta: &MetaData{
keySize: uint32(len(key)),
valueSize: uint32(len(value)),
timestamp: timestamp,
Flag: flag,
TTL: ttl,
bucket: []byte(bucket),
bucketSize: uint32(len(bucket)),
status: UnCommitted,
ds: ds,
txID: tx.id,
},
})
return nil
}