Release v0.3.0

This commit is contained in:
Manu Herrera
2020-11-09 10:05:29 -03:00
parent 4e9aa7a3c5
commit 8107c4478b
1265 changed files with 440488 additions and 107809 deletions

View File

@@ -7,7 +7,10 @@
package walletdb
import "io"
import (
"fmt"
"io"
)
// ReadTx represents a database transaction that can only be used for reads. If
// a database update must occur, use a ReadWriteTx.
@@ -126,6 +129,16 @@ type ReadWriteBucket interface {
// Tx returns the bucket's transaction.
Tx() ReadWriteTx
// NextSequence returns an autoincrementing integer for the bucket.
NextSequence() (uint64, error)
// SetSequence updates the sequence number for the bucket.
SetSequence(v uint64) error
// Sequence returns the current integer for the bucket without
// incrementing it.
Sequence() uint64
}
// ReadCursor represents a bucket cursor that can be positioned at the start or
@@ -191,6 +204,18 @@ type DB interface {
Close() error
}
// BatchDB is a special version of the main DB interface that allos the caller
// to specify write transactions that should be combine dtoegether if multiple
// goroutines are calling the Batch method.
type BatchDB interface {
DB
// Batch is similar to the package-level Update method, but it will
// attempt to optismitcally combine the invocation of several
// transaction functions into a single db write transaction.
Batch(func(tx ReadWriteTx) error) error
}
// View opens a database read transaction and executes the function f with the
// transaction passed as a parameter. After f exits, the transaction is rolled
// back. If f errors, its error is returned, not a rollback error (if any
@@ -200,11 +225,20 @@ func View(db DB, f func(tx ReadTx) error) error {
if err != nil {
return err
}
// Make sure the transaction rolls back in the event of a panic.
defer func() {
if tx != nil {
tx.Rollback()
}
}()
err = f(tx)
rollbackErr := tx.Rollback()
if err != nil {
return err
}
if rollbackErr != nil {
return rollbackErr
}
@@ -222,6 +256,14 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
if err != nil {
return err
}
// Make sure the transaction rolls back in the event of a panic.
defer func() {
if tx != nil {
tx.Rollback()
}
}()
err = f(tx)
if err != nil {
// Want to return the original error, not a rollback error if
@@ -229,9 +271,27 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
// Batch opens a database read/write transaction and executes the function f
// with the transaction passed as a parameter. After f exits, if f did not
// error, the transaction is committed. Otherwise, if f did error, the
// transaction is rolled back. If the rollback fails, the original error
// returned by f is still returned. If the commit fails, the commit error is
// returned.
//
// Batch is only useful when there are multiple goroutines calling it.
func Batch(db DB, f func(tx ReadWriteTx) error) error {
batchDB, ok := db.(BatchDB)
if !ok {
return fmt.Errorf("need batch")
}
return batchDB.Batch(f)
}
// Driver defines a structure for backend drivers to use when they registered
// themselves as a backend which implements the Db interface.
type Driver struct {