Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: height limited query with leveldb #3

Merged
merged 34 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
901ce69
feat: height limited leveldb
Oct 25, 2021
d6007c6
chore: remove redundant interfaces
Oct 25, 2021
244d420
fix: reverse iterator creation
Oct 28, 2021
2be1e76
feat: batch write with genesis block
Oct 28, 2021
c38a44c
fix: batch panic in genesis block execution
Oct 28, 2021
e53a857
feat: override cms
Nov 1, 2021
b543250
fix: mutate store's db instance
Nov 1, 2021
d0b76ce
feat: append byte for deletion flag into item rather than encode
Nov 3, 2021
d0efa68
feat: optimize key modifying
Nov 3, 2021
cff5f78
feat: separately store real data for sync
Nov 8, 2021
3697972
refactor: remove query handling middleware
Nov 9, 2021
6999b0d
refactor: rename function that duplicate hldb with new height
Nov 9, 2021
0ad00fe
refactor: remove test function
Nov 9, 2021
7f99247
refactor: rename key prefixes
Nov 9, 2021
c90578c
refactor: remove item struct
Nov 10, 2021
ec7c5d5
refactor: make heleveldb iterator composite tmdb.Iterator
Nov 12, 2021
be08080
Merge branch 'main' into feature/prefix-real-data
Nov 12, 2021
b5943d1
refactor: shorten key prefix
Nov 16, 2021
26efb07
feat: add archival cache, flush latest only
kjessec Nov 16, 2021
06de757
deps: tidy
kjessec Nov 16, 2021
22ba100
refactor: rename branching kvstore function.
Nov 16, 2021
47ab881
feat: cache last valid key in iterator
Nov 17, 2021
c5adc0e
fix: being locked when disable sync
Nov 17, 2021
b50fded
feat: invert key suffix not to use reverse iterator
Nov 17, 2021
a19144b
feat: cache last valid value in iterator
Nov 18, 2021
4a03387
feat: use default store on query without height
Nov 18, 2021
28b0531
Merge branch 'feature/prefix-real-data' into feature/prefix-real-data…
Nov 18, 2021
7864981
fix: check height from query string. check invalid height.
Nov 18, 2021
8efbb94
Merge pull request #2 from terra-money/feature/prefix-real-data-cache
Nov 18, 2021
f82117d
refactor: rename prefixing functions
Nov 18, 2021
5306124
Merge branch 'feature/height-limit-leveldb' into feature/key-suffix-desc
Nov 18, 2021
23eb9d3
style: add comments
Nov 18, 2021
e0225bd
Merge pull request #4 from terra-money/feature/key-suffix-desc
Nov 18, 2021
43fb510
feat: remove heleveldb test
Nov 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 0 additions & 131 deletions bin/v0.34.x/db/common/types.go

This file was deleted.

7 changes: 7 additions & 0 deletions bin/v0.34.x/db/heleveldb/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package heleveldb

type DriverConfig struct {
Name string
Dir string
Mode int
}
77 changes: 77 additions & 0 deletions bin/v0.34.x/db/heleveldb/leveldb_batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package heleveldb

import (
"math"

tmdb "github.com/tendermint/tm-db"
"github.com/terra-money/mantlemint-provider-v0.34.x/db/hld"
"github.com/terra-money/mantlemint/lib"
)

var _ hld.HeightLimitEnabledBatch = (*LevelBatch)(nil)

type LevelBatch struct {
height int64
batch tmdb.Batch
mode int
}

func (b *LevelBatch) keyBytesWithHeight(key []byte) []byte {
if b.mode == DriverModeKeySuffixAsc {
return append(prefixDataWithHeightKey(key), lib.UintToBigEndian(uint64(b.height))...)
} else {
return append(prefixDataWithHeightKey(key), lib.UintToBigEndian(math.MaxUint64-uint64(b.height))...)
}

}

func NewLevelDBBatch(atHeight int64, driver *Driver) *LevelBatch {
return &LevelBatch{
height: atHeight,
batch: driver.session.NewBatch(),
mode: driver.mode,
}
}

func (b *LevelBatch) Set(key, value []byte) error {
newKey := b.keyBytesWithHeight(key)

// make fixed size byte slice for performance
buf := make([]byte, 0, len(value)+1)
buf = append(buf, byte(0)) // 0 => not deleted
buf = append(buf, value...)

if err := b.batch.Set(prefixCurrentDataKey(key), buf[1:]); err != nil {
return err
}
if err := b.batch.Set(prefixKeysForIteratorKey(key), []byte{}); err != nil {
return err
}
return b.batch.Set(newKey, buf)
}

func (b *LevelBatch) Delete(key []byte) error {
newKey := b.keyBytesWithHeight(key)

buf := []byte{1}

if err := b.batch.Delete(prefixCurrentDataKey(key)); err != nil {
return err
}
if err := b.batch.Set(prefixKeysForIteratorKey(key), buf); err != nil {
return err
}
return b.batch.Set(newKey, buf)
}

