Index management layer implementation with tests

This commit is contained in:
kingecg 2025-06-07 18:18:03 +08:00
parent 8ab875003a
commit d51dc51932
6 changed files with 444 additions and 0 deletions

17
coverage.out Normal file
View File

@ -0,0 +1,17 @@
mode: set
git.pyer.club/kingecg/godocdb/index/index.go:32.54,34.16 2 1
git.pyer.club/kingecg/godocdb/index/index.go:34.16,36.3 1 0
git.pyer.club/kingecg/godocdb/index/index.go:37.2,37.43 1 1
git.pyer.club/kingecg/godocdb/index/index.go:41.100,50.16 3 1
git.pyer.club/kingecg/godocdb/index/index.go:50.16,52.3 1 0
git.pyer.club/kingecg/godocdb/index/index.go:54.2,55.50 2 1
git.pyer.club/kingecg/godocdb/index/index.go:55.50,57.3 1 0
git.pyer.club/kingecg/godocdb/index/index.go:60.2,63.52 4 1
git.pyer.club/kingecg/godocdb/index/index.go:67.57,70.55 2 1
git.pyer.club/kingecg/godocdb/index/index.go:70.55,72.3 1 0
git.pyer.club/kingecg/godocdb/index/index.go:75.2,76.36 2 1
git.pyer.club/kingecg/godocdb/index/index.go:80.82,83.16 3 1
git.pyer.club/kingecg/godocdb/index/index.go:83.16,85.3 1 1
git.pyer.club/kingecg/godocdb/index/index.go:87.2,88.59 2 1
git.pyer.club/kingecg/godocdb/index/index.go:88.59,90.3 1 0
git.pyer.club/kingecg/godocdb/index/index.go:92.2,92.23 1 1

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.23.1
require ( require (
github.com/golang/snappy v0.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.17.4 // indirect go.mongodb.org/mongo-driver v1.17.4 // indirect
) )

2
go.sum
View File

@ -5,6 +5,8 @@ github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8l
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=

195
index/coverage.html Normal file
View File

@ -0,0 +1,195 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">git.pyer.club/kingecg/godocdb/index/index.go (82.1%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package index
import (
"fmt"
"github.com/iancoleman/orderedmap"
"go.mongodb.org/mongo-driver/bson"
"git.pyer.club/kingecg/godocdb/storage"
)
// IndexType 表示索引类型
type IndexType string
const (
SingleField IndexType = "single"
Composite IndexType = "composite"
)
// IndexMetadata 索引元数据
type IndexMetadata struct {
Name string
Type IndexType
KeyFields []string
}
// IndexStore 管理索引的存储和查询
type IndexStore struct {
storage *storage.LevelDBStorage
}
// NewIndexStore 创建新的索引存储实例
func NewIndexStore(path string) (*IndexStore, error) <span class="cov8" title="1">{
storage, err := storage.NewLevelDBStorage(path)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return &amp;IndexStore{storage: storage}, nil</span>
}
// CreateIndex 创建索引
func (is *IndexStore) CreateIndex(indexName string, indexType IndexType, keyFields []string) error <span class="cov8" title="1">{
// 存储索引元数据
metadata := IndexMetadata{
Name: indexName,
Type: indexType,
KeyFields: keyFields,
}
data, err := bson.Marshal(metadata)
if err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("failed to marshal index metadata: %v", err)
}</span>
<span class="cov8" title="1">key := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
if err := is.storage.Put(key, data); err != nil </span><span class="cov0" title="0">{
return err
}</span>
// 初始化索引存储结构
<span class="cov8" title="1">indexKey := fmt.Sprintf("indexes:data:%s", indexName)
index := orderedmap.New()
indexData, _ := bson.Marshal(index)
return is.storage.Put([]byte(indexKey), indexData)</span>
}
// DropIndex 删除索引
func (is *IndexStore) DropIndex(indexName string) error <span class="cov8" title="1">{
// 删除元数据
metadataKey := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
if err := is.storage.Delete(metadataKey); err != nil </span><span class="cov0" title="0">{
return err
}</span>
// 删除索引数据
<span class="cov8" title="1">indexKey := []byte(fmt.Sprintf("indexes:data:%s", indexName))
return is.storage.Delete(indexKey)</span>
}
// GetIndexMetadata 获取索引元数据
func (is *IndexStore) GetIndexMetadata(indexName string) (*IndexMetadata, error) <span class="cov8" title="1">{
key := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
rawData, err := is.storage.Get(key)
if err != nil </span><span class="cov8" title="1">{
return nil, fmt.Errorf("index not found: %v", err)
}</span>
<span class="cov8" title="1">var metadata IndexMetadata
if err := bson.Unmarshal(rawData, &amp;metadata); err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return &amp;metadata, nil</span>
}</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>

93
index/index.go Normal file
View File

