txn

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLockTimeout is returned when a lock request times out.
	ErrLockTimeout = errors.New("db: lock request timed out")

	// ErrDeadlock is returned when a deadlock is detected.
	ErrDeadlock = errors.New("db: deadlock detected")

	// ErrLockNotHeld is returned when trying to unlock a key not held by the transaction.
	ErrLockNotHeld = errors.New("db: lock not held by transaction")
)

Lock Manager errors

Functions

This section is empty.

Types

type LockInfo

type LockInfo struct {
	// Holders are transactions that currently hold a lock on this key.
	// For shared locks, there can be multiple holders.
	// For exclusive locks, there is at most one holder.
	Holders map[uint64]LockType

	// WaitQueue is an ordered list of pending lock requests.
	WaitQueue []*LockRequest
}

LockInfo holds information about locks on a single key.

func NewLockInfo

func NewLockInfo() *LockInfo

NewLockInfo creates a new LockInfo.

func (*LockInfo) GetLockType

func (li *LockInfo) GetLockType(txnID uint64) LockType

GetLockType returns the lock type held by the transaction, or -1 if not held.

func (*LockInfo) HasExclusiveHolder

func (li *LockInfo) HasExclusiveHolder() bool

HasExclusiveHolder returns true if there's an exclusive lock holder.

func (*LockInfo) IsHeldBy

func (li *LockInfo) IsHeldBy(txnID uint64) bool

IsHeldBy returns true if the key is locked by the given transaction.

func (*LockInfo) NumHolders

func (li *LockInfo) NumHolders() int

NumHolders returns the number of lock holders.

type LockManager

type LockManager struct {
	// contains filtered or unexported fields
}

LockManager manages locks for pessimistic transactions. It supports shared and exclusive locks with deadlock detection.

func NewLockManager

func NewLockManager(opts LockManagerOptions) *LockManager

NewLockManager creates a new lock manager.

func (*LockManager) GetLockInfo

func (lm *LockManager) GetLockInfo(key []byte) *LockInfo

GetLockInfo returns information about a lock (for debugging/testing).

func (*LockManager) Lock

func (lm *LockManager) Lock(txnID uint64, key []byte, lockType LockType, timeout time.Duration) error

Lock attempts to acquire a lock on the given key for the transaction. If the lock cannot be immediately granted, it waits up to the timeout. Returns ErrDeadlock if acquiring the lock would cause a deadlock. Returns ErrLockTimeout if the timeout expires.

func (*LockManager) NumLocks

func (lm *LockManager) NumLocks() int

NumLocks returns the number of keys with active locks.

func (*LockManager) NumTxnLocks

func (lm *LockManager) NumTxnLocks(txnID uint64) int

NumTxnLocks returns the number of locks held by a transaction.

func (*LockManager) TryLock

func (lm *LockManager) TryLock(txnID uint64, key []byte, lockType LockType) bool

TryLock attempts to acquire a lock without waiting. Returns true if the lock was acquired, false otherwise.

func (*LockManager) Unlock

func (lm *LockManager) Unlock(txnID uint64, key []byte) error

Unlock releases the lock held by the transaction on the given key.

func (*LockManager) UnlockAll

func (lm *LockManager) UnlockAll(txnID uint64)

UnlockAll releases all locks held by the transaction.

type LockManagerOptions

type LockManagerOptions struct {
	DefaultTimeout time.Duration
}

LockManagerOptions configures the lock manager.

func DefaultLockManagerOptions

func DefaultLockManagerOptions() LockManagerOptions

DefaultLockManagerOptions returns default options.

type LockRequest

type LockRequest struct {
	TxnID    uint64
	LockType LockType
	Granted  bool
	Waiting  chan struct{} // Closed when the lock is granted
}

LockRequest represents a pending or granted lock request.

type LockType

type LockType int

LockType represents the type of lock.

const (
	// LockTypeShared allows multiple readers but no writers.
	LockTypeShared LockType = iota
	// LockTypeExclusive allows only one holder (reader or writer).
	LockTypeExclusive
)

func (LockType) String

func (lt LockType) String() string

String returns a string representation of the lock type.

type PreparedTransaction

type PreparedTransaction struct {
	// Name is the transaction identifier (XID)
	Name string

	// PrepareSeq is the sequence number at prepare time
	PrepareSeq uint64

	// WriteBatch contains the prepared writes
	WriteBatch *batch.WriteBatch
}

PreparedTransaction represents a recovered prepared transaction.

Jump to

Keyboard shortcuts

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