mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-24 00:28:44 +05:00
Update mongo driver
This commit is contained in:
committed by
Dan Christian Bogos
parent
a49663371d
commit
d1481fb46c
@@ -41,7 +41,6 @@ import (
|
||||
"github.com/mongodb/mongo-go-driver/mongo"
|
||||
"github.com/mongodb/mongo-go-driver/mongo/options"
|
||||
"github.com/mongodb/mongo-go-driver/x/bsonx"
|
||||
"github.com/mongodb/mongo-go-driver/x/mongo/driver/topology"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -143,16 +142,7 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes
|
||||
ctx := context.Background()
|
||||
url = "mongodb://" + url
|
||||
reg := bson.NewRegistryBuilder().RegisterDecoder(tTime, bsoncodec.ValueDecoderFunc(TimeDecodeValue1)).Build()
|
||||
opt := &options.ClientOptions{
|
||||
Registry: reg,
|
||||
TopologyOptions: []topology.Option{
|
||||
topology.WithServerOptions(func(opts ...topology.ServerOption) []topology.ServerOption {
|
||||
return []topology.ServerOption{
|
||||
topology.WithRegistry(func(r *bsoncodec.Registry) *bsoncodec.Registry { return reg }),
|
||||
}
|
||||
}),
|
||||
},
|
||||
}
|
||||
opt := options.Client().SetRegistry(reg)
|
||||
|
||||
client, err := mongo.NewClientWithOptions(url, opt)
|
||||
// client, err := mongo.NewClient(url)
|
||||
@@ -175,7 +165,7 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes
|
||||
isDataDB: isDataDB,
|
||||
}
|
||||
if err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) error {
|
||||
if col, err := ms.client.Database(dbName).ListCollections(sctx, nil, options.ListCollections().SetNameOnly(true)); err != nil {
|
||||
if col, err := ms.client.Database(dbName).ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
empty := true
|
||||
@@ -225,14 +215,14 @@ func (ms *MongoStorage) IsDataDB() bool {
|
||||
func (ms *MongoStorage) EnusureIndex(colName string, uniq bool, keys ...string) error {
|
||||
return ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) error {
|
||||
col := ms.getCol(colName)
|
||||
io := mongo.NewIndexOptionsBuilder().Unique(uniq)
|
||||
io := options.Index().SetUnique(uniq)
|
||||
var doc bsonx.Doc
|
||||
for _, k := range keys {
|
||||
doc = doc.Append(k, bsonx.Int32(1))
|
||||
}
|
||||
_, err := col.Indexes().CreateOne(sctx, mongo.IndexModel{
|
||||
Keys: doc,
|
||||
Options: io.Build(),
|
||||
Options: io,
|
||||
})
|
||||
return err
|
||||
})
|
||||
@@ -523,7 +513,7 @@ func (ms *MongoStorage) RemoveReverseForPrefix(prefix string) (err error) {
|
||||
// IsDBEmpty implementation
|
||||
func (ms *MongoStorage) IsDBEmpty() (resp bool, err error) {
|
||||
err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) error {
|
||||
col, err := ms.DB().ListCollections(sctx, nil)
|
||||
col, err := ms.DB().ListCollections(sctx, bson.D{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1151,7 +1141,7 @@ func (ms *MongoStorage) GetSubscribersDrv() (result map[string]*SubscriberData,
|
||||
Value *SubscriberData
|
||||
}
|
||||
if err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(colPbs).Find(sctx, nil)
|
||||
cur, err := ms.getCol(colPbs).Find(sctx, bson.D{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1214,7 +1204,7 @@ func (ms *MongoStorage) GetUserDrv(key string) (up *UserProfile, err error) {
|
||||
|
||||
func (ms *MongoStorage) GetUsersDrv() (result []*UserProfile, err error) {
|
||||
err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(colUsr).Find(sctx, nil)
|
||||
cur, err := ms.getCol(colUsr).Find(sctx, bson.D{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1802,7 +1792,7 @@ func (ms *MongoStorage) PopTask() (t *Task, err error) {
|
||||
Task *Task
|
||||
}{}
|
||||
if err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(colTsk).FindOneAndDelete(sctx, nil)
|
||||
cur := ms.getCol(colTsk).FindOneAndDelete(sctx, bson.D{})
|
||||
if err := cur.Decode(&v); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return utils.ErrNotFound
|
||||
|
||||
@@ -36,7 +36,7 @@ import (
|
||||
func (ms *MongoStorage) GetTpIds(colName string) (tpids []string, err error) {
|
||||
getTpIDs := func(ctx context.Context, col string, tpMap map[string]struct{}) (map[string]struct{}, error) {
|
||||
if strings.HasPrefix(col, "tp_") {
|
||||
result, err := ms.getCol(col).Distinct(ctx, "tpid", nil)
|
||||
result, err := ms.getCol(col).Distinct(ctx, "tpid", bson.D{})
|
||||
if err != nil {
|
||||
return tpMap, err
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (ms *MongoStorage) GetTpIds(colName string) (tpids []string, err error) {
|
||||
|
||||
if colName == "" {
|
||||
if err := ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) error {
|
||||
col, err := ms.DB().ListCollections(sctx, nil, options.ListCollections().SetNameOnly(true))
|
||||
col, err := ms.DB().ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -671,7 +671,7 @@ func (ms *MongoStorage) GetTPAccountActions(tp *utils.TPAccountActions) ([]*util
|
||||
func (ms *MongoStorage) RemTpData(table, tpid string, args map[string]string) error {
|
||||
if len(table) == 0 { // Remove tpid out of all tables
|
||||
return ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) error {
|
||||
col, err := ms.DB().ListCollections(sctx, nil, options.ListCollections().SetNameOnly(true))
|
||||
col, err := ms.DB().ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1616,7 +1616,7 @@ func (ms *MongoStorage) GetVersions(itm string) (vrs Versions, err error) {
|
||||
fop.SetProjection(bson.M{"_id": 0})
|
||||
}
|
||||
if err = ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(colVer).FindOne(sctx, nil, fop)
|
||||
cur := ms.getCol(colVer).FindOne(sctx, bson.D{}, fop)
|
||||
if err := cur.Decode(&vrs); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return utils.ErrNotFound
|
||||
@@ -1638,7 +1638,7 @@ func (ms *MongoStorage) SetVersions(vrs Versions, overwrite bool) (err error) {
|
||||
ms.RemoveVersions(nil)
|
||||
}
|
||||
return ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
_, err = ms.getCol(colVer).UpdateOne(sctx, nil, bson.M{"$set": vrs},
|
||||
_, err = ms.getCol(colVer).UpdateOne(sctx, bson.D{}, bson.M{"$set": vrs},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
@@ -1654,7 +1654,7 @@ func (ms *MongoStorage) SetVersions(vrs Versions, overwrite bool) (err error) {
|
||||
func (ms *MongoStorage) RemoveVersions(vrs Versions) (err error) {
|
||||
if len(vrs) == 0 {
|
||||
return ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
dr, err := ms.getCol(colVer).DeleteOne(sctx, nil)
|
||||
dr, err := ms.getCol(colVer).DeleteOne(sctx, bson.D{})
|
||||
if dr.DeletedCount == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
@@ -1663,7 +1663,7 @@ func (ms *MongoStorage) RemoveVersions(vrs Versions) (err error) {
|
||||
}
|
||||
return ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
for k := range vrs {
|
||||
if _, err = ms.getCol(colVer).UpdateOne(sctx, nil, bson.M{"$unset": bson.M{k: 1}},
|
||||
if _, err = ms.getCol(colVer).UpdateOne(sctx, bson.D{}, bson.M{"$unset": bson.M{k: 1}},
|
||||
options.Update().SetUpsert(true)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user