-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprolly_tree.go
480 lines (415 loc) · 13.1 KB
/
prolly_tree.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package prollytree
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"slices"
"sort"
"strconv"
"strings"
)
// GreaterThan checks if one node is 'greater than' another.
func (n *Node) GreaterThan(other *Node) bool {
return n.isTail || n.timestamp > other.timestamp
}
// LessThan checks if one node is 'less than' another.
func (n *Node) LessThan(other *Node) bool {
return (!n.isTail) && n.timestamp < other.timestamp
}
// EqualTo checks if one node is 'equal to' another.
func (n *Node) EqualTo(other *Node) bool {
return n.timestamp == other.timestamp
}
// NotEqualTo checks if one node is 'not equal to' another.
func (n *Node) NotEqualTo(other *Node) bool {
return n.timestamp != other.timestamp
}
// GreaterThanOrEqualTo checks if one node is 'greater than or equal to' another.
func (n *Node) GreaterThanOrEqualTo(other *Node) bool {
return n.isTail || n.timestamp >= other.timestamp
}
// LessThanOrEqualTo checks if one node is 'less than or equal to' another.
func (n *Node) LessThanOrEqualTo(other *Node) bool {
return (!n.isTail) && n.timestamp <= other.timestamp
}
// CalculateHash computes the SHA256 hash of a given string.
func CalculateHash(hashableStr string) string {
hasher := sha256.New()
hasher.Write([]byte(hashableStr))
return hex.EncodeToString(hasher.Sum(nil))
}
// IsBoundaryHash checks if the last hex digit of the hash is below a certain threshold.
func IsBoundaryHash(hashValue string, threshold int) bool {
// Convert the last hex digit of the hash to an integer.
lastHexDigit := hashValue[len(hashValue)-1:]
hashInt, _ := strconv.ParseInt(lastHexDigit, 16, 64)
return int(hashInt) < threshold
}
// BucketHash calculates the merkle hash of a bucket of nodes by concatenating their merkle hashes.
func BucketHash(nodes []*Node) string {
// // Concatenate the merkle hashes of the nodes.
var sb strings.Builder
for _, node := range nodes {
sb.WriteString(node.merkleHash)
}
// Calculate the hash of the concatenated string.
return CalculateHash(sb.String())
}
// Node represents each node in the tree structure.
type Node struct {
timestamp int
data string
nodeHash string
level int
up *Node
down *Node
left *Node
right *Node
merkleHash string
boundary *bool // using a pointer to differentiate between unset and false
isTail bool
}
// NewNode creates and initializes a new Node.
func NewNode(data string, timestamp int, isTail bool) *Node {
n := &Node{
timestamp: timestamp,
data: data,
isTail: isTail,
level: 0,
up: nil,
down: nil,
left: nil,
right: nil,
merkleHash: CalculateHash(data + strconv.Itoa(timestamp)),
nodeHash: CalculateHash(data + strconv.Itoa(timestamp)),
boundary: nil,
}
return n
}
// CreateHigherLevelNode creates a new node one level higher.
func (n *Node) CreateHigherLevelNode() *Node {
newNode := NewNode("", n.timestamp, n.isTail)
newNode.level = n.level + 1
newNode.down = n
n.up = newNode
newNode.nodeHash = CalculateHash(n.nodeHash)
return newNode
}
// Fill merkleHash fills the merkle hash of the node by going down walking left till you hit the boundary.
func (n *Node) FillmerkleHash() {
var bucketNodes []*Node
node := n.down
bucketNodes = append(bucketNodes, node)
for node.left != nil {
if node.left.IsBoundaryNode() {
break
}
node = node.left
bucketNodes = append(bucketNodes, node)
}
for _, node := range bucketNodes {
if node.merkleHash == "" {
node.FillmerkleHash()
}
}
// Reverse the slice of nodes.
slices.Reverse(bucketNodes)
n.merkleHash = BucketHash(bucketNodes)
}
// IsBoundaryNode checks if the node is a boundary node.
func (n *Node) IsBoundaryNode() bool {
if n.boundary != nil {
return *n.boundary
}
boundary := n.isTail || IsBoundaryHash(n.nodeHash, 7) // Assuming the threshold is 7
n.boundary = &boundary
return boundary
}
// FindNextBoundaryNode finds the next boundary node by going right till you hit the boundary node.
func (n *Node) FindNextBoundaryNode() *Node {
node := n
for node.right != nil {
if node.right.IsBoundaryNode() {
return node.right
}
node = node.right
}
return node
}
// Level represents a level in the ProllyTree.
type Level struct {
level int
tail *Node
}
// NewLevel creates a new Level instance.
func NewLevel(lvl int) *Level {
return &Level{level: lvl}
}
// ToList converts a level's nodes to a list.
func (l *Level) ToList() []*Node {
var nodes []*Node
node := l.tail
for node != nil {
nodes = append(nodes, node)
node = node.left
}
slices.Reverse(nodes)
// for node != nil {
// nodes = append([]*Node{node}, nodes...) // Prepend to reverse
// node = node.left
// }
return nodes
}
// String provides a string representation of the level.
func (l *Level) String() string {
var timestamps []int
for _, node := range l.ToList() {
timestamps = append(timestamps, node.timestamp)
}
return " <=> " + fmt.Sprint(timestamps)
}
// BaseLevel creates a base level from a list of messages.
func BaseLevel(messages []*Message) *Level {
level := NewLevel(0)
var nodes []*Node
// sort the messages by timestamp
sort.Slice(messages, func(i, j int) bool {
return messages[i].timestamp < messages[j].timestamp
})
for _, m := range messages {
nodes = append(nodes, NewNode(m.data, m.timestamp, false))
}
fakeTail := NewNode("Tail", -1, true)
nodes = append(nodes, fakeTail)
level.tail = LinkNodes(nodes)[len(nodes)-1]
return level
}
// NextLevel creates a level from the previous level.
func NextLevel(prevLevel *Level) *Level {
nodes := prevLevel.ToList()
var eligibleNodes []*Node
for _, node := range nodes {
if node.IsBoundaryNode() {
eligibleNodes = append(eligibleNodes, node.CreateHigherLevelNode())
}
}
linkedNodes := LinkNodes(eligibleNodes)
for _, node := range linkedNodes {
node.FillmerkleHash()
}
newLevel := NewLevel(prevLevel.level + 1)
newLevel.tail = linkedNodes[len(linkedNodes)-1]
return newLevel
}
// LinkNodes links nodes together in a level.
func LinkNodes(nodes []*Node) []*Node {
for i := 0; i < len(nodes)-1; i++ {
nodes[i].right = nodes[i+1]
nodes[i+1].left = nodes[i]
}
return nodes
}
// ProllyTree represents the entire tree structure.
type ProllyTree struct {
levels []*Level
}
// NewProllyTree creates a new ProllyTree instance with the given messages.
func NewProllyTree(messages []*Message) *ProllyTree {
tree := &ProllyTree{}
baseLevel := BaseLevel(messages)
tree.levels = append(tree.levels, baseLevel)
for len(baseLevel.ToList()) > 1 {
baseLevel = NextLevel(baseLevel)
tree.levels = append(tree.levels, baseLevel)
}
return tree
}
// Insert adds a message to the ProllyTree.
func (pt *ProllyTree) Insert(message *Message) {
newNode := NewNode(message.data, message.timestamp, false)
rightOfNewNode := pt.findNodeGreaterThan(newNode.timestamp)
pt.insertNodeAtLevel(newNode, 0, rightOfNewNode)
}
// Delete removes a node from the ProllyTree based on the timestamp.
func (pt *ProllyTree) Delete(timestamp int) *Node {
originalNode := pt.Search(timestamp)
node := originalNode
if originalNode == nil {
return nil
}
rightBoundaryNode := originalNode.FindNextBoundaryNode()
for node != nil {
leftNode := node.left
rightNode := node.right
if rightNode != nil {
rightNode.left = leftNode
}
if leftNode != nil {
leftNode.right = rightNode
}
node = node.up
}
// Clean up empty levels
var levelsToRemove []int
for i := len(pt.levels) - 1; i > 1; i-- {
levelBelow := pt.levels[i-1]
if levelBelow.tail.left == nil { // Only has tail node
levelsToRemove = append(levelsToRemove, i)
}
}
for _, levelIndex := range levelsToRemove {
pt.levels = append(pt.levels[:levelIndex], pt.levels[levelIndex+1:]...)
}
pt.updatePropagatemerkleHash(rightBoundaryNode.up)
return originalNode
}
// Search finds a node based on the timestamp.
func (pt *ProllyTree) Search(timestamp int) *Node {
rightNode := pt.findNodeGreaterThan(timestamp)
if rightNode.left != nil && rightNode.left.timestamp == timestamp {
return rightNode.left
}
return nil
}
// String provides a string representation of the ProllyTree.
func (pt *ProllyTree) String() string {
var levels []string
for _, level := range pt.levels {
levels = append(levels, level.String())
}
return fmt.Sprint(levels)
}
// findNodeGreaterThan finds the node with the smallest timestamp that is greater than the given timestamp.
func (pt *ProllyTree) findNodeGreaterThan(timestamp int) *Node {
nodeToFind := NewNode("", timestamp, false)
node := pt.levels[len(pt.levels)-1].tail // Start from the root
// Move down to the level 0 right boundary node of the given timestamp
for node.down != nil {
if node.left != nil && node.left.timestamp > nodeToFind.timestamp {
node = node.left
} else {
node = node.down
}
}
// Move left until the left element is the timestamp or lower than the timestamp
for node.left != nil && node.left.timestamp > nodeToFind.timestamp {
node = node.left
}
return node
}
// insertNodeAtLevel inserts a node at a given level and creates a new level if necessary.
func (pt *ProllyTree) insertNodeAtLevel(newNode *Node, levelIndex int, rightOfNewNode *Node) {
if levelIndex >= len(pt.levels) {
panic("Adding on a level that doesn't exist")
}
// Insert the new node to the right of the rightOfNewNode
ourLeftNode := rightOfNewNode.left
rightOfNewNode.left = newNode
newNode.right = rightOfNewNode
newNode.left = ourLeftNode
if ourLeftNode != nil {
ourLeftNode.right = newNode
}
if newNode.IsBoundaryNode() {
higherLevelNode := newNode.CreateHigherLevelNode()
nextBoundaryNode := newNode.FindNextBoundaryNode()
if levelIndex == len(pt.levels)-1 {
pt.addEmptyLevel()
}
pt.insertNodeAtLevel(higherLevelNode, levelIndex+1, nextBoundaryNode.up)
}
lastLevel := pt.levels[len(pt.levels)-1]
if len(lastLevel.ToList()) > 1 {
pt.addEmptyLevel()
}
}
// updatePropagatemerkleHash updates the merkle hash of the node and propagates it up.
func (pt *ProllyTree) updatePropagatemerkleHash(node *Node) {
node.FillmerkleHash()
if node.up == nil {
if node.isTail {
return
}
nextBoundaryNode := node.FindNextBoundaryNode()
pt.updatePropagatemerkleHash(nextBoundaryNode.up)
} else {
pt.updatePropagatemerkleHash(node.up)
}
}
func (pt *ProllyTree) addEmptyLevel() {
levelIndex := len(pt.levels)
newLevel := NewLevel(levelIndex)
pt.levels = append(pt.levels, newLevel)
upgradedTail := pt.levels[levelIndex-1].tail.CreateHigherLevelNode()
newLevel.tail = upgradedTail
}
// Message represents the data structure to be added to the tree.
type Message struct {
data string
timestamp int
}
// NewMessage creates and initializes a new Message.
func NewMessage(data string, timestamp int) *Message {
return &Message{data: data, timestamp: timestamp}
}
// String provides a string representation of the message, similar to Python's __repr__.
func (m *Message) String() string {
return fmt.Sprintf("Message(%s, %s)", m.data, fmt.Sprint(m.timestamp))
}
// to Test the prolly tree
// func main() {
// // Seed the random number generator.
// rand.Seed(time.Now().UnixNano())
// // Create a slice with 2 million integers.
// // const size int = 2e6
// const size int = 34_000_000
// mySlice := make([]int, size)
// // Populate the slice with sequential numbers.
// for i := 0; i < size; i++ {
// mySlice[i] = i
// }
// // Shuffle the slice.
// rand.Shuffle(len(mySlice), func(i, j int) {
// mySlice[i], mySlice[j] = mySlice[j], mySlice[i]
// })
// var messages []*Message
// for i := 0; i < size; i++ {
// messages = append(messages, NewMessage(strconv.Itoa(mySlice[i]), mySlice[i]))
// }
// half_size := size / 2
// first_half := messages[:half_size]
// second_half := messages[half_size:]
// fmt.Println("Creating a prolly tree with the messages")
// startTime := time.Now()
// tree := NewProllyTree(first_half)
// elapsed := time.Since(startTime)
// fmt.Println("Elapsed time to create", fmt.Sprint(half_size), "entries in tree is", fmt.Sprint(elapsed))
// // iterate through the second half of the messages and insert them into the tree
// startTime1 := time.Now()
// for _, message := range second_half {
// // fmt.Println("Inserting message: ", message)
// tree.Insert(message)
// }
// elapsed1 := time.Since(startTime1)
// fmt.Println("Elapsed time to insert", fmt.Sprint(half_size), "entries in tree is", fmt.Sprint(elapsed1))
// // fmt.Println("Printing the tree")
// // fmt.Println(tree)
// // fmt.Println("#############################################")
// // fmt.Println("Searching a node with timestamp 5")
// // foundNode := tree.Search(5)
// // if foundNode != nil {
// // fmt.Println("Key Found inside the tree: Node(", foundNode.data, ",", foundNode.timestamp, ")")
// // } else {
// // fmt.Println("Key not found in the tree.")
// // }
// // fmt.Println("#############################################")
// // fmt.Println("Deleting a node with timestamp 6")
// // tree.Delete(6)
// // fmt.Println("Printing the tree")
// // fmt.Println(tree)
// // fmt.Println("#############################################")
// // fmt.Println("Inserting a node with timestamp 12")
// // tree.Insert(NewMessage("12", 12))
// // fmt.Println("Printing the tree")
// // fmt.Println(tree)
// }