oc-lib/dbs/mongo/mongo.go

348 lines
10 KiB
Go
Raw Normal View History

2024-07-17 18:02:30 +02:00
package mongo
import (
"context"
"errors"
"time"
2024-07-18 13:35:14 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
2024-08-13 09:49:42 +02:00
"cloud.o-forge.io/core/oc-lib/static"
2024-07-18 13:35:14 +02:00
"github.com/rs/zerolog"
2024-07-17 18:02:30 +02:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
2024-08-01 11:31:50 +02:00
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
cancel context.CancelFunc
isConnected bool
2024-07-18 11:51:12 +02:00
existingCollections []string
2024-07-31 16:06:47 +02:00
mngoCollections []string
mngoConfig MongoConf
ResourceMap map[string]interface{}
2024-07-17 18:02:30 +02:00
)
2024-07-18 13:35:14 +02:00
var MONGOService = MongoDB{}
2024-07-18 14:11:13 +02:00
type MongoConf interface {
GetUrl() string
GetDatabase() string
}
2024-07-18 13:35:14 +02:00
type MongoDB struct {
Logger zerolog.Logger
}
2024-07-18 14:11:13 +02:00
func (m *MongoDB) Init(collections []string, config MongoConf) {
2024-07-17 18:02:30 +02:00
// var baseConfig string
2024-08-01 11:31:50 +02:00
isConnected = false
2024-07-18 17:55:27 +02:00
m.Logger = logs.GetLogger()
2024-07-17 18:02:30 +02:00
ResourceMap = make(map[string]interface{})
2024-07-18 11:51:12 +02:00
2024-07-18 17:55:27 +02:00
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
2024-07-31 16:06:47 +02:00
mngoCollections = collections
mngoConfig = config
2024-08-01 11:33:27 +02:00
if err := m.createClient(config.GetUrl()); err != nil {
m.Logger.Error().Msg(err.Error())
}
2024-07-17 18:02:30 +02:00
}
2024-08-01 11:33:27 +02:00
2024-07-31 16:06:47 +02:00
func (m *MongoDB) createClient(MongoURL string) error {
2024-08-20 12:20:30 +02:00
if mngoClient != nil {
return nil
}
2024-07-17 18:02:30 +02:00
var err error
// Allows us to use marshal and unmarshall with results of FindOne() and others
2024-07-18 11:51:12 +02:00
bsonOpts := &options.BSONOptions{
2024-07-17 18:02:30 +02:00
UseJSONStructTags: true,
2024-07-18 11:51:12 +02:00
NilSliceAsEmpty: true,
2024-07-17 18:02:30 +02:00
}
2024-08-01 11:11:41 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-17 18:02:30 +02:00
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
// Ping the primary
2024-08-02 15:45:57 +02:00
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil || mngoClient == nil {
2024-07-31 16:06:47 +02:00
mngoClient = nil
2024-08-01 11:31:50 +02:00
isConnected = false
2024-08-01 11:09:39 +02:00
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
2024-07-17 18:02:30 +02:00
}
2024-08-20 11:10:28 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 60*time.Second)
2024-08-02 16:39:10 +02:00
defer cancel()
2024-07-17 18:02:30 +02:00
2024-08-02 16:39:10 +02:00
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
2024-07-31 16:06:47 +02:00
mngoClient = nil
2024-08-01 11:31:50 +02:00
isConnected = false
2024-08-01 11:09:39 +02:00
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
2024-07-17 18:02:30 +02:00
}
2024-08-02 15:45:57 +02:00
if !isConnected && mngoClient != nil {
2024-08-01 11:31:50 +02:00
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
m.prepareDB(mngoCollections, mngoConfig)
m.Logger.Info().Msg("Database is READY")
}
2024-08-02 15:45:57 +02:00
2024-07-31 16:06:47 +02:00
return nil
2024-07-17 18:02:30 +02:00
}
2024-07-18 14:11:13 +02:00
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
2024-07-17 18:02:30 +02:00
var err error
2024-07-18 14:11:13 +02:00
mngoDB = mngoClient.Database(config.GetDatabase())
2024-08-02 15:06:40 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-18 11:51:12 +02:00
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
2024-07-17 18:02:30 +02:00
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
2024-07-17 18:02:30 +02:00
}
collectionMap := make(map[string]bool)
2024-07-18 11:51:12 +02:00
for _, name := range existingCollections {
collectionMap[name] = true
}
2024-07-17 18:02:30 +02:00
// Only do the collection definition process if it doesn't already exists
// we add the collection to the collection map from mongo/mongo_utils to provide faster access to the collection
2024-07-18 11:51:12 +02:00
for _, collection_name := range list_collection {
2024-07-17 18:02:30 +02:00
new_collection := mngoDB.Collection(collection_name)
if _, exists := collectionMap[collection_name]; !exists {
2024-07-18 13:35:14 +02:00
m.createCollection(collection_name, new_collection)
2024-08-13 09:49:42 +02:00
if collection_name == "peer" {
id, p := static.GetMyLocalBsonPeer()
m.StoreOne(p, id, collection_name)
}
2024-07-18 11:51:12 +02:00
} else {
2024-07-17 18:02:30 +02:00
CollectionMap[collection_name] = new_collection
}
2024-07-18 11:51:12 +02:00
}
2024-08-01 11:31:50 +02:00
isConnected = true
2024-07-17 18:02:30 +02:00
}
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
2024-07-18 13:35:14 +02:00
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
2024-08-02 15:06:40 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-18 11:51:12 +02:00
var err error
2024-07-17 18:02:30 +02:00
CollectionMap[collection_name] = new_collection
2024-07-18 11:51:12 +02:00
_, exists := IndexesMap[collection_name]
if exists {
2024-07-17 18:02:30 +02:00
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
2024-07-17 18:02:30 +02:00
panic(err)
} else if !errors.As(err, &cmdErr) {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
2024-07-17 18:02:30 +02:00
panic(err)
}
}
} else {
mngoDB.CreateCollection(MngoCtx, collection_name)
}
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
2024-07-22 14:46:49 +02:00
filter := bson.M{"_id": id}
2024-07-18 11:51:12 +02:00
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
2024-07-19 11:27:58 +02:00
return 0, 404, err
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
return result.DeletedCount, 200, nil
2024-07-18 11:51:12 +02:00
}
2024-07-23 16:14:46 +02:00
func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
2024-07-23 16:14:46 +02:00
filter := bson.D{}
for k, v := range f {
filter = append(filter, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteMany(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, 404, err
}
return result.DeletedCount, 200, nil
}
2024-07-25 09:28:55 +02:00
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
2024-07-25 09:28:55 +02:00
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return 0, 404, err
}
return res.UpsertedCount, 200, nil
}
2024-07-22 16:12:38 +02:00
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
2024-07-22 16:12:38 +02:00
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
2024-07-22 14:46:49 +02:00
filter := bson.M{"_id": id}
2024-07-18 11:51:12 +02:00
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-22 16:12:38 +02:00
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
2024-07-18 11:51:12 +02:00
if err != nil {
2024-07-19 11:27:58 +02:00
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err
2024-07-18 11:51:12 +02:00
}
2024-07-22 14:46:49 +02:00
return id, 200, nil
2024-07-18 11:51:12 +02:00
}
2024-07-22 14:02:48 +02:00
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
2024-07-22 14:02:48 +02:00
var doc map[string]interface{}
b, _ := bson.Marshal(obj)
bson.Unmarshal(b, &doc)
doc["_id"] = id
2024-07-17 18:02:30 +02:00
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-22 14:06:53 +02:00
_, err := targetDBCollection.InsertOne(MngoCtx, doc)
2024-07-17 18:02:30 +02:00
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
2024-07-19 11:27:58 +02:00
return "", 409, err
2024-07-17 18:02:30 +02:00
}
2024-07-22 14:06:53 +02:00
return id, 200, nil
2024-07-17 18:02:30 +02:00
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
2024-07-22 14:46:49 +02:00
filter := bson.M{"_id": id}
2024-07-17 18:02:30 +02:00
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-19 11:27:58 +02:00
res := targetDBCollection.FindOne(MngoCtx, filter)
2024-07-17 18:02:30 +02:00
if res.Err() != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
2024-07-19 11:27:58 +02:00
err := res.Err()
return nil, 404, err
2024-07-17 18:02:30 +02:00
}
2024-07-19 11:27:58 +02:00
return res, 200, nil
2024-07-17 18:02:30 +02:00
}
2024-07-23 11:22:50 +02:00
2024-08-01 11:19:14 +02:00
func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.Cursor, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
2024-07-26 13:45:10 +02:00
opts := options.Find()
opts.SetLimit(100)
targetDBCollection := CollectionMap[collection_name]
2024-08-01 10:02:40 +02:00
orList := bson.A{}
2024-08-02 15:36:52 +02:00
andList := bson.A{}
2024-08-01 11:19:14 +02:00
f := bson.D{}
if filters != nil {
for k, filter := range filters.Or {
2024-08-02 14:07:43 +02:00
for _, ff := range filter {
orList = append(orList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
2024-08-01 11:19:14 +02:00
}
for k, filter := range filters.And {
2024-08-02 14:07:43 +02:00
for _, ff := range filter {
2024-08-02 15:36:52 +02:00
andList = append(andList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
2024-08-02 14:07:43 +02:00
}
2024-08-01 11:19:14 +02:00
}
2024-08-02 15:36:52 +02:00
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
2024-08-02 17:38:51 +02:00
} else {
if len(orList) > 0 {
andList = append(andList, bson.M{"$or": orList})
}
2024-08-02 15:36:52 +02:00
f = bson.D{{"$and", andList}}
}
2024-08-01 09:13:10 +02:00
}
2024-08-01 10:02:40 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-26 13:45:10 +02:00
if cursor, err := targetDBCollection.Find(
2024-07-29 15:53:15 +02:00
MngoCtx,
2024-08-01 09:13:10 +02:00
f,
2024-07-26 13:45:10 +02:00
opts,
); err != nil {
return nil, 404, err
} else {
return cursor, 200, nil
}
}
2024-07-25 09:28:55 +02:00
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
2024-07-25 09:28:55 +02:00
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, f)
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}
2024-07-23 11:22:50 +02:00
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
2024-07-31 16:06:47 +02:00
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
2024-07-23 11:22:50 +02:00
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}