tx

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var ErrReadOnlyTransaction = errors.New("cannot write to read-only transaction")

ErrReadOnlyTransaction is returned when attempting to write to a read-only transaction.

View Source
var ErrTransactionClosed = errors.New("transaction is closed")

ErrTransactionClosed is returned when attempting to use a closed transaction.

View Source
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

func NewManager(store *storage.DB) *Manager

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()
    }
}()

type Tx added in v0.2.1

type Tx interface {
	Get(key []byte) ([]byte, error)
	Put(key, value []byte) error
	Delete(key []byte) error
	Commit() error
	Rollback() error
	IsReadOnly() bool
	IsClosed() bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL