Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez committed Nov 16, 2023
1 parent 8fc7e93 commit 49c6c6c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
13 changes: 12 additions & 1 deletion store/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package store

import (
"fmt"

"cosmossdk.io/errors"
)

Expand Down Expand Up @@ -32,7 +34,16 @@ var (
ErrClosed = errors.Register(StoreCodespace, 8, "closed")
ErrRecordNotFound = errors.Register(StoreCodespace, 9, "record not found")
ErrUnknownStoreKey = errors.Register(StoreCodespace, 10, "unknown store key")
ErrVersionPruned = errors.Register(StoreCodespace, 11, "version pruned")
ErrKeyEmpty = errors.Register(StoreCodespace, 12, "key empty")
ErrStartAfterEnd = errors.Register(StoreCodespace, 13, "start key after end key")
)

// ErrVersionPruned defines an error returned when a version queried is pruned
// or does not exist.
type ErrVersionPruned struct {
EarliestVersion uint64
}

func (e ErrVersionPruned) Error() string {
return fmt.Sprintf("requested version is pruned; earliest available version is: %d", e.EarliestVersion)
}
2 changes: 1 addition & 1 deletion store/storage/pebbledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (db *Database) Has(storeKey string, version uint64, key []byte) (bool, erro

func (db *Database) Get(storeKey string, targetVersion uint64, key []byte) ([]byte, error) {
if targetVersion < db.earliestVersion {
return nil, store.ErrVersionPruned
return nil, store.ErrVersionPruned{EarliestVersion: db.earliestVersion}
}

prefixedVal, err := getMVCCSlice(db.storage, storeKey, key, targetVersion)
Expand Down
40 changes: 33 additions & 7 deletions store/storage/rocksdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"strings"

"github.com/linxGnu/grocksdb"
"golang.org/x/exp/slices"
Expand All @@ -33,6 +32,10 @@ var (
type Database struct {
storage *grocksdb.DB
cfHandle *grocksdb.ColumnFamilyHandle

// tsLow reflects the full_history_ts_low CF value, which is earliest version
// supported
tsLow uint64
}

func New(dataDir string) (*Database, error) {
Expand All @@ -41,16 +44,40 @@ func New(dataDir string) (*Database, error) {
return nil, fmt.Errorf("failed to open RocksDB: %w", err)
}

slice, err := storage.GetFullHistoryTsLow(cfHandle)
if err != nil {
return nil, fmt.Errorf("failed to get full_history_ts_low: %w", err)
}

var tsLow uint64
tsLowBz := copyAndFreeSlice(slice)
if len(tsLowBz) > 0 {
tsLow = binary.LittleEndian.Uint64(tsLowBz)
}

return &Database{
storage: storage,
cfHandle: cfHandle,
tsLow: tsLow,
}, nil
}

func NewWithDB(storage *grocksdb.DB, cfHandle *grocksdb.ColumnFamilyHandle) (*Database, error) {
slice, err := storage.GetFullHistoryTsLow(cfHandle)
if err != nil {
return nil, fmt.Errorf("failed to get full_history_ts_low: %w", err)
}

var tsLow uint64
tsLowBz := copyAndFreeSlice(slice)
if len(tsLowBz) > 0 {
tsLow = binary.LittleEndian.Uint64(tsLowBz)
}

return &Database{
storage: storage,
cfHandle: cfHandle,
tsLow: tsLow,
}, nil
}

Expand All @@ -64,16 +91,15 @@ func (db *Database) Close() error {
}

func (db *Database) getSlice(storeKey string, version uint64, key []byte) (*grocksdb.Slice, error) {
slice, err := db.storage.GetCF(
if version < db.tsLow {
return nil, store.ErrVersionPruned{EarliestVersion: db.tsLow}
}

return db.storage.GetCF(
newTSReadOptions(version),
db.cfHandle,
prependStoreKey(storeKey, key),
)
if err != nil && strings.Contains(err.Error(), "is smaller than full_history_ts_low") {
return nil, store.ErrVersionPruned
}

return slice, err
}

func (db *Database) SetLatestVersion(version uint64) error {
Expand Down
2 changes: 1 addition & 1 deletion store/storage/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (db *Database) Has(storeKey string, version uint64, key []byte) (bool, erro

func (db *Database) Get(storeKey string, targetVersion uint64, key []byte) ([]byte, error) {
if targetVersion < db.earliestVersion {
return nil, store.ErrVersionPruned
return nil, store.ErrVersionPruned{EarliestVersion: db.earliestVersion}
}

stmt, err := db.storage.Prepare(`
Expand Down

0 comments on commit 49c6c6c

Please sign in to comment.