-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from basenana/feature/document
add document manager & collect doc
- Loading branch information
Showing
22 changed files
with
685 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2023 NanaFS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package document | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
|
||
"github.com/basenana/nanafs/pkg/metastore" | ||
"github.com/basenana/nanafs/pkg/types" | ||
"github.com/basenana/nanafs/utils/logger" | ||
) | ||
|
||
type Manager interface { | ||
ListDocuments(ctx context.Context) ([]*types.Document, error) | ||
CreateDocument(ctx context.Context, doc *types.Document) error | ||
UpdateDocument(ctx context.Context, doc *types.Document) error | ||
GetDocument(ctx context.Context, id string) (*types.Document, error) | ||
DeleteDocument(ctx context.Context, id string) error | ||
} | ||
|
||
type manager struct { | ||
logger *zap.SugaredLogger | ||
recorder metastore.DocumentRecorder | ||
} | ||
|
||
var _ Manager = &manager{} | ||
|
||
func NewManager(recorder metastore.DocumentRecorder) (Manager, error) { | ||
docLogger := logger.NewLogger("document") | ||
return &manager{ | ||
logger: docLogger, | ||
recorder: recorder, | ||
}, nil | ||
} | ||
|
||
func (m *manager) ListDocuments(ctx context.Context) ([]*types.Document, error) { | ||
result, err := m.recorder.ListDocument(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} | ||
|
||
func (m *manager) CreateDocument(ctx context.Context, doc *types.Document) error { | ||
if doc.ID == "" { | ||
doc.ID = uuid.New().String() | ||
} | ||
doc.CreatedAt = time.Now() | ||
doc.ChangedAt = time.Now() | ||
if err := m.recorder.SaveDocument(ctx, doc); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m *manager) UpdateDocument(ctx context.Context, doc *types.Document) error { | ||
doc.ChangedAt = time.Now() | ||
if err := m.recorder.SaveDocument(ctx, doc); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m *manager) GetDocument(ctx context.Context, id string) (*types.Document, error) { | ||
return m.recorder.GetDocument(ctx, id) | ||
} | ||
|
||
func (m *manager) DeleteDocument(ctx context.Context, id string) error { | ||
return m.recorder.DeleteDocument(ctx, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2023 NanaFS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package document | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/basenana/nanafs/pkg/types" | ||
) | ||
|
||
var _ = Describe("testDocumentManage", func() { | ||
var docId string | ||
Context("create document", func() { | ||
It("create should be succeed", func() { | ||
doc := &types.Document{ | ||
Name: "test_create_doc", | ||
} | ||
err := docManager.CreateDocument(context.TODO(), doc) | ||
Expect(err).Should(BeNil()) | ||
docId = doc.ID | ||
}) | ||
It("query should be succeed", func() { | ||
doc, err := docManager.GetDocument(context.TODO(), docId) | ||
Expect(err).Should(BeNil()) | ||
Expect(doc.Name).Should(Equal("test_create_doc")) | ||
}) | ||
}) | ||
Context("update document", func() { | ||
It("update should be succeed", func() { | ||
err := docManager.UpdateDocument(context.TODO(), &types.Document{ | ||
ID: docId, | ||
Name: "test_create_doc", | ||
Content: "abc", | ||
}) | ||
Expect(err).Should(BeNil()) | ||
doc, err := docManager.GetDocument(context.TODO(), docId) | ||
Expect(err).Should(BeNil()) | ||
Expect(doc.Content).Should(Equal("abc")) | ||
}) | ||
}) | ||
Context("delete document", func() { | ||
It("delete should be succeed", func() { | ||
err := docManager.DeleteDocument(context.TODO(), docId) | ||
Expect(err).Should(BeNil()) | ||
doc, err := docManager.GetDocument(context.TODO(), docId) | ||
Expect(err).ShouldNot(BeNil()) | ||
Expect(doc).Should(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2023 NanaFS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package document | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/basenana/nanafs/config" | ||
"github.com/basenana/nanafs/pkg/metastore" | ||
"github.com/basenana/nanafs/pkg/plugin" | ||
"github.com/basenana/nanafs/pkg/storage" | ||
"github.com/basenana/nanafs/pkg/types" | ||
"github.com/basenana/nanafs/utils/logger" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var ( | ||
docRecorder metastore.DocumentRecorder | ||
docManager Manager | ||
root *types.Metadata | ||
|
||
workdir string | ||
) | ||
|
||
func TestDEntry(t *testing.T) { | ||
logger.InitLogger() | ||
defer logger.Sync() | ||
RegisterFailHandler(Fail) | ||
|
||
var err error | ||
workdir, err = os.MkdirTemp(os.TempDir(), "ut-nanafs-dentry-") | ||
Expect(err).Should(BeNil()) | ||
t.Logf("unit test workdir on: %s", workdir) | ||
storage.InitLocalCache(config.Config{CacheDir: workdir, CacheSize: 1}) | ||
|
||
RunSpecs(t, "DEntry Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
memMeta, err := metastore.NewMetaStorage(storage.MemoryStorage, config.Meta{}) | ||
Expect(err).Should(BeNil()) | ||
docRecorder = memMeta | ||
docManager, _ = NewManager(docRecorder) | ||
|
||
// init plugin | ||
err = plugin.Init(&config.Plugin{}, memMeta) | ||
Expect(err).Should(BeNil()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.