mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 20:59:53 +05:00
Updated mongodb config handling
This commit is contained in:
committed by
Dan Christian Bogos
parent
6a3f8ddc57
commit
b1c80fe8cb
@@ -32,21 +32,24 @@ const (
|
||||
func (ms *MongoStorage) GetSection(ctx *context.Context, section string, val interface{}) error {
|
||||
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(ColCfg).FindOne(sctx, bson.M{"section": section},
|
||||
options.FindOne().SetProjection(bson.M{"cfg": 1 /*"section": 0, "_id": 0*/}))
|
||||
if err = cur.Decode(val); err != nil && err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
options.FindOne().SetProjection(bson.M{"cfg": 1, "_id": 0 /*"section": 0, */}))
|
||||
tmp := map[string]bson.Raw{}
|
||||
if err = cur.Decode(&tmp); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
return bson.UnmarshalWithRegistry(mongoReg, tmp["cfg"], val)
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetSection(ctx *context.Context, section string, jsn interface{}) (err error) {
|
||||
return ms.query(ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
_, err = ms.getCol(ColCfg).UpdateOne(sctx, bson.M{"section": section},
|
||||
bson.M{"$set": &struct {
|
||||
Section string
|
||||
Cfg interface{}
|
||||
}{Section: section, Cfg: jsn}},
|
||||
bson.M{"$set": bson.M{
|
||||
"section": section,
|
||||
"cfg": jsn}},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
|
||||
@@ -101,8 +101,18 @@ var (
|
||||
|
||||
tTime = reflect.TypeOf(time.Time{})
|
||||
decimalType = reflect.TypeOf(utils.Decimal{})
|
||||
|
||||
mongoReg *bsoncodec.Registry
|
||||
)
|
||||
|
||||
func init() {
|
||||
reg := bson.NewRegistryBuilder()
|
||||
reg.RegisterTypeDecoder(tTime, bsoncodec.ValueDecoderFunc(TimeDecodeValue1))
|
||||
reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(DecimalEncoder))
|
||||
reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(DecimalDecoder))
|
||||
mongoReg = reg.Build()
|
||||
}
|
||||
|
||||
func TimeDecodeValue1(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
|
||||
if vr.Type() != bsontype.DateTime {
|
||||
return fmt.Errorf("cannot decode %v into a time.Time", vr.Type())
|
||||
@@ -168,13 +178,9 @@ func NewMongoStorage(host, port, db, user, pass, mrshlerStr, storageType string,
|
||||
}
|
||||
ctx := context.TODO()
|
||||
url = "mongodb://" + url
|
||||
reg := bson.NewRegistryBuilder()
|
||||
reg.RegisterDecoder(tTime, bsoncodec.ValueDecoderFunc(TimeDecodeValue1))
|
||||
reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(DecimalEncoder))
|
||||
reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(DecimalDecoder))
|
||||
opt := options.Client().
|
||||
ApplyURI(url).
|
||||
SetRegistry(reg.Build()).
|
||||
SetRegistry(mongoReg).
|
||||
SetServerSelectionTimeout(ttl).
|
||||
SetRetryWrites(false) // set this option to false because as default it is on true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user