142 lines
3.3 KiB
Go
142 lines
3.3 KiB
Go
package engine
|
||
|
||
import "time"
|
||
|
||
// EngineConfig 存储引擎配置接口
|
||
type EngineConfig interface {
|
||
// 获取引擎类型
|
||
Type() string
|
||
}
|
||
|
||
// BaseConfig 基础配置,所有引擎共享
|
||
type BaseConfig struct {
|
||
// 数据保留时间,超过此时间的数据将被清理
|
||
RetentionPeriod time.Duration
|
||
|
||
// 是否启用同步写入,启用后每次写入都会立即持久化
|
||
SyncWrites bool
|
||
|
||
// 写入缓冲区大小,0表示不使用缓冲区
|
||
WriteBufferSize int
|
||
|
||
// 是否启用压缩
|
||
EnableCompaction bool
|
||
|
||
// 压缩间隔
|
||
CompactionInterval time.Duration
|
||
}
|
||
|
||
// MemoryEngineConfig 内存引擎配置
|
||
type MemoryEngineConfig struct {
|
||
BaseConfig
|
||
|
||
// 每个数据点保留的历史值数量(环形队列大小)
|
||
MaxHistoryValues int
|
||
|
||
// 最大数据点数量,超过此数量将拒绝写入,0表示不限制
|
||
MaxPoints int
|
||
|
||
// 是否启用持久化,启用后将定期将数据写入磁盘
|
||
EnablePersistence bool
|
||
|
||
// 持久化文件路径,仅在EnablePersistence为true时有效
|
||
PersistencePath string
|
||
|
||
// 持久化间隔,仅在EnablePersistence为true时有效
|
||
PersistenceInterval time.Duration
|
||
}
|
||
|
||
// 实现EngineConfig接口
|
||
func (c *MemoryEngineConfig) Type() string {
|
||
return "memory"
|
||
}
|
||
|
||
// MemoryConfig 返回内存引擎配置
|
||
func (c *MemoryEngineConfig) MemoryConfig() *MemoryEngineConfig {
|
||
return c
|
||
}
|
||
|
||
// BoltConfig 返回Bolt引擎配置
|
||
func (c *MemoryEngineConfig) BoltConfig() *BoltEngineConfig {
|
||
return nil
|
||
}
|
||
|
||
// BoltEngineConfig Bolt引擎配置
|
||
type BoltEngineConfig struct {
|
||
BaseConfig
|
||
|
||
// 数据库文件路径
|
||
Path string
|
||
|
||
// 存储桶名称
|
||
BucketName string
|
||
|
||
// 是否启用WAL日志
|
||
EnableWAL bool
|
||
|
||
// WAL日志目录,仅在EnableWAL为true时有效
|
||
WALDir string
|
||
|
||
// 是否启用批量写入
|
||
EnableBatch bool
|
||
|
||
// 批量写入大小,仅在EnableBatch为true时有效
|
||
BatchSize int
|
||
|
||
// 批量写入超时,仅在EnableBatch为true时有效
|
||
BatchTimeout time.Duration
|
||
}
|
||
|
||
// 实现EngineConfig接口
|
||
func (c *BoltEngineConfig) Type() string {
|
||
return "bolt"
|
||
}
|
||
|
||
// MemoryConfig 返回内存引擎配置
|
||
func (c *BoltEngineConfig) MemoryConfig() *MemoryEngineConfig {
|
||
return nil
|
||
}
|
||
|
||
// BoltConfig 返回Bolt引擎配置
|
||
func (c *BoltEngineConfig) BoltConfig() *BoltEngineConfig {
|
||
return c
|
||
}
|
||
|
||
// NewMemoryEngineConfig 创建默认的内存引擎配置
|
||
func NewMemoryEngineConfig() *MemoryEngineConfig {
|
||
return &MemoryEngineConfig{
|
||
BaseConfig: BaseConfig{
|
||
RetentionPeriod: 24 * time.Hour, // 默认保留24小时
|
||
SyncWrites: false,
|
||
WriteBufferSize: 1000,
|
||
EnableCompaction: true,
|
||
CompactionInterval: 1 * time.Hour,
|
||
},
|
||
MaxHistoryValues: 30, // 默认保留30个历史值
|
||
MaxPoints: 0, // 默认不限制
|
||
EnablePersistence: false,
|
||
PersistencePath: "",
|
||
PersistenceInterval: 10 * time.Minute,
|
||
}
|
||
}
|
||
|
||
// NewBoltEngineConfig 创建默认的Bolt引擎配置
|
||
func NewBoltEngineConfig(path string) *BoltEngineConfig {
|
||
return &BoltEngineConfig{
|
||
BaseConfig: BaseConfig{
|
||
RetentionPeriod: 24 * time.Hour, // 默认保留24小时
|
||
SyncWrites: false,
|
||
WriteBufferSize: 1000,
|
||
EnableCompaction: true,
|
||
CompactionInterval: 1 * time.Hour,
|
||
},
|
||
Path: path,
|
||
BucketName: "gotidb",
|
||
EnableWAL: true,
|
||
WALDir: path + "_wal",
|
||
EnableBatch: true,
|
||
BatchSize: 1000,
|
||
BatchTimeout: 1 * time.Second,
|
||
}
|
||
}
|