From 35c27f7cc74f551372b539e6ab538db01c7f67c3 Mon Sep 17 00:00:00 2001 From: kingecg Date: Sat, 7 Jun 2025 17:56:45 +0800 Subject: [PATCH] Document management layer implementation with tests --- document/document.go | 53 +++++++++++++++++++++++++++++++++++ document/document_test.go | 59 +++++++++++++++++++++++++++++++++++++++ go.mod | 3 +- go.sum | 4 +++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 document/document.go create mode 100644 document/document_test.go diff --git a/document/document.go b/document/document.go new file mode 100644 index 0000000..27d09bc --- /dev/null +++ b/document/document.go @@ -0,0 +1,53 @@ +package document + +import ( + "fmt" + + "go.mongodb.org/mongo-driver/bson" + "git.pyer.club/kingecg/godocdb/storage" +) + +// DocumentStore 管理文档的存储和检索 +type DocumentStore struct { + storage *storage.LevelDBStorage +} + +// NewDocumentStore 创建新的文档存储实例 +func NewDocumentStore(path string) (*DocumentStore, error) { + storage, err := storage.NewLevelDBStorage(path) + if err != nil { + return nil, err + } + return &DocumentStore{storage: storage}, nil +} + +// StoreDocument 存储文档 +func (ds *DocumentStore) StoreDocument(id string, doc interface{}) error { + // 使用BSON序列化文档 + data, err := bson.Marshal(doc) + if err != nil { + return fmt.Errorf("failed to marshal document: %v", err) + } + + // 存储文档(格式: collection:id → BSON数据) + key := []byte(fmt.Sprintf("documents:%s", id)) + return ds.storage.Put(key, data) +} + +// GetDocument 获取文档 +func (ds *DocumentStore) GetDocument(id string, result interface{}) error { + key := []byte(fmt.Sprintf("documents:%s", id)) + data, err := ds.storage.Get(key) + if err != nil { + return fmt.Errorf("document not found: %v", err) + } + + // 使用BSON反序列化文档 + return bson.Unmarshal(data, result) +} + +// DeleteDocument 删除文档 +func (ds *DocumentStore) DeleteDocument(id string) error { + key := []byte(fmt.Sprintf("documents:%s", id)) + return ds.storage.Delete(key) +} \ No newline at end of file diff --git a/document/document_test.go b/document/document_test.go new file mode 100644 index 0000000..28f1090 --- /dev/null +++ b/document/document_test.go @@ -0,0 +1,59 @@ +package document + +import ( + "os" + "testing" +) + +// 测试用结构体 +type TestDoc struct { + ID string `bson:"_id"` + Name string `bson:"name"` + Age int `bson:"age"` +} + +func TestDocumentStore(t *testing.T) { + // 测试目录 + dir := "./testdb" + defer os.RemoveAll(dir) + + // 初始化文档存储 + ds, err := NewDocumentStore(dir) + if err != nil { + t.Fatalf("Failed to create document store: %v", err) + } + defer ds.storage.Close() + + // 测试文档ID + testID := "001" + + // 测试文档内容 + testDoc := TestDoc{ + ID: testID, + Name: "Alice", + Age: 30, + } + + // 测试存储和获取 + if err := ds.StoreDocument(testID, testDoc); err != nil { + t.Errorf("StoreDocument failed: %v", err) + } + + var result TestDoc + if err := ds.GetDocument(testID, &result); err != nil { + t.Errorf("GetDocument failed: %v", err) + } + + if result.Name != testDoc.Name || result.Age != testDoc.Age { + t.Errorf("Retrieved document mismatch: got %+v want %+v", result, testDoc) + } + + // 测试删除功能 + if err := ds.DeleteDocument(testID); err != nil { + t.Errorf("DeleteDocument failed: %v", err) + } + + if err := ds.GetDocument(testID, &result); err == nil { + t.Errorf("Expected error after DeleteDocument") + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index c4f6464..a1cce25 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.pyer.club/kingecg/godocdb go 1.23.1 require ( - github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect + github.com/golang/snappy v0.0.4 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect + go.mongodb.org/mongo-driver v1.17.4 // indirect ) diff --git a/go.sum b/go.sum index e188042..af16e48 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,16 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +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/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/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw= +go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=