Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Unique Identity API update (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: badhri85 <[email protected]>
  • Loading branch information
badhrinathpa and badhri85 authored Jul 26, 2022
1 parent 19942de commit 2b5a8f2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions api_mongoDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,25 @@ func RestfulAPIGetMany(collName string, filter bson.M) []map[string]interface{}
}

/* Get unique identity from counter collection. */
func GetUniqueIdentity() int32 {
func GetUniqueIdentity(idName string) int32 {
counterCollection := Client.Database(dbName).Collection("counter")

counterFilter := bson.M{}
counterFilter["_id"] = "uniqueIdentity"
counterFilter["_id"] = idName

for {
count := counterCollection.FindOneAndUpdate(context.TODO(), counterFilter, bson.M{"$inc": bson.M{"count": 1}})

if count.Err() != nil {
logger.MongoDBLog.Println("FindOneAndUpdate error. Create entry for field ")
counterData := bson.M{}
counterData["count"] = 1
counterData["_id"] = "uniqueIdentity"
counterData["_id"] = idName
counterCollection.InsertOne(context.TODO(), counterData)

continue
} else {
logger.MongoDBLog.Println("found entry. inc and return")
data := bson.M{}
count.Decode(&data)
decodedCount := data["count"].(int32)
Expand Down
4 changes: 2 additions & 2 deletions dbtestapp/dbtestapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ func main() {
//time.Sleep(120 * time.Second)
//updateDocumentWithTimeout("timeout", "yak1", 200, "createdAt")

uniqueId := MongoDBLibrary.GetUniqueIdentity()
uniqueId := MongoDBLibrary.GetUniqueIdentity("tmsi")
log.Println(uniqueId)

uniqueId = MongoDBLibrary.GetUniqueIdentity()
uniqueId = MongoDBLibrary.GetUniqueIdentity("amfUeNgapId")
log.Println(uniqueId)

uniqueId = MongoDBLibrary.GetUniqueIdentityWithinRange(3, 6)
Expand Down

0 comments on commit 2b5a8f2

Please sign in to comment.