This commit is contained in:
@@ -109,6 +109,8 @@ type Iterator interface {
|
||||
// consider always using a transaction of the appropriate type. The
|
||||
// transaction isolation level is "read committed" - there are no dirty
|
||||
// reads.
|
||||
// Location returns the path to the database, as given to Open. The returned string
|
||||
// is empty for a db in memory.
|
||||
type Backend interface {
|
||||
Reader
|
||||
Writer
|
||||
@@ -116,6 +118,7 @@ type Backend interface {
|
||||
NewWriteTransaction(hooks ...CommitHook) (WriteTransaction, error)
|
||||
Close() error
|
||||
Compact() error
|
||||
Location() string
|
||||
}
|
||||
|
||||
type Tuning int
|
||||
|
||||
@@ -23,7 +23,12 @@ func OpenBadger(path string) (Backend, error) {
|
||||
opts := badger.DefaultOptions(path)
|
||||
opts = opts.WithMaxCacheSize(maxCacheSize).WithCompactL0OnClose(false)
|
||||
opts.Logger = nil
|
||||
return openBadger(opts)
|
||||
backend, err := openBadger(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
backend.location = path
|
||||
return backend, nil
|
||||
}
|
||||
|
||||
func OpenBadgerMemory() Backend {
|
||||
@@ -38,7 +43,7 @@ func OpenBadgerMemory() Backend {
|
||||
return backend
|
||||
}
|
||||
|
||||
func openBadger(opts badger.Options) (Backend, error) {
|
||||
func openBadger(opts badger.Options) (*badgerBackend, error) {
|
||||
// XXX: We should find good values for memory utilization in the "small"
|
||||
// and "large" cases we support for LevelDB. Some notes here:
|
||||
// https://github.com/dgraph-io/badger/tree/v2.0.3#memory-usage
|
||||
@@ -54,8 +59,9 @@ func openBadger(opts badger.Options) (Backend, error) {
|
||||
|
||||
// badgerBackend implements Backend on top of a badger
|
||||
type badgerBackend struct {
|
||||
bdb *badger.DB
|
||||
closeWG *closeWaitGroup
|
||||
bdb *badger.DB
|
||||
closeWG *closeWaitGroup
|
||||
location string
|
||||
}
|
||||
|
||||
func (b *badgerBackend) NewReadTransaction() (ReadTransaction, error) {
|
||||
@@ -217,6 +223,10 @@ func (b *badgerBackend) Compact() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *badgerBackend) Location() string {
|
||||
return b.location
|
||||
}
|
||||
|
||||
// badgerSnapshot implements backend.ReadTransaction
|
||||
type badgerSnapshot struct {
|
||||
txn *badger.Txn
|
||||
|
||||
@@ -28,14 +28,16 @@ const (
|
||||
|
||||
// leveldbBackend implements Backend on top of a leveldb
|
||||
type leveldbBackend struct {
|
||||
ldb *leveldb.DB
|
||||
closeWG *closeWaitGroup
|
||||
ldb *leveldb.DB
|
||||
closeWG *closeWaitGroup
|
||||
location string
|
||||
}
|
||||
|
||||
func newLeveldbBackend(ldb *leveldb.DB) *leveldbBackend {
|
||||
func newLeveldbBackend(ldb *leveldb.DB, location string) *leveldbBackend {
|
||||
return &leveldbBackend{
|
||||
ldb: ldb,
|
||||
closeWG: &closeWaitGroup{},
|
||||
ldb: ldb,
|
||||
closeWG: &closeWaitGroup{},
|
||||
location: location,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +118,10 @@ func (b *leveldbBackend) Compact() error {
|
||||
return wrapLeveldbErr(b.ldb.CompactRange(util.Range{}))
|
||||
}
|
||||
|
||||
func (b *leveldbBackend) Location() string {
|
||||
return b.location
|
||||
}
|
||||
|
||||
// leveldbSnapshot implements backend.ReadTransaction
|
||||
type leveldbSnapshot struct {
|
||||
snap *leveldb.Snapshot
|
||||
|
||||
@@ -42,7 +42,7 @@ func OpenLevelDB(location string, tuning Tuning) (Backend, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLeveldbBackend(ldb), nil
|
||||
return newLeveldbBackend(ldb, location), nil
|
||||
}
|
||||
|
||||
// OpenLevelDBAuto is OpenLevelDB with TuningAuto tuning.
|
||||
@@ -61,13 +61,13 @@ func OpenLevelDBRO(location string) (Backend, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLeveldbBackend(ldb), nil
|
||||
return newLeveldbBackend(ldb, location), nil
|
||||
}
|
||||
|
||||
// OpenMemory returns a new Backend referencing an in-memory database.
|
||||
func OpenLevelDBMemory() Backend {
|
||||
ldb, _ := leveldb.Open(storage.NewMemStorage(), nil)
|
||||
return newLeveldbBackend(ldb)
|
||||
return newLeveldbBackend(ldb, "")
|
||||
}
|
||||
|
||||
// optsFor returns the database options to use when opening a database with
|
||||
|
||||
Reference in New Issue
Block a user