storage

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DiffFile

type DiffFile struct {
	Seq       int64
	Path      string
	Timestamp string
	Diff      *ir.Node
	Pending   bool // true for .pending files, false for .diff files
}

DiffFile represents a diff file on disk.

type PendingDiff

type PendingDiff struct {
	Path      string
	DiffFile  string // Full filesystem path to the .pending file
	WrittenAt string // RFC3339 timestamp
}

PendingDiff represents a pending diff in a transaction.

type PendingFileRef

type PendingFileRef struct {
	VirtualPath string
	TxSeq       int64 // Transaction sequence number
}

PendingFileRef references a pending file that needs to be renamed.

type SeqState

type SeqState struct {
	CommitCount int64 // Monotonic commit count
	TxSeq       int64 // Transaction sequence number
}

SeqState represents the sequence number state.

type Snapshot

type Snapshot struct {
	CommitCount int64
	Path        string
	Timestamp   string
	State       *ir.Node
}

Snapshot represents a snapshot of state at a specific commit count.

type Storage

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

Storage provides filesystem-based storage for logd.

func Open

func Open(root string, umask int, logger *slog.Logger) (*Storage, error)

Open opens or creates a Storage instance with the given root directory. The root directory will be created if it doesn't exist. umask is applied to directory permissions (e.g., 022 for 0755 -> 0755). If logger is nil, slog.Default() will be used.

func (*Storage) AppendTransactionLog

func (s *Storage) AppendTransactionLog(entry *TransactionLogEntry) error

AppendTransactionLog appends a transaction commit log entry atomically.

func (*Storage) CurrentSeqState

func (s *Storage) CurrentSeqState() (*SeqState, error)

CurrentSeqState returns the current sequence state without incrementing.

func (*Storage) DeletePending

func (s *Storage) DeletePending(virtualPath string, txSeq int64) error

DeletePending deletes a .pending file.

func (*Storage) DeleteSnapshot

func (s *Storage) DeleteSnapshot(virtualPath string, commitCount int64) error

DeleteSnapshot deletes a snapshot file.

func (*Storage) DeleteTransactionState

func (s *Storage) DeleteTransactionState(transactionID string) error

DeleteTransactionState deletes a transaction state file.

func (*Storage) EnsurePathDir

func (s *Storage) EnsurePathDir(virtualPath string) error

EnsurePathDir ensures the directory for a virtual path exists.

func (*Storage) FilesystemToPath

func (s *Storage) FilesystemToPath(fsPath string) string

FilesystemToPath converts a filesystem directory path back to a virtual document path. Example: "/logd/paths/proc/processes" -> "/proc/processes"

func (*Storage) FindNearestSnapshot

func (s *Storage) FindNearestSnapshot(virtualPath string, targetCommitCount int64) (int64, error)

FindNearestSnapshot finds the nearest snapshot with commit count <= targetCommitCount. Returns 0 if no snapshot exists.

func (*Storage) ListDiffs

func (s *Storage) ListDiffs(virtualPath string) ([]struct{ CommitCount, TxSeq int64 }, error)

ListDiffs lists all committed diff files for a path, ordered by commit count. Only returns .diff files, not .pending files. Returns a slice of (commitCount, txSeq) pairs.

func (*Storage) ListSnapshots

func (s *Storage) ListSnapshots(virtualPath string) ([]int64, error)

ListSnapshots lists all snapshot commit counts for a path, ordered by commit count.

func (*Storage) NextCommitCount

func (s *Storage) NextCommitCount() (int64, error)

NextCommitCount atomically increments and returns the next commit count.

func (*Storage) NextTxSeq

func (s *Storage) NextTxSeq() (int64, error)

NextTxSeq atomically increments and returns the next transaction sequence number.

func (*Storage) PathToFilesystem

func (s *Storage) PathToFilesystem(virtualPath string) string

PathToFilesystem converts a virtual document path to a filesystem directory path. Example: "/proc/processes" -> "/logd/paths/proc/processes"

