This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
buntdbht.go
679 lines (619 loc) · 16.7 KB
/
buntdbht.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Copyright (C) 2013-2018, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// implements a buntdb based instance of HashTable
package holochain
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
. "github.com/holochain/holochain-proto/hash"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/tidwall/buntdb"
)
type BuntHT struct {
db *buntdb.DB
}
// linkEvent represents the value stored in buntDB associated with a
// link key for one source having stored one LinkingEntry
// (The Link struct defined in entry.go is encoded in the key used for buntDB)
type linkEvent struct {
Status int
Source string
LinksEntry string
}
func (ht *BuntHT) Open(options interface{}) (err error) {
file := options.(string)
db, err := buntdb.Open(file)
if err != nil {
panic(err)
}
db.CreateIndex("link", "link:*", buntdb.IndexString)
db.CreateIndex("idx", "idx:*", buntdb.IndexInt)
db.CreateIndex("peer", "peer:*", buntdb.IndexString)
db.CreateIndex("list", "list:*", buntdb.IndexString)
db.CreateIndex("entry", "entry:*", buntdb.IndexString)
ht.db = db
return
}
// Put stores a value to the DHT store
// N.B. This call assumes that the value has already been validated
func (ht *BuntHT) Put(m *Message, entryType string, key Hash, src peer.ID, value []byte, status int) (err error) {
k := key.String()
err = ht.db.Update(func(tx *buntdb.Tx) error {
_, err := incIdx(tx, m)
if err != nil {
return err
}
_, _, err = tx.Set("entry:"+k, string(value), nil)
if err != nil {
return err
}
_, _, err = tx.Set("type:"+k, entryType, nil)
if err != nil {
return err
}
_, _, err = tx.Set("src:"+k, peer.IDB58Encode(src), nil)
if err != nil {
return err
}
_, _, err = tx.Set("status:"+k, fmt.Sprintf("%d", status), nil)
if err != nil {
return err
}
return err
})
return
}
// Del moves the given hash to the StatusDeleted status
// N.B. this functions assumes that the validity of this action has been confirmed
func (ht *BuntHT) Del(m *Message, key Hash) (err error) {
k := key.String()
err = ht.db.Update(func(tx *buntdb.Tx) error {
err = _setStatus(tx, m, k, StatusDeleted)
return err
})
return
}
func _setStatus(tx *buntdb.Tx, m *Message, key string, status int) (err error) {
_, err = tx.Get("entry:" + key)
if err != nil {
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
return
}
_, err = incIdx(tx, m)
if err != nil {
return
}
_, _, err = tx.Set("status:"+key, fmt.Sprintf("%d", status), nil)
if err != nil {
return
}
return
}
// Mod moves the given hash to the StatusModified status
// N.B. this functions assumes that the validity of this action has been confirmed
func (ht *BuntHT) Mod(m *Message, key Hash, newkey Hash) (err error) {
k := key.String()
err = ht.db.Update(func(tx *buntdb.Tx) error {
err = _setStatus(tx, m, k, StatusModified)
if err == nil {
link := newkey.String()
err = _link(tx, k, link, SysTagReplacedBy, m.From, StatusLive, newkey)
if err == nil {
_, _, err = tx.Set("replacedBy:"+k, link, nil)
if err != nil {
return err
}
}
}
return err
})
return
}
func _get(tx *buntdb.Tx, k string, statusMask int) (string, error) {
val, err := tx.Get("entry:" + k)
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
return val, err
}
var statusVal string
statusVal, err = tx.Get("status:" + k)
if err == nil {
if statusMask == StatusDefault {
// if the status mask is not given (i.e. Default) then
// we return information about the status if it's other than live
switch statusVal {
case StatusDeletedVal:
err = ErrHashDeleted
case StatusModifiedVal:
val, err = tx.Get("replacedBy:" + k)
if err != nil {
panic("missing expected replacedBy record")
}
err = ErrHashModified
case StatusRejectedVal:
err = ErrHashRejected
case StatusLiveVal:
default:
panic("unknown status!")
}
} else {
// otherwise we return the value only if the status is in the mask
var status int
status, err = strconv.Atoi(statusVal)
if err == nil {
if (status & statusMask) == 0 {
err = ErrHashNotFound
}
}
}
}
return val, err
}
// Exists checks for the existence of the hash in the store
func (ht *BuntHT) Exists(key Hash, statusMask int) (err error) {
err = ht.db.View(func(tx *buntdb.Tx) error {
_, err := _get(tx, key.String(), statusMask)
return err
})
return
}
// Source returns the source node address of a given hash
func (ht *BuntHT) Source(key Hash) (id peer.ID, err error) {
err = ht.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("src:" + key.String())
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
if err == nil {
id, err = peer.IDB58Decode(val)
}
return err
})
return
}
// Get retrieves a value from the DHT store
func (ht *BuntHT) Get(key Hash, statusMask int, getMask int) (data []byte, entryType string, sources []string, status int, err error) {
if getMask == GetMaskDefault {
getMask = GetMaskEntry
}
err = ht.db.View(func(tx *buntdb.Tx) error {
k := key.String()
val, err := _get(tx, k, statusMask)
if err != nil {
data = []byte(val) // gotta do this because value is valid if ErrHashModified
return err
}
data = []byte(val)
if (getMask & GetMaskEntryType) != 0 {
entryType, err = tx.Get("type:" + k)
if err != nil {
return err
}
}
if (getMask & GetMaskSources) != 0 {
val, err = tx.Get("src:" + k)
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
if err == nil {
sources = append(sources, val)
}
if err != nil {
return err
}
}
val, err = tx.Get("status:" + k)
if err != nil {
return err
}
status, err = strconv.Atoi(val)
if err != nil {
return err
}
return err
})
return
}
// _link is a low level routine to add a link, also used by delLink
// this ensure monotonic recording of linking attempts
func _link(tx *buntdb.Tx, base string, link string, tag string, src peer.ID, status int, linkingEntryHash Hash) (err error) {
key := "link:" + base + ":" + link + ":" + tag
var val string
val, err = tx.Get(key)
source := peer.IDB58Encode(src)
lehStr := linkingEntryHash.String()
var records []linkEvent
if err == nil {
// load the previous value so we can append to it.
json.Unmarshal([]byte(val), &records)
// TODO: if the link exists, then load the statuses and see
// what we should do about this situation
/*
// search for the source and linking entry in the status
for _, s := range records {
if s.Source == source && s.LinksEntry == lehStr {
if status == StatusLive && s.Status != status {
err = ErrPutLinkOverDeleted
return
}
// return silently because this is just a duplicate putLink
break
}
} // fall through and add this linking event.
*/
} else if err == buntdb.ErrNotFound {
// when deleting the key must exist
if status == StatusDeleted {
err = ErrLinkNotFound
return
}
err = nil
} else {
return
}
records = append(records, linkEvent{status, source, lehStr})
var b []byte
b, err = json.Marshal(records)
if err != nil {
return
}
_, _, err = tx.Set(key, string(b), nil)
if err != nil {
return
}
return
}
func (ht *BuntHT) link(m *Message, base string, link string, tag string, status int) (err error) {
err = ht.db.Update(func(tx *buntdb.Tx) error {
_, err := _get(tx, base, StatusLive)
if err != nil {
return err
}
err = _link(tx, base, link, tag, m.From, status, m.Body.(HoldReq).EntryHash)
if err != nil {
return err
}
//var index string
_, err = incIdx(tx, m)
if err != nil {
return err
}
return nil
})
return
}
// PutLink associates a link with a stored hash
// N.B. this function assumes that the data associated has been properly retrieved
// and validated from the cource chain
func (ht *BuntHT) PutLink(m *Message, base string, link string, tag string) (err error) {
err = ht.link(m, base, link, tag, StatusLive)
return
}
// DelLink removes a link and tag associated with a stored hash
// N.B. this function assumes that the action has been properly validated
func (ht *BuntHT) DelLink(m *Message, base string, link string, tag string) (err error) {
err = ht.link(m, base, link, tag, StatusDeleted)
return
}
// GetLinks retrieves meta value associated with a base
func (ht *BuntHT) GetLinks(base Hash, tag string, statusMask int) (results []TaggedHash, err error) {
b := base.String()
err = ht.db.View(func(tx *buntdb.Tx) error {
_, err := _get(tx, b, StatusLive+StatusModified) //only get links on live and modified bases
if err != nil {
return err
}
if statusMask == StatusDefault {
statusMask = StatusLive
}
results = make([]TaggedHash, 0)
err = tx.Ascend("link", func(key, value string) bool {
x := strings.Split(key, ":")
t := string(x[3])
if string(x[1]) == b && (tag == "" || tag == t) {
var records []linkEvent
json.Unmarshal([]byte(value), &records)
l := len(records)
//TODO: this is totally bogus currently simply
// looking at the last item we ever got
if l > 0 {
entry := records[l-1]
if err == nil && (entry.Status&statusMask) > 0 {
th := TaggedHash{H: string(x[2]), Source: entry.Source}
if tag == "" {
th.T = t
}
results = append(results, th)
}
}
}
return true
})
return err
})
return
}
// Close cleans up any resources used by the table
func (ht *BuntHT) Close() {
ht.db.Close()
ht.db = nil
}
// incIdx adds a new index record to dht for gossiping later
func incIdx(tx *buntdb.Tx, m *Message) (index string, err error) {
// if message is nil we can't record this for gossiping
// this should only be the case for the DNA
if m == nil {
return
}
var idx int
idx, err = getIntVal("_idx", tx)
if err != nil {
return
}
idx++
index = fmt.Sprintf("%d", idx)
_, _, err = tx.Set("_idx", index, nil)
if err != nil {
return
}
var msg string
if m != nil {
var b []byte
b, err = ByteEncoder(m)
if err != nil {
return
}
msg = string(b)
}
_, _, err = tx.Set("idx:"+index, msg, nil)
if err != nil {
return
}
f, err := m.Fingerprint()
if err != nil {
return
}
_, _, err = tx.Set("f:"+f.String(), index, nil)
if err != nil {
return
}
return
}
// getIntVal returns an integer value at a given key, and assumes the value 0 if the key doesn't exist
func getIntVal(key string, tx *buntdb.Tx) (idx int, err error) {
var val string
val, err = tx.Get(key)
if err == buntdb.ErrNotFound {
err = nil
} else if err != nil {
return
} else {
idx, err = strconv.Atoi(val)
if err != nil {
return
}
}
return
}
// GetIdx returns the current index of changes to the HashTable
func (ht *BuntHT) GetIdx() (idx int, err error) {
err = ht.db.View(func(tx *buntdb.Tx) error {
var e error
idx, e = getIntVal("_idx", tx)
if e != nil {
return e
}
return nil
})
return
}
// GetIdxMessage returns the messages that causes the change at a given index
func (ht *BuntHT) GetIdxMessage(idx int) (msg Message, err error) {
err = ht.db.View(func(tx *buntdb.Tx) error {
msgStr, e := tx.Get(fmt.Sprintf("idx:%d", idx))
if e == buntdb.ErrNotFound {
return ErrNoSuchIdx
}
if e != nil {
return e
}
e = ByteDecoder([]byte(msgStr), &msg)
if err != nil {
return e
}
return nil
})
return
}
// DumpIdx converts message and data of a DHT change request to a string for human consumption
func (ht *BuntHT) dumpIdx(idx int) (str string, err error) {
var msg Message
msg, err = ht.GetIdxMessage(idx)
if err != nil {
return
}
f, _ := msg.Fingerprint()
str = fmt.Sprintf("MSG (fingerprint %v):\n %v\n", f, msg)
switch msg.Type {
case PUT_REQUEST:
key := msg.Body.(HoldReq).EntryHash
entry, entryType, _, _, e := ht.Get(key, StatusDefault, GetMaskAll)
if e != nil {
err = fmt.Errorf("couldn't get %v err:%v ", key, e)
return
} else {
str += fmt.Sprintf("DATA: type:%s entry: %v\n", entryType, entry)
}
}
return
}
func statusValueToString(val string) string {
//TODO
return val
}
// String converts the table into a human readable string
func (ht *BuntHT) String() (result string) {
idx, err := ht.GetIdx()
if err != nil {
return err.Error()
}
result += fmt.Sprintf("DHT changes: %d\n", idx)
for i := 1; i <= idx; i++ {
str, err := ht.dumpIdx(i)
if err != nil {
result += fmt.Sprintf("%d Error:%v\n", i, err)
} else {
result += fmt.Sprintf("%d\n%v\n", i, str)
}
}
result += fmt.Sprintf("DHT entries:\n")
err = ht.db.View(func(tx *buntdb.Tx) error {
err = tx.Ascend("entry", func(key, value string) bool {
x := strings.Split(key, ":")
k := string(x[1])
var status string
statusVal, err := tx.Get("status:" + k)
if err != nil {
status = fmt.Sprintf("<err getting status:%v>", err)
} else {
status = statusValueToString(statusVal)
}
var sources string
sources, err = tx.Get("src:" + k)
if err != nil {
sources = fmt.Sprintf("<err getting sources:%v>", err)
}
var links string
err = tx.Ascend("link", func(key, value string) bool {
x := strings.Split(key, ":")
base := x[1]
link := x[2]
tag := x[3]
if base == k {
links += fmt.Sprintf("Linked to: %s with tag %s\n", link, tag)
links += value + "\n"
}
return true
})
result += fmt.Sprintf("Hash--%s (status %s):\nValue: %s\nSources: %s\n%s\n", k, status, value, sources, links)
return true
})
return nil
})
return
}
// DumpIdxJSON converts message and data of a DHT change request to a JSON string representation.
func (ht *BuntHT) dumpIdxJSON(idx int) (str string, err error) {
var msg Message
var buffer bytes.Buffer
var msgField, dataField string
msg, err = ht.GetIdxMessage(idx)
if err != nil {
return "", err
}
f, _ := msg.Fingerprint()
buffer.WriteString(fmt.Sprintf("{ \"index\": %d,", idx))
msgField = fmt.Sprintf("\"message\": { \"fingerprint\": \"%v\", \"content\": \"%v\" },", f, msg)
switch msg.Type {
case PUT_REQUEST:
key := msg.Body.(HoldReq).EntryHash
entry, entryType, _, _, e := ht.Get(key, StatusAny, GetMaskAll)
if e != nil {
err = fmt.Errorf("couldn't get %v err:%v ", key, e)
return
}
dataField = fmt.Sprintf("\"data\": { \"type\": \"%s\", \"entry\": \"%v\" }", entryType, entry)
}
if len(dataField) > 0 {
buffer.WriteString(msgField)
buffer.WriteString(dataField)
} else {
buffer.WriteString(strings.TrimSuffix(msgField, ","))
}
buffer.WriteString("}")
return PrettyPrintJSON(buffer.Bytes())
}
// JSON converts the table into a JSON string representation.
func (ht *BuntHT) JSON() (result string, err error) {
var buffer, entries bytes.Buffer
idx, err := ht.GetIdx()
if err != nil {
return "", err
}
buffer.WriteString("{ \"dht_changes\": [")
for i := 1; i <= idx; i++ {
json, err := ht.dumpIdxJSON(i)
if err != nil {
return "", fmt.Errorf("DHT Change %d, Error: %v", i, err)
}
buffer.WriteString(json)
if i < idx {
buffer.WriteString(",")
}
}
buffer.WriteString("], \"dht_entries\": [")
err = ht.db.View(func(tx *buntdb.Tx) error {
err = tx.Ascend("entry", func(key, value string) bool {
x := strings.Split(key, ":")
k := string(x[1])
var status string
statusVal, err := tx.Get("status:" + k)
if err != nil {
status = fmt.Sprintf("<err getting status:%v>", err)
} else {
status = statusValueToString(statusVal)
}
var sources string
sources, err = tx.Get("src:" + k)
if err != nil {
sources = fmt.Sprintf("<err getting sources:%v>", err)
}
var links bytes.Buffer
err = tx.Ascend("link", func(key, value string) bool {
x := strings.Split(key, ":")
base := x[1]
link := x[2]
tag := x[3]
if base == k {
links.WriteString(fmt.Sprintf("{ \"linkTo\": \"%s\",", link))
links.WriteString(fmt.Sprintf("\"tag\": \"%s\",", tag))
links.WriteString(fmt.Sprintf("\"value\": \"%s\" },", EscapeJSONValue(value)))
}
return true
})
entries.WriteString(fmt.Sprintf("{ \"hash\": \"%s\",", k))
entries.WriteString(fmt.Sprintf("\"status\": \"%s\",", status))
entries.WriteString(fmt.Sprintf("\"value\": \"%s\",", EscapeJSONValue(value)))
entries.WriteString(fmt.Sprintf("\"sources\": \"%s\"", sources))
if links.Len() > 0 {
entries.WriteString(fmt.Sprintf(",\"links\": [%s]", strings.TrimSuffix(links.String(), ",")))
}
entries.WriteString("},")
return true
})
return nil
})
buffer.WriteString(strings.TrimSuffix(entries.String(), ","))
buffer.WriteString("]}")
return PrettyPrintJSON(buffer.Bytes())
}
func (ht *BuntHT) Iterate(fn HashTableIterateFn) {
ht.db.View(func(tx *buntdb.Tx) error {
err := tx.Ascend("entry", func(key, value string) bool {
x := strings.Split(key, ":")
k := string(x[1])
hash, err := NewHash(k)
if err != nil {
return false
}
return fn(hash)
})
return err
})
}