[NOD-900] Fix Seek not working as expected (#686)

* [NOD-900] Fix Seek not working at expected.

* [NOD-900] Wrap error messages.
This commit is contained in:
stasatdaglabs 2020-04-02 17:05:58 +03:00 committed by GitHub
parent f8b18e09d6
commit c1a039de3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 11 deletions

View File

@ -11,8 +11,9 @@ type Cursor interface {
First() bool First() bool
// Seek moves the iterator to the first key/value pair whose key is greater // Seek moves the iterator to the first key/value pair whose key is greater
// than or equal to the given key. It returns whether such pair exist. // than or equal to the given key. It returns ErrNotFound if such pair does not
Seek(key []byte) (bool, error) // exist.
Seek(key []byte) error
// Key returns the key of the current key/value pair, or ErrNotFound if done. // Key returns the key of the current key/value pair, or ErrNotFound if done.
// Note that the key is trimmed to not include the prefix the cursor was opened // Note that the key is trimmed to not include the prefix the cursor was opened

View File

@ -2,6 +2,7 @@ package ldb
import ( import (
"bytes" "bytes"
"encoding/hex"
"github.com/kaspanet/kaspad/database" "github.com/kaspanet/kaspad/database"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb/iterator" "github.com/syndtr/goleveldb/leveldb/iterator"
@ -45,12 +46,28 @@ func (c *LevelDBCursor) First() bool {
} }
// Seek moves the iterator to the first key/value pair whose key is greater // Seek moves the iterator to the first key/value pair whose key is greater
// than or equal to the given key. It returns whether such pair exist. // than or equal to the given key. It returns ErrNotFound if such pair does not
func (c *LevelDBCursor) Seek(key []byte) (bool, error) { // exist.
func (c *LevelDBCursor) Seek(key []byte) error {
if c.isClosed { if c.isClosed {
return false, errors.New("cannot seek a closed cursor") return errors.New("cannot seek a closed cursor")
} }
return c.ldbIterator.Seek(key), nil
found := c.ldbIterator.Seek(key)
if !found {
return errors.Wrapf(database.ErrNotFound, "key %s not "+
"found", hex.EncodeToString(key))
}
currentKey, err := c.Key()
if err != nil {
return err
}
if !bytes.Equal(currentKey, key) {
return errors.Wrapf(database.ErrNotFound, "key %s not "+
"found", hex.EncodeToString(key))
}
return nil
} }
// Key returns the key of the current key/value pair, or ErrNotFound if done. // Key returns the key of the current key/value pair, or ErrNotFound if done.

View File

@ -43,15 +43,16 @@ func BlockIndexCursorFrom(context Context, blockIndexKey []byte) (database.Curso
return nil, err return nil, err
} }
found, err := cursor.Seek(blockIndexKey) key := blockIndexBucket.Key(blockIndexKey)
if err != nil { err = cursor.Seek(key)
return nil, err if IsNotFoundError(err) {
}
if !found {
cursor.Close() cursor.Close()
return nil, errors.Wrapf(database.ErrNotFound, return nil, errors.Wrapf(database.ErrNotFound,
"entry not found for %s", hex.EncodeToString(blockIndexKey)) "entry not found for %s", hex.EncodeToString(blockIndexKey))
} }
if err != nil {
return nil, err
}
return cursor, nil return cursor, nil
} }