Skip to content

Commit

Permalink
introduce two layer trie
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed Feb 12, 2020
1 parent f85b085 commit f266fe9
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
4 changes: 4 additions & 0 deletions db/trie/branchroottrie.go
Expand Up @@ -74,6 +74,10 @@ func (tr *branchRootTrie) SetRootHash(rootHash []byte) error {
return nil
}

func (tr *branchRootTrie) IsEmpty() bool {
return tr.isEmptyRootHash(tr.rootHash)
}

func (tr *branchRootTrie) Get(key []byte) ([]byte, error) {
trieMtc.WithLabelValues("root", "Get").Inc()
kt, err := tr.checkKeyType(key)
Expand Down
2 changes: 2 additions & 0 deletions db/trie/trie.go
Expand Up @@ -58,6 +58,8 @@ type Trie interface {
RootHash() []byte
// SetRootHash sets a new root to trie
SetRootHash([]byte) error
// IsEmpty returns true is this is an empty trie
IsEmpty() bool
// DB returns the KVStore storing the node data
DB() KVStore
// deleteNodeFromDB deletes the data of node from db
Expand Down
100 changes: 100 additions & 0 deletions state/factory/twolayertrie.go
@@ -0,0 +1,100 @@
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package factory

import (
"context"

"github.com/iotexproject/iotex-core/db/trie"
"github.com/pkg/errors"
)

// TwoLayerTrie is a trie data structure with two layers
type TwoLayerTrie struct {
layerOne trie.Trie
}

// Start starts the layer one trie
func (tlt *TwoLayerTrie) Start(ctx context.Context) error {
return tlt.layerOne.Start(ctx)
}

// Stop stops the layer one trie
func (tlt *TwoLayerTrie) Stop(ctx context.Context) error {
return tlt.layerOne.Stop(ctx)
}

// RootHash returns the layer one trie root
func (tlt *TwoLayerTrie) RootHash() []byte {
return tlt.layerOne.RootHash()
}

func (tlt *TwoLayerTrie) layerTwoTrie(key []byte) (trie.Trie, error) {
value, err := tlt.layerOne.Get(key)
switch errors.Cause(err) {
case trie.ErrNotExist:
return trie.NewTrie(trie.KVStoreOption(tlt.layerOne.DB()))
case nil:
return trie.NewTrie(trie.KVStoreOption(tlt.layerOne.DB()), trie.RootHashOption(value))
default:
return nil, err
}
}

// Get returns the layer two value
func (tlt *TwoLayerTrie) Get(layerOneKey []byte, layerTwoKey []byte) ([]byte, error) {
layerTwo, err := tlt.layerTwoTrie(layerOneKey)
if err != nil {
return nil, err
}
if err := layerTwo.Start(context.Background()); err != nil {
return nil, err
}
defer layerTwo.Stop(context.Background())

return layerTwo.Get(layerTwoKey)
}

// Upsert upserts an item
func (tlt *TwoLayerTrie) Upsert(layerOneKey []byte, layerTwoKey []byte, value []byte) error {
layerTwo, err := tlt.layerTwoTrie(layerOneKey)
if err != nil {
return err
}
if err := layerTwo.Start(context.Background()); err != nil {
return err
}
defer layerTwo.Stop(context.Background())

if err := layerTwo.Upsert(layerTwoKey, value); err != nil {
return err
}

return tlt.layerOne.Upsert(layerOneKey, layerTwo.RootHash())
}

// Delete deletes an item
func (tlt *TwoLayerTrie) Delete(layerOneKey []byte, layerTwoKey []byte) error {
layerTwo, err := tlt.layerTwoTrie(layerOneKey)
if err != nil {
return err
}
if err := layerTwo.Start(context.Background()); err != nil {
return err
}
defer layerTwo.Stop(context.Background())

if err := layerTwo.Delete(layerTwoKey); err != nil {
return err
}

if layerTwo.IsEmpty() {
return tlt.layerOne.Delete(layerOneKey)
}

return tlt.layerOne.Upsert(layerOneKey, layerTwo.RootHash())
}
9 changes: 9 additions & 0 deletions state/factory/twolayertrie_test.go
@@ -0,0 +1,9 @@
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package factory

// TODO: add unit test for two layer trie
4 changes: 2 additions & 2 deletions test/mock/mock_factory/mock_workingset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/mock/mock_trie/mock_trie.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f266fe9

Please sign in to comment.