262 lines
7.2 KiB
Go
262 lines
7.2 KiB
Go
package engine
|
||
|
||
import (
|
||
"fmt"
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// EngineConfig 定义引擎配置接口
|
||
type EngineConfig interface {
|
||
// 通用配置方法
|
||
WithMaxRetention(duration time.Duration) EngineConfig
|
||
WithMaxPoints(points int) EngineConfig
|
||
WithFlushInterval(interval time.Duration) EngineConfig
|
||
|
||
// 获取配置值
|
||
MaxRetention() time.Duration
|
||
MaxPoints() int
|
||
FlushInterval() time.Duration
|
||
|
||
// 获取特定引擎的配置
|
||
MemoryConfig() *MemoryEngineConfig
|
||
FileConfig() *FileEngineConfig
|
||
}
|
||
|
||
// BaseEngineConfig 提供基本的配置实现
|
||
type BaseEngineConfig struct {
|
||
maxRetention time.Duration
|
||
maxPoints int
|
||
flushInterval time.Duration
|
||
memoryConfig *MemoryEngineConfig
|
||
fileConfig *FileEngineConfig
|
||
}
|
||
|
||
// NewEngineConfig 创建新的引擎配置
|
||
func NewEngineConfig() EngineConfig {
|
||
return &BaseEngineConfig{
|
||
maxRetention: 24 * time.Hour, // 默认保留24小时
|
||
maxPoints: 1000, // 默认最大点数
|
||
flushInterval: 10 * time.Second, // 默认刷新间隔
|
||
memoryConfig: NewMemoryEngineConfig(),
|
||
fileConfig: NewFileEngineConfig(),
|
||
}
|
||
}
|
||
|
||
// WithMaxRetention 设置最大保留时间
|
||
func (c *BaseEngineConfig) WithMaxRetention(duration time.Duration) EngineConfig {
|
||
c.maxRetention = duration
|
||
return c
|
||
}
|
||
|
||
// WithMaxPoints 设置最大点数
|
||
func (c *BaseEngineConfig) WithMaxPoints(points int) EngineConfig {
|
||
c.maxPoints = points
|
||
return c
|
||
}
|
||
|
||
// WithFlushInterval 设置刷新间隔
|
||
func (c *BaseEngineConfig) WithFlushInterval(interval time.Duration) EngineConfig {
|
||
c.flushInterval = interval
|
||
return c
|
||
}
|
||
|
||
// MaxRetention 获取最大保留时间
|
||
func (c *BaseEngineConfig) MaxRetention() time.Duration {
|
||
return c.maxRetention
|
||
}
|
||
|
||
// MaxPoints 获取最大点数
|
||
func (c *BaseEngineConfig) MaxPoints() int {
|
||
return c.maxPoints
|
||
}
|
||
|
||
// FlushInterval 获取刷新间隔
|
||
func (c *BaseEngineConfig) FlushInterval() time.Duration {
|
||
return c.flushInterval
|
||
}
|
||
|
||
// MemoryConfig 获取内存引擎配置
|
||
func (c *BaseEngineConfig) MemoryConfig() *MemoryEngineConfig {
|
||
return c.memoryConfig
|
||
}
|
||
|
||
// FileConfig 获取文件引擎配置
|
||
func (c *BaseEngineConfig) FileConfig() *FileEngineConfig {
|
||
return c.fileConfig
|
||
}
|
||
|
||
// MemoryEngineConfig 内存引擎特定配置
|
||
type MemoryEngineConfig struct {
|
||
MaxPointsPerSeries int // 每个序列保留的最大点数
|
||
UseCompression bool // 是否使用压缩
|
||
}
|
||
|
||
// NewMemoryEngineConfig 创建新的内存引擎配置
|
||
func NewMemoryEngineConfig() *MemoryEngineConfig {
|
||
return &MemoryEngineConfig{
|
||
MaxPointsPerSeries: 30, // 默认保留30个点
|
||
UseCompression: true, // 默认启用压缩
|
||
}
|
||
}
|
||
|
||
// FileEngineConfig 文件引擎特定配置
|
||
type FileEngineConfig struct {
|
||
DataDir string // 数据目录
|
||
SegmentSize int64 // 段文件大小
|
||
CompactWindow time.Duration // 压缩检查间隔
|
||
MaxSegments int // 最大段文件数
|
||
UseCompression bool // 是否启用压缩
|
||
CompressionLevel int // 压缩级别(1-9)
|
||
IndexCacheSize int64 // 索引缓存大小
|
||
WriteBufferSize int // 写入缓冲区大小
|
||
MaxOpenFiles int // 最大打开文件数
|
||
SyncWrites bool // 是否同步写入
|
||
RetentionPeriod time.Duration // 数据保留期限
|
||
CompactThreshold float64 // 压缩触发阈值(重叠率)
|
||
}
|
||
|
||
// NewFileEngineConfig 创建新的文件引擎配置
|
||
func NewFileEngineConfig() *FileEngineConfig {
|
||
return &FileEngineConfig{
|
||
DataDir: "data", // 默认数据目录
|
||
SegmentSize: 64 * 1024 * 1024, // 默认64MB
|
||
CompactWindow: 24 * time.Hour, // 默认24小时
|
||
MaxSegments: 10, // 默认最大10个段文件
|
||
UseCompression: true, // 默认启用压缩
|
||
CompressionLevel: 6, // 默认压缩级别
|
||
IndexCacheSize: 256 * 1024 * 1024, // 默认256MB索引缓存
|
||
WriteBufferSize: 4 * 1024 * 1024, // 默认4MB写入缓冲
|
||
MaxOpenFiles: 1000, // 默认最大打开文件数
|
||
SyncWrites: false, // 默认异步写入
|
||
RetentionPeriod: 7 * 24 * time.Hour, // 默认保留7天
|
||
CompactThreshold: 0.5, // 默认50%重叠率触发压缩
|
||
}
|
||
}
|
||
|
||
// WithDataDir 设置数据目录
|
||
func (c *FileEngineConfig) WithDataDir(dir string) *FileEngineConfig {
|
||
c.DataDir = dir
|
||
return c
|
||
}
|
||
|
||
// WithSegmentSize 设置段文件大小
|
||
func (c *FileEngineConfig) WithSegmentSize(size int64) *FileEngineConfig {
|
||
c.SegmentSize = size
|
||
return c
|
||
}
|
||
|
||
// WithCompactWindow 设置压缩检查间隔
|
||
func (c *FileEngineConfig) WithCompactWindow(window time.Duration) *FileEngineConfig {
|
||
c.CompactWindow = window
|
||
return c
|
||
}
|
||
|
||
// WithMaxSegments 设置最大段文件数
|
||
func (c *FileEngineConfig) WithMaxSegments(count int) *FileEngineConfig {
|
||
c.MaxSegments = count
|
||
return c
|
||
}
|
||
|
||
// WithCompression 设置压缩选项
|
||
func (c *FileEngineConfig) WithCompression(use bool, level int) *FileEngineConfig {
|
||
c.UseCompression = use
|
||
if level >= 1 && level <= 9 {
|
||
c.CompressionLevel = level
|
||
}
|
||
return c
|
||
}
|
||
|
||
// WithIndexCacheSize 设置索引缓存大小
|
||
func (c *FileEngineConfig) WithIndexCacheSize(size int64) *FileEngineConfig {
|
||
c.IndexCacheSize = size
|
||
return c
|
||
}
|
||
|
||
// WithWriteBufferSize 设置写入缓冲区大小
|
||
func (c *FileEngineConfig) WithWriteBufferSize(size int) *FileEngineConfig {
|
||
c.WriteBufferSize = size
|
||
return c
|
||
}
|
||
|
||
// WithMaxOpenFiles 设置最大打开文件数
|
||
func (c *FileEngineConfig) WithMaxOpenFiles(count int) *FileEngineConfig {
|
||
c.MaxOpenFiles = count
|
||
return c
|
||
}
|
||
|
||
// WithSyncWrites 设置同步写入选项
|
||
func (c *FileEngineConfig) WithSyncWrites(sync bool) *FileEngineConfig {
|
||
c.SyncWrites = sync
|
||
return c
|
||
}
|
||
|
||
// WithRetentionPeriod 设置数据保留期限
|
||
func (c *FileEngineConfig) WithRetentionPeriod(period time.Duration) *FileEngineConfig {
|
||
c.RetentionPeriod = period
|
||
return c
|
||
}
|
||
|
||
// WithCompactThreshold 设置压缩触发阈值
|
||
func (c *FileEngineConfig) WithCompactThreshold(threshold float64) *FileEngineConfig {
|
||
if threshold > 0 && threshold <= 1 {
|
||
c.CompactThreshold = threshold
|
||
}
|
||
return c
|
||
}
|
||
|
||
// EngineFactory 定义创建存储引擎的工厂函数类型
|
||
type EngineFactory func(config EngineConfig) (Engine, error)
|
||
|
||
// EngineRegistry 管理所有可用的存储引擎
|
||
type EngineRegistry struct {
|
||
mu sync.RWMutex
|
||
engines map[string]EngineFactory
|
||
}
|
||
|
||
// NewEngineRegistry 创建新的引擎注册表
|
||
func NewEngineRegistry() *EngineRegistry {
|
||
return &EngineRegistry{
|
||
engines: make(map[string]EngineFactory),
|
||
}
|
||
}
|
||
|
||
// Register 注册新引擎
|
||
func (r *EngineRegistry) Register(name string, factory EngineFactory) {
|
||
r.mu.Lock()
|
||
defer r.mu.Unlock()
|
||
r.engines[name] = factory
|
||
}
|
||
|
||
// Create 创建引擎实例
|
||
func (r *EngineRegistry) Create(name string, config EngineConfig) (Engine, error) {
|
||
r.mu.RLock()
|
||
factory, ok := r.engines[name]
|
||
r.mu.RUnlock()
|
||
|
||
if !ok {
|
||
return nil, fmt.Errorf("unknown engine: %s", name)
|
||
}
|
||
|
||
return factory(config)
|
||
}
|
||
|
||
// ListEngines 列出所有注册的引擎
|
||
func (r *EngineRegistry) ListEngines() []string {
|
||
r.mu.RLock()
|
||
defer r.mu.RUnlock()
|
||
|
||
engines := make([]string, 0, len(r.engines))
|
||
for name := range r.engines {
|
||
engines = append(engines, name)
|
||
}
|
||
return engines
|
||
}
|
||
|
||
// UnregisterEngine 注销引擎
|
||
func (r *EngineRegistry) UnregisterEngine(name string) {
|
||
r.mu.Lock()
|
||
defer r.mu.Unlock()
|
||
delete(r.engines, name)
|
||
}
|