func Example() { saver := new(savers.FileSystemSaver) saver.Report = true saver.Duration = time.Second * 3 // Run before any v1 or v2 UUIDs to ensure the saver takes uuid.RegisterSaver(saver) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) uP, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") u3 := uuid.NewV3(uP, uuid.Name("test")) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u5 := uuid.NewV5(uuid.NamespaceURL, uuid.Name("test")) if uuid.Equal(u1, u3) { fmt.Printf("Will never happen") } fmt.Print(uuid.Sprintf(uuid.CurlyHyphen, u5)) uuid.SwitchFormat(uuid.BracketHyphen) }
func ExampleNewV1() { // Must run before using V1 or V2 uuid.Init() u1 := uuid.NewV1() fmt.Printf("version %d variant %d: %d\n", u1.Version(), u1.Variant(), u1) }
func BenchmarkNewV1(b *testing.B) { //uuid.RegisterSaver(&savers.FileSystemSaver{Report:true, Duration: time.Second*1}) //b.ResetTimer() for i := 0; i < b.N; i++ { uuid.NewV1() } b.StopTimer() b.ReportAllocs() }
func ExampleRegisterSaver() { saver := new(savers.FileSystemSaver) saver.Report = true saver.Duration = 3 * time.Second // Run before any v1 or v2 UUIDs to ensure the saver takes uuid.RegisterSaver(saver) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) }
func FillControlHeaders(request *messaging.ObjectRequest) { //currentTime := time.Now().Local().String() currentTime := getTime() if request.Controls.Multiplicity == "single" { controlObject := messaging.ControlHeaders{} controlObject.Version = uuid.NewV1().String() controlObject.Namespace = request.Controls.Namespace controlObject.Class = request.Controls.Class controlObject.Tenant = "123" controlObject.LastUdated = currentTime request.Body.Object["__osHeaders"] = controlObject } else { for _, obj := range request.Body.Objects { controlObject := messaging.ControlHeaders{} controlObject.Version = uuid.NewV1().String() controlObject.Namespace = request.Controls.Namespace controlObject.Class = request.Controls.Class controlObject.Tenant = "123" controlObject.LastUdated = currentTime obj["__osHeaders"] = controlObject } } }
func Example() { var config = uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute} uuid.SetupFileSystemStateSaver(config) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) uP, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") u3 := uuid.NewV3(uP, uuid.Name("test")) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u5 := uuid.NewV5(uuid.NamespaceURL, uuid.Name("test")) if uuid.Equal(u1, u3) { fmt.Printf("Will never happen") } fmt.Printf(uuid.Formatter(u5, uuid.CurlyHyphen)) uuid.SwitchFormat(uuid.BracketHyphen) }
func Example() { saver := new(savers.FileSystemSaver) saver.Report = true saver.Duration = time.Second * 3 // Run before any v1 or v2 UUIDs to ensure the savers takes uuid.RegisterSaver(saver) up, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") fmt.Printf("version %d variant %x: %s\n", up.Version(), up.Variant(), up) uuid.New(up.Bytes()) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u3 := uuid.NewV3(u1, u4) url, _ := url.Parse("www.example.com") u5 := uuid.NewV5(uuid.NameSpaceURL, url) if uuid.Equal(u1, u3) { fmt.Println("Will never happen") } if uuid.Compare(uuid.NameSpaceDNS, uuid.NameSpaceDNS) == 0 { fmt.Println("They are equal") } // Default Format is Canonical fmt.Println(uuid.Formatter(u5, uuid.FormatCanonicalCurly)) uuid.SwitchFormat(uuid.FormatCanonicalBracket) }
func getPostgresSqlRecordID(request *messaging.ObjectRequest, obj map[string]interface{}) (returnID string) { isGUIDKey := false isAutoIncrementId := false //else MANUAL key from the user if obj == nil { //single request if (request.Controls.Id == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (request.Controls.Id == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } else { //multiple requests if (obj[request.Body.Parameters.KeyProperty].(string) == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (obj[request.Body.Parameters.KeyProperty].(string) == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } if isGUIDKey { request.Log("GUID Key generation requested!") returnID = uuid.NewV1().String() } else if isAutoIncrementId { request.Log("Automatic Increment Key generation requested!") session, isError, _ := getPostgresConnection(request) if isError { returnID = "" request.Log("Connecting to MySQL Failed!") } else { //Read Table domainClassAttributes request.Log("Reading maxCount from DB") rows, err := session.Query("SELECT maxCount FROM domainClassAttributes where class = '" + request.Controls.Class + "';") if err != nil { //If err create new domainClassAttributes table request.Log("No Class found.. Must be a new namespace") _, err = session.Query("create table domainClassAttributes ( class text primary key, maxcount text, version text);") if err != nil { returnID = "" return } else { //insert record with count 1 and return _, err := session.Query("INSERT INTO domainClassAttributes (class, maxcount,version) VALUES ('" + request.Controls.Class + "','1','" + uuid.NewV1().String() + "')") if err != nil { returnID = "" return } else { returnID = "1" return } } } else { //read value var myMap map[string]interface{} myMap = make(map[string]interface{}) columns, _ := rows.Columns() count := len(columns) values := make([]interface{}, count) valuePtrs := make([]interface{}, count) for rows.Next() { for i, _ := range columns { valuePtrs[i] = &values[i] } rows.Scan(valuePtrs...) for i, col := range columns { var v interface{} val := values[i] b, ok := val.([]byte) if ok { v = string(b) } else { v = val } myMap[col] = v } } if len(myMap) == 0 { request.Log("New Class! New record for this class will be inserted") _, err = session.Query("INSERT INTO domainClassAttributes (class,maxcount,version) values ('" + request.Controls.Class + "', '1', '" + uuid.NewV1().String() + "');") if err != nil { returnID = "" return } else { returnID = "1" return } } else { //inrement one and UPDATE request.Log("Record Available!") maxCount := 0 maxCount, err = strconv.Atoi(myMap["maxcount"].(string)) maxCount++ returnID = strconv.Itoa(maxCount) _, err = session.Query("UPDATE domainClassAttributes SET maxcount='" + returnID + "' WHERE class = '" + request.Controls.Class + "' ;") if err != nil { request.Log("Error Updating index table : " + err.Error()) returnID = "" return } } } } session.Close() } else { request.Log("Manual Key requested!") if obj == nil { returnID = request.Controls.Id } else { returnID = obj[request.Body.Parameters.KeyProperty].(string) } } return }
func ExampleNewV1() { u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) }
func getRedisRecordID(request *messaging.ObjectRequest, obj map[string]interface{}) (returnID string) { isGUIDKey := false isAutoIncrementId := false //else MANUAL key from the user if obj == nil { //single request if (request.Controls.Id == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (request.Controls.Id == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } else { //multiple requests if (obj[request.Body.Parameters.KeyProperty].(string) == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (obj[request.Body.Parameters.KeyProperty].(string) == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } if isGUIDKey { request.Log("GUID Key generation requested!") returnID = uuid.NewV1().String() } else if isAutoIncrementId { request.Log("Automatic Increment Key generation requested!") client, isError, _ := getRedisConnection(request) if isError { returnID = "" request.Log("Connecting to REDIS Failed! ") } else { //read Attributes table key := request.Controls.Namespace + "." + request.Controls.Class + "#domainClassAttributes" rawBytes, err := client.Get(key) if err != nil || string(rawBytes) == "" { request.Log("This is a freshly created Class. Inserting new Class record.") var ObjectBody map[string]interface{} ObjectBody = make(map[string]interface{}) ObjectBody["maxCount"] = "1" ObjectBody["version"] = uuid.NewV1().String() err = client.Set(key, getStringByObject(ObjectBody), 0, 0, false, false) if err != nil { request.Log("Update of maxCount Failed : " + err.Error()) returnID = "" } else { returnID = "1" } } else { var UpdatedCount int var returnData map[string]interface{} returnData = make(map[string]interface{}) json.Unmarshal(rawBytes, &returnData) for fieldName, fieldvalue := range returnData { if strings.ToLower(fieldName) == "maxcount" { UpdatedCount, _ = strconv.Atoi(fieldvalue.(string)) UpdatedCount++ returnID = strconv.Itoa(UpdatedCount) break } } //update the table //save to attributes table returnData["maxCount"] = returnID returnData["version"] = uuid.NewV1().String() err = client.Set(key, getStringByObject(returnData), 0, 0, false, false) if err != nil { request.Log("Update of maxCount Failed") returnID = "" } } } } else { request.Log("Manual Key requested!") if obj == nil { returnID = request.Controls.Id } else { returnID = obj[request.Body.Parameters.KeyProperty].(string) } } return }
func ExampleNewV1() { u1 := uuid.NewV1() fmt.Printf(print, u1.Version(), u1.Variant(), u1) }
func Test_NewV1(t *testing.T) { u1 := uuid.NewV1() fmt.Printf(print, u1.Version(), u1.Variant(), u1) }
func getNoSqlKeyByGUID(request *messaging.ObjectRequest) string { namespace := request.Controls.Namespace class := request.Controls.Class key := namespace + "." + class + "." + uuid.NewV1().String() return key }
func getMongoDBRecordID(request *messaging.ObjectRequest, obj map[string]interface{}) (returnID string) { isGUIDKey := false isAutoIncrementId := false //else MANUAL key from the user if obj == nil { //single request if (request.Controls.Id == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (request.Controls.Id == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } else { //multiple requests if (obj[request.Body.Parameters.KeyProperty].(string) == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (obj[request.Body.Parameters.KeyProperty].(string) == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } if isGUIDKey { request.Log("GUID Key generation requested!") returnID = uuid.NewV1().String() } else if isAutoIncrementId { request.Log("Automatic Increment Key generation requested!") collection, isError, _ := getCustomMongoConnection(request, getSQLnamespace(request), "domainClassAttributes") if isError { returnID = "" request.Log("Connecting to MongoDB Failed!") } else { //read Attributes table key := request.Controls.Class var data map[string]interface{} err := collection.Find(bson.M{"_id": key}).One(&data) fmt.Println(data) if err != nil { request.Log("This is a freshly created Class. Inserting new Class record.") var ObjectBody map[string]interface{} ObjectBody = make(map[string]interface{}) ObjectBody["_id"] = request.Controls.Class ObjectBody["maxCount"] = "1" ObjectBody["version"] = uuid.NewV1().String() err = collection.Insert(bson.M(ObjectBody)) if err != nil { request.Log("Inserting New DomainClassAttributes failed") returnID = "" } else { returnID = "1" } } else { var UpdatedCount int for fieldName, fieldvalue := range data { if strings.ToLower(fieldName) == "maxcount" { UpdatedCount, _ = strconv.Atoi(fieldvalue.(string)) UpdatedCount++ returnID = strconv.Itoa(UpdatedCount) break } } //update the table //save to attributes table data["maxCount"] = returnID data["version"] = uuid.NewV1().String() err := collection.Update(bson.M{"_id": key}, bson.M{"$set": data}) if err != nil { request.Log("Update of maxCount Failed") returnID = "" } } } } else { request.Log("Manual Key requested!") if obj == nil { returnID = request.Controls.Id } else { returnID = obj[request.Body.Parameters.KeyProperty].(string) } } return }
func generateRandomTestKey() string { return uuid.NewV1().String() }
func setManyElastic(request *messaging.ObjectRequest) RepositoryResponse { response := RepositoryResponse{} conn := getConnection()(request) request.Log("Starting Elastic Search bulk insert") request.Log("Determining which Key indexing method is used") isGUIDKey := false isAutoIncrementKey := false currentIndex := 0 maxCount := "" CountIndex := 0 var Data map[string]interface{} Data = make(map[string]interface{}) if (request.Body.Objects[0][request.Body.Parameters.KeyProperty].(string) == "-888") || (request.Body.Parameters.GUIDKey == true) { request.Log("GUID Key generation Requested") isGUIDKey = true } else if (request.Body.Objects[0][request.Body.Parameters.KeyProperty].(string) == "-999") || (request.Body.Parameters.AutoIncrement == true) { request.Log("Auto-Increment Key generation Requested") isAutoIncrementKey = true //set starting index //Read maxCount from domainClassAttributes table request.Log("Reading the max count") classkey := request.Controls.Class data, err := conn.Get(request.Controls.Namespace, "domainClassAttributes", classkey, nil) if err != nil { request.Log("No record Found. This is a NEW record. Inserting new attribute value") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = "0" newRecord["version"] = uuid.NewV1().String() _, err = conn.Index(request.Controls.Namespace, "domainClassAttributes", classkey, nil, newRecord) if err != nil { errorMessage := "Failed to create new Domain Class Attribute entry." request.Log(errorMessage) return response } else { response.IsSuccess = true response.Message = "Successfully new Domain Class Attribute to elastic search" request.Log(response.Message) maxCount = "0" } } else { request.Log("Successfully retrieved object from Elastic Search") var currentMap map[string]interface{} currentMap = make(map[string]interface{}) byteData, err := data.Source.MarshalJSON() if err != nil { request.Log("Data serialization to read maxCount failed") response.Message = "Data serialization to read maxCount failed" return response } json.Unmarshal(byteData, ¤tMap) maxCount = currentMap["maxCount"].(string) intTemp, _ := strconv.Atoi(maxCount) currentIndex = intTemp } } else { request.Log("Manual Keys supplied!") } numberOfSets := (len(request.Body.Objects) / 100) + 1 startIndex := 0 stopIndex := 100 for x := 0; x < numberOfSets; x++ { if x == numberOfSets-1 { indexer := conn.NewBulkIndexer(100) indexer2 := conn.NewBulkIndexer(100) nowTime := time.Now() if isAutoIncrementKey { //Read maxCount from domainClassAttributes table request.Log("Reading the max count") classkey := request.Controls.Class data, err := conn.Get(request.Controls.Namespace, "domainClassAttributes", classkey, nil) if err != nil { request.Log("No record Found. This is a NEW record. Inserting new attribute value") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = "0" newRecord["version"] = uuid.NewV1().String() _, err = conn.Index(request.Controls.Namespace, "domainClassAttributes", classkey, nil, newRecord) if err != nil { errorMessage := "Failed to create new Domain Class Attribute entry." request.Log(errorMessage) return response } else { response.IsSuccess = true response.Message = "Successfully new Domain Class Attribute to elastic search" request.Log(response.Message) maxCount = "0" } } else { request.Log("Successfully retrieved object from Elastic Search") var currentMap map[string]interface{} currentMap = make(map[string]interface{}) byteData, err := data.Source.MarshalJSON() if err != nil { request.Log("Data serialization to read maxCount failed") response.Message = "Data serialization to read maxCount failed" return response } json.Unmarshal(byteData, ¤tMap) maxCount = currentMap["maxCount"].(string) } //Increment by 100 and update tempCount, err := strconv.Atoi(maxCount) maxCount = strconv.Itoa(tempCount + (len(request.Body.Objects) - startIndex)) request.Log("Updating Domain Class Attribute table") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = maxCount newRecord["version"] = uuid.NewV1().String() _, err2 := conn.Index(request.Controls.Namespace, "domainClassAttributes", request.Controls.Class, nil, newRecord) if err2 != nil { request.Log("Inserting to Elastic Failed") response.Message = "Inserting to Elastic Failed" return response } else { request.Log("Inserting to Elastic Successfull") response.Message = "Inserting to Elastic Successfull" response.IsSuccess = true request.Log(response.Message) } } for _, obj := range request.Body.Objects[startIndex:len(request.Body.Objects)] { temp := "" nosqlid := "" if isGUIDKey { request.Log("GUIDKey keys requested") nosqlid = getNoSqlKeyByGUID(request) itemArray := strings.Split(nosqlid, (request.Controls.Namespace + "." + request.Controls.Class + ".")) obj[request.Body.Parameters.KeyProperty] = itemArray[1] temp = itemArray[1] } else if isAutoIncrementKey { currentIndex += 1 nosqlid = request.Controls.Namespace + "." + request.Controls.Class + "." + strconv.Itoa(currentIndex) obj[request.Body.Parameters.KeyProperty] = strconv.Itoa(currentIndex) temp = strconv.Itoa(currentIndex) } else { nosqlid = getNoSqlKeyById(request, obj) temp = obj[request.Body.Parameters.KeyProperty].(string) } fmt.Println(temp) CountIndex++ Data[strconv.Itoa(CountIndex)] = temp indexer.Index(request.Controls.Namespace, request.Controls.Class, nosqlid, "10", &nowTime, obj, false) indexer2.Index(getKibanaElasticName(request), getKibanaElasticName(request), nosqlid, "10", &nowTime, obj, false) } indexer.Start() numerrors := indexer.NumErrors() indexer2.Start() numerrors2 := indexer2.NumErrors() if numerrors != 0 && numerrors2 != 0 { request.Log("Elastic Search bulk insert error") response.GetErrorResponse("Elastic Search bulk insert error") } else { response.IsSuccess = true response.Message = "Successfully inserted bulk to Elastic Search" request.Log(response.Message) } } else { indexer := conn.NewBulkIndexer(100) indexer2 := conn.NewBulkIndexer(100) nowTime := time.Now() if isAutoIncrementKey { //Read maxCount from domainClassAttributes table request.Log("Reading the max count") classkey := request.Controls.Class data, err := conn.Get(request.Controls.Namespace, "domainClassAttributes", classkey, nil) if err != nil { request.Log("No record Found. This is a NEW record. Inserting new attribute value") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = "0" newRecord["version"] = uuid.NewV1().String() _, err = conn.Index(request.Controls.Namespace, "domainClassAttributes", classkey, nil, newRecord) if err != nil { errorMessage := "Failed to create new Domain Class Attribute entry." request.Log(errorMessage) return response } else { response.IsSuccess = true response.Message = "Successfully new Domain Class Attribute to elastic search" request.Log(response.Message) maxCount = "0" } } else { request.Log("Successfully retrieved object from Elastic Search") var currentMap map[string]interface{} currentMap = make(map[string]interface{}) byteData, err := data.Source.MarshalJSON() if err != nil { request.Log("Data serialization to read maxCount failed") response.Message = "Data serialization to read maxCount failed" return response } json.Unmarshal(byteData, ¤tMap) maxCount = currentMap["maxCount"].(string) } //Increment by 100 and update tempCount, err := strconv.Atoi(maxCount) maxCount = strconv.Itoa(tempCount + 100) request.Log("Updating Domain Class Attribute table") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = maxCount newRecord["version"] = uuid.NewV1().String() _, err2 := conn.Index(request.Controls.Namespace, "domainClassAttributes", request.Controls.Class, nil, newRecord) if err2 != nil { request.Log("Inserting to Elastic Failed") response.Message = "Inserting to Elastic Failed" return response } else { request.Log("Inserting to Elastic Successfull") response.Message = "Inserting to Elastic Successfull" response.IsSuccess = true request.Log(response.Message) } } for _, obj := range request.Body.Objects[startIndex:stopIndex] { nosqlid := "" temp := "" if isGUIDKey { nosqlid = getNoSqlKeyByGUID(request) itemArray := strings.Split(nosqlid, (request.Controls.Namespace + "." + request.Controls.Class + ".")) obj[request.Body.Parameters.KeyProperty] = itemArray[1] temp = itemArray[1] } else if isAutoIncrementKey { currentIndex += 1 nosqlid = request.Controls.Namespace + "." + request.Controls.Class + "." + strconv.Itoa(currentIndex) obj[request.Body.Parameters.KeyProperty] = strconv.Itoa(currentIndex) temp = strconv.Itoa(currentIndex) } else { nosqlid = getNoSqlKeyById(request, obj) temp = obj[request.Body.Parameters.KeyProperty].(string) } fmt.Println(temp) CountIndex++ Data[strconv.Itoa(CountIndex)] = temp indexer.Index(request.Controls.Namespace, request.Controls.Class, nosqlid, "10", &nowTime, obj, false) indexer2.Index(getKibanaElasticName(request), getKibanaElasticName(request), nosqlid, "10", &nowTime, obj, false) } indexer.Start() numerrors := indexer.NumErrors() indexer2.Start() numerrors2 := indexer.NumErrors() if numerrors != 0 && numerrors2 != 0 { request.Log("Elastic Search bulk insert error") response.GetErrorResponse("Elastic Search bulk insert error") } else { response.IsSuccess = true response.Message = "Successfully inserted bulk to Elastic Search" request.Log(response.Message) } } startIndex += 100 stopIndex += 100 } fmt.Println(Data) //Update Response var DataMap []map[string]interface{} DataMap = make([]map[string]interface{}, 1) var actualInput map[string]interface{} actualInput = make(map[string]interface{}) actualInput["ID"] = Data DataMap[0] = actualInput response.Data = DataMap return response }
func setOneElastic(request *messaging.ObjectRequest) RepositoryResponse { response := RepositoryResponse{} conn := getConnection()(request) isAutoIncrementing := false isRandomKeyID := false returnID := "" if (request.Controls.Id == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementing = true } if (request.Controls.Id == "-888") || (request.Body.Parameters.GUIDKey == true) { isRandomKeyID = true } if isRandomKeyID { request.Log("GUID Key Selected!") key := getNoSqlKeyByGUID(request) itemArray := strings.Split(key, (request.Controls.Namespace + "." + request.Controls.Class + ".")) request.Body.Object[request.Body.Parameters.KeyProperty] = itemArray[1] returnID = itemArray[1] _, err := conn.Index(request.Controls.Namespace, request.Controls.Class, key, nil, request.Body.Object) _, err = conn.Index(getKibanaElasticName(request), getKibanaElasticName(request), key, nil, request.Body.Object) if err != nil { errorMessage := "Elastic Search Single Insert Error : " + err.Error() request.Log(errorMessage) response.GetErrorResponse(errorMessage) } else { response.IsSuccess = true request.Log(response.Message) response.Message = "Successfully inserted one to elastic search" response.Message = key } } else if isAutoIncrementing { //Read maxCount from domainClassAttributes table request.Log("Automatic Increment Key Selected!") key := request.Controls.Class data, err := conn.Get(request.Controls.Namespace, "domainClassAttributes", key, nil) maxCount := "" if err != nil { request.Log("No record Found. This is a NEW record. Inserting new attribute value") var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = "0" newRecord["version"] = uuid.NewV1().String() _, err = conn.Index(request.Controls.Namespace, "domainClassAttributes", key, nil, newRecord) if err != nil { errorMessage := "Elastic Search Insert Error : " + err.Error() request.Log(errorMessage) return response } else { response.IsSuccess = true response.Message = "Successfully inserted one to elastic search" request.Log(response.Message) maxCount = "0" } } else { request.Log("Successfully retrieved object from Elastic Search") var currentMap map[string]interface{} currentMap = make(map[string]interface{}) byteData, err := data.Source.MarshalJSON() if err != nil { request.Log("Data serialization to read maxCount failed") response.Message = "Data serialization to read maxCount failed" return response } json.Unmarshal(byteData, ¤tMap) maxCount = currentMap["maxCount"].(string) } tempCount, err := strconv.Atoi(maxCount) maxCount = strconv.Itoa(tempCount + 1) request.Log("Inserting Actual data Body") recordKey := request.Controls.Namespace + "." + request.Controls.Class + "." + maxCount request.Body.Object[request.Body.Parameters.KeyProperty] = maxCount returnID = maxCount _, err1 := conn.Index(getKibanaElasticName(request), getKibanaElasticName(request), recordKey, nil, request.Body.Object) _, err1 = conn.Index(request.Controls.Namespace, request.Controls.Class, recordKey, nil, request.Body.Object) //Update the Count var newRecord map[string]interface{} newRecord = make(map[string]interface{}) newRecord["class"] = request.Controls.Class newRecord["maxCount"] = maxCount newRecord["version"] = uuid.NewV1().String() _, err2 := conn.Index(request.Controls.Namespace, "domainClassAttributes", request.Controls.Class, nil, newRecord) if err1 != nil || err2 != nil { request.Log("Inserting to Elastic Failed") response.Message = "Inserting to Elastic Failed" return response } else { request.Log("Inserting to Elastic Successfull") response.Message = "Inserting to Elastic Successfull" response.IsSuccess = true request.Log(response.Message) response.Message = recordKey } } else { request.Log("Manual ID Selected!") key := getNoSqlKey(request) request.Log("Inserting single object to Elastic Search : " + key) returnID = request.Controls.Id _, err := conn.Index(request.Controls.Namespace, request.Controls.Class, key, nil, request.Body.Object) _, err = conn.Index(getKibanaElasticName(request), getKibanaElasticName(request), key, nil, request.Body.Object) if err != nil { errorMessage := "Elastic Search Single Insert Error : " + err.Error() request.Log(errorMessage) response.GetErrorResponse(errorMessage) return response } else { response.IsSuccess = true response.Message = "Successfully inserted one to elastic search" request.Log(response.Message) response.Message = key } } //Update Response var Data []map[string]interface{} Data = make([]map[string]interface{}, 1) var actualData map[string]interface{} actualData = make(map[string]interface{}) actualData["ID"] = returnID Data[0] = actualData response.Data = Data return response }
func getCassandraRecordID(request *messaging.ObjectRequest, obj map[string]interface{}) (returnID string) { isGUIDKey := false isAutoIncrementId := false //else MANUAL key from the user if obj == nil { //single request if (request.Controls.Id == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (request.Controls.Id == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } else { //multiple requests if (obj[request.Body.Parameters.KeyProperty].(string) == "-999") || (request.Body.Parameters.AutoIncrement == true) { isAutoIncrementId = true } if (obj[request.Body.Parameters.KeyProperty].(string) == "-888") || (request.Body.Parameters.GUIDKey == true) { isGUIDKey = true } } if isGUIDKey { request.Log("GUID Key generation requested!") returnID = uuid.NewV1().String() } else if isAutoIncrementId { request.Log("Automatic Increment Key generation requested!") session, isError, _ := getCassandraConnection(request) if isError { returnID = "" request.Log("Connecting to Cassandra Failed!") } else { //read Attributes table iter2 := session.Query("SELECT maxCount FROM domainClassAttributes where class = '" + request.Controls.Class + "'").Iter() result, _ := iter2.SliceMap() iter2.Close() if len(result) == 0 { request.Log("This is a freshly created namespace. Inserting new Class record.") err := session.Query("INSERT INTO domainClassAttributes (class,maxCount,version) VALUES ('" + request.Controls.Class + "'," + "'1'" + ",'" + uuid.NewV1().String() + "');").Exec() if err != nil { request.Log("Error inserting new record to domainClassAttributes") return "" } returnID = "1" } else { //get current count var UpdatedCount int for _, value := range result { for fieldName, fieldvalue := range value { if strings.ToLower(fieldName) == "maxcount" { UpdatedCount, _ = strconv.Atoi(fieldvalue.(string)) UpdatedCount++ returnID = strconv.Itoa(UpdatedCount) break } } } //save to attributes table err := session.Query("UPDATE domainClassAttributes SET maxCount ='" + strconv.Itoa(UpdatedCount) + "' WHERE class =" + "'" + request.Controls.Class + "'").Exec() if err != nil { request.Log("Update of maxCount Failed") returnID = "" } } } } else { request.Log("Manual Key requested!") if obj == nil { returnID = request.Controls.Id } else { returnID = obj[request.Body.Parameters.KeyProperty].(string) } } return }
func ExampleSetupFileSystemStateSaver() { var config = uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute} uuid.SetupFileSystemStateSaver(config) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) }