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
|
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
|
||||||
|
@ -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.
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user