-
Notifications
You must be signed in to change notification settings - Fork 759
Move database creation to database factory package #3899
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
Changes from all commits
57daae4
fffa034
eb0570f
9ff05a7
53e9336
facf929
45461e0
56e4806
1e5fa92
13fd948
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package factory | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/api/metrics" | ||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/database/corruptabledb" | ||
"github.com/ava-labs/avalanchego/database/leveldb" | ||
"github.com/ava-labs/avalanchego/database/memdb" | ||
"github.com/ava-labs/avalanchego/database/meterdb" | ||
"github.com/ava-labs/avalanchego/database/pebbledb" | ||
"github.com/ava-labs/avalanchego/database/versiondb" | ||
"github.com/ava-labs/avalanchego/utils/logging" | ||
) | ||
|
||
// New creates a new database instance based on the provided configuration. | ||
// | ||
// It also wraps the database with a corruptable DB and a meter DB. | ||
// | ||
// dbName is the name of the database, either leveldb, memdb, or pebbledb. | ||
// dbPath is the path to the database folder. | ||
// readOnly indicates if the database should be read-only. | ||
// dbConfig is the database configuration in JSON format. | ||
// dbMetricsPrefix is used to create a new metrics registerer for the database. | ||
// meterDBRegName is used to create a new metrics registerer for the meter DB. | ||
func New( | ||
name string, | ||
path string, | ||
readOnly bool, | ||
config []byte, | ||
gatherer metrics.MultiGatherer, | ||
logger logging.Logger, | ||
metricsPrefix string, | ||
meterDBRegName string, | ||
) (database.Database, error) { | ||
dbRegisterer, err := metrics.MakeAndRegister( | ||
gatherer, | ||
metricsPrefix, | ||
) | ||
if err != nil { | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could you create more specific formatted errors to the ones here ? same for the others error in this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this comment but I don't think it makes sense to block this PR on it because this is a strict refactor. |
||
} | ||
var db database.Database | ||
// start the db | ||
switch name { | ||
case leveldb.Name: | ||
db, err = leveldb.New(path, config, logger, dbRegisterer) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't create %s at %s: %w", leveldb.Name, path, err) | ||
} | ||
case memdb.Name: | ||
db = memdb.New() | ||
case pebbledb.Name: | ||
db, err = pebbledb.New(path, config, logger, dbRegisterer) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't create %s at %s: %w", pebbledb.Name, path, err) | ||
} | ||
default: | ||
return nil, fmt.Errorf( | ||
"db-type was %q but should have been one of {%s, %s, %s}", | ||
name, | ||
leveldb.Name, | ||
memdb.Name, | ||
pebbledb.Name, | ||
) | ||
} | ||
|
||
// Wrap with corruptable DB | ||
db = corruptabledb.New(db) | ||
|
||
if readOnly && name != memdb.Name { | ||
db = versiondb.New(db) | ||
} | ||
|
||
meterDBReg, err := metrics.MakeAndRegister( | ||
gatherer, | ||
meterDBRegName, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
db, err = meterdb.New(meterDBReg, db) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create meterdb: %w", err) | ||
} | ||
|
||
return db, nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.