public static string GenerateUniqueIdentityRecordId(string uniqueValue, ISagaInfo sagaInfo) { // use MD5 hash to get a 16-byte hash of the string using (var provider = new MD5CryptoServiceProvider()) { var inputBytes = Encoding.Default.GetBytes(uniqueValue); byte[] hashBytes = provider.ComputeHash(inputBytes); // generate a guid from the hash: var value = new Guid(hashBytes); var id = string.Format("{0}/{1}", sagaInfo.SagaUniqueIdentityKeyPrefix, value); // raven has a size limit of 255 bytes == 127 unicode chars if (id.Length > 127) { // generate a guid from the hash: var key = new Guid( provider.ComputeHash(Encoding.Default.GetBytes(sagaInfo.SagaDataTypeFullName + sagaInfo.UniqueField))); id = string.Format("MoreThan127/{0}/{1}", key, value); } return id; } }
public static string GenerateUniqueIdentityRecordId(string uniqueValue, ISagaInfo sagaInfo) { // use MD5 hash to get a 16-byte hash of the string using (var provider = new MD5CryptoServiceProvider()) { var inputBytes = Encoding.Default.GetBytes(uniqueValue); byte[] hashBytes = provider.ComputeHash(inputBytes); // generate a guid from the hash: var value = new Guid(hashBytes); var id = string.Format("{0}/{1}", sagaInfo.SagaUniqueIdentityKeyPrefix, value); // raven has a size limit of 255 bytes == 127 unicode chars if (id.Length > 127) { // generate a guid from the hash: var key = new Guid( provider.ComputeHash(Encoding.Default.GetBytes(sagaInfo.SagaDataTypeFullName + sagaInfo.UniqueField))); id = string.Format("MoreThan127/{0}/{1}", key, value); } return(id); } }
public void Run(ISagaInfo sagaInfo) { PrintHelper.PrintFunctionCallDetails("SetSagaUniqueValueInMetadata", sagaInfo); var documentStore = new DocumentStore { Url = sagaInfo.RavenUrl, DefaultDatabase = sagaInfo.DatabaseName }; documentStore.Initialize(); const int pageSize = 100; var commandsList = new List <ICommandData>(); for (var start = 0; ; start += pageSize) { var documents = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in documents) { if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && !DocumentHelper.HasServicebusUniqueValue(document)) { var body = document.DataAsJson; var uniqueValue = body.Value <string>(sagaInfo.UniqueField); var patchRequests = new[] { new PatchRequest { Type = PatchCommandType.Modify, Name = "@metadata", Nested = new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "NServiceBus-UniqueValue", Value = uniqueValue, } } } }; ICommandData command = new PatchCommandData { Key = document.Key, Patches = patchRequests }; commandsList.Add(command); } } if (documents.Length < pageSize) { break; } } Console.WriteLine("Attempt to patch {0} saga documents", commandsList.Count); var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray()); PrintHelper.PrintResults("SetSagaUniqueValueInMetadata", results); }
public static void PrintFunctionCallDetails(string name, ISagaInfo sagaInfo) { Console.WriteLine("***"); Console.WriteLine("BEGIN: Start running {0} at {1}", name, DateTime.Now); Console.WriteLine("{0}: {1}", "SagaDataTypeFullName", sagaInfo.SagaDataTypeFullName); Console.WriteLine("{0}: {1}", "DatabaseName", sagaInfo.DatabaseName); Console.WriteLine("{0}: {1}", "SagaKeyPrefix", sagaInfo.SagaKeyPrefix); Console.WriteLine("{0}: {1}", "SagaUniqueIdentityKeyPrefix", sagaInfo.SagaUniqueIdentityKeyPrefix); Console.WriteLine("{0}: {1}", "UniqueField", sagaInfo.UniqueField); Console.WriteLine("***"); }
public void Run(ISagaInfo sagaInfo) { PrintHelper.PrintFunctionCallDetails("SetSagaUniqueValueInMetadata", sagaInfo); var documentStore = new DocumentStore { Url = sagaInfo.RavenUrl, DefaultDatabase = sagaInfo.DatabaseName }; documentStore.Initialize(); const int pageSize = 100; var commandsList = new List<ICommandData>(); for (var start = 0; ; start += pageSize) { var documents = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in documents) { if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && !DocumentHelper.HasServicebusUniqueValue(document)) { var body = document.DataAsJson; var uniqueValue = body.Value<string>(sagaInfo.UniqueField); var patchRequests = new[] { new PatchRequest { Type = PatchCommandType.Modify, Name = "@metadata", Nested = new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "NServiceBus-UniqueValue", Value = uniqueValue, } } } }; ICommandData command = new PatchCommandData { Key = document.Key, Patches = patchRequests }; commandsList.Add(command); } } if (documents.Length < pageSize) break; } Console.WriteLine("Attempt to patch {0} saga documents", commandsList.Count); var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray()); PrintHelper.PrintResults("SetSagaUniqueValueInMetadata", results); }
public static List<string> GetExistingSagaUniqueIdentityUniqueValues(DocumentStore documentStore, ISagaInfo sagaInfo) { const int pageSize = 100; var keys = new List<string>(); for (var start = 0; ; start += pageSize) { var documents = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in documents) { if (document.Key.StartsWith(sagaInfo.SagaUniqueIdentityKeyPrefix)) { keys.Add(document.DataAsJson.Value<string>("UniqueValue")); } } if (documents.Length < pageSize) break; } return keys; }
public void Run(ISagaInfo sagaInfo) { PrintHelper.PrintFunctionCallDetails("PopulateSagaUniqueIdentityRecords", sagaInfo); var documentStore = new DocumentStore { Url = sagaInfo.RavenUrl, DefaultDatabase = sagaInfo.DatabaseName }; documentStore.Initialize(); var existingSagaUniqueIdentityUniqueValues = DocumentHelper.GetExistingSagaUniqueIdentityUniqueValues(documentStore, sagaInfo); var commandsList = new List <ICommandData>(); var dictionarytest = new Dictionary <string, int>(); const int pageSize = 100; for (var start = 0; ; start += pageSize) { var sagaDocuments = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in sagaDocuments) { if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && DocumentHelper.HasServicebusUniqueValue(document)) { var body = document.DataAsJson; var uniqueValue = body.Value <string>(sagaInfo.UniqueField); if (existingSagaUniqueIdentityUniqueValues.Exists(x => x == uniqueValue)) { PrintHelper.PrintMessage(string.Format("Skip uniquevalue {0} because it already exists", uniqueValue)); continue; // Already has SagaUniqueIdentity record } var sagaId = document.Key.Replace(sagaInfo.SagaKeyPrefix + "/", ""); var documentBody = new RavenJObject(); documentBody.Add("SagaId", sagaId); documentBody.Add("UniqueValue", uniqueValue); documentBody.Add("SagaDocId", document.Key); var documentMetadata = new RavenJObject(); documentMetadata.Add("Content-Type", "application/json; charset=utf-8"); documentMetadata.Add("Raven-Entity-Name", "SagaUniqueIdentity"); documentMetadata.Add("Raven-Clr-Type", "NServiceBus.SagaPersisters.Raven.SagaUniqueIdentity, NServiceBus.Core"); ICommandData command = new PutCommandData { Key = DocumentHelper.GenerateUniqueIdentityRecordId(uniqueValue, sagaInfo), Document = documentBody, Metadata = documentMetadata, }; if (dictionarytest.ContainsKey(uniqueValue)) { dictionarytest[uniqueValue] += 1; } else { dictionarytest[uniqueValue] = 1; } commandsList.Add(command); } } if (sagaDocuments.Length < pageSize) { break; } } PrintHelper.PrintMessage(string.Format("Attempt to insert {0} SagaUniqueIdentity documents", commandsList.Count)); foreach (var item in dictionarytest.Keys) { if (dictionarytest[item] > 1) { PrintHelper.PrintMessage("the following has duplicate " + item + " : " + dictionarytest[item]); } } var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray()); PrintHelper.PrintResults("PopulateSagaUniqueIdentityRecords", results); }
public void Run(ISagaInfo sagaInfo) { PrintHelper.PrintFunctionCallDetails("PopulateSagaUniqueIdentityRecords", sagaInfo); var documentStore = new DocumentStore { Url = sagaInfo.RavenUrl, DefaultDatabase = sagaInfo.DatabaseName }; documentStore.Initialize(); var existingSagaUniqueIdentityUniqueValues = DocumentHelper.GetExistingSagaUniqueIdentityUniqueValues(documentStore, sagaInfo); var commandsList = new List<ICommandData>(); var dictionarytest = new Dictionary<string, int>(); const int pageSize = 100; for (var start = 0; ; start += pageSize) { var sagaDocuments = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in sagaDocuments) { if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && DocumentHelper.HasServicebusUniqueValue(document)) { var body = document.DataAsJson; var uniqueValue = body.Value<string>(sagaInfo.UniqueField); if (existingSagaUniqueIdentityUniqueValues.Exists(x => x == uniqueValue)) { PrintHelper.PrintMessage(string.Format("Skip uniquevalue {0} because it already exists", uniqueValue)); continue; // Already has SagaUniqueIdentity record } var sagaId = document.Key.Replace(sagaInfo.SagaKeyPrefix + "/", ""); var documentBody = new RavenJObject(); documentBody.Add("SagaId", sagaId); documentBody.Add("UniqueValue", uniqueValue); documentBody.Add("SagaDocId", document.Key); var documentMetadata = new RavenJObject(); documentMetadata.Add("Content-Type", "application/json; charset=utf-8"); documentMetadata.Add("Raven-Entity-Name", "SagaUniqueIdentity"); documentMetadata.Add("Raven-Clr-Type", "NServiceBus.SagaPersisters.Raven.SagaUniqueIdentity, NServiceBus.Core"); ICommandData command = new PutCommandData { Key = DocumentHelper.GenerateUniqueIdentityRecordId(uniqueValue, sagaInfo), Document = documentBody, Metadata = documentMetadata, }; if (dictionarytest.ContainsKey(uniqueValue)) { dictionarytest[uniqueValue] += 1; } else { dictionarytest[uniqueValue] = 1; } commandsList.Add(command); } } if (sagaDocuments.Length < pageSize) break; } PrintHelper.PrintMessage(string.Format("Attempt to insert {0} SagaUniqueIdentity documents", commandsList.Count)); foreach (var item in dictionarytest.Keys) { if (dictionarytest[item] > 1) { PrintHelper.PrintMessage("the following has duplicate " + item + " : " + dictionarytest[item]); } } var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray()); PrintHelper.PrintResults("PopulateSagaUniqueIdentityRecords", results); }
public static List <string> GetExistingSagaUniqueIdentityUniqueValues(DocumentStore documentStore, ISagaInfo sagaInfo) { const int pageSize = 100; var keys = new List <string>(); for (var start = 0; ; start += pageSize) { var documents = documentStore.DatabaseCommands.GetDocuments(start, pageSize); foreach (var document in documents) { if (document.Key.StartsWith(sagaInfo.SagaUniqueIdentityKeyPrefix)) { keys.Add(document.DataAsJson.Value <string>("UniqueValue")); } } if (documents.Length < pageSize) { break; } } return(keys); }