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); }
private void SaveEmployeeEvents( Employee employee ) { var patches = new List<PatchRequest>(); foreach (var evt in employee.PendingEvents) { patches.Add(new PatchRequest { Type = PatchCommandType.Add, Name = "Events", Value = RavenJObject.FromObject(evt, _serializer) }); } var localId = $"employees/{employee.Id}"; var addEmployeeEvents = new PatchCommandData() { Key = localId, Patches = patches.ToArray() }; var updateMetadata = new ScriptedPatchCommandData() { Key = localId, Patch = new ScriptedPatchRequest { Script = $"this['@metadata']['{EmployeeEntityVersion}'] = {employee.Version}; " } }; _store.DatabaseCommands.Batch(new ICommandData[] { addEmployeeEvents, updateMetadata }); }