db

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MetaKeyRepoPath = "repo_path"
)

Metadata keys

View Source
const SchemaVersion = 1

Schema version for migrations

Variables

This section is empty.

Functions

func SetDefault

func SetDefault(db *DB)

SetDefault sets the default database instance

Types

type Blob

type Blob struct {
	Path          string
	CommitID      string
	Content       []byte  // file bytes (empty for empty files)
	ContentHash   *string // nil = deleted, otherwise hash of content
	Mode          int
	IsSymlink     bool
	SymlinkTarget *string
}

Blob represents a file at a specific commit

type Commit

type Commit struct {
	ID          string
	ParentID    *string
	TreeHash    string
	Message     string
	AuthorName  string
	AuthorEmail string
	CreatedAt   time.Time
}

Commit represents a commit in the database

type DB

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

DB holds the database connection pool

func Connect

func Connect(ctx context.Context, url string) (*DB, error)

Connect establishes a connection to the database

func Default

func Default() *DB

Default returns the default database instance

func (*DB) Begin

func (db *DB) Begin(ctx context.Context) (pgx.Tx, error)

Begin starts a transaction

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts pgx.TxOptions) (pgx.Tx, error)

BeginTx starts a transaction with options

func (*DB) BlobExists

func (db *DB) BlobExists(ctx context.Context, path, commitID string) (bool, error)

BlobExists checks if a blob exists at a specific commit

func (*DB) Close

func (db *DB) Close()

Close closes the database connection

func (*DB) CommitExists

func (db *DB) CommitExists(ctx context.Context, id string) (bool, error)

CommitExists checks if a commit exists

func (*DB) CountCommits

func (db *DB) CountCommits(ctx context.Context) (int, error)

CountCommits returns the total number of commits

func (*DB) CreateBlob

func (db *DB) CreateBlob(ctx context.Context, b *Blob) error

CreateBlob inserts a new blob into the database

func (*DB) CreateBlobs

func (db *DB) CreateBlobs(ctx context.Context, blobs []*Blob) error

CreateBlobs inserts multiple blobs using COPY for speed

func (*DB) CreateCommit

func (db *DB) CreateCommit(ctx context.Context, c *Commit) error

CreateCommit inserts a new commit into the database

func (*DB) CreateCommitsBatch added in v1.1.0

func (db *DB) CreateCommitsBatch(ctx context.Context, commits []*Commit) error

CreateCommitsBatch inserts multiple commits using pgx.CopyFrom for speed Commits must be in order (parents before children)

func (*DB) DeleteCommitsAfter

func (db *DB) DeleteCommitsAfter(ctx context.Context, commitID string) error

DeleteCommitsAfter deletes all commits after (and including) the given commit ID This is used during the "rebuild on divergence" process

func (*DB) DeleteMetadata added in v1.1.0

func (db *DB) DeleteMetadata(ctx context.Context, key string) error

DeleteMetadata removes a metadata key

func (*DB) DeleteRef

func (db *DB) DeleteRef(ctx context.Context, name string) error

DeleteRef deletes a ref

func (*DB) DeleteSyncState

func (db *DB) DeleteSyncState(ctx context.Context, remoteName string) error

DeleteSyncState deletes the sync state for a remote

func (*DB) DropSchema

func (db *DB) DropSchema(ctx context.Context) error

DropSchema drops all pgit tables (use with caution!)

func (*DB) EnsureMetadataTable added in v1.1.0

func (db *DB) EnsureMetadataTable(ctx context.Context) error

EnsureMetadataTable creates the metadata table if it doesn't exist

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, sql string, args ...any) error

Exec executes a query without returning rows

func (*DB) FindCommonAncestor

func (db *DB) FindCommonAncestor(ctx context.Context, commitA, commitB string) (string, error)

FindCommonAncestor finds the common ancestor between two commits

func (*DB) GetAllCommits

func (db *DB) GetAllCommits(ctx context.Context) ([]*Commit, error)

