-
Notifications
You must be signed in to change notification settings - Fork 50
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
Reinstate BoltDB and ClevelDB as backend DBs #177
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c1d9bc8
Revert "remove deprecated boltdb and cleveldb (#155)"
alesforz d64f2bb
updated cleveldb Iterator API docs to conform to the changes made in …
alesforz 295578b
updated go.mod
alesforz e377c94
updated boltDB Iterator APIs to conform to the changes made in #168
alesforz e2ec2b6
added changelog entry
alesforz d573cfb
Formatting
alesforz 91fdf59
formatting to please the linter
alesforz b20d222
added additional context in the docs of backend types constants
alesforz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
.changelog/unreleased/dependencies/177-reinstate-boltdb-cleveldb.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- reinstate BoltDB and ClevelDB as backend DBs | ||
([\#177](https://github.com/cometbft/cometbft-db/pull/177)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
//go:build boltdb | ||
// +build boltdb | ||
|
||
package db | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"go.etcd.io/bbolt" | ||
) | ||
|
||
var bucket = []byte("tm") | ||
|
||
func init() { | ||
registerDBCreator(BoltDBBackend, func(name, dir string) (DB, error) { | ||
return NewBoltDB(name, dir) | ||
}) | ||
} | ||
|
||
// BoltDB is a wrapper around etcd's fork of bolt (https://github.com/etcd-io/bbolt). | ||
// | ||
// NOTE: All operations (including Set, Delete) are synchronous by default. One | ||
// can globally turn it off by using NoSync config option (not recommended). | ||
// | ||
// A single bucket ([]byte("tm")) is used per a database instance. This could | ||
// lead to performance issues when/if there will be lots of keys. | ||
type BoltDB struct { | ||
db *bbolt.DB | ||
} | ||
|
||
var _ DB = (*BoltDB)(nil) | ||
|
||
// NewBoltDB returns a BoltDB with default options. | ||
// | ||
// Deprecated: boltdb is deprecated and will be removed in the future. | ||
func NewBoltDB(name, dir string) (DB, error) { | ||
return NewBoltDBWithOpts(name, dir, bbolt.DefaultOptions) | ||
} | ||
|
||
// NewBoltDBWithOpts allows you to supply *bbolt.Options. ReadOnly: true is not | ||
// supported because NewBoltDBWithOpts creates a global bucket. | ||
func NewBoltDBWithOpts(name string, dir string, opts *bbolt.Options) (DB, error) { | ||
if opts.ReadOnly { | ||
return nil, errors.New("ReadOnly: true is not supported") | ||
} | ||
|
||
dbPath := filepath.Join(dir, name+".db") | ||
db, err := bbolt.Open(dbPath, os.ModePerm, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// create a global bucket | ||
err = db.Update(func(tx *bbolt.Tx) error { | ||
_, err := tx.CreateBucketIfNotExists(bucket) | ||
return err | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BoltDB{db: db}, nil | ||
} | ||
|
||
// Get implements DB. | ||
func (bdb *BoltDB) Get(key []byte) (value []byte, err error) { | ||
if len(key) == 0 { | ||
return nil, errKeyEmpty | ||
} | ||
err = bdb.db.View(func(tx *bbolt.Tx) error { | ||
b := tx.Bucket(bucket) | ||
if v := b.Get(key); v != nil { | ||
value = append([]byte{}, v...) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return | ||
} | ||
|
||
// Has implements DB. | ||
func (bdb *BoltDB) Has(key []byte) (bool, error) { | ||
bytes, err := bdb.Get(key) | ||
if err != nil { | ||
return false, err | ||
} | ||
return bytes != nil, nil | ||
} | ||
|
||
// Set implements DB. | ||
func (bdb *BoltDB) Set(key, value []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
if value == nil { | ||
return errValueNil | ||
} | ||
err := bdb.db.Update(func(tx *bbolt.Tx) error { | ||
b := tx.Bucket(bucket) | ||
return b.Put(key, value) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// SetSync implements DB. | ||
func (bdb *BoltDB) SetSync(key, value []byte) error { | ||
return bdb.Set(key, value) | ||
} | ||
|
||
// Delete implements DB. | ||
func (bdb *BoltDB) Delete(key []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
err := bdb.db.Update(func(tx *bbolt.Tx) error { | ||
return tx.Bucket(bucket).Delete(key) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// DeleteSync implements DB. | ||
func (bdb *BoltDB) DeleteSync(key []byte) error { | ||
return bdb.Delete(key) | ||
} | ||
|
||
// Close implements DB. | ||
func (bdb *BoltDB) Close() error { | ||
return bdb.db.Close() | ||
} | ||
|
||
// Print implements DB. | ||
func (bdb *BoltDB) Print() error { | ||
stats := bdb.db.Stats() | ||
fmt.Printf("%v\n", stats) | ||
|
||
err := bdb.db.View(func(tx *bbolt.Tx) error { | ||
tx.Bucket(bucket).ForEach(func(k, v []byte) error { | ||
fmt.Printf("[%X]:\t[%X]\n", k, v) | ||
return nil | ||
}) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Stats implements DB. | ||
func (bdb *BoltDB) Stats() map[string]string { | ||
stats := bdb.db.Stats() | ||
m := make(map[string]string) | ||
|
||
// Freelist stats | ||
m["FreePageN"] = fmt.Sprintf("%v", stats.FreePageN) | ||
m["PendingPageN"] = fmt.Sprintf("%v", stats.PendingPageN) | ||
m["FreeAlloc"] = fmt.Sprintf("%v", stats.FreeAlloc) | ||
m["FreelistInuse"] = fmt.Sprintf("%v", stats.FreelistInuse) | ||
|
||
// Transaction stats | ||
m["TxN"] = fmt.Sprintf("%v", stats.TxN) | ||
m["OpenTxN"] = fmt.Sprintf("%v", stats.OpenTxN) | ||
|
||
return m | ||
} | ||
|
||
// NewBatch implements DB. | ||
func (bdb *BoltDB) NewBatch() Batch { | ||
return newBoltDBBatch(bdb) | ||
} | ||
|
||
// WARNING: Any concurrent writes or reads will block until the iterator is | ||
// closed. | ||
func (bdb *BoltDB) Iterator(start, end []byte) (Iterator, error) { | ||
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { | ||
return nil, errKeyEmpty | ||
} | ||
tx, err := bdb.db.Begin(false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newBoltDBIterator(tx, start, end, false), nil | ||
} | ||
|
||
// WARNING: Any concurrent writes or reads will block until the iterator is | ||
// closed. | ||
func (bdb *BoltDB) ReverseIterator(start, end []byte) (Iterator, error) { | ||
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { | ||
return nil, errKeyEmpty | ||
} | ||
tx, err := bdb.db.Begin(false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newBoltDBIterator(tx, start, end, true), nil | ||
} | ||
|
||
func (bdb *BoltDB) Compact(start, end []byte) error { | ||
// There is no explicit CompactRange support in BoltDB, only a function that copies the | ||
// entire DB from one place to another while doing deletions. Hence we do not support it. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//go:build boltdb | ||
// +build boltdb | ||
|
||
package db | ||
|
||
import "go.etcd.io/bbolt" | ||
|
||
// boltDBBatch stores operations internally and dumps them to BoltDB on Write(). | ||
type boltDBBatch struct { | ||
db *BoltDB | ||
ops []operation | ||
} | ||
|
||
var _ Batch = (*boltDBBatch)(nil) | ||
|
||
func newBoltDBBatch(db *BoltDB) *boltDBBatch { | ||
return &boltDBBatch{ | ||
db: db, | ||
ops: []operation{}, | ||
} | ||
} | ||
|
||
// Set implements Batch. | ||
func (b *boltDBBatch) Set(key, value []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
if value == nil { | ||
return errValueNil | ||
} | ||
if b.ops == nil { | ||
return errBatchClosed | ||
} | ||
b.ops = append(b.ops, operation{opTypeSet, key, value}) | ||
return nil | ||
} | ||
|
||
// Delete implements Batch. | ||
func (b *boltDBBatch) Delete(key []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
if b.ops == nil { | ||
return errBatchClosed | ||
} | ||
b.ops = append(b.ops, operation{opTypeDelete, key, nil}) | ||
return nil | ||
} | ||
|
||
// Write implements Batch. | ||
func (b *boltDBBatch) Write() error { | ||
if b.ops == nil { | ||
return errBatchClosed | ||
} | ||
err := b.db.db.Batch(func(tx *bbolt.Tx) error { | ||
bkt := tx.Bucket(bucket) | ||
for _, op := range b.ops { | ||
switch op.opType { | ||
case opTypeSet: | ||
if err := bkt.Put(op.key, op.value); err != nil { | ||
return err | ||
} | ||
case opTypeDelete: | ||
if err := bkt.Delete(op.key); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors. | ||
return b.Close() | ||
} | ||
|
||
// WriteSync implements Batch. | ||
func (b *boltDBBatch) WriteSync() error { | ||
return b.Write() | ||
} | ||
|
||
// Close implements Batch. | ||
func (b *boltDBBatch) Close() error { | ||
b.ops = nil | ||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this changelog? Isn't easier to remove the previous one?