Skip to content

Commit

Permalink
clean up error handling in Batch.Write()
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Nov 4, 2024
1 parent 56ea0e5 commit 200f7dd
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions store/v2/storage/sqlite/batch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlite

import (
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -74,29 +75,31 @@ func (b *Batch) Delete(storeKey, key []byte) error {
return nil
}

func (b *Batch) Write() error {
func (b *Batch) Write() (err error) {
b.lock.Lock()
defer b.lock.Unlock()
err := b.db.Begin()
err = b.db.Begin()
if err != nil {
return fmt.Errorf("failed to start SQL transaction: %w", err)
}
err = b.db.Exec(reservedUpsertStmt, reservedStoreKey, keyLatestHeight, b.version, 0, b.version)
if err != nil {
defer func() {
if err != nil {
err = errors.Join(err, b.db.Rollback())
}
}()
if err := b.db.Exec(reservedUpsertStmt, reservedStoreKey, keyLatestHeight, b.version, 0, b.version); err != nil {
return fmt.Errorf("failed to exec SQL statement: %w", err)
}

for _, op := range b.ops {
switch op.action {
case batchActionSet:
err := b.db.Exec(upsertStmt, op.storeKey, op.key, op.value, b.version, op.value)
if err != nil {
if err := b.db.Exec(upsertStmt, op.storeKey, op.key, op.value, b.version, op.value); err != nil {
return fmt.Errorf("failed to exec SQL statement: %w", err)
}

case batchActionDel:
err := b.db.Exec(delStmt, b.version, op.storeKey, op.key, b.version)
if err != nil {
if err := b.db.Exec(delStmt, b.version, op.storeKey, op.key, b.version); err != nil {
return fmt.Errorf("failed to exec SQL statement: %w", err)
}
}
Expand Down

0 comments on commit 200f7dd

Please sign in to comment.