-
Notifications
You must be signed in to change notification settings - Fork 0
/
buckets.go
136 lines (113 loc) · 2.71 KB
/
buckets.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
package xisdb
import (
"fmt"
"sync"
)
// Bucket is the user-facing representation of a bucket that enables transctions
type Bucket struct {
tx *Tx
managed *bucket
}
// Get retrieves a value by its key, or errors
func (b *Bucket) Get(key string) (string, error) {
return b.tx.get(b.managed, key)
}
// Exists returns whether or not a key is present in the Bucket
func (b *Bucket) Exists(key string) bool {
exists, _ := b.tx.exists(b.managed, key)
return exists
}
// Set will add or update a value
func (b *Bucket) Set(key, value string) error {
return b.tx.set(b.managed, key, value, nil)
}
// Delete will delete a key from the bucket. Returns whether or not it actually was
func (b *Bucket) Delete(key string) (bool, error) {
return b.tx.delete(b.managed, key)
}
// Clear will empty a bucket of all of its keys
func (b *Bucket) Clear() error {
return b.tx.clear(b.managed)
}
// AddIndex adds an index to the bucket's data
func (b *Bucket) AddIndex() error {
return nil
}
// DeleteIndex removes an index from the bucket's data
func (b *Bucket) DeleteIndex(name string) error {
return nil
}
// Size is how many items are in the bucket
func (b *Bucket) Size() int {
return b.managed.size()
}
// bucket is a collection of key-value pairs, much like a traditional DB table
type bucket struct {
name string
db *DB
mutex sync.RWMutex // lock on a per-bucket level -- TODO: maybe not
data map[string]Item // the data itself
indexes map[string]*index // indexes on the data
}
func newBucket(name string, db *DB) *bucket {
return &bucket{
name: name,
db: db,
data: make(map[string]Item),
indexes: make(map[string]*index),
}
}
func (b *bucket) isRoot() bool {
return b.db != nil && b == b.db.root()
}
func (b *bucket) get(key string) (*Item, bool) {
value, exists := b.data[key]
if !exists {
return nil, false
}
return &value, exists
}
func (b *bucket) insert(item *Item) {
b.data[item.Key] = *item
}
func (b *bucket) exists(key string) bool {
_, exists := b.data[key]
return exists
}
// Delete removes a key from a bucket and returns whether or not it was removed
func (b *bucket) delete(key string) bool {
item, ok := b.data[key]
if !ok {
return ok
}
delete(b.data, key)
for _, idx := range b.indexes {
if idx.match(&item) {
idx.remove(&item)
}
}
return ok
}
func (b *bucket) clear() error {
for _, idx := range b.indexes {
fmt.Printf("Idx:%s\n", idx)
// idx.clear()
}
for key := range b.data {
delete(b.data, key)
}
return nil
}
func (b *bucket) size() int {
return len(b.data)
}
func (b *bucket) rollback(info *rollbackInfo) error {
for key, value := range info.items {
if value == nil {
b.delete(key)
continue
}
b.insert(value)
}
return nil
}