refactor(store): rename transaction helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e55a8a8457
commit
e73d55d5ca
3 changed files with 18 additions and 18 deletions
|
|
@ -241,7 +241,7 @@ All operations are safe to call from multiple goroutines concurrently. The race
|
|||
|
||||
## Transaction API
|
||||
|
||||
`Store.Transaction(func(tx *StoreTx) error)` opens a SQLite transaction and hands a `StoreTx` helper to the callback. The helper exposes transaction-scoped write methods such as `Set`, `SetWithTTL`, `Delete`, `DeleteGroup`, and `DeletePrefix`. If the callback returns an error, the transaction rolls back. If the callback succeeds, the transaction commits and the staged events are published after commit.
|
||||
`Store.Transaction(func(transaction *StoreTransaction) error)` opens a SQLite transaction and hands a `StoreTransaction` helper to the callback. The helper exposes transaction-scoped write methods such as `Set`, `SetWithTTL`, `Delete`, `DeleteGroup`, and `DeletePrefix`. If the callback returns an error, the transaction rolls back. If the callback succeeds, the transaction commits and the staged events are published after commit.
|
||||
|
||||
This API is the supported way to perform atomic multi-group operations without exposing raw `Begin`/`Commit` control to callers.
|
||||
|
||||
|
|
|
|||
|
|
@ -7,15 +7,15 @@ import (
|
|||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// Usage example: `err := storeInstance.Transaction(func(tx *store.StoreTx) error { return tx.Set("config", "colour", "blue") })`
|
||||
type StoreTx struct {
|
||||
// Usage example: `err := storeInstance.Transaction(func(transaction *store.StoreTransaction) error { return transaction.Set("config", "colour", "blue") })`
|
||||
type StoreTransaction struct {
|
||||
store *Store
|
||||
transaction *sql.Tx
|
||||
pendingEvents []Event
|
||||
}
|
||||
|
||||
// Usage example: `err := storeInstance.Transaction(func(tx *store.StoreTx) error { if err := tx.Set("tenant-a:config", "colour", "blue"); err != nil { return err }; return tx.Set("tenant-b:config", "language", "en-GB") })`
|
||||
func (storeInstance *Store) Transaction(operation func(*StoreTx) error) error {
|
||||
// Usage example: `err := storeInstance.Transaction(func(transaction *store.StoreTransaction) error { if err := transaction.Set("tenant-a:config", "colour", "blue"); err != nil { return err }; return transaction.Set("tenant-b:config", "language", "en-GB") })`
|
||||
func (storeInstance *Store) Transaction(operation func(*StoreTransaction) error) error {
|
||||
if err := storeInstance.ensureReady("store.Transaction"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ func (storeInstance *Store) Transaction(operation func(*StoreTx) error) error {
|
|||
return core.E("store.Transaction", "begin transaction", err)
|
||||
}
|
||||
|
||||
storeTransaction := &StoreTx{
|
||||
storeTransaction := &StoreTransaction{
|
||||
store: storeInstance,
|
||||
transaction: transaction,
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ func (storeInstance *Store) Transaction(operation func(*StoreTx) error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (storeTransaction *StoreTx) ensureReady(operation string) error {
|
||||
func (storeTransaction *StoreTransaction) ensureReady(operation string) error {
|
||||
if storeTransaction == nil {
|
||||
return core.E(operation, "transaction is nil", nil)
|
||||
}
|
||||
|
|
@ -70,7 +70,7 @@ func (storeTransaction *StoreTx) ensureReady(operation string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (storeTransaction *StoreTx) recordEvent(event Event) {
|
||||
func (storeTransaction *StoreTransaction) recordEvent(event Event) {
|
||||
if storeTransaction == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -78,7 +78,7 @@ func (storeTransaction *StoreTx) recordEvent(event Event) {
|
|||
}
|
||||
|
||||
// Usage example: `value, err := tx.Get("config", "colour")`
|
||||
func (storeTransaction *StoreTx) Get(group, key string) (string, error) {
|
||||
func (storeTransaction *StoreTransaction) Get(group, key string) (string, error) {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.Get"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ func (storeTransaction *StoreTx) Get(group, key string) (string, error) {
|
|||
}
|
||||
|
||||
// Usage example: `if err := tx.Set("config", "colour", "blue"); err != nil { return err }`
|
||||
func (storeTransaction *StoreTx) Set(group, key, value string) error {
|
||||
func (storeTransaction *StoreTransaction) Set(group, key, value string) error {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.Set"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -123,7 +123,7 @@ func (storeTransaction *StoreTx) Set(group, key, value string) error {
|
|||
}
|
||||
|
||||
// Usage example: `if err := tx.SetWithTTL("session", "token", "abc123", time.Minute); err != nil { return err }`
|
||||
func (storeTransaction *StoreTx) SetWithTTL(group, key, value string, timeToLive time.Duration) error {
|
||||
func (storeTransaction *StoreTransaction) SetWithTTL(group, key, value string, timeToLive time.Duration) error {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.SetWithTTL"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ func (storeTransaction *StoreTx) SetWithTTL(group, key, value string, timeToLive
|
|||
}
|
||||
|
||||
// Usage example: `if err := tx.Delete("config", "colour"); err != nil { return err }`
|
||||
func (storeTransaction *StoreTx) Delete(group, key string) error {
|
||||
func (storeTransaction *StoreTransaction) Delete(group, key string) error {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.Delete"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -165,7 +165,7 @@ func (storeTransaction *StoreTx) Delete(group, key string) error {
|
|||
}
|
||||
|
||||
// Usage example: `if err := tx.DeleteGroup("cache"); err != nil { return err }`
|
||||
func (storeTransaction *StoreTx) DeleteGroup(group string) error {
|
||||
func (storeTransaction *StoreTransaction) DeleteGroup(group string) error {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.DeleteGroup"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -188,7 +188,7 @@ func (storeTransaction *StoreTx) DeleteGroup(group string) error {
|
|||
}
|
||||
|
||||
// Usage example: `if err := tx.DeletePrefix("tenant-a:"); err != nil { return err }`
|
||||
func (storeTransaction *StoreTx) DeletePrefix(groupPrefix string) error {
|
||||
func (storeTransaction *StoreTransaction) DeletePrefix(groupPrefix string) error {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.DeletePrefix"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -230,7 +230,7 @@ func (storeTransaction *StoreTx) DeletePrefix(groupPrefix string) error {
|
|||
}
|
||||
|
||||
// Usage example: `keyCount, err := tx.Count("config")`
|
||||
func (storeTransaction *StoreTx) Count(group string) (int, error) {
|
||||
func (storeTransaction *StoreTransaction) Count(group string) (int, error) {
|
||||
if err := storeTransaction.ensureReady("store.Transaction.Count"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestTransaction_Transaction_Good_CommitsMultipleWrites(t *testing.T) {
|
|||
events := storeInstance.Watch("*")
|
||||
defer storeInstance.Unwatch("*", events)
|
||||
|
||||
err := storeInstance.Transaction(func(transaction *StoreTx) error {
|
||||
err := storeInstance.Transaction(func(transaction *StoreTransaction) error {
|
||||
if err := transaction.Set("alpha", "first", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ func TestTransaction_Transaction_Good_RollbackOnError(t *testing.T) {
|
|||
storeInstance, _ := New(":memory:")
|
||||
defer storeInstance.Close()
|
||||
|
||||
err := storeInstance.Transaction(func(transaction *StoreTx) error {
|
||||
err := storeInstance.Transaction(func(transaction *StoreTransaction) error {
|
||||
if err := transaction.Set("alpha", "first", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -68,7 +68,7 @@ func TestTransaction_Transaction_Good_DeletesAtomically(t *testing.T) {
|
|||
require.NoError(t, storeInstance.Set("alpha", "first", "1"))
|
||||
require.NoError(t, storeInstance.Set("beta", "second", "2"))
|
||||
|
||||
err := storeInstance.Transaction(func(transaction *StoreTx) error {
|
||||
err := storeInstance.Transaction(func(transaction *StoreTransaction) error {
|
||||
if err := transaction.DeletePrefix(""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue