@@ -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
6635var (
@@ -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.
293262func (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 {
538507func (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-
658611func (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
813766func (ndb * nodeDB ) HasRoot (version int64 ) (bool , error ) {
814- return ndb .db .Has (ndb . rootKey (version ))
767+ return ndb .db .Has (getRootKey (version ))
815768}
816769
817770func (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??
821775func (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.
0 commit comments