From 3c70163798a88580988169058ff528eeea95023b Mon Sep 17 00:00:00 2001 From: bukton Date: Sat, 18 Apr 2020 23:48:38 +0700 Subject: [PATCH] filer mongodb insert entry and find entry --- go.mod | 2 +- weed/filer2/mongodb/mongodb_store.go | 59 ++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index f12b58d21..2efbec9e5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/weed/filer2/mongodb/mongodb_store.go b/weed/filer2/mongodb/mongodb_store.go index 75df85172..ae76c0bf1 100644 --- a/weed/filer2/mongodb/mongodb_store.go +++ b/weed/filer2/mongodb/mongodb_store.go @@ -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 {