Skip to content

Commit baa713d

Browse files
committed
Some minimal repo cleanup
1 parent a324be8 commit baa713d

File tree

12 files changed

+79
-357
lines changed

12 files changed

+79
-357
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ cpu*.pdf
1414
mem*.pdf
1515

1616
# IDE files
17-
.idea/*
17+
.idea/*
18+
.vscode/*

docs/node/nodedb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (ndb *nodeDB) deleteOrphans(version int64) {
4646
// can delete the orphan. Otherwise, we shorten its lifetime, by
4747
// moving its endpoint to the previous version.
4848
if predecessor < fromVersion || fromVersion == toVersion {
49-
ndb.batch.Delete(ndb.nodeKey(hash))
49+
ndb.batch.Delete(getNodeKey(hash))
5050
ndb.uncacheNode(hash)
5151
} else {
5252
ndb.saveOrphan(hash, fromVersion, predecessor)

import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ func (i *Importer) Commit() error {
162162

163163
switch len(i.stack) {
164164
case 0:
165-
if err := i.batch.Set(i.tree.ndb.rootKey(i.version), []byte{}); err != nil {
165+
if err := i.batch.Set(getRootKey(i.version), []byte{}); err != nil {
166166
panic(err)
167167
}
168168
case 1:
169-
if err := i.batch.Set(i.tree.ndb.rootKey(i.version), i.stack[0].hash); err != nil {
169+
if err := i.batch.Set(getRootKey(i.version), i.stack[0].hash); err != nil {
170170
panic(err)
171171
}
172172
default:

key_format.go renamed to keyformat/key_format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package iavl
1+
package keyformat
22

33
import (
44
"encoding/binary"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package iavl
1+
package keyformat
22

33
import (
44
"testing"

keyformats.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package iavl
2+
3+
import "github.com/cosmos/iavl/keyformat"
4+
5+
var (
6+
// All node keys are prefixed with the byte 'n'. This ensures no collision is
7+
// possible with the other keys, and makes them easier to traverse. They are indexed by the node hash.
8+
nodeKeyFormat = keyformat.NewKeyFormat('n', hashSize) // n<hash>
9+
10+
// Orphans are keyed in the database by their expected lifetime.
11+
// The first number represents the *last* version at which the orphan needs
12+
// to exist, while the second number represents the *earliest* version at
13+
// which it is expected to exist - which starts out by being the version
14+
// of the node being orphaned.
15+
// To clarify:
16+
// When I write to key {X} with value V and old value O, we orphan O with <last-version>=time of write
17+
// and <first-version> = version O was created at.
18+
orphanKeyFormat = keyformat.NewKeyFormat('o', int64Size, int64Size, hashSize) // o<last-version><first-version><hash>
19+
20+
// Key Format for making reads and iterates go through a data-locality preserving db.
21+
// The value at an entry will list what version it was written to.
22+
// Then to query values, you first query state via this fast method.
23+
// If its present, then check the tree version. If tree version >= result_version,
24+
// return result_version. Else, go through old (slow) IAVL get method that walks through tree.
25+
fastKeyFormat = keyformat.NewKeyFormat('f', 0) // f<keystring>
26+
27+
// Key Format for storing metadata about the chain such as the vesion number.
28+
// The value at an entry will be in a variable format and up to the caller to
29+
// decide how to parse.
30+
metadataKeyFormat = keyformat.NewKeyFormat('m', 0) // v<keystring>
31+
32+
// Root nodes are indexed separately by their version
33+
rootKeyFormat = keyformat.NewKeyFormat('r', int64Size) // r<version>
34+
)
35+
36+
func getRootKey(version int64) []byte {
37+
return rootKeyFormat.Key(version)
38+
}
39+
40+
func getNodeKey(hash []byte) []byte {
41+
return nodeKeyFormat.KeyBytes(hash)
42+
}
43+
44+
func getFastNodeKey(key []byte) []byte {
45+
return fastKeyFormat.KeyBytes(key)
46+
}
47+
48+
func getOrphanKey(fromVersion, toVersion int64, hash []byte) []byte {
49+
return orphanKeyFormat.Key(toVersion, fromVersion, hash)
50+
}

mutable_tree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestDelete(t *testing.T) {
3333
k1Value, _, _ := tree.GetVersionedWithProof([]byte("k1"), version)
3434
require.Nil(t, k1Value)
3535

36-
key := tree.ndb.rootKey(version)
36+
key := getRootKey(version)
3737
err = memDB.Set(key, hash)
3838
require.NoError(t, err)
3939
tree.versions[version] = true

nodedb.go

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,7 @@ const (
2929
// Using semantic versioning: https://semver.org/
3030
defaultStorageVersionValue = "1.0.0"
3131
fastStorageVersionValue = "1.1.0"
32-
fastNodeCacheLimit = 100000
33-
)
34-
35-
var (
36-
// All node keys are prefixed with the byte 'n'. This ensures no collision is
37-
// possible with the other keys, and makes them easier to traverse. They are indexed by the node hash.
38-
nodeKeyFormat = NewKeyFormat('n', hashSize) // n<hash>
39-
40-
// Orphans are keyed in the database by their expected lifetime.
41-
// The first number represents the *last* version at which the orphan needs
42-
// to exist, while the second number represents the *earliest* version at
43-
// which it is expected to exist - which starts out by being the version
44-
// of the node being orphaned.
45-
// To clarify:
46-
// When I write to key {X} with value V and old value O, we orphan O with <last-version>=time of write
47-
// and <first-version> = version O was created at.
48-
orphanKeyFormat = NewKeyFormat('o', int64Size, int64Size, hashSize) // o<last-version><first-version><hash>
49-
50-
// Key Format for making reads and iterates go through a data-locality preserving db.
51-
// The value at an entry will list what version it was written to.
52-
// Then to query values, you first query state via this fast method.
53-
// If its present, then check the tree version. If tree version >= result_version,
54-
// return result_version. Else, go through old (slow) IAVL get method that walks through tree.
55-
fastKeyFormat = NewKeyFormat('f', 0) // f<keystring>
56-
57-
// Key Format for storing metadata about the chain such as the vesion number.
58-
// The value at an entry will be in a variable format and up to the caller to
59-
// decide how to parse.
60-
metadataKeyFormat = NewKeyFormat('m', 0) // v<keystring>
61-
62-
// Root nodes are indexed separately by their version
63-
rootKeyFormat = NewKeyFormat('r', int64Size) // r<version>
32+
fastNodeCacheLimit = 100000
6433
)
6534

6635
var (
@@ -119,12 +88,12 @@ func (ndb *nodeDB) GetNode(hash []byte) *Node {
11988
}
12089

12190
// Doesn't exist, load.
122-
buf, err := ndb.db.Get(ndb.nodeKey(hash))
91+
buf, err := ndb.db.Get(getNodeKey(hash))
12392
if err != nil {
12493
panic(fmt.Sprintf("can't get node %X: %v", hash, err))
12594
}
12695
if buf == nil {
127-
panic(fmt.Sprintf("Value missing for hash %x corresponding to nodeKey %x", hash, ndb.nodeKey(hash)))
96+
panic(fmt.Sprintf("Value missing for hash %x corresponding to nodeKey %x", hash, getNodeKey(hash)))
12897
}
12998

13099
node, err := MakeNode(buf)
@@ -156,7 +125,7 @@ func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) {
156125
}
157126

158127
// Doesn't exist, load.
159-
buf, err := ndb.db.Get(ndb.fastNodeKey(key))
128+
buf, err := ndb.db.Get(getFastNodeKey(key))
160129
if err != nil {
161130
return nil, fmt.Errorf("can't get FastNode %X: %w", key, err)
162131
}
@@ -193,7 +162,7 @@ func (ndb *nodeDB) SaveNode(node *Node) {
193162
panic(err)
194163
}
195164

196-
if err := ndb.batch.Set(ndb.nodeKey(node.hash), buf.Bytes()); err != nil {
165+
if err := ndb.batch.Set(getNodeKey(node.hash), buf.Bytes()); err != nil {
197166
panic(err)
198167
}
199168
debug("BATCH SAVE %X %p\n", node.hash, node)
@@ -280,7 +249,7 @@ func (ndb *nodeDB) saveFastNodeUnlocked(node *FastNode, shouldAddToCache bool) e
280249
return fmt.Errorf("error while writing fastnode bytes. Err: %w", err)
281250
}
282251

283-
if err := ndb.batch.Set(ndb.fastNodeKey(node.key), buf.Bytes()); err != nil {
252+
if err := ndb.batch.Set(getFastNodeKey(node.key), buf.Bytes()); err != nil {
284253
return fmt.Errorf("error while writing key/val to nodedb batch. Err: %w", err)
285254
}
286255
if shouldAddToCache {
@@ -291,7 +260,7 @@ func (ndb *nodeDB) saveFastNodeUnlocked(node *FastNode, shouldAddToCache bool) e
291260

292261
// Has checks if a hash exists in the database.
293262
func (ndb *nodeDB) Has(hash []byte) (bool, error) {
294-
key := ndb.nodeKey(hash)
263+
key := getNodeKey(hash)
295264

296265
if ldb, ok := ndb.db.(*dbm.GoLevelDB); ok {
297266
exists, err := ldb.DB().Has(key, nil)
@@ -418,7 +387,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
418387
if err = ndb.batch.Delete(key); err != nil {
419388
return err
420389
}
421-
if err = ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
390+
if err = ndb.batch.Delete(getNodeKey(hash)); err != nil {
422391
return err
423392
}
424393
ndb.nodeCache.Remove(hash)
@@ -507,7 +476,7 @@ func (ndb *nodeDB) DeleteVersionsRange(fromVersion, toVersion int64) error {
507476
return err
508477
}
509478
if from > predecessor {
510-
if err := ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
479+
if err := ndb.batch.Delete(getNodeKey(hash)); err != nil {
511480
panic(err)
512481
}
513482
ndb.nodeCache.Remove(hash)
@@ -538,7 +507,7 @@ func (ndb *nodeDB) DeleteVersionsRange(fromVersion, toVersion int64) error {
538507
func (ndb *nodeDB) DeleteFastNode(key []byte) error {
539508
ndb.mtx.Lock()
540509
defer ndb.mtx.Unlock()
541-
if err := ndb.batch.Delete(ndb.fastNodeKey(key)); err != nil {
510+
if err := ndb.batch.Delete(getFastNodeKey(key)); err != nil {
542511
return err
543512
}
544513
ndb.fastNodeCache.Remove(key)
@@ -565,7 +534,7 @@ func (ndb *nodeDB) deleteNodesFrom(version int64, hash []byte) error {
565534
}
566535

567536
if node.version >= version {
568-
if err := ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
537+
if err := ndb.batch.Delete(getNodeKey(hash)); err != nil {
569538
return err
570539
}
571540

@@ -594,7 +563,7 @@ func (ndb *nodeDB) saveOrphan(hash []byte, fromVersion, toVersion int64) {
594563
if fromVersion > toVersion {
595564
panic(fmt.Sprintf("Orphan expires before it comes alive. %d > %d", fromVersion, toVersion))
596565
}
597-
key := ndb.orphanKey(fromVersion, toVersion, hash)
566+
key := getOrphanKey(fromVersion, toVersion, hash)
598567
if err := ndb.batch.Set(key, hash); err != nil {
599568
panic(err)
600569
}
@@ -627,7 +596,7 @@ func (ndb *nodeDB) deleteOrphans(version int64) error {
627596
// moving its endpoint to the previous version.
628597
if predecessor < fromVersion || fromVersion == toVersion {
629598
debug("DELETE predecessor:%v fromVersion:%v toVersion:%v %X\n", predecessor, fromVersion, toVersion, hash)
630-
if err := ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
599+
if err := ndb.batch.Delete(getNodeKey(hash)); err != nil {
631600
return err
632601
}
633602
ndb.nodeCache.Remove(hash)
@@ -639,22 +608,6 @@ func (ndb *nodeDB) deleteOrphans(version int64) error {
639608
})
640609
}
641610

642-
func (ndb *nodeDB) nodeKey(hash []byte) []byte {
643-
return nodeKeyFormat.KeyBytes(hash)
644-
}
645-
646-
func (ndb *nodeDB) fastNodeKey(key []byte) []byte {
647-
return fastKeyFormat.KeyBytes(key)
648-
}
649-
650-
func (ndb *nodeDB) orphanKey(fromVersion, toVersion int64, hash []byte) []byte {
651-
return orphanKeyFormat.Key(toVersion, fromVersion, hash)
652-
}
653-
654-
func (ndb *nodeDB) rootKey(version int64) []byte {
655-
return rootKeyFormat.Key(version)
656-
}
657-
658611
func (ndb *nodeDB) getLatestVersion() int64 {
659612
if ndb.latestVersion == 0 {
660613
ndb.latestVersion = ndb.getPreviousVersion(1<<63 - 1)
@@ -701,7 +654,7 @@ func (ndb *nodeDB) deleteRoot(version int64, checkLatestVersion bool) error {
701654
if checkLatestVersion && version == ndb.getLatestVersion() {
702655
return errors.New("Tried to delete latest version")
703656
}
704-
if err := ndb.batch.Delete(ndb.rootKey(version)); err != nil {
657+
if err := ndb.batch.Delete(getRootKey(version)); err != nil {
705658
return err
706659
}
707660
return nil
@@ -811,13 +764,14 @@ func (ndb *nodeDB) Commit() error {
811764
}
812765

813766
func (ndb *nodeDB) HasRoot(version int64) (bool, error) {
814-
return ndb.db.Has(ndb.rootKey(version))
767+
return ndb.db.Has(getRootKey(version))
815768
}
816769

817770
func (ndb *nodeDB) getRoot(version int64) ([]byte, error) {
818-
return ndb.db.Get(ndb.rootKey(version))
771+
return ndb.db.Get(getRootKey(version))
819772
}
820773

774+
// TODO: Why does this method exist??
821775
func (ndb *nodeDB) getRoots() (map[int64][]byte, error) {
822776
roots := map[int64][]byte{}
823777

@@ -854,7 +808,7 @@ func (ndb *nodeDB) saveRoot(hash []byte, version int64) error {
854808
return fmt.Errorf("must save consecutive versions; expected %d, got %d", latest+1, version)
855809
}
856810

857-
if err := ndb.batch.Set(ndb.rootKey(version), hash); err != nil {
811+
if err := ndb.batch.Set(getRootKey(version), hash); err != nil {
858812
return err
859813
}
860814

@@ -926,11 +880,6 @@ func (ndb *nodeDB) orphans() ([][]byte, error) {
926880
return orphans, nil
927881
}
928882

929-
func (ndb *nodeDB) roots() map[int64][]byte {
930-
roots, _ := ndb.getRoots()
931-
return roots
932-
}
933-
934883
// Not efficient.
935884
// NOTE: DB cannot implement Size() because
936885
// mutations are not always synchronous.

nodedb_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ import (
1414
"github.com/cosmos/iavl/mock"
1515
)
1616

17-
func BenchmarkNodeKey(b *testing.B) {
18-
ndb := &nodeDB{}
19-
hashes := makeHashes(b, 2432325)
20-
for i := 0; i < b.N; i++ {
21-
ndb.nodeKey(hashes[i])
22-
}
23-
}
24-
25-
func BenchmarkOrphanKey(b *testing.B) {
26-
ndb := &nodeDB{}
27-
hashes := makeHashes(b, 2432325)
28-
for i := 0; i < b.N; i++ {
29-
ndb.orphanKey(1234, 1239, hashes[i])
30-
}
31-
}
32-
3317
func TestNewNoDbStorage_StorageVersionInDb_Success(t *testing.T) {
3418
const expectedVersion = defaultStorageVersionValue
3519

0 commit comments

Comments
 (0)