Start adding versioned queries support
This commit is contained in:
parent
bc5edbdc89
commit
75e2591a68
26
app/store.go
26
app/store.go
|
@ -60,7 +60,7 @@ func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error) {
|
||||||
dbm.NewMemDB(),
|
dbm.NewMemDB(),
|
||||||
)
|
)
|
||||||
store := &Store{
|
store := &Store{
|
||||||
State: state.NewState(tree, false),
|
State: state.NewState(tree, true),
|
||||||
height: initialHeight,
|
height: initialHeight,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
|
@ -148,7 +148,7 @@ func (s *Store) Commit() abci.Result {
|
||||||
Height: s.height,
|
Height: s.height,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
hash, err := s.State.Commit()
|
hash, err := s.State.Commit(s.height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return abci.NewError(abci.CodeType_InternalError, err.Error())
|
return abci.NewError(abci.CodeType_InternalError, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -164,25 +164,25 @@ func (s *Store) Commit() abci.Result {
|
||||||
|
|
||||||
// Query implements abci.Application
|
// Query implements abci.Application
|
||||||
func (s *Store) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
|
func (s *Store) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
|
||||||
|
|
||||||
if reqQuery.Height != 0 {
|
|
||||||
// TODO: support older commits
|
|
||||||
resQuery.Code = abci.CodeType_InternalError
|
|
||||||
resQuery.Log = "merkleeyes only supports queries on latest commit"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the query response height to current
|
// set the query response height to current
|
||||||
resQuery.Height = s.height
|
|
||||||
|
|
||||||
tree := s.State.Committed()
|
tree := s.State.Committed()
|
||||||
|
|
||||||
|
height := reqQuery.Height
|
||||||
|
if height == 0 {
|
||||||
|
if tree.Tree.VersionExists(s.height - 1) {
|
||||||
|
height = s.height - 1
|
||||||
|
} else {
|
||||||
|
height = s.height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resQuery.Height = height
|
||||||
|
|
||||||
switch reqQuery.Path {
|
switch reqQuery.Path {
|
||||||
case "/store", "/key": // Get by key
|
case "/store", "/key": // Get by key
|
||||||
key := reqQuery.Data // Data holds the key bytes
|
key := reqQuery.Data // Data holds the key bytes
|
||||||
resQuery.Key = key
|
resQuery.Key = key
|
||||||
if reqQuery.Prove {
|
if reqQuery.Prove {
|
||||||
value, proof, err := tree.GetWithProof(key)
|
value, proof, err := tree.GetVersionedWithProof(key, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resQuery.Log = err.Error()
|
resQuery.Log = err.Error()
|
||||||
break
|
break
|
||||||
|
|
|
@ -54,6 +54,10 @@ func (b *Bonsai) GetWithProof(key []byte) ([]byte, iavl.KeyProof, error) {
|
||||||
return b.Tree.GetWithProof(key)
|
return b.Tree.GetWithProof(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bonsai) GetVersionedWithProof(key []byte, version uint64) ([]byte, iavl.KeyProof, error) {
|
||||||
|
return b.Tree.GetVersionedWithProof(key, version)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bonsai) List(start, end []byte, limit int) []Model {
|
func (b *Bonsai) List(start, end []byte, limit int) []Model {
|
||||||
res := []Model{}
|
res := []Model{}
|
||||||
stopAtCount := func(key []byte, value []byte) (stop bool) {
|
stopAtCount := func(key []byte, value []byte) (stop bool) {
|
||||||
|
|
|
@ -61,7 +61,7 @@ func (s *State) BatchSet(key, value []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit save persistent nodes to the database and re-copies the trees
|
// Commit save persistent nodes to the database and re-copies the trees
|
||||||
func (s *State) Commit() ([]byte, error) {
|
func (s *State) Commit(version uint64) ([]byte, error) {
|
||||||
// commit (if we didn't do hash earlier)
|
// commit (if we didn't do hash earlier)
|
||||||
err := s.committed.Commit(s.deliverTx)
|
err := s.committed.Commit(s.deliverTx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -70,10 +70,11 @@ func (s *State) Commit() ([]byte, error) {
|
||||||
|
|
||||||
var hash []byte
|
var hash []byte
|
||||||
if s.persistent {
|
if s.persistent {
|
||||||
nextVersion := s.committed.Tree.LatestVersion() + 1
|
if s.committed.Tree.Size() > 0 || s.committed.Tree.LatestVersion() > 0 {
|
||||||
hash, err = s.committed.Tree.SaveVersion(nextVersion)
|
hash, err = s.committed.Tree.SaveVersion(version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hash = s.committed.Tree.Hash()
|
hash = s.committed.Tree.Hash()
|
||||||
|
|
Loading…
Reference in New Issue