mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
[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:
parent
f8b18e09d6
commit
c1a039de3f
@ -11,8 +11,9 @@ type Cursor interface {
|
||||
First() bool
|
||||
|
||||
// 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.
|
||||
Seek(key []byte) (bool, error)
|
||||
// than or equal to the given key. It returns ErrNotFound if such pair does not
|
||||
// exist.
|
||||
Seek(key []byte) error
|
||||
|
||||
// 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
|
||||
|
@ -2,6 +2,7 @@ package ldb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"github.com/kaspanet/kaspad/database"
|
||||
"github.com/pkg/errors"
|
||||
"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
|
||||
// than or equal to the given key. It returns whether such pair exist.
|
||||
func (c *LevelDBCursor) Seek(key []byte) (bool, error) {
|
||||
// than or equal to the given key. It returns ErrNotFound if such pair does not
|
||||
// exist.
|
||||
func (c *LevelDBCursor) Seek(key []byte) error {
|
||||
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.
|
||||
|
@ -43,15 +43,16 @@ func BlockIndexCursorFrom(context Context, blockIndexKey []byte) (database.Curso
|
||||
return nil, err
|
||||
}
|
||||
|
||||
found, err := cursor.Seek(blockIndexKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !found {
|
||||
key := blockIndexBucket.Key(blockIndexKey)
|
||||
err = cursor.Seek(key)
|
||||
if IsNotFoundError(err) {
|
||||
cursor.Close()
|
||||
return nil, errors.Wrapf(database.ErrNotFound,
|
||||
"entry not found for %s", hex.EncodeToString(blockIndexKey))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cursor, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user