fix default index store

This commit is contained in:
kingecg 2025-06-07 23:02:49 +08:00
parent a2fbb970c6
commit a07d62577c
3 changed files with 23 additions and 11 deletions

View File

@ -3,19 +3,20 @@ package document
import (
"fmt"
"os"
"sync"
"strings"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/bson"
"sync"
"git.pyer.club/kingecg/godocdb/index"
"git.pyer.club/kingecg/godocdb/storage"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// DocumentStore 管理带命名空间的文档存储
type DocumentStore struct {
storage *storage.LevelDBStorage
indexStore *index.IndexStore // 添加索引存储依赖
mu sync.RWMutex // 保护并发访问
mu sync.RWMutex // 保护并发访问
}
// NewDocumentStore 创建新的文档存储实例
@ -36,6 +37,9 @@ func NewDocumentStore(path string) (*DocumentStore, error) {
storage.Close()
return nil, fmt.Errorf("failed to create index store: %v", err)
}
if errCreate := is.CreateIndex("default_index", index.NonUnique, []string{"_id"}, []index.IndexSortOrder{index.Ascending}); errCreate != nil {
return nil, errCreate
}
return &DocumentStore{
storage: storage,

View File

@ -1,13 +1,14 @@
package index
import (
"bytes"
"fmt"
"sync"
"os"
"strings"
"bytes"
"go.mongodb.org/mongo-driver/bson"
"sync"
"git.pyer.club/kingecg/godocdb/storage"
"go.mongodb.org/mongo-driver/bson"
)
// IndexType 索引类型
@ -45,6 +46,13 @@ type IndexStore struct {
// NewIndexStore 创建新的索引存储实例
func NewIndexStore(path string) (*IndexStore, error) {
// 确保目录存在
if strings.HasPrefix(path, "/") {
path = strings.TrimSuffix(path, "/")
}
if !strings.HasSuffix(path, "_index") {
// add _index suffix to avoid name confilict with document store, do remove this
path += "_index"
}
if err := os.MkdirAll(path, 0755); err != nil {
return nil, fmt.Errorf("failed to create storage directory: %v", err)
}

View File

@ -35,7 +35,7 @@ func TestIndexStore(t *testing.T) {
}
if metadata.Name != indexName || metadata.Type != NonUnique {
t.Errorf("Metadata mismatch: got %+v want name=%s type=%s", metadata, indexName, NonUnique)
t.Errorf("Metadata mismatch: got %+v want name=%s type=%d", metadata, indexName, NonUnique)
}
// 测试删除索引
@ -78,7 +78,7 @@ func TestCompositeIndex(t *testing.T) {
}
if metadata.Type != NonUnique || len(metadata.KeyFields) != 2 {
t.Errorf("Composite index metadata mismatch: got %+v want type=%s fieldsCount=2", metadata, NonUnique)
t.Errorf("Composite index metadata mismatch: got %+v want type=%d fieldsCount=2", metadata, NonUnique)
}
}
@ -158,7 +158,7 @@ func TestCompositeIndexSortOrders(t *testing.T) {
}
if metadata.Type != NonUnique || len(metadata.KeyFields) != 2 {
t.Errorf("Composite index metadata mismatch: got %+v want type=%s fieldsCount=2", metadata, NonUnique)
t.Errorf("Composite index metadata mismatch: got %+v want type=%d fieldsCount=2", metadata, NonUnique)
}
// 验证每个字段的排序方式