Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 1 deletion goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ func (db *GoLevelDB) ForceCompact(start, limit []byte) error {

// NewBatch implements DB.
func (db *GoLevelDB) NewBatch() Batch {
return newGoLevelDBBatch(db)
return newGoLevelDBBatchWithSize(db, 0)
}

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

// Iterator implements DB.
Expand Down
4 changes: 2 additions & 2 deletions goleveldb_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type goLevelDBBatch struct {

var _ Batch = (*goLevelDBBatch)(nil)

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

Expand Down
7 changes: 6 additions & 1 deletion memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ func (db *MemDB) Stats() map[string]string {

// NewBatch implements DB.
func (db *MemDB) NewBatch() Batch {
return newMemDBBatch(db)
return newMemDBBatchWithSize(db, 0)
}

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

// Iterator implements DB.
Expand Down
6 changes: 3 additions & 3 deletions memdb_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type memDBBatch struct {

var _ Batch = (*memDBBatch)(nil)

// newMemDBBatch creates a new memDBBatch
func newMemDBBatch(db *MemDB) *memDBBatch {
// newMemDBBatchWithSize creates a new memDBBatch
func newMemDBBatchWithSize(db *MemDB, size int) *memDBBatch {
return &memDBBatch{
db: db,
ops: []operation{},
ops: make([]operation, 0, size),
}
}

Expand Down
8 changes: 8 additions & 0 deletions prefixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ func (pdb *PrefixDB) NewBatch() Batch {
return newPrefixBatch(pdb.prefix, pdb.db.NewBatch())
}

// NewBatchWithSize implements DB.
func (pdb *PrefixDB) NewBatchWithSize(size int) Batch {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

return newPrefixBatch(pdb.prefix, pdb.db.NewBatchWithSize(size))
}

// Close implements DB.
func (pdb *PrefixDB) Close() error {
pdb.mtx.Lock()
Expand Down
4 changes: 2 additions & 2 deletions remotedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type batch struct {

var _ db.Batch = (*batch)(nil)

func newBatch(rdb *RemoteDB) *batch {
func newBatchWithSize(rdb *RemoteDB, size int) *batch {
return &batch{
db: rdb,
ops: []*protodb.Operation{},
ops: make([]*protodb.Operation, 0, size),
}
}

Expand Down
6 changes: 5 additions & 1 deletion remotedb/remotedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func (rd *RemoteDB) ReverseIterator(start, end []byte) (db.Iterator, error) {
}

func (rd *RemoteDB) NewBatch() db.Batch {
return newBatch(rd)
return newBatchWithSize(rd, 0)
}

func (rd *RemoteDB) NewBatchWithSize(size int) db.Batch {
return newBatchWithSize(rd, size)
}

// TODO: Implement Print when db.DB implements a method
Expand Down
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type DB interface {
// NewBatch creates a batch for atomic updates. The caller must call Batch.Close.
NewBatch() Batch

// NewBatch creates a batch for atomic updates with pre-allocated buffer. The caller must call Batch.Close.
NewBatchWithSize(size int) Batch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part of me thinks it would make sense to not break that api and merely use it goleveldb. Unless we see improvements on rocks and others

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

I'm making changes to IAVL now to benchmark how much of a difference this change makes. I will come back here after and look into other databases. Otherwise, rethink this PR


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

Expand Down