Skip to content

Commit

Permalink
move NewHashBatch to erigon-lib, remove oddb package (erigontech#8408)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Oct 9, 2023
1 parent afd88ed commit 62395c7
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 1,048 deletions.
10 changes: 5 additions & 5 deletions cmd/hack/db/lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func generate6(_ kv.RwDB, tx kv.RwTx) (bool, error) {
}

func dropT(_ kv.RwDB, tx kv.RwTx) (bool, error) {
if err := tx.(kv.BucketMigrator).ClearBucket("t"); err != nil {
if err := tx.ClearBucket("t"); err != nil {
return false, err
}
return true, nil
Expand Down Expand Up @@ -607,14 +607,14 @@ func generate7(_ kv.RwDB, tx kv.RwTx) (bool, error) {
}

func dropT1(_ kv.RwDB, tx kv.RwTx) (bool, error) {
if err := tx.(kv.BucketMigrator).ClearBucket("t1"); err != nil {
if err := tx.ClearBucket("t1"); err != nil {
return false, err
}
return true, nil
}

func dropT2(_ kv.RwDB, tx kv.RwTx) (bool, error) {
if err := tx.(kv.BucketMigrator).ClearBucket("t2"); err != nil {
if err := tx.ClearBucket("t2"); err != nil {
return false, err
}
return true, nil
Expand All @@ -624,7 +624,7 @@ func dropT2(_ kv.RwDB, tx kv.RwTx) (bool, error) {
func generate8(_ kv.RwDB, tx kv.RwTx) (bool, error) {
for i := 0; i < 100; i++ {
k := fmt.Sprintf("table_%05d", i)
if err := tx.(kv.BucketMigrator).CreateBucket(k); err != nil {
if err := tx.CreateBucket(k); err != nil {
return false, err
}
}
Expand Down Expand Up @@ -656,7 +656,7 @@ func generate9(tx kv.RwTx, entries int) error {
func dropAll(_ kv.RwDB, tx kv.RwTx) (bool, error) {
for i := 0; i < 100; i++ {
k := fmt.Sprintf("table_%05d", i)
if err := tx.(kv.BucketMigrator).DropBucket(k); err != nil {
if err := tx.DropBucket(k); err != nil {
return false, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ledgerwatch/erigon/core/types/accounts"
)

func ReadAccount(db kv.Tx, addr libcommon.Address, acc *accounts.Account) (bool, error) {
func ReadAccount(db kv.Getter, addr libcommon.Address, acc *accounts.Account) (bool, error) {
enc, err := db.GetOne(kv.PlainState, addr[:])
if err != nil {
return false, err
Expand Down
13 changes: 11 additions & 2 deletions erigon-lib/kv/kv_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,6 @@ type StatelessReadTx interface {
// Sequence changes become visible outside the current write transaction after it is committed, and discarded on abort.
// Starts from 0.
ReadSequence(table string) (uint64, error)

BucketSize(table string) (uint64, error)
}

type StatelessWriteTx interface {
Expand Down Expand Up @@ -340,6 +338,16 @@ type StatelessRwTx interface {
StatelessWriteTx
}

// PendingMutations in-memory storage of changes
// Later they can either be flushed to the database or abandon
type PendingMutations interface {
StatelessRwTx
// Flush all in-memory data into `tx`
Flush(ctx context.Context, tx RwTx) error
Close()
BatchSize() int
}

// Tx
// WARNING:
// - Tx is not threadsafe and may only be used in the goroutine that created it
Expand Down Expand Up @@ -397,6 +405,7 @@ type Tx interface {

// Pointer to the underlying C transaction handle (e.g. *C.MDBX_txn)
CHandle() unsafe.Pointer
BucketSize(table string) (uint64, error)
}

// RwTx
Expand Down
5 changes: 1 addition & 4 deletions erigon-lib/kv/mdbx/kv_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func TestBucketCRUD(t *testing.T) {

normalBucket := kv.ChaindataTables[15]
deprecatedBucket := kv.ChaindataDeprecatedTables[0]
migrator, ok := tx.(kv.BucketMigrator)
if !ok {
return
}
migrator := tx

// check thad buckets have unique DBI's
uniquness := map[kv.DBI]bool{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//go:build !js

package olddb
package membatch

import (
"bytes"
Expand All @@ -29,9 +29,6 @@ import (

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -207,42 +204,3 @@ func TestParallelPutGet(t *testing.T) {
}
pending.Wait()
}

var hexEntries = map[string]string{
"6b": "89c6",
"91": "c476",
"a8": "0a514e",
"bb": "7a",
"bd": "fe76",
"c0": "12",
}

var startKey = common.FromHex("a0")
var fixedBits = 3

var keysInRange = [][]byte{common.FromHex("a8"), common.FromHex("bb"), common.FromHex("bd")}

func TestWalk(t *testing.T) {
_, tx := memdb.NewTestTx(t)

for k, v := range hexEntries {
err := tx.Put(testBucket, common.FromHex(k), common.FromHex(v))
if err != nil {
t.Fatalf("put failed: %v", err)
}
}

var gotKeys [][]byte
c, err := tx.Cursor(testBucket)
if err != nil {
panic(err)
}
defer c.Close()
err = ethdb.Walk(c, startKey, fixedBits, func(key, val []byte) (bool, error) {
gotKeys = append(gotKeys, common.CopyBytes(key))
return true, nil
})
assert.NoError(t, err)

assert.Equal(t, keysInRange, gotKeys)
}
Loading

0 comments on commit 62395c7

Please sign in to comment.