forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pending.go
105 lines (94 loc) · 3.04 KB
/
pending.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
package nutsdb
// EntryStatus represents the Entry status in the current Tx
type EntryStatus = uint8
const (
// NotFoundEntry means there is no changes for this entry in current Tx
NotFoundEntry EntryStatus = 0
// EntryDeleted means this Entry has been deleted in the current Tx
EntryDeleted EntryStatus = 1
// EntryUpdated means this Entry has been updated in the current Tx
EntryUpdated EntryStatus = 2
)
// BucketStatus represents the current status of bucket in current Tx
type BucketStatus = uint8
const (
// BucketStatusExistAlready means this bucket already exists
BucketStatusExistAlready = 1
// BucketStatusDeleted means this bucket is already deleted
BucketStatusDeleted = 2
// BucketStatusNew means this bucket is created in current Tx
BucketStatusNew = 3
// BucketStatusUpdated means this bucket is updated in current Tx
BucketStatusUpdated = 4
// BucketStatusUnknown means this bucket doesn't exist
BucketStatusUnknown = 5
)
// pendingBucketList the uncommitted bucket changes in this Tx
type pendingBucketList map[Ds]map[BucketName]*Bucket
// pendingEntriesInBTree means the changes Entries in DataStructureBTree in the Tx
type pendingEntriesInBTree map[BucketName]map[string]*Entry
// pendingEntryList the uncommitted Entry changes in this Tx
type pendingEntryList struct {
entriesInBTree pendingEntriesInBTree
entries map[Ds]map[BucketName][]*Entry
size int
}
// newPendingEntriesList create a new pendingEntryList object for a Tx
func newPendingEntriesList() *pendingEntryList {
pending := &pendingEntryList{
entriesInBTree: map[BucketName]map[string]*Entry{},
entries: map[Ds]map[BucketName][]*Entry{},
size: 0,
}
return pending
}
// submitEntry submit an entry into pendingEntryList
func (pending *pendingEntryList) submitEntry(ds Ds, bucket string, e *Entry) {
switch ds {
case DataStructureBTree:
if _, exist := pending.entriesInBTree[bucket]; !exist {
pending.entriesInBTree[bucket] = map[string]*Entry{}
}
pending.entriesInBTree[bucket][string(e.Key)] = e
default:
if _, exist := pending.entries[ds]; !exist {
pending.entries[ds] = map[BucketName][]*Entry{}
}
entries := pending.entries[ds][bucket]
entries = append(entries, e)
pending.entries[ds][bucket] = entries
}
pending.size++
}
// rangeBucket input a range handler function f and call it with every bucket in pendingBucketList
func (p pendingBucketList) rangeBucket(f func(bucket *Bucket) error) error {
for _, bucketsInDs := range p {
for _, bucket := range bucketsInDs {
err := f(bucket)
if err != nil {
return err
}
}
}
return nil
}
// toList collect all the entries in pendingEntryList to a list.
func (pending *pendingEntryList) toList() []*Entry {
list := make([]*Entry, pending.size)
var i int
for _, entriesInBucket := range pending.entriesInBTree {
for _, entry := range entriesInBucket {
list[i] = entry
i++
}
}
for _, entriesInDS := range pending.entries {
for _, entries := range entriesInDS {
for _, entry := range entries {
list[i] = entry
i++
}
}
}
return list
}