Documentation
¶
Index ¶
- Constants
- Variables
- func CreateAlterTable(ti *tableMeta, metric influx.Metric, seq *BoltSeq, asTags Matcher) []string
- func CreateCreateTable(metric influx.Metric, seq *BoltSeq, asTags Matcher) (tableColumns, scripts []string)
- func IndexN(s, sep string, n int) int
- func JitterInterval(interval, jitter time.Duration) time.Duration
- func MultiInsertBinds(columnsNum, rowsNum int) string
- func OnConflictGen(tags map[string]bool) func(columns []string) string
- func ParseTime(tm string) (t time.Time, err error)
- func ParseTimeMilli(tm string) (unixMilli int64, err error)
- func ParseTimeWindow(timeWindow string) (window time.Duration, err error)
- func QuoteSlice(ss []string) []string
- func RemoveFilesPrefix(prefix string, debug bool) (removeFiles []file, totalSize int64)
- func SortMap(m map[string]any) (keys []string, values []any)
- func Tick[T any](interval, jitter time.Duration, ch chan T, chItemFn func(T) error, ...) error
- func ToDBFieldValue(x any) any
- type BoltSeq
- type Config
- type ConfigFn
- func WithAllowDBErrors(val []string) ConfigFn
- func WithBadTableSubs(val []string) ConfigFn
- func WithConfig(val *Config) ConfigFn
- func WithDebug(val bool) ConfigFn
- func WithDividedBy(val DividedBy) ConfigFn
- func WithDriverName(val string) ConfigFn
- func WithDsnOptions(val string) ConfigFn
- func WithPrefix(val string) ConfigFn
- func WithSeqKeysDB(val *BoltSeq) ConfigFn
- func WithSeqKeysDBName(val string) ConfigFn
- type DbFile
- type DebugDB
- type DividedBy
- type File
- type Insert
- type LastError
- type Matcher
- type Prepared
- type QueryType
- type RecycleResult
- type SqlitePlus
- func (q *SqlitePlus) Close() error
- func (q *SqlitePlus) ListDiskTables() (map[string][]*DbFile, error)
- func (q *SqlitePlus) Read(table, query string, dividedTime time.Time, emptyStruct any, args ...any) (*sqlrun.Result, error)
- func (q *SqlitePlus) Recycle(before string) (rr RecycleResult)
- func (q *SqlitePlus) TableFileBase(table, dividedBy string) string
- func (q *SqlitePlus) TableFilePath(table, dividedBy string) string
- func (q *SqlitePlus) WriteMetric(metric influx.Metric) error
- type TableIndexInfo
- type TimeSpan
- type TimeSpanUnit
Constants ¶
const ( // DefaultMaxIdle 默认最大数据库读写空闲时间,超期关闭数据库 DefaultMaxIdle = 5 * time.Minute // DefaultBatchInterval 批量执行时间间隔 DefaultBatchInterval = 10 * time.Second // DefaultBatchSize 批次大学 DefaultBatchSize = 50 // DefaultRecycleInterval 回收周期间隔 DefaultRecycleInterval = 24 * time.Hour )
const (
DefaultKeySeqName = "keys.boltdb"
)
Variables ¶
var ( // DefaultTimeSeriesKeep 默认保留打点数据 DefaultTimeSeriesKeep = TimeSpan{Value: 1, Unit: UnitMonth} // 1个月 )
var (
ErrBadTimeSpan = errors.New("bad TimeSpan expr")
)
var ( // ErrNotFound is returned when an entry is not found. ErrNotFound = errors.New("not found") )
var ErrUnknownDividedString = errors.New("unknown divided string")
ErrUnknownDividedString 分割时间模式字符串无法识别
Functions ¶
func CreateAlterTable ¶
CreateAlterTable 根据指标,生成添加字段、创建索引等 SQL 语句
func CreateCreateTable ¶
func CreateCreateTable(metric influx.Metric, seq *BoltSeq, asTags Matcher) (tableColumns, scripts []string)
CreateCreateTable 创建 建表 SQL, 索引 SQL
func IndexN ¶
IndexN 返回字符串 s 中第 n 个子字符串 sub 的索引。 如果 n 是正数,则从左到右查找第 n 个 sub 的位置。 如果 n 是负数,则从右到左查找第 -n 个 sub 的位置。 如果找不到,返回 -1
func MultiInsertBinds ¶
func OnConflictGen ¶
OnConflictGen 生成 on conflict 子语句函数
func ParseTime ¶
ParseTime 解析时间字符串 格式1(绝对时间): RFC3339 "2006-01-02T15:04:05Z07:00" 格式2(偏移间隔): -10d 10天前的此时
func ParseTimeMilli ¶
func QuoteSlice ¶
func RemoveFilesPrefix ¶
RemoveFilesPrefix 删除以特定前缀开头的文件
func ToDBFieldValue ¶
Types ¶
type BoltSeq ¶
func CreateSeqKeysDB ¶
CreateSeqKeysDB 创建 keys 字符串转换为枚举数字的 boltdb 库名字(可以包括路径),默认 keyseq.bolt 为 sqlite 的 tag 字符串值生成唯一的对应序号(减少sqlite数据库文件大小而优化设计)
prefix 设定库文件的前缀(包括完整路径) seqKeysDBName 库名, "off" 表示不使用, "" 使用 DefaultKeySeqName seqKeysDB 外部已经提前创建好的库,在 seqKeysDBName != "off" 时优先使用
func NewBoltSeq ¶
type Config ¶
type Config struct {
// DriverName 驱动名称, 例如 sqlite3
DriverName string
// Prefix 设定库文件的前缀(包括完整路径)
Prefix string
// WriteDsnOptions 连接字符串选项,比如 _journal=WAL
WriteDsnOptions string
// ReadDsnOptions 连接字符串选项,比如 _txlock=immediate
ReadDsnOptions string
// BadTableSubs 非法表名子串
BadTableSubs []string
// AllowDBErrors 允许的DB错误字眼,否则被认为库文件损坏
AllowDBErrors []string
// MaxIdle 最大数据库读写空闲时间,超期关闭数据库
MaxIdle time.Duration
// BatchInsertInterval 批量插入时间间隔
BatchInsertInterval time.Duration
// BatchInsertSize 批量插入大小
BatchInsertSize int
// SeqKeysDBName keys 字符串转换为枚举数字的 boltdb 库名字(可以包括路径),默认 keyseq.bolt
// 为 sqlite 的 tag 字符串值生成唯一的对应序号(减少sqlite数据库文件大小而优化设计)
SeqKeysDBName string
// SeqKeysDB 是 SeqKeysDB 对应的对象
SeqKeysDB *BoltSeq
// Debug 是否开启 Debug 模式,打印 SQL 等
Debug bool
// AsTags 用来转换普通字段为索引字段的判断器
AsTags Matcher
// TimeSeriesKeep 保留打点数据时间, 默认 DefaultTimeSeriesKeep
TimeSeriesKeep *TimeSpan
// TimeSeriesMaxSize 保留打点文件最大大小, 默认0表示不限制
TimeSeriesMaxSize int64
// RecycleInterval 回收时间间隔, 默认 DefaultRecycleInterval
RecycleInterval time.Duration
// DividedBy 按时间分库模式
DividedBy
}
func (*Config) ValidateTable ¶
ValidateTable 校验表明是否合法
type ConfigFn ¶
type ConfigFn func(*Config)
func WithAllowDBErrors ¶
func WithBadTableSubs ¶
func WithConfig ¶
func WithDividedBy ¶
func WithDriverName ¶
func WithDsnOptions ¶
func WithPrefix ¶
func WithSeqKeysDB ¶
func WithSeqKeysDBName ¶
type DbFile ¶
type DbFile struct {
// Table 表名, e.g. disk
Table string
// DividedBy 时间划分, e.g. month.202407
DividedBy string
// 主数据库文件, e.g. testdata/metric.t.disk.month.202407.db
File File
// Relatives 关联的文件,主要用于计算数据空间大小
// e.g. testdata/metric.t.disk.month.202407.db 关联
// testdata/metric.t.disk.month.202407.db-shm
// testdata/metric.t.disk.month.202407.db-wal
Relatives []File
}
DbFile 数据库文件对象
type DebugDB ¶
type DividedBy ¶
type DividedBy int
DividedBy 分库文件的时间分割模式
func ParseDivideString ¶
ParseDivideString 解析分割时间模式字符串
func (DividedBy) CutoffDays ¶
CutoffDays 根据时间 t, 以及保留天数 days, 计算切断时间点所在的划分时间值(如果等于当前时间划分值,则往前退一个时间划分)
type Prepared ¶
type Prepared struct {
Debug bool
// contains filtered or unexported fields
}
Prepared 预备语句
type RecycleResult ¶
type SqlitePlus ¶
type SqlitePlus struct {
*Config
// contains filtered or unexported fields
}
SqlitePlus SqlitePlus 结构体对象
func (*SqlitePlus) Close ¶
func (q *SqlitePlus) Close() error
Close 关闭 SqlitePlus 所有操作,包括关闭库文件、退出回收协程等
func (*SqlitePlus) ListDiskTables ¶
func (q *SqlitePlus) ListDiskTables() (map[string][]*DbFile, error)
ListDiskTables 列出磁盘上的所有数据库文件
func (*SqlitePlus) Read ¶
func (q *SqlitePlus) Read(table, query string, dividedTime time.Time, emptyStruct any, args ...any) (*sqlrun.Result, error)
Read 执行查询 table 表名称 query 查询 SQL dividedTime 查询落在的时间划分(哪个时间分区库上) emptyStruct 从结果集映射到哪个结构体上
func (*SqlitePlus) Recycle ¶
func (q *SqlitePlus) Recycle(before string) (rr RecycleResult)
Recycle 手动触发回收 before 设置为空时,按照系统配置的策略执行一次回收, 格式1(绝对时间): RFC3339 "2006-01-02T15:04:05Z07:00" 格式2(偏移间隔): -10d 10天前的此时
func (*SqlitePlus) TableFileBase ¶
func (q *SqlitePlus) TableFileBase(table, dividedBy string) string
TableFileBase 返回表文件的基础文件名前缀,例如: "disk.month.202408.db"
func (*SqlitePlus) TableFilePath ¶
func (q *SqlitePlus) TableFilePath(table, dividedBy string) string
TableFilePath 返回表文件的完整前缀,例如: "testdata/metric.t.disk.month.202408.db"
func (*SqlitePlus) WriteMetric ¶
func (q *SqlitePlus) WriteMetric(metric influx.Metric) error
WriteMetric 写入指标 此过程,会涉及到建库、建表/索引,或者已有库的修正表及索引
type TableIndexInfo ¶
TableIndexInfo 表索引对象
func ParseTableIndexInfo ¶
func ParseTableIndexInfo(db *DebugDB, table string) (*TableIndexInfo, error)
ParseTableIndexInfo 解析表的索引字段
type TimeSpan ¶
type TimeSpan struct {
Value int
Unit TimeSpanUnit
}
func ParseTimeSpan ¶
type TimeSpanUnit ¶
type TimeSpanUnit int
const ( UnitMonth TimeSpanUnit = iota UnitWeek UnitDay )
func (TimeSpanUnit) Of ¶
func (u TimeSpanUnit) Of(value int) TimeSpan
