fix default index store
This commit is contained in:
parent
a2fbb970c6
commit
a07d62577c
|
@ -3,19 +3,20 @@ package document
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"strings"
|
"strings"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"sync"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"git.pyer.club/kingecg/godocdb/index"
|
"git.pyer.club/kingecg/godocdb/index"
|
||||||
"git.pyer.club/kingecg/godocdb/storage"
|
"git.pyer.club/kingecg/godocdb/storage"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DocumentStore 管理带命名空间的文档存储
|
// DocumentStore 管理带命名空间的文档存储
|
||||||
type DocumentStore struct {
|
type DocumentStore struct {
|
||||||
storage *storage.LevelDBStorage
|
storage *storage.LevelDBStorage
|
||||||
indexStore *index.IndexStore // 添加索引存储依赖
|
indexStore *index.IndexStore // 添加索引存储依赖
|
||||||
mu sync.RWMutex // 保护并发访问
|
mu sync.RWMutex // 保护并发访问
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDocumentStore 创建新的文档存储实例
|
// NewDocumentStore 创建新的文档存储实例
|
||||||
|
@ -36,6 +37,9 @@ func NewDocumentStore(path string) (*DocumentStore, error) {
|
||||||
storage.Close()
|
storage.Close()
|
||||||
return nil, fmt.Errorf("failed to create index store: %v", err)
|
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{
|
return &DocumentStore{
|
||||||
storage: storage,
|
storage: storage,
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"bytes"
|
"sync"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"git.pyer.club/kingecg/godocdb/storage"
|
"git.pyer.club/kingecg/godocdb/storage"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IndexType 索引类型
|
// IndexType 索引类型
|
||||||
|
@ -45,6 +46,13 @@ type IndexStore struct {
|
||||||
// NewIndexStore 创建新的索引存储实例
|
// NewIndexStore 创建新的索引存储实例
|
||||||
func NewIndexStore(path string) (*IndexStore, error) {
|
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 {
|
if err := os.MkdirAll(path, 0755); err != nil {
|
||||||
return nil, fmt.Errorf("failed to create storage directory: %v", err)
|
return nil, fmt.Errorf("failed to create storage directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ func TestIndexStore(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if metadata.Name != indexName || metadata.Type != NonUnique {
|
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 {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证每个字段的排序方式
|
// 验证每个字段的排序方式
|
||||||
|
|
Loading…
Reference in New Issue