68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package engine
|
||
|
||
import (
|
||
"fmt"
|
||
)
|
||
|
||
// NewEngine 创建指定类型的存储引擎
|
||
func NewEngine(config EngineConfig) (eng Engine, err error) {
|
||
if config == nil {
|
||
return nil, fmt.Errorf("engine config cannot be nil")
|
||
}
|
||
|
||
engineType := ""
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
// 如果 Type() 方法调用导致 panic,返回错误
|
||
fmt.Printf("Recovered from panic in NewEngine: %v\n", r)
|
||
// return nil,r
|
||
eng = nil
|
||
err = fmt.Errorf("engine type %s not supported", engineType)
|
||
}
|
||
}()
|
||
|
||
// 尝试获取引擎类型,如果失败则返回错误
|
||
engineType = config.Type()
|
||
|
||
switch engineType {
|
||
case "memory":
|
||
// 检查配置类型是否正确
|
||
memConfig, ok := config.(*MemoryEngineConfig)
|
||
if !ok {
|
||
return nil, fmt.Errorf("invalid config type for memory engine: %T", config)
|
||
}
|
||
return newMemoryEngine(memConfig)
|
||
case "bolt":
|
||
// 检查配置类型是否正确
|
||
boltConfig, ok := config.(*BoltEngineConfig)
|
||
if !ok {
|
||
return nil, fmt.Errorf("invalid config type for bolt engine: %T", config)
|
||
}
|
||
return newBoltEngine(boltConfig)
|
||
default:
|
||
return nil, fmt.Errorf("unsupported engine type")
|
||
}
|
||
}
|
||
|
||
// newMemoryEngine 创建内存引擎
|
||
// 这个函数将在memory包中实现,这里只是声明
|
||
var newMemoryEngine = func(config *MemoryEngineConfig) (Engine, error) {
|
||
return nil, fmt.Errorf("memory engine not implemented")
|
||
}
|
||
|
||
// newBoltEngine 创建Bolt引擎
|
||
// 这个函数将在bolt包中实现,这里只是声明
|
||
var newBoltEngine = func(config *BoltEngineConfig) (Engine, error) {
|
||
return nil, fmt.Errorf("bolt engine not implemented")
|
||
}
|
||
|
||
// RegisterMemoryEngine 注册内存引擎创建函数
|
||
func RegisterMemoryEngine(creator func(config *MemoryEngineConfig) (Engine, error)) {
|
||
newMemoryEngine = creator
|
||
}
|
||
|
||
// RegisterBoltEngine 注册Bolt引擎创建函数
|
||
func RegisterBoltEngine(creator func(config *BoltEngineConfig) (Engine, error)) {
|
||
newBoltEngine = creator
|
||
}
|