db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

func CloseLogDB

func CloseLogDB() error

CloseLogDB 断开独立日志库连接(用于关闭后台日志时释放资源)。 共用主库模式下为空操作——绝不关闭主库连接。返回后 GetLogDB 在独立库 模式下会返回 nil,直到 ReopenLogDB 重连。

func EnqueueWrite

func EnqueueWrite(job WriteJob)

EnqueueWrite submits a job to the serial writer. If the queue is full, it waits up to 5 seconds; if still full, it executes synchronously as a fallback to avoid data loss.

func FastClearTable

func FastClearTable(conn *gorm.DB, model any, tableName string) error

FastClearTable 以方言最快的方式清空整张表,并重建表结构与索引。

相比逐行 DELETE(百万级数据在 SQLite + WAL + 单连接下可能耗时数十分钟), 本函数对各方言采用近乎瞬时的清空策略:

  • MySQL/Postgres:TRUNCATE TABLE,瞬时清空并回收空间;
  • SQLite:DROP TABLE + AutoMigrate 重建(pure-Go 驱动下 TRUNCATE 不可用), 直接丢弃整张表的数据页,并通过 struct tag 完整恢复索引。

FastClearTable clears a table quickly using dialect-specific fast paths. 参数 model 用于 AutoMigrate 重建(SQLite),tableName 用于 TRUNCATE 拼接。 重建依赖 model 的 struct tag 完整声明索引;relay_logs 的 time 索引即来自 model.RelayLog 的字段 tag,因此可被正确恢复。

func GetDB

func GetDB() *gorm.DB

func GetLogDB

func GetLogDB() *gorm.DB

GetLogDB 返回日志库连接。独立库模式下返回独立连接;若该连接已被 CloseLogDB 断开(logDB == nil 但仍配置了独立库),返回 nil,调用方需 自行判断(日志关闭场景下不应再写入)。共用主库模式下返回主库连接。

func InitDB

func InitDB(dbType, dsn string, debug bool) error

func InitLogDB

func InitLogDB(logType, logPath string, debug bool) error

InitLogDB 初始化独立日志库。logType/logPath 任一为空时视为「共用主库」, 不建立独立连接(GetLogDB 将回落到主库)。配置完整时打开独立连接并只迁移 relay_logs 表结构。必须在 InitDB 之后调用。

func IsLogDBSeparate

func IsLogDBSeparate() bool

IsLogDBSeparate 报告日志是否使用独立数据库(而非共用主库)。

func IsLogSQLite

func IsLogSQLite() bool

IsLogSQLite 报告日志库是否为 SQLite。独立库时取日志库类型,否则取主库类型。

func IsSQLite

func IsSQLite() bool

func Migrate

func Migrate(conn *gorm.DB) error

func MigrateLogDB

func MigrateLogDB(conn *gorm.DB) error

MigrateLogDB 仅迁移日志库需要的表结构(relay_logs + relay_log_attempts)。

func OpenStandalone

func OpenStandalone(dbType, dsn string, debug bool) (*gorm.DB, error)

func ReopenLogDB

func ReopenLogDB() error

ReopenLogDB 重新打开独立日志库连接(用于重新开启后台日志)。 共用主库模式下为空操作。若连接已存在则直接返回。

func SQLiteFilePath

func SQLiteFilePath(dsn string) (string, bool)

SQLiteFilePath resolves a SQLite DSN to the on-disk file it designates. ok is false for in-memory DSNs (":memory:", "file::memory:", "?mode=memory"), which never touch the filesystem.

Exported so request-boundary validators can reason about exactly the path this package will later create in ensureSQLiteDir. Resolving the DSN a second time with a separately written parser would reintroduce the hole it is meant to close: any form the two parsers disagree on (the "file:" URI branch below, Windows drive letters, opaque URIs) would pass validation and still land somewhere else on disk.

func StartSerialWriter

func StartSerialWriter(parentCtx context.Context)

func StopSerialWriter

func StopSerialWriter()

StopSerialWriter cancels the serial writer context and waits for remaining jobs to drain. It should be called during shutdown after all producers have stopped.

Types

type WriteJob

type WriteJob struct {
	Name string
	Fn   func(ctx context.Context) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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