2020-04-18 23:26:57 +08:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-19 00:48:38 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-04-19 00:48:38 +08:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2020-04-18 23:26:57 +08:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 15:21:19 +08:00
|
|
|
filer.Stores = append(filer.Stores, &MongodbStore{})
|
2020-04-18 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MongodbStore struct {
|
2020-04-19 00:48:38 +08:00
|
|
|
connect *mongo.Client
|
|
|
|
database string
|
|
|
|
collectionName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
Directory string `bson:"directory"`
|
|
|
|
Name string `bson:"name"`
|
|
|
|
Meta []byte `bson:"meta"`
|
2020-04-18 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) GetName() string {
|
|
|
|
return "mongodb"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
2020-04-19 00:48:38 +08:00
|
|
|
store.database = configuration.GetString(prefix + "database")
|
|
|
|
store.collectionName = "filemeta"
|
2020-04-19 18:51:32 +08:00
|
|
|
poolSize := configuration.GetInt(prefix + "option_pool_size")
|
2020-04-21 08:48:06 +08:00
|
|
|
return store.connection(configuration.GetString(prefix+"uri"), uint64(poolSize))
|
2020-04-18 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2020-04-19 18:51:32 +08:00
|
|
|
func (store *MongodbStore) connection(uri string, poolSize uint64) (err error) {
|
2020-04-18 23:26:57 +08:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
2020-04-19 18:51:32 +08:00
|
|
|
opts := options.Client().ApplyURI(uri)
|
|
|
|
|
|
|
|
if poolSize > 0 {
|
|
|
|
opts.SetMaxPoolSize(poolSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := mongo.Connect(ctx, opts)
|
2020-04-20 09:13:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := client.Database(store.database).Collection(store.collectionName)
|
|
|
|
err = store.indexUnique(c)
|
2020-04-18 23:26:57 +08:00
|
|
|
store.connect = client
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:48:06 +08:00
|
|
|
func (store *MongodbStore) createIndex(c *mongo.Collection, index mongo.IndexModel, opts *options.CreateIndexesOptions) error {
|
2020-04-20 09:13:57 +08:00
|
|
|
_, err := c.Indexes().CreateOne(context.Background(), index, opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) indexUnique(c *mongo.Collection) error {
|
|
|
|
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
|
|
|
|
|
|
|
|
unique := new(bool)
|
|
|
|
*unique = true
|
|
|
|
|
|
|
|
index := mongo.IndexModel{
|
2023-07-25 02:09:51 +08:00
|
|
|
Keys: bson.D{{Key: "directory", Value: int32(1)}, {Key: "name", Value: int32(1)}},
|
2020-04-20 09:13:57 +08:00
|
|
|
Options: &options.IndexOptions{
|
|
|
|
Unique: unique,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.createIndex(c, index, opts)
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:57 +08:00
|
|
|
func (store *MongodbStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) CommitTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) RollbackTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *MongodbStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2020-04-19 00:48:38 +08:00
|
|
|
|
2020-09-18 12:50:52 +08:00
|
|
|
return store.UpdateEntry(ctx, entry)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
|
|
|
|
2020-04-19 00:48:38 +08:00
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
meta, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encode %s: %s", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:33:36 +08:00
|
|
|
if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
|
2020-09-04 02:00:20 +08:00
|
|
|
meta = util.MaybeGzipData(meta)
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:48:38 +08:00
|
|
|
c := store.connect.Database(store.database).Collection(store.collectionName)
|
|
|
|
|
2020-09-18 12:50:52 +08:00
|
|
|
opts := options.Update().SetUpsert(true)
|
|
|
|
filter := bson.D{{"directory", dir}, {"name", name}}
|
|
|
|
update := bson.D{{"$set", bson.D{{"meta", meta}}}}
|
|
|
|
|
|
|
|
_, err = c.UpdateOne(ctx, filter, update, opts)
|
2020-04-19 00:48:38 +08:00
|
|
|
|
2020-09-03 08:17:44 +08:00
|
|
|
if err != nil {
|
2020-09-18 12:50:52 +08:00
|
|
|
return fmt.Errorf("UpdateEntry %s: %v", entry.FullPath, err)
|
2020-09-03 08:17:44 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:57 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *MongodbStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
2020-04-19 00:48:38 +08:00
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
var data Model
|
|
|
|
|
2020-04-19 01:16:50 +08:00
|
|
|
var where = bson.M{"directory": dir, "name": name}
|
2020-04-19 00:48:38 +08:00
|
|
|
err = store.connect.Database(store.database).Collection(store.collectionName).FindOne(ctx, where).Decode(&data)
|
|
|
|
if err != mongo.ErrNoDocuments && err != nil {
|
2020-09-03 12:42:12 +08:00
|
|
|
glog.Errorf("find %s: %v", fullpath, err)
|
2020-04-19 00:48:38 +08:00
|
|
|
return nil, filer_pb.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data.Meta) == 0 {
|
|
|
|
return nil, filer_pb.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
entry = &filer.Entry{
|
2020-04-19 00:48:38 +08:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
|
|
|
|
2020-09-04 02:00:20 +08:00
|
|
|
err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data.Meta))
|
2020-04-19 00:48:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
2020-04-18 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
|
|
|
|
|
2020-04-19 01:31:49 +08:00
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
|
|
|
|
where := bson.M{"directory": dir, "name": name}
|
2022-02-20 01:54:09 +08:00
|
|
|
_, err := store.connect.Database(store.database).Collection(store.collectionName).DeleteMany(ctx, where)
|
2020-04-19 01:20:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:57 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 23:23:20 +08:00
|
|
|
func (store *MongodbStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
|
2020-04-18 23:26:57 +08:00
|
|
|
|
2020-04-19 01:31:49 +08:00
|
|
|
where := bson.M{"directory": fullpath}
|
2020-05-07 18:11:26 +08:00
|
|
|
_, err := store.connect.Database(store.database).Collection(store.collectionName).DeleteMany(ctx, where)
|
2020-04-19 01:31:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:26:57 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
func (store *MongodbStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
|
2020-08-06 01:19:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
func (store *MongodbStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2020-04-18 23:26:57 +08:00
|
|
|
|
2021-01-15 14:28:51 +08:00
|
|
|
var where = bson.M{"directory": string(dirPath), "name": bson.M{"$gt": startFileName}}
|
|
|
|
if includeStartFile {
|
2020-04-19 01:16:50 +08:00
|
|
|
where["name"] = bson.M{
|
|
|
|
"$gte": startFileName,
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 15:56:24 +08:00
|
|
|
optLimit := int64(limit)
|
2020-04-21 08:48:06 +08:00
|
|
|
opts := &options.FindOptions{Limit: &optLimit, Sort: bson.M{"name": 1}}
|
2020-04-19 18:51:32 +08:00
|
|
|
cur, err := store.connect.Database(store.database).Collection(store.collectionName).Find(ctx, where, opts)
|
2021-12-03 18:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return lastFileName, fmt.Errorf("failed to list directory entries: find error: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-19 01:16:50 +08:00
|
|
|
for cur.Next(ctx) {
|
|
|
|
var data Model
|
2022-02-16 08:15:44 +08:00
|
|
|
err = cur.Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
break
|
2020-04-19 01:16:50 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
entry := &filer.Entry{
|
2021-01-15 14:28:51 +08:00
|
|
|
FullPath: util.NewFullPath(string(dirPath), data.Name),
|
2020-04-19 01:16:50 +08:00
|
|
|
}
|
2021-01-16 15:56:24 +08:00
|
|
|
lastFileName = data.Name
|
2020-09-04 02:00:20 +08:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data.Meta)); decodeErr != nil {
|
2020-04-19 01:16:50 +08:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2020-04-19 01:16:50 +08:00
|
|
|
|
2021-01-15 14:28:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-19 01:16:50 +08:00
|
|
|
if err := cur.Close(ctx); err != nil {
|
|
|
|
glog.V(0).Infof("list iterator close: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
return lastFileName, err
|
2020-04-18 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MongodbStore) Shutdown() {
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
store.connect.Disconnect(ctx)
|
|
|
|
}
|