Skip to content

Commit

Permalink
Merge pull request open-horizon#151 from LiilyZhang/zhangl/Issue150
Browse files Browse the repository at this point in the history
open-horizon#Issue150 - Bug: Should throw out error when failed to cr…
  • Loading branch information
LiilyZhang authored Aug 1, 2024
2 parents ba8f438 + 0260e74 commit dabec2b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions core/storage/mongoStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ func (store *MongoStorage) Init() common.SyncServiceError {
db := mongoClient.Database(common.Configuration.MongoDbName)
destinationsCollection := db.Collection(destinations)
indexModel := mongo.IndexModel{Keys: bson.D{{"destination.destination-org-id", -1}}}
destinationsCollection.Indexes().CreateOne(context.TODO(), indexModel)
if _, err = destinationsCollection.Indexes().CreateOne(context.TODO(), indexModel); err != nil {
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", destinations, err)
log.Error(message)
return &Error{message}
}

notificationsCollection := db.Collection(notifications)
indexModel1 := mongo.IndexModel{
Expand All @@ -233,7 +237,11 @@ func (store *MongoStorage) Init() common.SyncServiceError {
},
}
indexModel2 := mongo.IndexModel{Keys: bson.D{{"notification.resend-time", -1}, {"notification.status", -1}}}
notificationsCollection.Indexes().CreateMany(context.TODO(), []mongo.IndexModel{indexModel1, indexModel2})
if _, err = notificationsCollection.Indexes().CreateMany(context.TODO(), []mongo.IndexModel{indexModel1, indexModel2}); err != nil {
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", notifications, err)
log.Error(message)
return &Error{message}
}

objectsCollection := db.Collection(objects)
indexModel = mongo.IndexModel{Keys: bson.D{{"metadata.destination-org-id", -1}}}
Expand All @@ -250,7 +258,9 @@ func (store *MongoStorage) Init() common.SyncServiceError {
// need to set index name???
})
if err != nil {
log.Error("Failed to create an index on %s. Error: %s", objects, err)
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", objects, err)
log.Error(message)
return &Error{message}
}

_, err = objectsCollection.Indexes().CreateOne(
Expand All @@ -263,20 +273,21 @@ func (store *MongoStorage) Init() common.SyncServiceError {
Options: options.Index().SetSparse(true),
})
if err != nil {
log.Error("Failed to create an index on %s. Error: %s", objects, err)
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", objects, err)
log.Error(message)
return &Error{message}
}
db.Collection(acls).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{{"org-id", 1}, {"acl-type", 1}}})
gridfsBucket, err := gridfs.NewBucket(db)
if err != nil {
trace.Error("Error creating gridfs buket Error was: " + err.Error())
message := fmt.Sprintf("Error creating gridfs buket Error was: " + err.Error())
log.Error(message)
return &Error{message}
}

store.client = mongoClient
store.database = db
store.gridfsBucket = gridfsBucket

//store.openFiles = make(map[string]*fileHandle2)

sleepInMS = common.Configuration.MongoSleepTimeBetweenRetry

if trace.IsLogging(logger.TRACE) {
Expand Down

0 comments on commit dabec2b

Please sign in to comment.