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 batch pool #245

Merged
merged 2 commits into from
Aug 3, 2023
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
31 changes: 31 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ func (db *DB) NewBatch(options BatchOptions) *Batch {
return batch
}

func makeBatch() interface{} {
node, err := snowflake.NewNode(1)
if err != nil {
panic(fmt.Sprintf("snowflake.NewNode(1) failed: %v", err))
}
return &Batch{
options: DefaultBatchOptions,
batchId: node,
}
}

func (b *Batch) init(rdonly, sync bool, db *DB) *Batch {
b.options.ReadOnly = rdonly
b.options.Sync = sync
b.db = db
b.lock()
return b
}

func (b *Batch) withPendingWrites() *Batch {
b.pendingWrites = make(map[string]*LogRecord)
return b
}

func (b *Batch) reset() {
b.db = nil
b.pendingWrites = nil
b.committed = false
b.rollbacked = false
}

func (b *Batch) lock() {
if b.options.ReadOnly {
b.db.mu.RLock()
Expand Down
37 changes: 23 additions & 14 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type DB struct {
mu sync.RWMutex
closed bool
mergeRunning uint32 // indicate if the database is merging
batchPool sync.Pool
}

// Stat represents the statistics of the database.
Expand Down Expand Up @@ -109,6 +110,7 @@ func Open(options Options) (*DB, error) {
index: index.NewIndexer(),
options: options,
fileLock: fileLock,
batchPool: sync.Pool{New: makeBatch},
}

// load index frm hint file
Expand Down Expand Up @@ -178,12 +180,15 @@ func (db *DB) Stat() *Stat {
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Put operation.
func (db *DB) Put(key []byte, value []byte) error {
options := DefaultBatchOptions
batch := db.batchPool.Get().(*Batch)
defer func() {
batch.reset()
db.batchPool.Put(batch)
}()
// This is a single delete operation, we can set Sync to false.
// Because the data will be written to the WAL,
// and the WAL file will be synced to disk according to the DB options.
options.Sync = false
batch := db.NewBatch(options)
batch.init(false, false, db).withPendingWrites()
if err := batch.Put(key, value); err != nil {
return err
}
Expand All @@ -194,12 +199,12 @@ func (db *DB) Put(key []byte, value []byte) error {
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Get operation.
func (db *DB) Get(key []byte) ([]byte, error) {
options := DefaultBatchOptions
// Read-only operation
options.ReadOnly = true
batch := db.NewBatch(options)
batch := db.batchPool.Get().(*Batch)
batch.init(true, false, db)
defer func() {
_ = batch.Commit()
batch.reset()
db.batchPool.Put(batch)
}()
return batch.Get(key)
}
Expand All @@ -208,13 +213,17 @@ func (db *DB) Get(key []byte) ([]byte, error) {
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Delete operation.
func (db *DB) Delete(key []byte) error {
options := DefaultBatchOptions
batch := db.batchPool.Get().(*Batch)
defer func() {
batch.reset()
db.batchPool.Put(batch)
}()
// This is a single delete operation, we can set Sync to false.
// Because the data will be written to the WAL,
// and the WAL file will be synced to disk according to the DB options.
options.Sync = false
batch := db.NewBatch(options)
batch.init(false, false, db).withPendingWrites()
if err := batch.Delete(key); err != nil {
_ = batch.Rollback()
return err
}
return batch.Commit()
Expand All @@ -224,12 +233,12 @@ func (db *DB) Delete(key []byte) error {
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Exist operation.
func (db *DB) Exist(key []byte) (bool, error) {
options := DefaultBatchOptions
// Read-only operation
options.ReadOnly = true
batch := db.NewBatch(options)
batch := db.batchPool.Get().(*Batch)
batch.init(true, false, db)
defer func() {
_ = batch.Commit()
batch.reset()
db.batchPool.Put(batch)
}()
return batch.Exist(key)
}
Expand Down