-
Notifications
You must be signed in to change notification settings - Fork 182
/
nodedb.go
741 lines (620 loc) · 19.4 KB
/
nodedb.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package iavl
import (
"bytes"
"container/list"
"fmt"
"math"
"sort"
"sync"
"github.com/tendermint/go-amino"
"github.com/okex/exchain/libs/iavl/config"
"github.com/okex/exchain/libs/tendermint/crypto/tmhash"
dbm "github.com/okex/exchain/libs/tm-db"
"github.com/pkg/errors"
)
const (
int64Size = 8
hashSize = tmhash.Size
genesisVersion = 1
)
var (
// All node keys are prefixed with the byte 'n'. This ensures no collision is
// possible with the other keys, and makes them easier to traverse. They are indexed by the node hash.
nodeKeyFormat = NewKeyFormat('n', hashSize) // n<hash>
// Orphans are keyed in the database by their expected lifetime.
// The first number represents the *last* version at which the orphan needs
// to exist, while the second number represents the *earliest* version at
// which it is expected to exist - which starts out by being the version
// of the node being orphaned.
orphanKeyFormat = NewKeyFormat('o', int64Size, int64Size, hashSize) // o<last-version><first-version><hash>
// Root nodes are indexed separately by their version
rootKeyFormat = NewKeyFormat('r', int64Size) // r<version>
)
type nodeDB struct {
mtx sync.RWMutex // Read/write lock.
db dbm.DB // Persistent node storage.
opts Options // Options to customize for pruning/writing
versionReaders map[int64]uint32 // Number of active version readers
latestVersion int64
nodeCache map[string]*list.Element // Node cache.
nodeCacheSize int // Node cache size limit in elements.
nodeCacheQueue *syncList // LRU queue of cache elements. Used for deletion.
orphanNodeCache map[string]*Node
heightOrphansCacheQueue *list.List
heightOrphansCacheSize int
heightOrphansMap map[int64]*heightOrphansItem
prePersistNodeCache map[string]*Node
tppMap map[int64]*tppItem
tppVersionList *list.List
dbReadTime int64
dbReadCount int64
nodeReadCount int64
dbWriteCount int64
totalPersistedCount int64
totalPersistedSize int64
totalDeletedCount int64
totalOrphanCount int64
name string
}
func makeNodeCacheMap(cacheSize int, initRatio float64) map[string]*list.Element {
if initRatio <= 0 {
return make(map[string]*list.Element)
}
if initRatio >= 1 {
return make(map[string]*list.Element, cacheSize)
}
cacheSize = int(float64(cacheSize) * initRatio)
return make(map[string]*list.Element, cacheSize)
}
func newNodeDB(db dbm.DB, cacheSize int, opts *Options) *nodeDB {
if opts == nil {
o := DefaultOptions()
opts = &o
}
return &nodeDB{
db: db,
opts: *opts,
latestVersion: 0, // initially invalid
nodeCache: makeNodeCacheMap(cacheSize, IavlCacheInitRatio),
nodeCacheSize: cacheSize,
nodeCacheQueue: newSyncList(),
versionReaders: make(map[int64]uint32, 8),
orphanNodeCache: make(map[string]*Node),
heightOrphansCacheQueue: list.New(),
heightOrphansCacheSize: HeightOrphansCacheSize,
heightOrphansMap: make(map[int64]*heightOrphansItem),
prePersistNodeCache: make(map[string]*Node),
tppMap: make(map[int64]*tppItem),
tppVersionList: list.New(),
dbReadCount: 0,
dbReadTime: 0,
dbWriteCount: 0,
name: ParseDBName(db),
}
}
// GetNode gets a node from memory or disk. If it is an inner node, it does not
// load its children.
func (ndb *nodeDB) GetNode(hash []byte) *Node {
res := func() *Node {
ndb.mtx.RLock()
defer ndb.mtx.RUnlock()
ndb.addNodeReadCount()
if len(hash) == 0 {
panic("nodeDB.GetNode() requires hash")
}
if elem, ok := ndb.prePersistNodeCache[string(hash)]; ok {
return elem
}
if elem, ok := ndb.getNodeInTpp(hash); ok { // GetNode from tpp
return elem
}
// Check the cache.
if elem, ok := ndb.nodeCache[string(hash)]; ok {
// Already exists. Move to back of nodeCacheQueue.
ndb.nodeCacheQueue.MoveToBack(elem)
return elem.Value.(*Node)
}
if elem, ok := ndb.orphanNodeCache[string(hash)]; ok {
return elem
}
return nil
}()
if res != nil {
return res
}
// Doesn't exist, load.
node := ndb.makeNodeFromDbByHash(hash)
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
node.hash = hash
node.persisted = true
ndb.cacheNodeByCheck(node)
return node
}
func (ndb *nodeDB) getDbName() string {
return ndb.name
}
// SaveNode saves a node to disk.
func (ndb *nodeDB) SaveNode(batch dbm.Batch, node *Node) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if node.hash == nil {
panic("Expected to find node.hash, but none found.")
}
if node.persisted {
panic("Shouldn't be calling save on an already persisted node.")
}
// Save node bytes to db.
var buf bytes.Buffer
buf.Grow(node.aminoSize())
if err := node.writeBytesToBuffer(&buf); err != nil {
panic(err)
}
batch.Set(ndb.nodeKey(node.hash), buf.Bytes())
ndb.log(IavlDebug, "BATCH SAVE", "hash", amino.BytesHexStringer(node.hash))
node.persisted = true
ndb.addDBWriteCount(1)
ndb.cacheNode(node)
}
// Has checks if a hash exists in the database.
func (ndb *nodeDB) Has(hash []byte) (bool, error) {
key := ndb.nodeKey(hash)
if ldb, ok := ndb.db.(*dbm.GoLevelDB); ok {
exists, err := ldb.DB().Has(key, nil)
if err != nil {
return false, err
}
return exists, nil
}
value, err := ndb.dbGet(key)
if err != nil {
return false, err
}
return value != nil, nil
}
// SaveBranch saves the given node and all of its descendants.
// NOTE: This function clears leftNode/rigthNode recursively and
// calls _hash() on the given node.
// TODO refactor, maybe use hashWithCount() but provide a callback.
func (ndb *nodeDB) SaveBranch(batch dbm.Batch, node *Node, savedNodes map[string]*Node) []byte {
if node.persisted {
return node.hash
}
if node.leftNode != nil {
node.leftHash = ndb.SaveBranch(batch, node.leftNode, savedNodes)
}
if node.rightNode != nil {
node.rightHash = ndb.SaveBranch(batch, node.rightNode, savedNodes)
}
node._hash()
//resetBatch only working on generate a genesis block
if node.version == genesisVersion {
tmpBatch := ndb.NewBatch()
ndb.SaveNode(tmpBatch, node)
ndb.resetBatch(tmpBatch)
} else {
ndb.SaveNode(batch, node)
}
node.leftNode = nil
node.rightNode = nil
// TODO: handle magic number
savedNodes[string(node.hash)] = node
return node.hash
}
//resetBatch reset the db batch, keep low memory used
func (ndb *nodeDB) resetBatch(batch dbm.Batch) {
var err error
if ndb.opts.Sync {
err = batch.WriteSync()
} else {
err = batch.Write()
}
if err != nil {
panic(err)
}
batch.Close()
}
// DeleteVersionsFrom permanently deletes all tree versions from the given version upwards.
func (ndb *nodeDB) DeleteVersionsFrom(batch dbm.Batch, version int64) error {
latest := ndb.getLatestVersion()
if latest < version {
return nil
}
root, err := ndb.getRoot(latest)
if err != nil {
return err
}
if root == nil {
return errors.Errorf("root for version %v not found", latest)
}
for v, r := range ndb.versionReaders {
if v >= version && r != 0 {
return errors.Errorf("unable to delete version %v with %v active readers", v, r)
}
}
// First, delete all active nodes in the current (latest) version whose node version is after
// the given version.
err = ndb.deleteNodesFrom(batch, version, root)
if err != nil {
return err
}
// Next, delete orphans:
// - Delete orphan entries *and referred nodes* with fromVersion >= version
// - Delete orphan entries with toVersion >= version-1 (since orphans at latest are not orphans)
ndb.traverseOrphans(func(key, hash []byte) {
var fromVersion, toVersion int64
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)
if fromVersion >= version {
batch.Delete(key)
batch.Delete(ndb.nodeKey(hash))
ndb.uncacheNode(hash)
} else if toVersion >= version-1 {
batch.Delete(key)
}
})
// Finally, delete the version root entries
ndb.traverseRange(rootKeyFormat.Key(version), rootKeyFormat.Key(int64(math.MaxInt64)), func(k, v []byte) {
batch.Delete(k)
})
return nil
}
// DeleteVersionsRange deletes versions from an interval (not inclusive).
func (ndb *nodeDB) DeleteVersionsRange(batch dbm.Batch, fromVersion, toVersion int64) error {
if fromVersion >= toVersion {
return errors.New("toVersion must be greater than fromVersion")
}
if toVersion == 0 {
return errors.New("toVersion must be greater than 0")
}
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
latest := ndb.getLatestVersion()
if latest < toVersion {
return errors.Errorf("cannot delete latest saved version (%d)", latest)
}
predecessor := ndb.getPreviousVersion(fromVersion)
for v, r := range ndb.versionReaders {
if v < toVersion && v > predecessor && r != 0 {
return errors.Errorf("unable to delete version %v with %v active readers", v, r)
}
}
// If the predecessor is earlier than the beginning of the lifetime, we can delete the orphan.
// Otherwise, we shorten its lifetime, by moving its endpoint to the predecessor version.
for version := fromVersion; version < toVersion; version++ {
ndb.traverseOrphansVersion(version, func(key, hash []byte) {
var from, to int64
orphanKeyFormat.Scan(key, &to, &from)
batch.Delete(key)
if from > predecessor {
batch.Delete(ndb.nodeKey(hash))
ndb.uncacheNode(hash)
} else {
ndb.saveOrphan(batch, hash, from, predecessor)
}
})
}
// Delete the version root entries
ndb.traverseRange(rootKeyFormat.Key(fromVersion), rootKeyFormat.Key(toVersion), func(k, v []byte) {
batch.Delete(k)
})
return nil
}
// deleteNodesFrom deletes the given node and any descendants that have versions after the given
// (inclusive). It is mainly used via LoadVersionForOverwriting, to delete the current version.
func (ndb *nodeDB) deleteNodesFrom(batch dbm.Batch, version int64, hash []byte) error {
if len(hash) == 0 {
return nil
}
node := ndb.GetNode(hash)
if node.leftHash != nil {
if err := ndb.deleteNodesFrom(batch, version, node.leftHash); err != nil {
return err
}
}
if node.rightHash != nil {
if err := ndb.deleteNodesFrom(batch, version, node.rightHash); err != nil {
return err
}
}
if node.version >= version {
batch.Delete(ndb.nodeKey(hash))
ndb.uncacheNode(hash)
}
return nil
}
// Saves a single orphan to disk.
func (ndb *nodeDB) saveOrphan(batch dbm.Batch, hash []byte, fromVersion, toVersion int64) {
if fromVersion > toVersion {
panic(fmt.Sprintf("Orphan expires before it comes alive. %d > %d", fromVersion, toVersion))
}
key := ndb.orphanKey(fromVersion, toVersion, hash)
batch.Set(key, hash)
}
func (ndb *nodeDB) log(level int, msg string, kv ...interface{}) {
iavlLog(ndb.name, level, msg, kv...)
}
// deleteOrphans deletes orphaned nodes from disk, and the associated orphan
// entries.
func (ndb *nodeDB) deleteOrphans(batch dbm.Batch, version int64) {
// Will be zero if there is no previous version.
predecessor := ndb.getPreviousVersion(version)
// Traverse orphans with a lifetime ending at the version specified.
// TODO optimize.
ndb.traverseOrphansVersion(version, func(key, hash []byte) {
var fromVersion, toVersion int64
// See comment on `orphanKeyFmt`. Note that here, `version` and
// `toVersion` are always equal.
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)
// Delete orphan key and reverse-lookup key.
batch.Delete(key)
// If there is no predecessor, or the predecessor is earlier than the
// beginning of the lifetime (ie: negative lifetime), or the lifetime
// spans a single version and that version is the one being deleted, we
// can delete the orphan. Otherwise, we shorten its lifetime, by
// moving its endpoint to the previous version.
if predecessor < fromVersion || fromVersion == toVersion {
ndb.log(IavlDebug, "DELETE", "predecessor", predecessor, "fromVersion", fromVersion, "toVersion", toVersion, "hash", hash)
batch.Delete(ndb.nodeKey(hash))
ndb.syncUnCacheNode(hash)
ndb.totalDeletedCount++
} else {
ndb.log(IavlDebug, "MOVE", "predecessor", predecessor, "fromVersion", fromVersion, "toVersion", toVersion, "hash", hash)
ndb.saveOrphan(batch, hash, fromVersion, predecessor)
}
})
}
func (ndb *nodeDB) nodeKey(hash []byte) []byte {
return nodeKeyFormat.KeyBytes(hash)
}
func (ndb *nodeDB) orphanKey(fromVersion, toVersion int64, hash []byte) []byte {
// return orphanKeyFormat.Key(toVersion, fromVersion, hash)
// we use orphanKeyFast to replace orphanKeyFormat.Key(toVersion, fromVersion, hash) for performance
return orphanKeyFast(fromVersion, toVersion, hash)
}
func (ndb *nodeDB) rootKey(version int64) []byte {
return rootKeyFormat.Key(version)
}
func (ndb *nodeDB) getLatestVersion() int64 {
if ndb.latestVersion == 0 {
ndb.latestVersion = ndb.getPreviousVersion(1<<63 - 1)
}
return ndb.latestVersion
}
func (ndb *nodeDB) updateLatestVersion(version int64) {
if ndb.latestVersion < version {
ndb.latestVersion = version
}
}
func (ndb *nodeDB) resetLatestVersion(version int64) {
ndb.latestVersion = version
}
func (ndb *nodeDB) getPreviousVersion(version int64) int64 {
itr, err := ndb.db.ReverseIterator(
rootKeyFormat.Key(1),
rootKeyFormat.Key(version),
)
if err != nil {
panic(err)
}
defer itr.Close()
pversion := int64(-1)
for ; itr.Valid(); itr.Next() {
k := itr.Key()
rootKeyFormat.Scan(k, &pversion)
return pversion
}
return 0
}
// deleteRoot deletes the root entry from disk, but not the node it points to.
func (ndb *nodeDB) deleteRoot(batch dbm.Batch, version int64, checkLatestVersion bool) {
if checkLatestVersion && version == ndb.getLatestVersion() {
panic("Tried to delete latest version")
}
batch.Delete(ndb.rootKey(version))
}
func (ndb *nodeDB) traverseOrphans(fn func(k, v []byte)) {
ndb.traversePrefix(orphanKeyFormat.Key(), fn)
}
// Traverse orphans ending at a certain version.
func (ndb *nodeDB) traverseOrphansVersion(version int64, fn func(k, v []byte)) {
ndb.traversePrefix(orphanKeyFormat.Key(version), fn)
}
// Traverse all keys.
func (ndb *nodeDB) traverse(fn func(key, value []byte)) {
ndb.traverseRange(nil, nil, fn)
}
// Traverse all keys between a given range (excluding end).
func (ndb *nodeDB) traverseRange(start []byte, end []byte, fn func(k, v []byte)) {
itr, err := ndb.db.Iterator(start, end)
if err != nil {
panic(err)
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
fn(itr.Key(), itr.Value())
}
}
// Traverse all keys with a certain prefix.
func (ndb *nodeDB) traversePrefix(prefix []byte, fn func(k, v []byte)) {
itr, err := dbm.IteratePrefix(ndb.db, prefix)
if err != nil {
panic(err)
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
fn(itr.Key(), itr.Value())
}
}
func (ndb *nodeDB) uncacheNode(hash []byte) {
if elem, ok := ndb.nodeCache[string(hash)]; ok {
ndb.nodeCacheQueue.Remove(elem)
delete(ndb.nodeCache, string(hash))
}
}
// Add a node to the cache and pop the least recently used node if we've
// reached the cache size limit.
func (ndb *nodeDB) cacheNode(node *Node) {
elem := ndb.nodeCacheQueue.PushBack(node)
ndb.nodeCache[string(node.hash)] = elem
for ndb.nodeCacheQueue.Len() > config.DynamicConfig.GetIavlCacheSize() {
oldest := ndb.nodeCacheQueue.Front()
hash := ndb.nodeCacheQueue.Remove(oldest).(*Node).hash
delete(ndb.nodeCache, string(hash))
}
}
func (ndb *nodeDB) cacheNodeByCheck(node *Node) {
if _, ok := ndb.nodeCache[string(node.hash)]; !ok {
ndb.cacheNode(node)
}
}
// Write to disk.
func (ndb *nodeDB) Commit(batch dbm.Batch) error {
ndb.log(IavlDebug, "committing data to disk")
var err error
if ndb.opts.Sync {
err = batch.WriteSync()
} else {
err = batch.Write()
}
if err != nil {
return errors.Wrap(err, "failed to write batch")
}
batch.Close()
return nil
}
func (ndb *nodeDB) getRoot(version int64) ([]byte, error) {
return ndb.dbGet(ndb.rootKey(version))
}
func (ndb *nodeDB) getRoots() (map[int64][]byte, error) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
roots := map[int64][]byte{}
ndb.traversePrefix(rootKeyFormat.Key(), func(k, v []byte) {
var version int64
rootKeyFormat.Scan(k, &version)
roots[version] = v
})
return roots, nil
}
// SaveRoot creates an entry on disk for the given root, so that it can be
// loaded later.
func (ndb *nodeDB) SaveRoot(batch dbm.Batch, root *Node, version int64) error {
if len(root.hash) == 0 {
panic("SaveRoot: root hash should not be empty")
}
ndb.log(IavlDebug, "saving root to disk", "version", version)
return ndb.saveRoot(batch, root.hash, version)
}
// SaveEmptyRoot creates an entry on disk for an empty root.
func (ndb *nodeDB) SaveEmptyRoot(batch dbm.Batch, version int64) error {
return ndb.saveRoot(batch, []byte{}, version)
}
func (ndb *nodeDB) saveRoot(batch dbm.Batch, hash []byte, version int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if !EnableAsyncCommit {
// We allow the initial version to be arbitrary
latest := ndb.getLatestVersion()
if !ignoreVersionCheck && latest > 0 && version != latest+1 {
return fmt.Errorf("must save consecutive versions; expected %d, got %d", latest+1, version)
}
}
batch.Set(ndb.rootKey(version), hash)
ndb.updateLatestVersion(version)
return nil
}
func (ndb *nodeDB) incrVersionReaders(version int64) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
ndb.versionReaders[version]++
}
func (ndb *nodeDB) decrVersionReaders(version int64) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if ndb.versionReaders[version] > 0 {
ndb.versionReaders[version]--
}
}
// Utility and test functions
func (ndb *nodeDB) leafNodes() []*Node {
leaves := []*Node{}
ndb.traverseNodes(func(hash []byte, node *Node) {
if node.isLeaf() {
leaves = append(leaves, node)
}
})
return leaves
}
func (ndb *nodeDB) nodes() []*Node {
nodes := []*Node{}
ndb.traverseNodes(func(hash []byte, node *Node) {
nodes = append(nodes, node)
})
return nodes
}
func (ndb *nodeDB) orphans() [][]byte {
orphans := [][]byte{}
ndb.traverseOrphans(func(k, v []byte) {
orphans = append(orphans, v)
})
return orphans
}
func (ndb *nodeDB) roots() map[int64][]byte {
roots, _ := ndb.getRoots()
return roots
}
// Not efficient.
// NOTE: DB cannot implement Size() because
// mutations are not always synchronous.
func (ndb *nodeDB) size() int {
size := 0
ndb.traverse(func(k, v []byte) {
size++
})
return size
}
func (ndb *nodeDB) traverseNodes(fn func(hash []byte, node *Node)) {
nodes := []*Node{}
ndb.traversePrefix(nodeKeyFormat.Key(), func(key, value []byte) {
node, err := MakeNode(value)
if err != nil {
panic(fmt.Sprintf("Couldn't decode node from database: %v", err))
}
nodeKeyFormat.Scan(key, &node.hash)
nodes = append(nodes, node)
})
sort.Slice(nodes, func(i, j int) bool {
return bytes.Compare(nodes[i].key, nodes[j].key) < 0
})
for _, n := range nodes {
fn(n.hash, n)
}
}
func (ndb *nodeDB) String() string {
var str string
index := 0
ndb.traversePrefix(rootKeyFormat.Key(), func(key, value []byte) {
str += fmt.Sprintf("%s: %x\n", string(key), value)
})
str += "\n"
ndb.traverseOrphans(func(key, value []byte) {
str += fmt.Sprintf("%s: %x\n", string(key), value)
})
str += "\n"
ndb.traverseNodes(func(hash []byte, node *Node) {
switch {
case len(hash) == 0:
str += "<nil>\n"
case node == nil:
str += fmt.Sprintf("%s%40x: <nil>\n", nodeKeyFormat.Prefix(), hash)
case node.value == nil && node.height > 0:
str += fmt.Sprintf("%s%40x: %s %-16s h=%d version=%d\n",
nodeKeyFormat.Prefix(), hash, node.key, "", node.height, node.version)
default:
str += fmt.Sprintf("%s%40x: %s = %-16s h=%d version=%d\n",
nodeKeyFormat.Prefix(), hash, node.key, node.value, node.height, node.version)
}
index++
})
return "-" + "\n" + str + "-"
}