rewrote the mongo client handler

This commit is contained in:
pb
2024-07-17 17:20:25 +02:00
parent 90e9bcf378
commit 02b0a2a595
4 changed files with 189 additions and 59 deletions

138
mongo.go
View File

@@ -7,35 +7,46 @@ import (
"os"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
mongo_utils "oc-lib/mongo"
)
var (
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
cancel context.CancelFunc
existingCollections []string
ResourceMap map[string]interface{}
)
// Trying to get vscode to display this
func MongoInit() {
func init() {
// var baseConfig string
var err error
var cancel context.CancelFunc
var conf map[string]string
var MongoURL string
var DBname string
ResourceMap = make(map[string]interface{})
ResourceMap["data"] = Data{}
db_conf, err := os.ReadFile("oclib_conf.json")
logger = CreateLogger("oclib","")
db_conf, err := os.ReadFile("tests/oclib_conf.json")
if err != nil {
logger.Fatal().Msg("Could not find configuration file")
}
json.Unmarshal(db_conf,&conf)
if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
MongoURL = conf["DB_URL_LOCAL"]
} else {
@@ -46,6 +57,7 @@ func MongoInit() {
logger.Info().Msg("Connecting to" + MongoURL)
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@@ -58,16 +70,19 @@ func MongoInit() {
}
// TODO : Soit retourner MngoCtx, soit comprendre comment passer sa référence et la mettre à jour
func createClient(MongoURL string){
var err error
clientOptions := options.Client().ApplyURI(MongoURL)
mngoClient, _ = mongo.NewClient(options.Client().ApplyURI(MongoURL))
// Allows us to use marshal and unmarshall with results of FindOne() and others
bsonOpts := &options.BSONOptions {
UseJSONStructTags: true,
NilSliceAsEmpty: true,
}
if err = mngoClient.Connect(MngoCtx); err != nil {
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
mngoClient, err = mongo.Connect(MngoCtx,clientOptions)
if err != nil {
logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err" )
panic(err)
}
@@ -88,40 +103,97 @@ func createClient(MongoURL string){
func prepareDB(dc_name string, db_point string) {
// var err error
var err error
DBname := dc_name + "-" + db_point
mngoDB = mngoClient.Database(DBname)
list_collection := [...]string{"data","processing","storage","datacenter","workspace","schedule","workflow"}
list_collection := [...]string{"Data","Computing","Storage","Datacenter","Workspace","Schedule"}
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
}
collectionMap := make(map[string]bool)
for _, name := range existingCollections {
collectionMap[name] = true
}
// 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
for _, collection_name := range(list_collection){
new_collection := mngoDB.Collection(collection_name)
createCollection(new_collection)
if _, exists := collectionMap[collection_name]; !exists {
createCollection(collection_name, new_collection)
} else{
mongo_utils.CollectionMap[collection_name] = new_collection
}
}
}
func createCollection(new_collection *mongo.Collection) {
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
func createCollection(collection_name string, new_collection *mongo.Collection) {
var err error
if _, err = new_collection.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
{
Keys: bsonx.Doc{
{Key: "description", Value: bsonx.String("text")},
{Key: "example", Value: bsonx.String("text")},
},
},
}); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
logger.Fatal().Msg("It failed but I saw it: " + err.Error())
panic(err)
} else if !errors.As(err, &cmdErr) {
logger.Fatal().Msg("Unexpected error: " + err.Error())
panic(err)
mongo_utils.CollectionMap[collection_name] = new_collection
_, exists := mongo_utils.IndexesMap[collection_name];
if exists{
if _, err = new_collection.Indexes().CreateMany(MngoCtx, mongo_utils.IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
panic(err)
} else if !errors.As(err, &cmdErr) {
logger.Fatal().Msg("Unexpected error: " + err.Error())
panic(err)
}
}
} else {
mngoDB.CreateCollection(MngoCtx, collection_name)
}
}
}
func StoreOne(obj interface{}, collection_name string) (string, error) {
targetDBCollection := mongo_utils.CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.InsertOne(MngoCtx,obj)
if err != nil {
logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
}
return result.InsertedID.(primitive.ObjectID).Hex(), nil
}
func LoadOne(id string, collection_name string) ( res *mongo.SingleResult , err error){
// new_obj := ResourceMap[collection_name]
// var doc bson.D
filter := bson.M{"_id": getObjIDFromString(id)}
targetDBCollection := mongo_utils.CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res = targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err = res.Err()
return nil, err
}
return res, nil
}