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

ethdb, core, cmd: polish and fixes #50

Merged
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
7 changes: 4 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ var (
Category: flags.LoggingCategory,
}
BackingDBFlag = &cli.StringFlag{
Name: "backingdb",
Usage: "Backing database implementation to use",
Value: "leveldb",
Name: "backingdb",
Usage: "Backing database implementation to use",
Value: "leveldb",
Category: flags.EthCategory,
}
AncientFlag = &flags.DirectoryFlag{
Name: "datadir.ancient",
Expand Down
21 changes: 13 additions & 8 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ const (
// instantiated at that location, and if so, returns the type of database (or the
// empty string).
func hasPreexistingDb(path string) string {
if _, err := os.Stat(path + "/CURRENT"); err != nil {
if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
return "" // No pre-existing db
}
if matches, err := filepath.Glob(path + "/OPTIONS*"); len(matches) > 0 || err != nil {
if matches, err := filepath.Glob(filepath.Join(path, "OPTIONS*")); len(matches) > 0 || err != nil {
if err != nil {
panic(err) // only possible if the pattern is malformed
}
Expand All @@ -334,14 +334,19 @@ type OpenOptions struct {
Type string // "leveldb" | "pebble"
Directory string // the datadir
AncientsDirectory string // the ancients-dir
Namespace string
Cache int // Cache size in MiB
Handles int
Namespace string // the namespace for database relevant metrics
Cache int // the capacity(in megabytes) of the data caching
Handles int // the capacity of the open files caching
ReadOnly bool
}

// openKvDb Opens a disk-based key-value database, e.g. leveldb or pebble.
func openKvDb(o OpenOptions) (ethdb.Database, error) {
// openKeyValueDatabase opens a disk-based key-value database, e.g. leveldb or pebble.
//
// type == null type != null
// +----------------------------------------
// db is non-existent | leveldb default | specified type
// db is existent | from db | specified type (if compatible)
func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
existingDb := hasPreexistingDb(o.Directory)
if len(existingDb) != 0 && len(o.Type) != 0 && o.Type != existingDb {
return nil, fmt.Errorf("backingdb choice was %v but found pre-existing %v database in specified data directory", o.Type, existingDb)
Expand All @@ -366,7 +371,7 @@ func openKvDb(o OpenOptions) (ethdb.Database, error) {
// The passed o.AncientDir indicates the path of root ancient directory where
// the chain freezer can be opened.
func Open(o OpenOptions) (ethdb.Database, error) {
kvdb, err := openKvDb(o)
kvdb, err := openKeyValueDatabase(o)
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions core/rawdb/databases_64bit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>

//go:build arm64 || amd64

package rawdb
Expand Down
16 changes: 16 additions & 0 deletions core/rawdb/databases_non64bit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

//go:build !(arm64 || amd64)

package rawdb
Expand Down
1 change: 0 additions & 1 deletion ethdb/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func NewCustom(file string, namespace string, customize func(options *opt.Option
ldb.level0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/level0", nil)
ldb.nonlevel0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/nonlevel0", nil)
ldb.seekCompGauge = metrics.NewRegisteredGauge(namespace+"compact/seek", nil)

ldb.manualMemAllocGauge = metrics.NewRegisteredGauge(namespace+"memory/manualalloc", nil)

// Start up the metrics gathering and return
Expand Down
14 changes: 7 additions & 7 deletions ethdb/pebble/pebble.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The go-ethereum Authors
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -299,28 +299,28 @@ type snapshot struct {
// data store.
func (snap *snapshot) Has(key []byte) (bool, error) {
_, closer, err := snap.db.Get(key)
defer closer.Close()

if err != nil {
if err != pebble.ErrNotFound {
return false, err
} else {
return false, nil
}
}
closer.Close()
return true, nil
}

// Get retrieves the given key if it's present in the snapshot backing by
// key-value data store.
func (snap *snapshot) Get(key []byte) ([]byte, error) {
val, closer, err := snap.db.Get(key)

dat, closer, err := snap.db.Get(key)
if err != nil {
return nil, err
}
ret := make([]byte, len(dat))
copy(ret, dat)
closer.Close()
return val, nil
return ret, nil
}

// Release releases associated resources. Release should always succeed and can
Expand Down Expand Up @@ -365,7 +365,7 @@ func (d *Database) Stat(property string) (string, error) {
// is treated as a key after all keys in the data store. If both is nil then it
// will compact entire data store.
func (d *Database) Compact(start []byte, limit []byte) error {
return d.db.Compact(start, limit, false)
return d.db.Compact(start, limit, true) // Parallelization is preferred
}

// Path returns the path to the database directory.
Expand Down
2 changes: 1 addition & 1 deletion ethdb/pebble/pebble_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 The go-ethereum Authors
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
Expand Down