-
Notifications
You must be signed in to change notification settings - Fork 49
/
htree.go
244 lines (192 loc) · 5.47 KB
/
htree.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
/*
* EliasDB
*
* Copyright 2016 Matthias Ladkau. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
Package hash provides a HTree implementation to provide key-value storage functionality
for a StorageManager.
The HTree provides a persistent hashtable. Storing values in buckets on
pages as the tree gorws. It is not possible to store nil values. Storing a nil value
is equivalent to removing a key.
As the tree grows each tree level contains pages with links to underlying pages.
The last link is always to a bucket. The default tree has 4 levels each with
256 possible children. A hash code for the tree has 32 bits = 4 levels * 8 bit.
Hash buckets are on the lowest level of the tree and contain actual keys and
values. The object stores multiple keys and values if there are hash collisions.
In a sparsely populated tree buckets can also be found on the upper levels.
Iterator
Entries in the HTree can be iterated by using an HTreeIterator. The HTree may
change behind the iterator's back. The iterator will try to cope with best
effort and only report an error as a last resort.
Hash function
The HTree uses an implementation of Austin Appleby's MurmurHash3 (32bit) function
as hash function.
Reference implementation: http://code.google.com/p/smhasher/wiki/MurmurHash3
*/
package hash
import (
"fmt"
"sync"
"github.com/krotik/eliasdb/storage"
)
/*
MaxTreeDepth is the maximum number of non-leaf levels in the tree (i.e. the complete tree has
a total of MAX_DEPTH+1 levels)
*/
const MaxTreeDepth = 3
/*
PageLevelBits is the number of significant bits per page level
*/
const PageLevelBits = 8
/*
MaxPageChildren is the maximum of children per page - (stored in PageLevelBits bits)
*/
const MaxPageChildren = 256
/*
MaxBucketElements is the maximum umber of elements a bucket can contain before it
is converted into a page except leaf buckets which grow indefinitely
*/
const MaxBucketElements = 8
/*
HTree data structure
*/
type HTree struct {
Root *htreePage // Root page of the HTree
mutex *sync.Mutex // Mutex to protect tree operations
}
/*
htreeNode data structure - this object models the
HTree storage structure on disk
*/
type htreeNode struct {
tree *HTree // Reference to the HTree which owns this node (not persisted)
loc uint64 // Storage location of this page (not persisted)
sm storage.Manager // StorageManager instance which stores the tree data (not persisted)
Depth byte // Depth of this node
Children []uint64 // Storage locations of children (only used for pages)
Keys [][]byte // Stored keys (only used for buckets)
Values []interface{} // Stored values (only used for buckets)
BucketSize byte // Bucket size (only used for buckets)
}
/*
Fetch a HTree node from the storage.
*/
func (n *htreeNode) fetchNode(loc uint64) (*htreeNode, error) {
var node *htreeNode
if obj, _ := n.sm.FetchCached(loc); obj == nil {
var res htreeNode
if err := n.sm.Fetch(loc, &res); err != nil {
return nil, err
}
node = &res
} else {
node = obj.(*htreeNode)
}
return node, nil
}
/*
NewHTree creates a new HTree.
*/
func NewHTree(sm storage.Manager) (*HTree, error) {
tree := &HTree{}
// Protect tree creation
cm := &sync.Mutex{}
cm.Lock()
defer cm.Unlock()
tree.Root = newHTreePage(tree, 0)
loc, err := sm.Insert(tree.Root.htreeNode)
if err != nil {
return nil, err
}
tree.Root.loc = loc
tree.Root.sm = sm
tree.mutex = &sync.Mutex{}
return tree, nil
}
/*
LoadHTree fetches a HTree from storage
*/
func LoadHTree(sm storage.Manager, loc uint64) (*HTree, error) {
var tree *HTree
// Protect tree creation
cm := &sync.Mutex{}
cm.Lock()
defer cm.Unlock()
if obj, _ := sm.FetchCached(loc); obj == nil {
var res htreeNode
if err := sm.Fetch(loc, &res); err != nil {
return nil, err
}
tree = &HTree{&htreePage{&res}, nil}
} else {
tree = &HTree{&htreePage{obj.(*htreeNode)}, nil}
}
tree.Root.loc = loc
tree.Root.sm = sm
tree.mutex = &sync.Mutex{}
return tree, nil
}
/*
Location returns the HTree location on disk.
*/
func (t *HTree) Location() uint64 {
return t.Root.loc
}
/*
Get gets a value for a given key.
*/
func (t *HTree) Get(key []byte) (interface{}, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
res, _, err := t.Root.Get(key)
return res, err
}
/*
GetValueAndLocation returns the value and the storage location for a given key.
*/
func (t *HTree) GetValueAndLocation(key []byte) (interface{}, uint64, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
res, bucket, err := t.Root.Get(key)
if bucket != nil {
return res, bucket.loc, err
}
return res, 0, err
}
/*
Exists checks if an element exists.
*/
func (t *HTree) Exists(key []byte) (bool, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
return t.Root.Exists(key)
}
/*
Put adds or updates a new key / value pair.
*/
func (t *HTree) Put(key []byte, value interface{}) (interface{}, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
return t.Root.Put(key, value)
}
/*
Remove removes a key / value pair.
*/
func (t *HTree) Remove(key []byte) (interface{}, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
return t.Root.Remove(key)
}
/*
String returns a string representation of this tree.
*/
func (t *HTree) String() string {
t.mutex.Lock()
defer t.mutex.Unlock()
return fmt.Sprintf("HTree: %v (%v)\n%v", t.Root.sm.Name(), t.Root.loc, t.Root.String())
}