-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42d7ae7
commit e3cb0a9
Showing
15 changed files
with
658 additions
and
64 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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,57 @@ | ||
package pebble | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/cockroachdb/pebble" | ||
) | ||
|
||
type Batch struct { | ||
writer *pebble.Batch | ||
|
||
lock sync.RWMutex | ||
callbacks []func() | ||
} | ||
|
||
func NewBatch(db *pebble.DB) *Batch { | ||
batch := db.NewBatch() | ||
return &Batch{ | ||
writer: batch, | ||
callbacks: make([]func(), 0), | ||
} | ||
} | ||
|
||
func (b *Batch) GetWriter() *pebble.Batch { | ||
return b.writer | ||
} | ||
|
||
// OnSucceed adds a callback to execute after the batch has | ||
// been successfully flushed. | ||
// useful for implementing the cache where we will only cache | ||
// after the batch has been successfully flushed | ||
func (b *Batch) OnSucceed(callback func()) { | ||
b.lock.Lock() | ||
defer b.lock.Unlock() | ||
b.callbacks = append(b.callbacks, callback) | ||
} | ||
|
||
// Flush will call the badger Batch's Flush method, in | ||
// addition, it will call the callbacks added by | ||
// OnSucceed | ||
func (b *Batch) Flush() error { | ||
err := b.writer.Commit(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b.lock.RLock() | ||
defer b.lock.RUnlock() | ||
for _, callback := range b.callbacks { | ||
callback() | ||
} | ||
return nil | ||
} | ||
|
||
func (b *Batch) Close() error { | ||
return b.writer.Close() | ||
} |
Oops, something went wrong.