func (b *LevelBatch) Write() error {
return b.batch.Write()
}

func (b *LevelBatch) WriteSync() error {
return b.batch.WriteSync()
}

func (b *LevelBatch) Close() error {
return b.batch.Close()
}
155 changes: 155 additions & 0 deletions bin/v0.34.x/db/heleveldb/leveldb_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package heleveldb

import (
"fmt"
"math"

tmdb "github.com/tendermint/tm-db"
"github.com/terra-money/mantlemint-provider-v0.34.x/db/hld"
"github.com/terra-money/mantlemint/lib"
)

type Driver struct {
session *tmdb.GoLevelDB
mode int
}

func NewLevelDBDriver(config *DriverConfig) (*Driver, error) {
ldb, err := tmdb.NewGoLevelDB(config.Name, config.Dir)
if err != nil {
return nil, err
}

return &Driver{
session: ldb,
mode: config.Mode,
}, nil
}

func (d *Driver) newInnerIterator(requestHeight int64, pdb *tmdb.PrefixDB) (tmdb.Iterator, error) {
if d.mode == DriverModeKeySuffixAsc {
heightEnd := lib.UintToBigEndian(uint64(requestHeight + 1))
return pdb.ReverseIterator(nil, heightEnd)
} else {
heightStart := lib.UintToBigEndian(math.MaxUint64 - uint64(requestHeight))
return pdb.Iterator(heightStart, nil)
}
}

func (d *Driver) Get(maxHeight int64, key []byte) ([]byte, error) {
if maxHeight == 0 {
return d.session.Get(prefixCurrentDataKey(key))
}
var requestHeight = hld.Height(maxHeight).CurrentOrLatest().ToInt64()
var requestHeightMin = hld.Height(0).CurrentOrNever().ToInt64()

// check if requestHeightMin is
if requestHeightMin > requestHeight {
return nil, fmt.Errorf("invalid height")
}

pdb := tmdb.NewPrefixDB(d.session, prefixDataWithHeightKey(key))

iter, _ := d.newInnerIterator(requestHeight, pdb)
defer iter.Close()

// in tm-db@v0.6.4, key not found is NOT an error
if !iter.Valid() {
return nil, nil
}

value := iter.Value()
deleted := value[0]
if deleted == 1 {
return nil, nil
} else {
if len(value) > 1 {
return value[1:], nil
}
return []byte{}, nil
}
}

func (d *Driver) Has(maxHeight int64, key []byte) (bool, error) {
if maxHeight == 0 {
return d.session.Has(prefixCurrentDataKey(key))
}
var requestHeight = hld.Height(maxHeight).CurrentOrLatest().ToInt64()
var requestHeightMin = hld.Height(0).CurrentOrNever().ToInt64()

// check if requestHeightMin is
if requestHeightMin > requestHeight {
return false, fmt.Errorf("invalid height")
}

pdb := tmdb.NewPrefixDB(d.session, prefixDataWithHeightKey(key))

iter, _ := d.newInnerIterator(requestHeight, pdb)
defer iter.Close()

// in tm-db@v0.6.4, key not found is NOT an error
if !iter.Valid() {
return false, nil
}

deleted := iter.Value()[0]

if deleted == 1 {
return false, nil
} else {
return true, nil
}
}

func (d *Driver) Set(atHeight int64, key, value []byte) error {
// should never reach here, all should be batched in tiered+hld
panic("should never reach here")
}

func (d *Driver) SetSync(atHeight int64, key, value []byte) error {
// should never reach here, all should be batched in tiered+hld
panic("should never reach here")
}

func (d *Driver) Delete(atHeight int64, key []byte) error {
// should never reach here, all should be batched in tiered+hld
panic("should never reach here")
}

func (d *Driver) DeleteSync(atHeight int64, key []byte) error {
return d.Delete(atHeight, key)
}

func (d *Driver) Iterator(maxHeight int64, start, end []byte) (hld.HeightLimitEnabledIterator, error) {
if maxHeight == 0 {
pdb := tmdb.NewPrefixDB(d.session, cCurrentDataPrefix)
return pdb.Iterator(start, end)
}
return NewLevelDBIterator(d, maxHeight, start, end)
}

func (d *Driver) ReverseIterator(maxHeight int64, start, end []byte) (hld.HeightLimitEnabledIterator, error) {
if maxHeight == 0 {
pdb := tmdb.NewPrefixDB(d.session, cCurrentDataPrefix)
return pdb.ReverseIterator(start, end)
}
return NewLevelDBReverseIterator(d, maxHeight, start, end)
}

func (d *Driver) Close() error {
d.session.Close()
return nil
}

func (d *Driver) NewBatch(atHeight int64) hld.HeightLimitEnabledBatch {
return NewLevelDBBatch(atHeight, d)
}

// TODO: Implement me
func (d *Driver) Print() error {
return nil
}

func (d *Driver) Stats() map[string]string {
return nil
}
Loading