func (*Storage) ReadDiff

func (s *Storage) ReadDiff(virtualPath string, commitCount, txSeq int64, pending bool) (*DiffFile, error)

ReadDiff reads a diff file from disk. For pending files, commitCount is ignored (can be 0).

func (*Storage) ReadSnapshot

func (s *Storage) ReadSnapshot(virtualPath string, commitCount int64) (*Snapshot, error)

ReadSnapshot reads a snapshot file from disk.

func (*Storage) ReadTransactionLog

func (s *Storage) ReadTransactionLog(minCommitCount *int64) ([]*TransactionLogEntry, error)

ReadTransactionLog reads transaction log entries. If minCommitCount is nil, reads all entries. If minCommitCount is provided, uses binary search to find entries at or after that commit count.

func (*Storage) ReadTransactionState

func (s *Storage) ReadTransactionState(transactionID string) (*TransactionState, error)

ReadTransactionState reads a transaction state file from disk.

func (*Storage) RecoverTransactions

func (s *Storage) RecoverTransactions() error

RecoverTransactions replays the transaction log to complete any partial commits.

func (*Storage) RenamePendingToDiff

func (s *Storage) RenamePendingToDiff(virtualPath string, newCommitCount, txSeq int64) error

RenamePendingToDiff atomically renames a .pending file to .diff file with new commit count.

func (*Storage) Root

func (s *Storage) Root() string

Root returns the root directory path.

func (*Storage) UpdateTransactionState

func (s *Storage) UpdateTransactionState(transactionID string, updateFn func(*TransactionState)) error

UpdateTransactionState updates an existing transaction state file.

func (*Storage) WriteDiff

func (s *Storage) WriteDiff(virtualPath string, commitCount, txSeq int64, timestamp string, diff *ir.Node, pending bool) error

WriteDiff writes a diff file. For atomic sequence allocation and file writing, use WriteDiffAtomically instead.

func (*Storage) WriteDiffAtomically

func (s *Storage) WriteDiffAtomically(virtualPath string, timestamp string, diff *ir.Node, pending bool) (commitCount, txSeq int64, err error)

WriteDiff writes a diff file to disk. commitCount should be 0 for pending files, and the actual commit count for committed files. If pending is true, writes as .pending file; otherwise writes as .diff file. WriteDiffAtomically atomically allocates sequence numbers and writes the diff file. This ensures that files are written in the order that sequence numbers are allocated, preventing race conditions where a later sequence number is written before an earlier one.

func (*Storage) WriteSnapshot

func (s *Storage) WriteSnapshot(virtualPath string, commitCount int64, state *ir.Node) error

WriteSnapshot writes a snapshot file atomically.

func (*Storage) WriteTransactionState

func (s *Storage) WriteTransactionState(state *TransactionState) error

WriteTransactionState writes a transaction state file to disk.

type TransactionLogEntry

type TransactionLogEntry struct {
	CommitCount   int64 // Commit count assigned to this transaction
	TransactionID string
	Timestamp     string // RFC3339 timestamp
	PendingFiles  []PendingFileRef
}

TransactionLogEntry represents a transaction commit log entry.

func NewTransactionLogEntry

func NewTransactionLogEntry(commitCount int64, transactionID string, pendingFiles []PendingFileRef) *TransactionLogEntry

NewTransactionLogEntry creates a new TransactionLogEntry.

type TransactionState

type TransactionState struct {
	TransactionID        string
	ParticipantCount     int
	ParticipantsReceived int
	Status               string // "pending", "committed", "aborted"
	CreatedAt            string // RFC3339 timestamp
	Diffs                []PendingDiff
}

TransactionState represents the state of a transaction.

func NewTransactionState

func NewTransactionState(transactionID string, participantCount int) *TransactionState

NewTransactionState creates a new TransactionState with the given transaction ID and participant count.

Jump to

Keyboard shortcuts

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