database

package
v0.0.0-...-77e1a6a Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedRepository

type CachedRepository struct {
	*Repository
	// contains filtered or unexported fields
}

CachedRepository wraps the base repository with caching functionality

func NewCachedRepository

func NewCachedRepository(db *Database, cacheClient *cache.Cache, logger *logrus.Logger) *CachedRepository

NewCachedRepository creates a new cached repository instance

func (*CachedRepository) CreateExecution

func (cr *CachedRepository) CreateExecution(ctx context.Context, execution *models.CarbonExecution) error

CreateExecution creates a new execution record with caching considerations

func (*CachedRepository) CreateFunction

func (cr *CachedRepository) CreateFunction(ctx context.Context, function *models.CarbonFunction) error

CreateFunction creates a new function and invalidates cache

func (*CachedRepository) DeleteFunction

func (cr *CachedRepository) DeleteFunction(ctx context.Context, id uuid.UUID) error

DeleteFunction deletes a function and invalidates cache

func (*CachedRepository) GetCacheStats

func (cr *CachedRepository) GetCacheStats(ctx context.Context) (map[string]interface{}, error)

GetCacheStats returns cache statistics

func (*CachedRepository) GetExecutionByJobID

func (cr *CachedRepository) GetExecutionByJobID(ctx context.Context, jobID string) (*models.CarbonExecution, error)

GetExecutionByJobID retrieves an execution by job ID with potential result caching

func (*CachedRepository) GetFunction

func (cr *CachedRepository) GetFunction(ctx context.Context, id uuid.UUID) (*models.CarbonFunction, error)

GetFunction retrieves a function by ID with caching

func (*CachedRepository) GetFunctionByName

func (cr *CachedRepository) GetFunctionByName(ctx context.Context, serviceID uuid.UUID, name string) (*models.CarbonFunction, error)

GetFunctionByName retrieves a function by name with caching

func (*CachedRepository) InvalidateAllCaches

func (cr *CachedRepository) InvalidateAllCaches(ctx context.Context) error

InvalidateAllCaches clears all function-related caches

func (*CachedRepository) ListFunctions

func (cr *CachedRepository) ListFunctions(ctx context.Context, serviceID uuid.UUID) ([]models.CarbonFunction, error)

ListFunctions returns all functions for a service with caching

func (*CachedRepository) Transaction

func (cr *CachedRepository) Transaction(ctx context.Context, fn func(*CachedRepository) error) error

Transaction provides a way to execute multiple repository operations in a transaction Note: Cache operations are not transactional and will be applied immediately

func (*CachedRepository) UpdateFunction

func (cr *CachedRepository) UpdateFunction(ctx context.Context, function *models.CarbonFunction) error

UpdateFunction updates a function and invalidates cache

func (*CachedRepository) WarmupCache

func (cr *CachedRepository) WarmupCache(ctx context.Context) error

WarmupCache pre-loads frequently accessed functions into cache

type Config

type Config struct {
	Host         string `yaml:"host" validate:"required"`
	Port         int    `yaml:"port" validate:"required,min=1,max=65535"`
	Database     string `yaml:"database" validate:"required"`
	User         string `yaml:"user" validate:"required"`
	Password     string `yaml:"password" validate:"required"`
	SSLMode      string `yaml:"ssl_mode"`
	MaxConns     int    `yaml:"max_connections"`
	MaxIdleConns int    `yaml:"max_idle_connections"`
	MaxLifetime  string `yaml:"max_lifetime"`
}

Config represents database configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default database configuration

type Database

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

Database represents the database connection and operations

func New

func New(config *Config, logger *logrus.Logger) (*Database, error)

New creates a new database instance

func (*Database) Close

func (d *Database) Close() error

Close closes the database connection

func (*Database) DB

func (d *Database) DB() *sqlx.DB

DB returns the underlying sqlx.DB instance

func (*Database) GetMigrationVersion

func (d *Database) GetMigrationVersion(migrationsPath string) (uint, bool, error)

GetMigrationVersion returns the current migration version

func (*Database) HealthCheck

func (d *Database) HealthCheck() error

HealthCheck performs a health check on the database

func (*Database) Migrate

func (d *Database) Migrate(migrationsPath string) error

Migrate runs database migrations

func (*Database) MigrateDown

func (d *Database) MigrateDown(migrationsPath string, steps int) error

MigrateDown rolls back database migrations

func (*Database) Ping

func (d *Database) Ping() error

Ping tests the database connection

func (*Database) Stats

func (d *Database) Stats() sql.DBStats

Stats returns database connection statistics

func (*Database) Transaction

func (d *Database) Transaction(fn func(*sqlx.Tx) error) error

Transaction executes a function within a database transaction

type Queryer

type Queryer interface {
	sqlx.Queryer
	sqlx.Execer
	sqlx.ExtContext
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
}

Queryer interface is implemented by both sqlx.DB and sqlx.Tx

