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

Add NewBatchWithSize to DB interface #64

Merged
merged 5 commits into from
Dec 22, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

- added bloom filter: https://github.com/cosmos/cosmos-db/pull/42/files
- Removed Badger & Boltdb
- Add `NewBatchWithSize` to `DB` interface: https://github.com/cosmos/cosmos-db/pull/64
5 changes: 5 additions & 0 deletions goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ func (db *GoLevelDB) NewBatch() Batch {
return newGoLevelDBBatch(db)
}

// NewBatchWithSize implements DB.
func (db *GoLevelDB) NewBatchWithSize(size int) Batch {
return newGoLevelDBBatchWithSize(db, size)
}

// Iterator implements DB.
func (db *GoLevelDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
Expand Down
7 changes: 7 additions & 0 deletions goleveldb_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func newGoLevelDBBatch(db *GoLevelDB) *goLevelDBBatch {
}
}

func newGoLevelDBBatchWithSize(db *GoLevelDB, size int) *goLevelDBBatch {
return &goLevelDBBatch{
db: db,
batch: leveldb.MakeBatch(size),
}
}

// Set implements Batch.
func (b *goLevelDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
Expand Down
6 changes: 6 additions & 0 deletions memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (db *MemDB) NewBatch() Batch {
return newMemDBBatch(db)
}

// NewBatchWithSize implements DB.
// It does the same thing as NewBatch because we can't pre-allocate memDBBatch
func (db *MemDB) NewBatchWithSize(size int) Batch {
return newMemDBBatch(db)
}

// Iterator implements DB.
// Takes out a read-lock on the database until the iterator is closed.
func (db *MemDB) Iterator(start, end []byte) (Iterator, error) {
Expand Down
6 changes: 6 additions & 0 deletions pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ func (db *PebbleDB) NewBatch() Batch {
return newPebbleDBBatch(db)
}

// NewBatchWithSize implements DB.
// It does the same thing as NewBatch because we can't pre-allocate pebbleDBBatch
func (db *PebbleDB) NewBatchWithSize(size int) Batch {
return newPebbleDBBatch(db)
}

// Iterator implements DB.
func (db *PebbleDB) Iterator(start, end []byte) (Iterator, error) {
// fmt.Println("PebbleDB.Iterator")
Expand Down
5 changes: 5 additions & 0 deletions prefixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func (pdb *PrefixDB) NewBatch() Batch {
return newPrefixBatch(pdb.prefix, pdb.db.NewBatch())
}

// NewBatchWithSize implements DB.
func (pdb *PrefixDB) NewBatchWithSize(size int) Batch {
return newPrefixBatch(pdb.prefix, pdb.db.NewBatchWithSize(size))
}

// Close implements DB.
func (pdb *PrefixDB) Close() error {
pdb.mtx.Lock()
Expand Down
6 changes: 6 additions & 0 deletions rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ func (db *RocksDB) NewBatch() Batch {
return newRocksDBBatch(db)
}

// NewBatchWithSize implements DB.
// It does the same thing as NewBatch because we can't pre-allocate rocksDBBatch
func (db *RocksDB) NewBatchWithSize(size int) Batch {
return newRocksDBBatch(db)
}

// Iterator implements DB.
func (db *RocksDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
Expand Down
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type DB interface {
// NewBatch creates a batch for atomic updates. The caller must call Batch.Close.
NewBatch() Batch

// NewBatchWithSize create a new batch for atomic updates, but with pre-allocated size.
// This will does the same thing as NewBatch if the batch implementation doesn't support pre-allocation.
NewBatchWithSize(int) Batch

// Print is used for debugging.
Print() error

Expand Down