Documentation
¶
Overview ¶
Package tx provides transaction management for gograph's storage layer.
Package tx provides transaction management for gograph's storage layer. It implements ACID transactions using PebbleDB's batch operations.
Transactions ensure that multiple operations either all succeed or all fail, maintaining data consistency. The package supports both read-only and read-write transactions.
Basic Usage:
manager := tx.NewManager(store)
// Begin a read-write transaction
tx, err := manager.Begin(false)
if err != nil {
log.Fatal(err)
}
// Perform operations
err = tx.Put([]byte("key"), []byte("value"))
if err != nil {
tx.Rollback()
log.Fatal(err)
}
// Commit the transaction
err = tx.Commit()
Thread Safety:
Manager is safe for concurrent use. Each transaction is independent and should only be used by a single goroutine.
Index ¶
- Variables
- type Manager
- type MemoryManager
- type MemoryTransaction
- func (t *MemoryTransaction) Commit() error
- func (t *MemoryTransaction) Delete(key []byte) error
- func (t *MemoryTransaction) Get(key []byte) ([]byte, error)
- func (t *MemoryTransaction) IsClosed() bool
- func (t *MemoryTransaction) IsReadOnly() bool
- func (t *MemoryTransaction) Put(key, value []byte) error
- func (t *MemoryTransaction) Rollback() error
- type Transaction
- func (t *Transaction) Commit() error
- func (t *Transaction) Delete(key []byte) error
- func (t *Transaction) Get(key []byte) ([]byte, error)
- func (t *Transaction) IsClosed() bool
- func (t *Transaction) IsReadOnly() bool
- func (t *Transaction) Put(key, value []byte) error
- func (t *Transaction) Rollback() error
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTransactionCommitted is returned when attempting to use a committed transaction. ErrTransactionCommitted = errors.New("transaction already committed") // ErrTransactionRolledBack is returned when attempting to use a rolled back transaction. ErrTransactionRolledBack = errors.New("transaction already rolled back") )
var ErrReadOnlyTransaction = errors.New("cannot write to read-only transaction")
ErrReadOnlyTransaction is returned when attempting to write to a read-only transaction.
var ErrTransactionClosed = errors.New("transaction is closed")
ErrTransactionClosed is returned when attempting to use a closed transaction.
var ErrTxClosed = errors.New("transaction is closed")
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager creates and manages transactions for the storage layer. It provides methods to begin new transactions with different isolation levels.
func NewManager ¶
NewManager creates a new transaction manager for the given storage.
Parameters:
- store: The storage database to manage transactions for
Returns a new Manager instance.
Example:
store, _ := storage.Open("/path/to/db")
manager := tx.NewManager(store)
func (*Manager) Begin ¶
func (m *Manager) Begin(readOnly bool) (*Transaction, error)
Begin starts a new transaction.
Parameters:
- readOnly: If true, the transaction cannot modify data but may have better performance. If false, the transaction supports both reads and writes.
Returns a new Transaction or an error if the transaction cannot be started.
Example:
// Read-only transaction for queries
tx, _ := manager.Begin(true)
value, _ := tx.Get([]byte("key"))
tx.Commit()
// Read-write transaction for updates
tx, _ := manager.Begin(false)
tx.Put([]byte("key"), []byte("value"))
tx.Commit()
type MemoryManager ¶ added in v0.2.1
type MemoryManager struct {
// contains filtered or unexported fields
}
func NewMemoryManager ¶ added in v0.2.1
func NewMemoryManager(store *storage.MemoryDB) *MemoryManager
func (*MemoryManager) Begin ¶ added in v0.2.1
func (m *MemoryManager) Begin(readOnly bool) (*MemoryTransaction, error)
type MemoryTransaction ¶ added in v0.2.1
type MemoryTransaction struct {
// contains filtered or unexported fields
}
func (*MemoryTransaction) Commit ¶ added in v0.2.1
func (t *MemoryTransaction) Commit() error
func (*MemoryTransaction) Delete ¶ added in v0.2.1
func (t *MemoryTransaction) Delete(key []byte) error
func (*MemoryTransaction) Get ¶ added in v0.2.1
func (t *MemoryTransaction) Get(key []byte) ([]byte, error)
func (*MemoryTransaction) IsClosed ¶ added in v0.2.1
func (t *MemoryTransaction) IsClosed() bool
func (*MemoryTransaction) IsReadOnly ¶ added in v0.2.1
func (t *MemoryTransaction) IsReadOnly() bool
func (*MemoryTransaction) Put ¶ added in v0.2.1
func (t *MemoryTransaction) Put(key, value []byte) error
func (*MemoryTransaction) Rollback ¶ added in v0.2.1
func (t *MemoryTransaction) Rollback() error
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a database transaction. It provides methods for reading and writing data within a transaction context.
Transactions support:
- Read operations (Get)
- Write operations (Put, Delete) - only in read-write transactions
- Atomic commit or rollback
Example:
tx, _ := manager.Begin(false)
defer func() {
if err != nil {
tx.Rollback()
}
}()
// Perform operations
value, _ := tx.Get([]byte("key"))
tx.Put([]byte("key"), []byte("new value"))
// Commit
err = tx.Commit()
func (*Transaction) Commit ¶
func (t *Transaction) Commit() error
Commit commits the transaction, making all changes permanent. For read-only transactions, this simply marks the transaction as closed.
Returns an error if the transaction is already closed or if the commit fails.
Example:
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
func (*Transaction) Delete ¶
func (t *Transaction) Delete(key []byte) error
Delete removes the value for the given key within the transaction. The operation is not persisted until Commit is called.
Parameters:
- key: The key to delete
Returns an error if the transaction is closed or read-only.
Example:
err := tx.Delete([]byte("key"))
if err != nil {
tx.Rollback()
log.Fatal(err)
}
func (*Transaction) Get ¶
func (t *Transaction) Get(key []byte) ([]byte, error)
Get retrieves the value for the given key within the transaction.
Parameters:
- key: The key to look up
Returns the value as a byte slice, or an error if the key is not found or if the transaction is closed.
Example:
value, err := tx.Get([]byte("mykey"))
if err != nil {
log.Printf("Key not found: %v", err)
}
func (*Transaction) IsClosed ¶
func (t *Transaction) IsClosed() bool
IsClosed returns true if the transaction has been closed. A transaction is closed after Commit or Rollback is called.
Example:
if !tx.IsClosed() {
tx.Rollback()
}
func (*Transaction) IsReadOnly ¶
func (t *Transaction) IsReadOnly() bool
IsReadOnly returns true if the transaction is read-only.
Example:
if tx.IsReadOnly() {
fmt.Println("This is a read-only transaction")
}
func (*Transaction) Put ¶
func (t *Transaction) Put(key, value []byte) error
Put stores a key-value pair within the transaction. The operation is not persisted until Commit is called.
Parameters:
- key: The key to store
- value: The value to store
Returns an error if the transaction is closed or read-only.
Example:
err := tx.Put([]byte("key"), []byte("value"))
if err != nil {
tx.Rollback()
log.Fatal(err)
}
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback aborts the transaction and discards all changes. This is safe to call even if the transaction is already closed.
Returns nil if the rollback succeeds.
Example:
defer func() {
if err != nil {
tx.Rollback()
}
}()