lock

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package lock provides deadlock detection for XxSql storage engine.

Package lock provides latch management for B+ tree operations.

Package lock provides lock management for XxSql storage engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CrabbingProtocol

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

CrabbingProtocol implements the crabbing protocol for B+ tree traversal. Acquires latches from root to leaf, releasing parent latches when safe.

func NewCrabbingProtocol

func NewCrabbingProtocol(manager *LatchManager) *CrabbingProtocol

NewCrabbingProtocol creates a new crabbing protocol handler.

func (*CrabbingProtocol) AcquireRoot

func (c *CrabbingProtocol) AcquireRoot(pageID uint64, latchType LatchType) bool

AcquireRoot acquires a latch on the root page.

func (*CrabbingProtocol) CrabDown

func (c *CrabbingProtocol) CrabDown(parentID, childID uint64, childType LatchType, isSafe bool) bool

CrabDown moves from parent to child, releasing parent if safe.

func (*CrabbingProtocol) HeldCount

func (c *CrabbingProtocol) HeldCount() int

HeldCount returns the number of held latches.

func (*CrabbingProtocol) ReleaseAll

func (c *CrabbingProtocol) ReleaseAll()

ReleaseAll releases all held latches.

type Latch

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

Latch is a lightweight lock for B+ tree nodes.

func (*Latch) Acquire

func (l *Latch) Acquire(latchType LatchType, timeout time.Duration) bool

Acquire acquires a latch.

func (*Latch) Release

func (l *Latch) Release(latchType LatchType)

Release releases a latch.

func (*Latch) TryAcquire

func (l *Latch) TryAcquire(latchType LatchType) bool

TryAcquire attempts to acquire a latch without blocking.

type LatchManager

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

LatchManager manages latches for B+ tree pages.

func NewLatchManager

func NewLatchManager(timeout time.Duration) *LatchManager

NewLatchManager creates a new latch manager.

func (*LatchManager) Acquire

func (m *LatchManager) Acquire(pageID uint64, latchType LatchType) bool

Acquire acquires a latch on a page.

func (*LatchManager) Clear

func (m *LatchManager) Clear()

Clear removes all latches (use with caution).

func (*LatchManager) Release

func (m *LatchManager) Release(pageID uint64, latchType LatchType)

Release releases a latch on a page.

func (*LatchManager) Remove

func (m *LatchManager) Remove(pageID uint64)

Remove removes a latch for a page.

func (*LatchManager) Stats

func (m *LatchManager) Stats() LatchStats

Stats returns latch manager statistics.

func (*LatchManager) TryAcquire

func (m *LatchManager) TryAcquire(pageID uint64, latchType LatchType) bool

TryAcquire attempts to acquire a latch without blocking.

type LatchStats

type LatchStats struct {
	LatchCount    int    `json:"latch_count"`
	TotalAcquires uint64 `json:"total_acquires"`
	TotalWaits    uint64 `json:"total_waits"`
}

LatchStats holds latch manager statistics.

type LatchType

type LatchType uint8

LatchType represents the type of latch.

const (
	LatchTypeShared LatchType = iota
	LatchTypeExclusive
)

func (LatchType) String

func (t LatchType) String() string

String returns the string representation.

type Lock

type Lock struct {
	ID        LockID
	Type      LockType
	Holder    uint64 // Transaction ID
	Granted   bool
	Timestamp int64
}

Lock represents a lock on a resource.

type LockID

type LockID struct {
	Level   LockLevel
	TableID uint64
	PageID  uint64
	RowID   uint64
}

LockID uniquely identifies a lockable resource.

func (LockID) String

func (id LockID) String() string

String returns a string representation.

type LockLevel

type LockLevel uint8

LockLevel represents the granularity level of a lock.

const (
	LockLevelGlobal LockLevel = iota
	LockLevelCatalog
	LockLevelTable
	LockLevelPage
	LockLevelRow
)

func (LockLevel) String

func (t LockLevel) String() string

String returns the string representation.

type LockRequest

type LockRequest struct {
	ID         LockID
	Type       LockType
	TxnID      uint64
	Granted    bool
	WaitStart  time.Time
	ResultChan chan error
}

LockRequest represents a lock request.

type LockStats

type LockStats struct {
	WaitCount    uint64 `json:"wait_count"`
	GrantCount   uint64 `json:"grant_count"`
	TimeoutCount uint64 `json:"timeout_count"`
	TableLocks   int    `json:"table_locks"`
	PageLocks    int    `json:"page_locks"`
	RowLocks     int    `json:"row_locks"`
}

LockStats holds lock manager statistics.

type LockType

type LockType uint8

LockType represents the type of lock.

const (
	LockTypeShared LockType = iota
	LockTypeExclusive
)

func (LockType) String

func (t LockType) String() string

