2025-06-07 22:28:29 +08:00
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2025-06-08 14:07:38 +08:00
|
|
|
|
"fmt"
|
2025-06-07 22:28:29 +08:00
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"git.pyer.club/kingecg/godocdb/document"
|
2025-06-07 23:07:29 +08:00
|
|
|
|
"git.pyer.club/kingecg/godocdb/index" // "fmt"
|
2025-06-08 14:07:38 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2025-06-07 22:28:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
2025-06-07 23:07:29 +08:00
|
|
|
|
// "go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
|
2025-06-07 22:28:29 +08:00
|
|
|
|
// Collection MongoDB兼容的集合结构
|
|
|
|
|
type Collection struct {
|
|
|
|
|
name string
|
|
|
|
|
indexStore *index.IndexStore
|
|
|
|
|
documentStore *document.DocumentStore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2025-06-07 23:07:29 +08:00
|
|
|
|
indexStores = make(map[string]*index.IndexStore)
|
|
|
|
|
indexStoreMu sync.RWMutex
|
2025-06-07 22:28:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewCollection 创建新的集合实例
|
|
|
|
|
func NewCollection(name string, storagePath string) (*Collection, error) {
|
|
|
|
|
ds, err := document.NewDocumentStore(storagePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is, err := getIndexStore(storagePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Collection{
|
|
|
|
|
name: name,
|
|
|
|
|
documentStore: ds,
|
|
|
|
|
indexStore: is,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertOne 插入单个文档
|
|
|
|
|
func (coll *Collection) InsertOne(doc interface{}) error {
|
|
|
|
|
// 自动生成文档ID
|
2025-06-07 23:07:29 +08:00
|
|
|
|
// docID := generateID()
|
2025-06-08 14:07:38 +08:00
|
|
|
|
docMap, ok := doc.(map[string]interface{})
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("document must be a map[string]interface{}")
|
|
|
|
|
}
|
|
|
|
|
docID, exists := docMap["_id"].(primitive.ObjectID)
|
|
|
|
|
if !exists {
|
|
|
|
|
docID = primitive.NewObjectID()
|
|
|
|
|
docMap["_id"] = docID
|
|
|
|
|
}
|
2025-06-07 22:28:29 +08:00
|
|
|
|
|
|
|
|
|
// 将collection信息传递给文档管理层和索引管理层
|
2025-06-08 14:07:38 +08:00
|
|
|
|
if err := coll.documentStore.StoreDocument(coll.name, docID.String(), doc); err != nil {
|
2025-06-07 22:28:29 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建相关索引(需要实现generateID)
|
|
|
|
|
return coll.createRelatedIndexes(doc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find 查询文档
|
|
|
|
|
func (coll *Collection) Find(filter interface{}) (*Cursor, error) {
|
|
|
|
|
// 使用collection信息进行索引查询
|
|
|
|
|
fieldValue := extractFilterValue(filter) // 需要实现过滤器解析
|
|
|
|
|
docIDs, err := coll.indexStore.GetIndexedDocuments(coll.name, "default_index", fieldValue)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取实际文档数据
|
|
|
|
|
docs := make([]interface{}, 0, len(docIDs))
|
|
|
|
|
for _, id := range docIDs {
|
|
|
|
|
var doc interface{}
|
|
|
|
|
if err := coll.documentStore.GetDocument(coll.name, id, &doc); err == nil {
|
|
|
|
|
docs = append(docs, doc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newCursor(docs), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 以下为简化实现,需要补充完整
|
|
|
|
|
|
|
|
|
|
func (coll *Collection) createRelatedIndexes(doc interface{}) error {
|
|
|
|
|
// 实现自动创建相关索引(根据注解或配置)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getIndexStore(path string) (*index.IndexStore, error) {
|
|
|
|
|
indexStoreMu.RLock()
|
|
|
|
|
if is, ok := indexStores[path]; ok {
|
|
|
|
|
indexStoreMu.RUnlock()
|
|
|
|
|
return is, nil
|
|
|
|
|
}
|
|
|
|
|
indexStoreMu.RUnlock()
|
|
|
|
|
|
|
|
|
|
indexStoreMu.Lock()
|
|
|
|
|
defer indexStoreMu.Unlock()
|
|
|
|
|
|
|
|
|
|
// Double-check in case it was added before acquiring the lock
|
|
|
|
|
if is, ok := indexStores[path]; ok {
|
|
|
|
|
return is, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is, err := index.NewIndexStore(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexStores[path] = is
|
|
|
|
|
return is, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateID() string {
|
|
|
|
|
// 生成唯一文档ID
|
|
|
|
|
return "doc_123456"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func extractFilterValue(filter interface{}) interface{} {
|
|
|
|
|
// 解析过滤条件获取字段值
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cursor 游标用于遍历查询结果
|
|
|
|
|
type Cursor struct {
|
|
|
|
|
// ... 实现细节 ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCursor(docs []interface{}) *Cursor {
|
|
|
|
|
// 创建新的游标实例
|
|
|
|
|
return &Cursor{}
|
|
|
|
|
}
|