Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align Cursor API with LMDB/MDBX #1558

Merged
merged 3 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions common/changeset/account_changeset_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (

func findInAccountChangeSet(c ethdb.CursorDupSort, blockNumber uint64, key []byte, keyLen int) ([]byte, error) {
fromDBFormat := FromDBFormat(keyLen)
k, v, err := c.SeekBothRange(dbutils.EncodeBlockNumber(blockNumber), key)
k := dbutils.EncodeBlockNumber(blockNumber)
v, err := c.SeekBothRange(k, key)
if err != nil {
return nil, err
}
if k == nil {
return nil, nil
}
_, k, v = fromDBFormat(k, v)
if !bytes.HasPrefix(k, key) {
return nil, nil
Expand Down
6 changes: 2 additions & 4 deletions common/changeset/storage_changeset_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ func doSearch2(
binary.BigEndian.PutUint64(seek, blockNumber)
copy(seek[8:], addrBytesToFind)
binary.BigEndian.PutUint64(seek[8+keyPrefixLen:], incarnation)
k, v, err := c.SeekBothRange(seek, keyBytesToFind)
k := seek
v, err := c.SeekBothRange(seek, keyBytesToFind)
if err != nil {
return nil, err
}
if k == nil {
return nil, ErrNotFound
}
if !bytes.HasPrefix(v, keyBytesToFind) {
return nil, ErrNotFound
}
Expand Down
6 changes: 4 additions & 2 deletions core/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func WalkAsOfStorage(tx ethdb.Tx, address common.Address, incarnation uint64, st
copy(csKey[:], dbutils.EncodeBlockNumber(changeSetBlock))
copy(csKey[8:], address[:]) // address + incarnation
binary.BigEndian.PutUint64(csKey[8+common.AddressLength:], incarnation)
kData, data, err3 := csCursor.SeekBothRange(csKey, hLoc)
kData := csKey
data, err3 := csCursor.SeekBothRange(csKey, hLoc)
if err3 != nil {
return err3
}
Expand Down Expand Up @@ -299,7 +300,8 @@ func WalkAsOfAccounts(tx ethdb.Tx, startAddress common.Address, timestamp uint64
if ok {
// Extract value from the changeSet
csKey := dbutils.EncodeBlockNumber(changeSetBlock)
kData, data, err3 := csCursor.SeekBothRange(csKey, hK)
kData := csKey
data, err3 := csCursor.SeekBothRange(csKey, hK)
if err3 != nil {
return err3
}
Expand Down
4 changes: 2 additions & 2 deletions ethdb/kv_abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ type CursorDupSort interface {
// SeekBothExact -
// second parameter can be nil only if searched key has no duplicates, or return error
SeekBothExact(key, value []byte) ([]byte, []byte, error)
SeekBothRange(key, value []byte) ([]byte, []byte, error)
SeekBothRange(key, value []byte) ([]byte, error)
FirstDup() ([]byte, error) // FirstDup - position at first data item of current key
NextDup() ([]byte, []byte, error) // NextDup - position at next data item of current key
NextNoDup() ([]byte, []byte, error) // NextNoDup - position at first data item of next key
LastDup(k []byte) ([]byte, error) // LastDup - position at last data item of current key
LastDup() ([]byte, error) // LastDup - position at last data item of current key

CountDuplicates() (uint64, error) // CountDuplicates - number of duplicates for the current key
DeleteCurrentDuplicates() error // DeleteCurrentDuplicates - deletes all of the data items for the current key
Expand Down
39 changes: 20 additions & 19 deletions ethdb/kv_lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (tx *lmdbTx) GetOne(bucket string, key []byte) ([]byte, error) {
return nil, err
}
defer c.Close()
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if lmdb.IsNotFound(err) {
return nil, nil
Expand Down Expand Up @@ -720,7 +720,7 @@ func (tx *lmdbTx) HasOne(bucket string, key []byte) (bool, error) {
return false, err
}
defer c.Close()
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if lmdb.IsNotFound(err) {
return false, nil
Expand Down Expand Up @@ -836,15 +836,16 @@ func (c *LmdbCursor) getBoth(k, v []byte) ([]byte, []byte, error) {
func (c *LmdbCursor) setRange(k []byte) ([]byte, []byte, error) {
return c.c.Get(k, nil, lmdb.SetRange)
}
func (c *LmdbCursor) getBothRange(k, v []byte) ([]byte, []byte, error) {
return c.c.Get(k, v, lmdb.GetBothRange)
func (c *LmdbCursor) getBothRange(k, v []byte) ([]byte, error) {
_, v, err := c.c.Get(k, v, lmdb.GetBothRange)
return v, err
}
func (c *LmdbCursor) firstDup() ([]byte, error) {
_, v, err := c.c.Get(nil, nil, lmdb.FirstDup)
return v, err
}
func (c *LmdbCursor) lastDup(k []byte) ([]byte, error) {
_, v, err := c.c.Get(k, nil, lmdb.LastDup)
func (c *LmdbCursor) lastDup() ([]byte, error) {
_, v, err := c.c.Get(nil, nil, lmdb.LastDup)
return v, err
}

Expand Down Expand Up @@ -985,7 +986,7 @@ func (c *LmdbCursor) seekDupSort(seek []byte) (k, v []byte, err error) {
}

if seek2 != nil && bytes.Equal(seek1, k) {
k, v, err = c.getBothRange(seek1, seek2)
v, err = c.getBothRange(seek1, seek2)
if err != nil && lmdb.IsNotFound(err) {
k, v, err = c.next()
if err != nil {
Expand Down Expand Up @@ -1166,7 +1167,7 @@ func (c *LmdbCursor) deleteDupSort(key []byte) error {
}

if len(key) == from {
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil { // if key not found, or found another one - then nothing to delete
if lmdb.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -1250,7 +1251,7 @@ func (c *LmdbCursor) putDupSort(key []byte, value []byte) error {

value = append(key[to:], value...)
key = key[:to]
_, v, err := c.getBothRange(key, value[:from-to])
v, err := c.getBothRange(key, value[:from-to])
if err != nil { // if key not found, or found another one - then just insert
if lmdb.IsNotFound(err) {
return c.put(key, value)
Expand Down Expand Up @@ -1300,7 +1301,7 @@ func (c *LmdbCursor) SeekExact(key []byte) ([]byte, []byte, error) {
b := c.bucketCfg
if b.AutoDupSortKeysConversion && len(key) == b.DupFromLen {
from, to := b.DupFromLen, b.DupToLen
k, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if lmdb.IsNotFound(err) {
return nil, nil, nil
Expand All @@ -1310,7 +1311,7 @@ func (c *LmdbCursor) SeekExact(key []byte) ([]byte, []byte, error) {
if !bytes.Equal(key[to:], v[:from-to]) {
return nil, nil, nil
}
return k, v[from-to:], nil
return key[:to], v[from-to:], nil
}

k, v, err := c.set(key)
Expand Down Expand Up @@ -1430,21 +1431,21 @@ func (c *LmdbDupSortCursor) SeekBothExact(key, value []byte) ([]byte, []byte, er
return k, v, nil
}

func (c *LmdbDupSortCursor) SeekBothRange(key, value []byte) ([]byte, []byte, error) {
func (c *LmdbDupSortCursor) SeekBothRange(key, value []byte) ([]byte, error) {
if c.c == nil {
if err := c.initCursor(); err != nil {
return []byte{}, nil, err
return nil, err
}
}

k, v, err := c.getBothRange(key, value)
v, err := c.getBothRange(key, value)
if err != nil {
if lmdb.IsNotFound(err) {
return nil, nil, nil
return nil, nil
}
return []byte{}, nil, fmt.Errorf("in SeekBothRange: %w", err)
return nil, fmt.Errorf("in SeekBothRange: %w", err)
}
return k, v, nil
return v, nil
}

func (c *LmdbDupSortCursor) FirstDup() ([]byte, error) {
Expand Down Expand Up @@ -1534,14 +1535,14 @@ func (c *LmdbDupSortCursor) PrevNoDup() ([]byte, []byte, error) {
return k, v, nil
}

func (c *LmdbDupSortCursor) LastDup(k []byte) ([]byte, error) {
func (c *LmdbDupSortCursor) LastDup() ([]byte, error) {
if c.c == nil {
if err := c.initCursor(); err != nil {
return nil, err
}
}

v, err := c.lastDup(k)
v, err := c.lastDup()
if err != nil {
if lmdb.IsNotFound(err) {
return nil, nil
Expand Down
52 changes: 27 additions & 25 deletions ethdb/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func (tx *MdbxTx) GetOne(bucket string, key []byte) ([]byte, error) {
return nil, err
}
defer c.Close()
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if mdbx.IsNotFound(err) {
return nil, nil
Expand Down Expand Up @@ -762,7 +762,7 @@ func (tx *MdbxTx) HasOne(bucket string, key []byte) (bool, error) {
return false, err
}
defer c.Close()
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if mdbx.IsNotFound(err) {
return false, nil
Expand Down Expand Up @@ -879,21 +879,23 @@ func (c *MdbxCursor) putNoDupData(k, v []byte) error { return c.c.Put(k
func (c *MdbxCursor) append(k, v []byte) error { return c.c.Put(k, v, mdbx.Append) }
func (c *MdbxCursor) appendDup(k, v []byte) error { return c.c.Put(k, v, mdbx.AppendDup) }
func (c *MdbxCursor) reserve(k []byte, n int) ([]byte, error) { return c.c.PutReserve(k, n, 0) }
func (c *MdbxCursor) getBoth(k, v []byte) ([]byte, []byte, error) {
return c.c.Get(k, v, mdbx.GetBoth)
func (c *MdbxCursor) getBoth(k, v []byte) ([]byte, error) {
_, v, err := c.c.Get(k, v, mdbx.GetBoth)
return v, err
}
func (c *MdbxCursor) setRange(k []byte) ([]byte, []byte, error) {
return c.c.Get(k, nil, mdbx.SetRange)
}
func (c *MdbxCursor) getBothRange(k, v []byte) ([]byte, []byte, error) {
return c.c.Get(k, v, mdbx.GetBothRange)
func (c *MdbxCursor) getBothRange(k, v []byte) ([]byte, error) {
_, v, err := c.c.Get(k, v, mdbx.GetBothRange)
return v, err
}
func (c *MdbxCursor) firstDup() ([]byte, error) {
_, v, err := c.c.Get(nil, nil, mdbx.FirstDup)
return v, err
}
func (c *MdbxCursor) lastDup(k []byte) ([]byte, error) {
_, v, err := c.c.Get(k, nil, mdbx.LastDup)
func (c *MdbxCursor) lastDup() ([]byte, error) {
_, v, err := c.c.Get(nil, nil, mdbx.LastDup)
return v, err
}

Expand Down Expand Up @@ -1035,7 +1037,7 @@ func (c *MdbxCursor) seekDupSort(seek []byte) (k, v []byte, err error) {
}

if seek2 != nil && bytes.Equal(seek1, k) {
k, v, err = c.getBothRange(seek1, seek2)
v, err = c.getBothRange(seek1, seek2)
if err != nil && mdbx.IsNotFound(err) {
k, v, err = c.next()
if err != nil {
Expand Down Expand Up @@ -1161,7 +1163,7 @@ func (c *MdbxCursor) Delete(k, v []byte) error {
}

if c.bucketCfg.Flags&mdbx.DupSort != 0 {
_, _, err := c.getBoth(k, v)
_, err := c.getBoth(k, v)
if err != nil {
if mdbx.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -1215,7 +1217,7 @@ func (c *MdbxCursor) deleteDupSort(key []byte) error {
}

if len(key) == from {
_, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil { // if key not found, or found another one - then nothing to delete
if mdbx.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -1299,7 +1301,7 @@ func (c *MdbxCursor) putDupSort(key []byte, value []byte) error {

value = append(key[to:], value...)
key = key[:to]
_, v, err := c.getBothRange(key, value[:from-to])
v, err := c.getBothRange(key, value[:from-to])
if err != nil { // if key not found, or found another one - then just insert
if mdbx.IsNotFound(err) {
return c.put(key, value)
Expand Down Expand Up @@ -1349,7 +1351,7 @@ func (c *MdbxCursor) SeekExact(key []byte) ([]byte, []byte, error) {
b := c.bucketCfg
if b.AutoDupSortKeysConversion && len(key) == b.DupFromLen {
from, to := b.DupFromLen, b.DupToLen
k, v, err := c.getBothRange(key[:to], key[to:])
v, err := c.getBothRange(key[:to], key[to:])
if err != nil {
if mdbx.IsNotFound(err) {
return nil, nil, nil
Expand All @@ -1359,7 +1361,7 @@ func (c *MdbxCursor) SeekExact(key []byte) ([]byte, []byte, error) {
if !bytes.Equal(key[to:], v[:from-to]) {
return nil, nil, nil
}
return k, v[from-to:], nil
return key[:to], v[from-to:], nil
}

k, v, err := c.set(key)
Expand Down Expand Up @@ -1462,7 +1464,7 @@ func (c *MdbxDupSortCursor) DeleteExact(k1, k2 []byte) error {
}
}

_, _, err := c.getBoth(k1, k2)
_, err := c.getBoth(k1, k2)
if err != nil { // if key not found, or found another one - then nothing to delete
if mdbx.IsNotFound(err) {
return nil
Expand All @@ -1479,31 +1481,31 @@ func (c *MdbxDupSortCursor) SeekBothExact(key, value []byte) ([]byte, []byte, er
}
}

k, v, err := c.getBoth(key, value)
v, err := c.getBoth(key, value)
if err != nil {
if mdbx.IsNotFound(err) {
return nil, nil, nil
}
return []byte{}, nil, fmt.Errorf("in SeekBothExact: %w", err)
}
return k, v, nil
return key, v, nil
}

func (c *MdbxDupSortCursor) SeekBothRange(key, value []byte) ([]byte, []byte, error) {
func (c *MdbxDupSortCursor) SeekBothRange(key, value []byte) ([]byte, error) {
if c.c == nil {
if err := c.initCursor(); err != nil {
return []byte{}, nil, err
return nil, err
}
}

k, v, err := c.getBothRange(key, value)
v, err := c.getBothRange(key, value)
if err != nil {
if mdbx.IsNotFound(err) {
return nil, nil, nil
return nil, nil
}
return []byte{}, nil, fmt.Errorf("in SeekBothRange: %w", err)
return nil, fmt.Errorf("in SeekBothRange: %w", err)
}
return k, v, nil
return v, nil
}

func (c *MdbxDupSortCursor) FirstDup() ([]byte, error) {
Expand Down Expand Up @@ -1593,14 +1595,14 @@ func (c *MdbxDupSortCursor) PrevNoDup() ([]byte, []byte, error) {
return k, v, nil
}

func (c *MdbxDupSortCursor) LastDup(k []byte) ([]byte, error) {
func (c *MdbxDupSortCursor) LastDup() ([]byte, error) {
if c.c == nil {
if err := c.initCursor(); err != nil {
return nil, err
}
}

v, err := c.lastDup(k)
v, err := c.lastDup()
if err != nil {
if mdbx.IsNotFound(err) {
return nil, nil
Expand Down
Loading