type Repository

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

Repository provides database operations for Carbon Runtime

func NewRepository

func NewRepository(db *Database) *Repository

NewRepository creates a new repository instance

func (*Repository) CleanupOldContainers

func (r *Repository) CleanupOldContainers(ctx context.Context, olderThan time.Duration) (int64, error)

CleanupOldContainers removes containers older than the specified duration

func (*Repository) CreateAPIKey

func (r *Repository) CreateAPIKey(ctx context.Context, apiKey *models.CarbonAPIKey) error

CreateAPIKey creates a new API key record

func (*Repository) CreateContainer

func (r *Repository) CreateContainer(ctx context.Context, container *models.CarbonContainer) error

CreateContainer creates a new container record

func (*Repository) CreateExecution

func (r *Repository) CreateExecution(ctx context.Context, execution *models.CarbonExecution) error

CreateExecution creates a new execution record

func (*Repository) CreateFunction

func (r *Repository) CreateFunction(ctx context.Context, function *models.CarbonFunction) error

CreateFunction creates a new function record

func (*Repository) CreateService

func (r *Repository) CreateService(ctx context.Context, service *models.CarbonService) error

CreateService creates a new service record

func (*Repository) DeleteContainer

func (r *Repository) DeleteContainer(ctx context.Context, id uuid.UUID) error

DeleteContainer deletes a container by ID

func (*Repository) DeleteFunction

func (r *Repository) DeleteFunction(ctx context.Context, id uuid.UUID) error

DeleteFunction deletes a function by ID

func (*Repository) GetAPIKey

func (r *Repository) GetAPIKey(ctx context.Context, id uuid.UUID) (*models.CarbonAPIKey, error)

GetAPIKey retrieves an API key by ID

func (*Repository) GetContainer

func (r *Repository) GetContainer(ctx context.Context, id uuid.UUID) (*models.CarbonContainer, error)

GetContainer retrieves a container by ID

func (*Repository) GetContainerByContainerID

func (r *Repository) GetContainerByContainerID(ctx context.Context, containerID string) (*models.CarbonContainer, error)

GetContainerByContainerID retrieves a container by Docker container ID

func (*Repository) GetExecution

func (r *Repository) GetExecution(ctx context.Context, id uuid.UUID) (*models.CarbonExecution, error)

GetExecution retrieves an execution by ID

func (*Repository) GetExecutionByJobID

func (r *Repository) GetExecutionByJobID(ctx context.Context, jobID string) (*models.CarbonExecution, error)

GetExecutionByJobID retrieves an execution by job ID

func (*Repository) GetExecutionCountsByStatus

func (r *Repository) GetExecutionCountsByStatus(ctx context.Context, functionID uuid.UUID, since time.Time) (map[string]int, error)

GetExecutionCountsByStatus returns execution counts grouped by status for a function

func (*Repository) GetFunction

func (r *Repository) GetFunction(ctx context.Context, id uuid.UUID) (*models.CarbonFunction, error)

GetFunction retrieves a function by ID

func (*Repository) GetFunctionByName

func (r *Repository) GetFunctionByName(ctx context.Context, serviceID uuid.UUID, name string) (*models.CarbonFunction, error)

GetFunctionByName retrieves a function by service ID and name

func (*Repository) GetFunctionStats

func (r *Repository) GetFunctionStats(ctx context.Context, functionID uuid.UUID, date time.Time) (*models.CarbonFunctionStats, error)

GetFunctionStats retrieves function statistics for a specific date

func (*Repository) GetService

func (r *Repository) GetService(ctx context.Context, id uuid.UUID) (*models.CarbonService, error)

GetService retrieves a service by ID

func (*Repository) GetServiceByName

func (r *Repository) GetServiceByName(ctx context.Context, name string) (*models.CarbonService, error)

GetServiceByName retrieves a service by name

func (*Repository) ListActiveAPIKeys

func (r *Repository) ListActiveAPIKeys(ctx context.Context) ([]*models.CarbonAPIKey, error)

ListActiveAPIKeys returns all active API keys

func (*Repository) ListAllAPIKeys

func (r *Repository) ListAllAPIKeys(ctx context.Context) ([]*models.CarbonAPIKey, error)

ListAllAPIKeys returns all API keys (active and inactive)

func (*Repository) ListContainersByFunction

func (r *Repository) ListContainersByFunction(ctx context.Context, functionID uuid.UUID) ([]models.CarbonContainer, error)

ListContainersByFunction returns containers for a function

func (*Repository) ListExecutions

func (r *Repository) ListExecutions(ctx context.Context, functionID uuid.UUID, limit, offset int) ([]models.CarbonExecution, error)

ListExecutions returns executions for a function with pagination

func (*Repository) ListFunctions

func (r *Repository) ListFunctions(ctx context.Context, serviceID uuid.UUID) ([]models.CarbonFunction, error)

