|
@ -2,8 +2,11 @@ package mongodb |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"context" |
|
|
"context" |
|
|
|
|
|
"fmt" |
|
|
"github.com/chrislusf/seaweedfs/weed/filer2" |
|
|
"github.com/chrislusf/seaweedfs/weed/filer2" |
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" |
|
|
"github.com/chrislusf/seaweedfs/weed/util" |
|
|
"github.com/chrislusf/seaweedfs/weed/util" |
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson" |
|
|
"go.mongodb.org/mongo-driver/mongo" |
|
|
"go.mongodb.org/mongo-driver/mongo" |
|
|
"go.mongodb.org/mongo-driver/mongo/options" |
|
|
"go.mongodb.org/mongo-driver/mongo/options" |
|
|
"time" |
|
|
"time" |
|
@ -15,6 +18,14 @@ func init() { |
|
|
|
|
|
|
|
|
type MongodbStore struct { |
|
|
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 { |
|
|
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) { |
|
|
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")) |
|
|
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) { |
|
|
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 |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (store *MongodbStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) { |
|
|
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) { |
|
|
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 { |
|
|
func (store *MongodbStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error { |
|
|