filer mongodb insert entry and find entry

This commit is contained in:
bukton 2020-04-18 23:48:38 +07:00
parent cd9cccec55
commit 3c70163798
2 changed files with 57 additions and 4 deletions

2
go.mod
View File

@ -75,7 +75,7 @@ require (
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
go.etcd.io/etcd v3.3.15+incompatible
go.mongodb.org/mongo-driver v1.3.2 // indirect
go.mongodb.org/mongo-driver v1.3.2
go.uber.org/multierr v1.2.0 // indirect
gocloud.dev v0.16.0
gocloud.dev/pubsub/natspubsub v0.16.0

View File

@ -2,8 +2,11 @@ package mongodb
import (
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
@ -14,7 +17,15 @@ func init() {
}
type MongodbStore struct {
connect *mongo.Client
connect *mongo.Client
database string
collectionName string
}
type Model struct {
Directory string `bson:"directory"`
Name string `bson:"name"`
Meta []byte `bson:"meta"`
}
func (store *MongodbStore) GetName() string {
@ -22,6 +33,8 @@ func (store *MongodbStore) GetName() string {
}
func (store *MongodbStore) Initialize(configuration util.Configuration, prefix string) (err error) {
store.database = configuration.GetString(prefix + "database")
store.collectionName = "filemeta"
return store.connection(configuration.GetString(prefix + "uri"))
}
@ -45,15 +58,55 @@ func (store *MongodbStore) RollbackTransaction(ctx context.Context) error {
}
func (store *MongodbStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
dir, name := entry.FullPath.DirAndName()
meta, err := entry.EncodeAttributesAndChunks()
if err != nil {
return fmt.Errorf("encode %s: %s", entry.FullPath, err)
}
c := store.connect.Database(store.database).Collection(store.collectionName)
_, err = c.InsertOne(ctx, Model{
Directory: dir,
Name: name,
Meta: meta,
})
fmt.Println(err)
return nil
}
func (store *MongodbStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
return nil
return store.UpdateEntry(ctx, entry)
}
func (store *MongodbStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
return nil, nil
dir, name := fullpath.DirAndName()
var data Model
var where = bson.M{ "directory": dir, "name": name }
err = store.connect.Database(store.database).Collection(store.collectionName).FindOne(ctx, where).Decode(&data)
if err != mongo.ErrNoDocuments && err != nil {
return nil, filer_pb.ErrNotFound
}
if len(data.Meta) == 0 {
return nil, filer_pb.ErrNotFound
}
entry = &filer2.Entry{
FullPath: fullpath,
}
err = entry.DecodeAttributesAndChunks(data.Meta)
if err != nil {
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
}
return entry, nil
}
func (store *MongodbStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {