160 lines
4.0 KiB
Go
160 lines
4.0 KiB
Go
// Package storage 实现存储引擎接口
|
|
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// StorageEngine 存储引擎接口
|
|
type StorageEngine interface {
|
|
// 数据库操作
|
|
CreateDatabase(name string) error
|
|
DropDatabase(name string) error
|
|
ListDatabases() ([]string, error)
|
|
|
|
// 集合操作
|
|
CreateCollection(dbName, collName string) error
|
|
DropCollection(dbName, collName string) error
|
|
ListCollections(dbName string) ([]string, error)
|
|
|
|
// 文档操作
|
|
Insert(dbName, collName string, document []byte) error
|
|
Query(dbName, collName string, query []byte) ([][]byte, error)
|
|
Update(dbName, collName string, query, update []byte) error
|
|
Delete(dbName, collName string, query []byte) error
|
|
}
|
|
|
|
// NewMemoryEngine 创建内存存储引擎实例
|
|
func NewMemoryEngine() (StorageEngine, error) {
|
|
return &memoryEngine{
|
|
databases: make(map[string]*memoryDatabase),
|
|
}, nil
|
|
}
|
|
|
|
// 内存存储引擎实现
|
|
type memoryEngine struct {
|
|
databases map[string]*memoryDatabase
|
|
}
|
|
|
|
func (e *memoryEngine) CreateDatabase(name string) error {
|
|
if _, exists := e.databases[name]; exists {
|
|
return fmt.Errorf("database %s already exists", name)
|
|
}
|
|
e.databases[name] = &memoryDatabase{
|
|
collections: make(map[string]*memoryCollection),
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) DropDatabase(name string) error {
|
|
if _, exists := e.databases[name]; !exists {
|
|
return fmt.Errorf("database %s does not exist", name)
|
|
}
|
|
delete(e.databases, name)
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) ListDatabases() ([]string, error) {
|
|
names := make([]string, 0, len(e.databases))
|
|
for name := range e.databases {
|
|
names = append(names, name)
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
func (e *memoryEngine) CreateCollection(dbName, collName string) error {
|
|
db, exists := e.databases[dbName]
|
|
if !exists {
|
|
return fmt.Errorf("database %s does not exist", dbName)
|
|
}
|
|
|
|
if _, collExists := db.collections[collName]; collExists {
|
|
return fmt.Errorf("collection %s already exists in database %s", collName, dbName)
|
|
}
|
|
|
|
db.collections[collName] = &memoryCollection{
|
|
data: make([][]byte, 0),
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) DropCollection(dbName, collName string) error {
|
|
db, exists := e.databases[dbName]
|
|
if !exists {
|
|
return fmt.Errorf("database %s does not exist", dbName)
|
|
}
|
|
|
|
if _, collExists := db.collections[collName]; !collExists {
|
|
return fmt.Errorf("collection %s does not exist in database %s", collName, dbName)
|
|
}
|
|
|
|
delete(db.collections, collName)
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) ListCollections(dbName string) ([]string, error) {
|
|
db, exists := e.databases[dbName]
|
|
if !exists {
|
|
return nil, fmt.Errorf("database %s does not exist", dbName)
|
|
}
|
|
|
|
names := make([]string, 0, len(db.collections))
|
|
for name := range db.collections {
|
|
names = append(names, name)
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
func (e *memoryEngine) Insert(dbName, collName string, document []byte) error {
|
|
db, exists := e.databases[dbName]
|
|
if !exists {
|
|
return fmt.Errorf("database %s does not exist", dbName)
|
|
}
|
|
|
|
coll, exists := db.collections[collName]
|
|
if !exists {
|
|
// 自动创建集合
|
|
coll = &memoryCollection{
|
|
data: make([][]byte, 0),
|
|
}
|
|
db.collections[collName] = coll
|
|
}
|
|
|
|
coll.data = append(coll.data, document)
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) Query(dbName, collName string, query []byte) ([][]byte, error) {
|
|
db, exists := e.databases[dbName]
|
|
if !exists {
|
|
return nil, fmt.Errorf("database %s does not exist", dbName)
|
|
}
|
|
|
|
coll, exists := db.collections[collName]
|
|
if !exists {
|
|
return nil, fmt.Errorf("collection %s does not exist in database %s", collName, dbName)
|
|
}
|
|
|
|
// TODO: 实现实际的查询逻辑,目前返回所有文档
|
|
return coll.data, nil
|
|
}
|
|
|
|
func (e *memoryEngine) Update(dbName, collName string, query, update []byte) error {
|
|
// TODO: 实现更新逻辑
|
|
return nil
|
|
}
|
|
|
|
func (e *memoryEngine) Delete(dbName, collName string, query []byte) error {
|
|
// TODO: 实现删除逻辑
|
|
return nil
|
|
}
|
|
|
|
// 内存数据库结构
|
|
type memoryDatabase struct {
|
|
collections map[string]*memoryCollection
|
|
}
|
|
|
|
// 内存集合结构
|
|
type memoryCollection struct {
|
|
data [][]byte
|
|
} |