GetAllCommits retrieves all commits ordered by ID (ULID = time order)

func (*DB) GetAllPaths

func (db *DB) GetAllPaths(ctx context.Context) ([]string, error)

GetAllPaths returns all unique file paths in the repository

func (*DB) GetAllRefs

func (db *DB) GetAllRefs(ctx context.Context) ([]*Ref, error)

GetAllRefs retrieves all refs

func (*DB) GetAllSyncStates

func (db *DB) GetAllSyncStates(ctx context.Context) ([]*SyncState, error)

GetAllSyncStates retrieves all sync states

func (*DB) GetBlob

func (db *DB) GetBlob(ctx context.Context, path, commitID string) (*Blob, error)

GetBlob retrieves a specific blob

func (*DB) GetBlobStats

func (db *DB) GetBlobStats(ctx context.Context) (map[string]interface{}, error)

GetBlobStats returns statistics about blobs using xpatch.stats() for O(1) performance

func (*DB) GetBlobsAtCommit

func (db *DB) GetBlobsAtCommit(ctx context.Context, commitID string) ([]*Blob, error)

GetBlobsAtCommit retrieves all blobs at a specific commit

func (*DB) GetChangedFiles

func (db *DB) GetChangedFiles(ctx context.Context, fromCommit, toCommit string) ([]*Blob, error)

GetChangedFiles returns files that changed between two commits

func (*DB) GetCommit

func (db *DB) GetCommit(ctx context.Context, id string) (*Commit, error)

GetCommit retrieves a commit by ID

func (*DB) GetCommitLog

func (db *DB) GetCommitLog(ctx context.Context, limit int) ([]*Commit, error)

GetCommitLog retrieves commits starting from HEAD, walking up the parent chain

func (*DB) GetCommitLogFrom

func (db *DB) GetCommitLogFrom(ctx context.Context, commitID string, limit int) ([]*Commit, error)

GetCommitLogFrom retrieves commits starting from a specific commit

func (*DB) GetCommitStats

func (db *DB) GetCommitStats(ctx context.Context) (map[string]interface{}, error)

GetCommitStats returns statistics about commits using xpatch.stats() for O(1) performance

func (*DB) GetCurrentTree

func (db *DB) GetCurrentTree(ctx context.Context) ([]*Blob, error)

GetCurrentTree retrieves the tree at HEAD

func (*DB) GetFileAtCommit

func (db *DB) GetFileAtCommit(ctx context.Context, path, commitID string) (*Blob, error)

GetFileAtCommit retrieves a file at a specific commit (or the latest version before it)

func (*DB) GetFileHistory

func (db *DB) GetFileHistory(ctx context.Context, path string) ([]*Blob, error)

GetFileHistory retrieves all versions of a file

func (*DB) GetHead

func (db *DB) GetHead(ctx context.Context) (string, error)

GetHead returns the HEAD commit ID

func (*DB) GetHeadCommit

func (db *DB) GetHeadCommit(ctx context.Context) (*Commit, error)

GetHeadCommit retrieves the commit that HEAD points to

func (*DB) GetLatestCommitID

func (db *DB) GetLatestCommitID(ctx context.Context) (string, error)

GetLatestCommitID returns the ID of the latest commit (by ULID order)

func (*DB) GetMetadata added in v1.1.0

func (db *DB) GetMetadata(ctx context.Context, key string) (string, error)

GetMetadata retrieves a metadata value by key

func (*DB) GetRef

func (db *DB) GetRef(ctx context.Context, name string) (*Ref, error)

GetRef retrieves a ref by name

func (*DB) GetRepoPath added in v1.1.0

func (db *DB) GetRepoPath(ctx context.Context) string

GetRepoPath returns the stored repository path, or empty string if not set

func (*DB) GetRepoStatsFast

func (db *DB) GetRepoStatsFast(ctx context.Context) (*RepoStats, error)

GetRepoStatsFast returns repository statistics using xpatch.stats() for O(1) performance