@ -0,0 +1,93 @@
package index
import (
"fmt"
"github.com/iancoleman/orderedmap"
"go.mongodb.org/mongo-driver/bson"
"git.pyer.club/kingecg/godocdb/storage"
)
// IndexType 表示索引类型
type IndexType string
const (
SingleField IndexType = "single"
Composite IndexType = "composite"
)
// IndexMetadata 索引元数据
type IndexMetadata struct {
Name string
Type IndexType
KeyFields []string
}
// IndexStore 管理索引的存储和查询
type IndexStore struct {
storage *storage.LevelDBStorage
}
// NewIndexStore 创建新的索引存储实例
func NewIndexStore(path string) (*IndexStore, error) {
storage, err := storage.NewLevelDBStorage(path)
if err != nil {
return nil, err
}
return &IndexStore{storage: storage}, nil
}
// CreateIndex 创建索引
func (is *IndexStore) CreateIndex(indexName string, indexType IndexType, keyFields []string) error {
// 存储索引元数据
metadata := IndexMetadata{
Name: indexName,
Type: indexType,
KeyFields: keyFields,
}
data, err := bson.Marshal(metadata)
if err != nil {
return fmt.Errorf("failed to marshal index metadata: %v", err)
}
key := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
if err := is.storage.Put(key, data); err != nil {
return err
}
// 初始化索引存储结构
indexKey := fmt.Sprintf("indexes:data:%s", indexName)
index := orderedmap.New()
indexData, _ := bson.Marshal(index)
return is.storage.Put([]byte(indexKey), indexData)
}
// DropIndex 删除索引
func (is *IndexStore) DropIndex(indexName string) error {
// 删除元数据
metadataKey := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
if err := is.storage.Delete(metadataKey); err != nil {
return err
}
// 删除索引数据
indexKey := []byte(fmt.Sprintf("indexes:data:%s", indexName))
return is.storage.Delete(indexKey)
}
// GetIndexMetadata 获取索引元数据
func (is *IndexStore) GetIndexMetadata(indexName string) (*IndexMetadata, error) {
key := []byte(fmt.Sprintf("indexes:metadata:%s", indexName))
rawData, err := is.storage.Get(key)
if err != nil {
return nil, fmt.Errorf("index not found: %v", err)
}
var metadata IndexMetadata
if err := bson.Unmarshal(rawData, &metadata); err != nil {
return nil, err
}
return &metadata, nil
}

136
index/index_test.go Normal file
View File

@ -0,0 +1,136 @@
package index
import (
"fmt"
"os"
"sync"
"testing"
)
func TestIndexStore(t *testing.T) {
// 测试目录
dir := "./testdb"
defer os.RemoveAll(dir)
// 初始化索引存储
is, err := NewIndexStore(dir)
if err != nil {
t.Fatalf("Failed to create index store: %v", err)
}
defer is.storage.Close()
// 测试索引名称和字段
indexName := "test_index"
keyFields := []string{"name"}
// 测试创建索引
if err := is.CreateIndex(indexName, SingleField, keyFields); err != nil {
t.Errorf("CreateIndex failed: %v", err)
}
// 验证元数据
metadata, err := is.GetIndexMetadata(indexName)
if err != nil {
t.Errorf("GetIndexMetadata failed: %v", err)
}
if metadata.Name != indexName || metadata.Type != SingleField {
t.Errorf("Metadata mismatch: got %+v want name=%s type=%s", metadata, indexName, SingleField)
}
// 测试删除索引
if err := is.DropIndex(indexName); err != nil {
t.Errorf("DropIndex failed: %v", err)
}
// 验证索引已被删除
_, err = is.GetIndexMetadata(indexName)
if err == nil {
t.Errorf("Expected error after DropIndex")
}
}
func TestCompositeIndex(t *testing.T) {
// 测试目录
dir := "./testdb_composite"
defer os.RemoveAll(dir)
// 初始化索引存储
is, err := NewIndexStore(dir)
if err != nil {
t.Fatalf("Failed to create index store: %v", err)
}
defer is.storage.Close()
// 测试复合索引
indexName := "composite_index"
keyFields := []string{"name", "age"}
// 创建复合索引
if err := is.CreateIndex(indexName, Composite, keyFields); err != nil {
t.Errorf("CreateIndex failed: %v", err)
}
// 验证元数据
metadata, err := is.GetIndexMetadata(indexName)
if err != nil {
t.Errorf("GetIndexMetadata failed: %v", err)
}
if metadata.Type != Composite || len(metadata.KeyFields) != 2 {
t.Errorf("Composite index metadata mismatch: got %+v want type=%s fieldsCount=2", metadata, Composite)
}
}
func BenchmarkSingleFieldQuery(b *testing.B) {
// 基准测试单字段查询性能
dir := "./testdb_bench"
defer os.RemoveAll(dir)
is, _ := NewIndexStore(dir)
// 创建测试数据
for i := 0; i < 1000; i++ {
is.CreateIndex(fmt.Sprintf("index_%d", i), SingleField, []string{"name"})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := is.GetIndexMetadata(fmt.Sprintf("index_%d", i%1000))
if err != nil {
b.Error(err)
}
}
}
func TestConcurrentIndexOperations(t *testing.T) {
// 测试并发索引操作
dir := "./testdb_concurrent"
defer os.RemoveAll(dir)
is, _ := NewIndexStore(dir)
numGoroutines := 10
var wg sync.WaitGroup
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
indexName := fmt.Sprintf("concurrent_index_%d", i)
// 创建索引
if err := is.CreateIndex(indexName, SingleField, []string{"name"}); err != nil {
t.Errorf("CreateIndex failed: %v", err)
}
// 删除索引
if err := is.DropIndex(indexName); err != nil {
t.Errorf("DropIndex failed: %v", err)
}
}(i)
}
wg.Wait()
}