Documentation
¶
Index ¶
- func Close() error
- func CloseLogDB() error
- func EnqueueWrite(job WriteJob)
- func FastClearTable(conn *gorm.DB, model any, tableName string) error
- func GetDB() *gorm.DB
- func GetLogDB() *gorm.DB
- func InitDB(dbType, dsn string, debug bool) error
- func InitLogDB(logType, logPath string, debug bool) error
- func IsLogDBSeparate() bool
- func IsLogSQLite() bool
- func IsSQLite() bool
- func Migrate(conn *gorm.DB) error
- func MigrateLogDB(conn *gorm.DB) error
- func OpenStandalone(dbType, dsn string, debug bool) (*gorm.DB, error)
- func ReopenLogDB() error
- func SQLiteFilePath(dsn string) (string, bool)
- func StartSerialWriter(parentCtx context.Context)
- func StopSerialWriter()
- type WriteJob
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 GetLogDB ¶
GetLogDB 返回日志库连接。独立库模式下返回独立连接;若该连接已被 CloseLogDB 断开(logDB == nil 但仍配置了独立库),返回 nil,调用方需 自行判断(日志关闭场景下不应再写入)。共用主库模式下返回主库连接。
func InitLogDB ¶
InitLogDB 初始化独立日志库。logType/logPath 任一为空时视为「共用主库」, 不建立独立连接(GetLogDB 将回落到主库)。配置完整时打开独立连接并只迁移 relay_logs 表结构。必须在 InitDB 之后调用。
func MigrateLogDB ¶
MigrateLogDB 仅迁移日志库需要的表结构(relay_logs + relay_log_attempts)。
func ReopenLogDB ¶
func ReopenLogDB() error
ReopenLogDB 重新打开独立日志库连接(用于重新开启后台日志)。 共用主库模式下为空操作。若连接已存在则直接返回。
func SQLiteFilePath ¶
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 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.