String returns the string representation.

type Manager

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

Manager manages locks for the database.

func NewManager

func NewManager(timeout time.Duration) *Manager

NewManager creates a new lock manager.

func (*Manager) DetectDeadlock

func (m *Manager) DetectDeadlock() []uint64

DetectDeadlock checks for deadlocks.

func (*Manager) GetLockInfo

func (m *Manager) GetLockInfo(id LockID) (shared int, exclusive uint64)

GetLockInfo returns information about locks on a resource.

func (*Manager) IsLocked

func (m *Manager) IsLocked(id LockID) bool

IsLocked checks if a resource is locked.

func (*Manager) Lock

func (m *Manager) Lock(id LockID, lockType LockType, txnID uint64) error

Lock acquires a lock.

func (*Manager) LockCatalog

func (m *Manager) LockCatalog(lockType LockType, txnID uint64) error

LockCatalog acquires the catalog lock.

func (*Manager) LockGlobal

func (m *Manager) LockGlobal(lockType LockType, txnID uint64) error

LockGlobal acquires the global lock.

func (*Manager) LockPage

func (m *Manager) LockPage(tableID, pageID uint64, lockType LockType, txnID uint64) error

LockPage acquires a page lock.

func (*Manager) LockRow

func (m *Manager) LockRow(tableID, pageID, rowID uint64, lockType LockType, txnID uint64) error

LockRow acquires a row lock.

func (*Manager) LockTable

func (m *Manager) LockTable(tableID uint64, lockType LockType, txnID uint64) error

LockTable acquires a table lock.

func (*Manager) ReleaseAllLocks

func (m *Manager) ReleaseAllLocks(txnID uint64)

ReleaseAllLocks releases all locks held by a transaction.

func (*Manager) Stats

func (m *Manager) Stats() LockStats

Stats returns lock manager statistics.

func (*Manager) TryLock

func (m *Manager) TryLock(id LockID, lockType LockType, txnID uint64) bool

TryLock attempts to acquire a lock without waiting.

func (*Manager) Unlock

func (m *Manager) Unlock(id LockID, txnID uint64) error

Unlock releases a lock.

func (*Manager) UnlockCatalog

func (m *Manager) UnlockCatalog(txnID uint64) error

UnlockCatalog releases the catalog lock.

func (*Manager) UnlockGlobal

func (m *Manager) UnlockGlobal(txnID uint64) error

UnlockGlobal releases the global lock.

func (*Manager) UnlockPage

func (m *Manager) UnlockPage(tableID, pageID uint64, txnID uint64) error

UnlockPage releases a page lock.

func (*Manager) UnlockRow

func (m *Manager) UnlockRow(tableID, pageID, rowID uint64, txnID uint64) error

UnlockRow releases a row lock.

func (*Manager) UnlockTable

func (m *Manager) UnlockTable(tableID uint64, txnID uint64) error

UnlockTable releases a table lock.

type WaitForGraph

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

WaitForGraph represents a wait-for graph for deadlock detection.

func NewWaitForGraph

func NewWaitForGraph() *WaitForGraph

NewWaitForGraph creates a new wait-for graph.

func (*WaitForGraph) AddEdge

func (g *WaitForGraph) AddEdge(txn1, txn2 uint64)

AddEdge adds a wait-for edge from txn1 to txn2.

func (*WaitForGraph) DetectCycle

func (g *WaitForGraph) DetectCycle() []uint64

DetectCycle detects if there is a cycle in the graph. Returns the transaction IDs involved in the cycle, or nil if no cycle.

func (*WaitForGraph) EdgeCount

func (g *WaitForGraph) EdgeCount() int

EdgeCount returns the number of edges in the graph.

func (*WaitForGraph) GetWaiters

func (g *WaitForGraph) GetWaiters(txn uint64) []uint64

GetWaiters returns all transactions that txn is waiting for.

func (*WaitForGraph) GetWaitersOf

func (g *WaitForGraph) GetWaitersOf(txn uint64) []uint64

GetWaitersOf returns all transactions waiting for txn.

func (*WaitForGraph) HasEdge

func (g *WaitForGraph) HasEdge(txn1, txn2 uint64) bool

HasEdge checks if there's an edge from txn1 to txn2.

func (*WaitForGraph) NodeCount

func (g *WaitForGraph) NodeCount() int

NodeCount returns the number of nodes in the graph.

func (*WaitForGraph) RemoveAllEdges

func (g *WaitForGraph) RemoveAllEdges(txn uint64)

RemoveAllEdges removes all edges for a transaction.

func (*WaitForGraph) RemoveEdge

func (g *WaitForGraph) RemoveEdge(txn1, txn2 uint64)

RemoveEdge removes a wait-for edge from txn1 to txn2.

Jump to

Keyboard shortcuts

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