ListFunctions returns all functions for a service

func (*Repository) ListServices

func (r *Repository) ListServices(ctx context.Context) ([]models.CarbonService, error)

ListServices returns all services

func (*Repository) ListWarmContainersByFunction

func (r *Repository) ListWarmContainersByFunction(ctx context.Context, functionID uuid.UUID) ([]models.CarbonContainer, error)

ListWarmContainersByFunction returns warm containers for a function

func (*Repository) Transaction

func (r *Repository) Transaction(ctx context.Context, fn func(*Repository) error) error

Transaction provides a way to execute multiple repository operations in a transaction

func (*Repository) UpdateAPIKey

func (r *Repository) UpdateAPIKey(ctx context.Context, apiKey *models.CarbonAPIKey) error

UpdateAPIKey updates an API key record

func (*Repository) UpdateContainer

func (r *Repository) UpdateContainer(ctx context.Context, container *models.CarbonContainer) error

UpdateContainer updates a container record

func (*Repository) UpdateContainerLastUsed

func (r *Repository) UpdateContainerLastUsed(ctx context.Context, containerID string) error

UpdateContainerLastUsed updates the last used timestamp for a container

func (*Repository) UpdateExecution

func (r *Repository) UpdateExecution(ctx context.Context, execution *models.CarbonExecution) error

UpdateExecution updates an execution record

func (*Repository) UpdateFunction

func (r *Repository) UpdateFunction(ctx context.Context, function *models.CarbonFunction) error

UpdateFunction updates a function record

func (*Repository) UpdateServiceHeartbeat

func (r *Repository) UpdateServiceHeartbeat(ctx context.Context, id uuid.UUID) error

UpdateServiceHeartbeat updates the last heartbeat timestamp for a service

func (*Repository) UpsertFunctionStats

func (r *Repository) UpsertFunctionStats(ctx context.Context, stats *models.CarbonFunctionStats) error

UpsertFunctionStats creates or updates function statistics

type RepositoryInterface

type RepositoryInterface interface {
	// Service operations
	CreateService(ctx context.Context, service *models.CarbonService) error
	GetService(ctx context.Context, id uuid.UUID) (*models.CarbonService, error)
	GetServiceByName(ctx context.Context, name string) (*models.CarbonService, error)
	UpdateServiceHeartbeat(ctx context.Context, id uuid.UUID) error
	ListServices(ctx context.Context) ([]models.CarbonService, error)

	// Function operations
	CreateFunction(ctx context.Context, function *models.CarbonFunction) error
	GetFunction(ctx context.Context, id uuid.UUID) (*models.CarbonFunction, error)
	GetFunctionByName(ctx context.Context, serviceID uuid.UUID, name string) (*models.CarbonFunction, error)
	UpdateFunction(ctx context.Context, function *models.CarbonFunction) error
	DeleteFunction(ctx context.Context, id uuid.UUID) error
	ListFunctions(ctx context.Context, serviceID uuid.UUID) ([]models.CarbonFunction, error)

	// Execution operations
	CreateExecution(ctx context.Context, execution *models.CarbonExecution) error
	GetExecution(ctx context.Context, id uuid.UUID) (*models.CarbonExecution, error)
	GetExecutionByJobID(ctx context.Context, jobID string) (*models.CarbonExecution, error)
	UpdateExecution(ctx context.Context, execution *models.CarbonExecution) error
	ListExecutions(ctx context.Context, functionID uuid.UUID, limit, offset int) ([]models.CarbonExecution, error)

	// Container operations
	CreateContainer(ctx context.Context, container *models.CarbonContainer) error
	GetContainer(ctx context.Context, id uuid.UUID) (*models.CarbonContainer, error)
	GetContainerByContainerID(ctx context.Context, containerID string) (*models.CarbonContainer, error)
	UpdateContainer(ctx context.Context, container *models.CarbonContainer) error
	UpdateContainerLastUsed(ctx context.Context, containerID string) error
	DeleteContainer(ctx context.Context, id uuid.UUID) error
	ListContainersByFunction(ctx context.Context, functionID uuid.UUID) ([]models.CarbonContainer, error)
	ListWarmContainersByFunction(ctx context.Context, functionID uuid.UUID) ([]models.CarbonContainer, error)
	CleanupOldContainers(ctx context.Context, olderThan time.Duration) (int64, error)

	// Stats operations
	GetFunctionStats(ctx context.Context, functionID uuid.UUID, date time.Time) (*models.CarbonFunctionStats, error)
	UpsertFunctionStats(ctx context.Context, stats *models.CarbonFunctionStats) error
	GetExecutionCountsByStatus(ctx context.Context, functionID uuid.UUID, since time.Time) (map[string]int, error)
}

RepositoryInterface defines the interface for database operations

type ServiceRepository

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

ServiceRepository handles carbon_services table operations

Jump to

Keyboard shortcuts

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