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 support for transaction complete listeners. Fixes #64 #65

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
32 changes: 27 additions & 5 deletions boltz/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ type Db interface {

// AddRestoreListener adds a callback which will be invoked asynchronously when a snapshot is restored
AddRestoreListener(listener func())

// AddTxCompleteListener adds a listener which is called all tx processing is complete, including
// post-commit hooks
AddTxCompleteListener(listener func(ctx MutateContext))
}

type DbImpl struct {
rootBucket string
reloadLock sync.RWMutex
db *bbolt.DB
restoreListeners concurrenz.CopyOnWriteSlice[func()]
rootBucket string
reloadLock sync.RWMutex
db *bbolt.DB
restoreListeners concurrenz.CopyOnWriteSlice[func()]
txCompleteListeners concurrenz.CopyOnWriteSlice[func(ctx MutateContext)]
}

func Open(path string, rootBucket string) (*DbImpl, error) {
Expand Down Expand Up @@ -106,6 +111,10 @@ func (self *DbImpl) Close() error {
return self.db.Close()
}

func (self *DbImpl) AddTxCompleteListener(listener func(ctx MutateContext)) {
self.txCompleteListeners.Append(listener)
}

func (self *DbImpl) Update(ctx MutateContext, fn func(ctx MutateContext) error) error {
if ctx == nil {
ctx = NewMutateContext(context.Background())
Expand All @@ -122,7 +131,20 @@ func (self *DbImpl) Update(ctx MutateContext, fn func(ctx MutateContext) error)
if err := fn(ctx); err != nil {
return err
}
return ctx.runPreCommitActions()
if err := ctx.runPreCommitActions(); err != nil {
return err
}

txCompleteListeners := self.txCompleteListeners.Value()
if txCompleteListeners != nil {
tx.OnCommit(func() {
for _, listener := range txCompleteListeners {
listener(ctx)
}
})
}

return nil
})
}

Expand Down
Loading