func (*DB) GetSyncState

func (db *DB) GetSyncState(ctx context.Context, remoteName string) (*SyncState, error)

GetSyncState retrieves the sync state for a remote

func (*DB) GetTreeAtCommit

func (db *DB) GetTreeAtCommit(ctx context.Context, commitID string) ([]*Blob, error)

GetTreeAtCommit retrieves the full tree (all files) at a commit This uses the ULID ordering to find the latest version of each file

func (*DB) GetXpatchStats

func (db *DB) GetXpatchStats(ctx context.Context, tableName string) (*XpatchStats, error)

GetXpatchStats retrieves compression statistics for a table

func (*DB) InitSchema

func (db *DB) InitSchema(ctx context.Context) error

InitSchema creates the pgit schema in the database

func (*DB) IsConnected

func (db *DB) IsConnected() bool

IsConnected returns true if the database is connected

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping tests the database connection

func (*DB) Pool

func (db *DB) Pool() *pgxpool.Pool

Pool returns the underlying connection pool

func (*DB) Query

func (db *DB) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query executes a query and returns rows

func (*DB) QueryRow

func (db *DB) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow executes a query and returns a single row

func (*DB) RefExists

func (db *DB) RefExists(ctx context.Context, name string) (bool, error)

RefExists checks if a ref exists

func (*DB) SchemaExists

func (db *DB) SchemaExists(ctx context.Context) (bool, error)

SchemaExists checks if the pgit schema exists

func (*DB) SearchAllBlobs

func (db *DB) SearchAllBlobs(ctx context.Context, pathPattern string) ([]*Blob, error)

SearchAllBlobs retrieves all blobs, optionally filtered by path pattern

func (*DB) SetHead

func (db *DB) SetHead(ctx context.Context, commitID string) error

SetHead sets the HEAD to point to a commit

func (*DB) SetMetadata added in v1.1.0

func (db *DB) SetMetadata(ctx context.Context, key, value string) error

SetMetadata sets a metadata key-value pair (upsert)

func (*DB) SetRef

func (db *DB) SetRef(ctx context.Context, name, commitID string) error

SetRef creates or updates a ref

func (*DB) SetRepoPath added in v1.1.0

func (db *DB) SetRepoPath(ctx context.Context, path string) error

SetRepoPath stores the repository working directory path

func (*DB) SetSyncState

func (db *DB) SetSyncState(ctx context.Context, remoteName string, lastCommitID *string) error

SetSyncState creates or updates the sync state for a remote

func (*DB) URL

func (db *DB) URL() string

URL returns the connection URL

func (*DB) WithTx

func (db *DB) WithTx(ctx context.Context, fn func(tx pgx.Tx) error) error

WithTx executes a function within a transaction

type Ref

type Ref struct {
	Name     string
	CommitID string
}

Ref represents a named reference to a commit

type RepoStats

type RepoStats struct {
	// Commit stats
	TotalCommits  int64
	FirstCommitID *string
	LastCommitID  *string

	// Blob stats
	TotalBlobs       int64
	UniqueFiles      int64
	TotalContentSize int64
	DeletedEntries   int64

	// Size from PostgreSQL (actual disk usage)
	CommitsTableSize int64
	BlobsTableSize   int64
	TotalIndexSize   int64
}

RepoStats contains all repository statistics

type SyncState

type SyncState struct {
	RemoteName   string
	LastCommitID *string
	SyncedAt     time.Time
}

SyncState represents the sync state with a remote

type XpatchStats

type XpatchStats struct {
	TotalRows        int64
	TotalGroups      int64
	KeyframeCount    int64
	DeltaCount       int64
	RawSizeBytes     int64
	CompressedBytes  int64
	CompressionRatio float64
	CacheHits        int64
	CacheMisses      int64
	AvgChainLength   float64
}

XpatchStats represents compression statistics from pg-xpatch

Jump to

Keyboard shortcuts

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