public void Initialize() { List <Delta> result = ReadAllDeltas(); affectedEntities = new AffectedEntities(); if (result.Count <= 0) { return; } GetDictionaries().GetAwaiter().GetResult(); foreach (var delta in result) { ResourceDescription tempRd = new ResourceDescription(); GetShallowCopyModel(); try { foreach (ResourceDescription rd in delta.InsertOperations) { InsertEntity(rd, out tempRd); } foreach (ResourceDescription rd in delta.UpdateOperations) { UpdateEntity(rd); } foreach (ResourceDescription rd in delta.DeleteOperations) { DeleteEntity(rd); } MergeModelsFinal(); } catch (Exception ex) { CommonTrace.WriteTrace(CommonTrace.TraceError, "Error while applying delta (id = {0}) during service initialization. {1}", delta.Id, ex.Message); RestoreModel(); } } if (!TryApplyTransaction()) { RestoreModel(); } }
public UpdateResult ApplyDelta(Delta delta) { GetDictionaries().GetAwaiter().GetResult(); bool applyingStarted = false; bool transactionSucceded = false; UpdateResult updateResult = new UpdateResult(); Delta newDelta = new Delta(); GetShallowCopyModel(); affectedEntities = new AffectedEntities(); try { CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Applying delta to network model."); Dictionary <short, int> typesCounters = GetCounters(); Dictionary <long, long> globalIdPairs = new Dictionary <long, long>(); var result = GetAllCounters(typesCounters); Delta.Counter = result; delta.FixNegativeToPositiveIds(ref typesCounters, ref globalIdPairs); updateResult.GlobalIdPairs = globalIdPairs; delta.SortOperations(); applyingStarted = true; foreach (ResourceDescription rd in delta.InsertOperations) { // We need newRd because old-new mapping InsertEntity(rd, out ResourceDescription newRd); newDelta.AddDeltaOperation(DeltaOpType.Insert, newRd, true); } #region Update&Delete foreach (ResourceDescription rd in delta.UpdateOperations) { UpdateEntity(rd); } foreach (ResourceDescription rd in delta.DeleteOperations) { DeleteEntity(rd); } #endregion Update&Delete SetDictionaries().GetAwaiter().GetResult(); transactionSucceded = TryApplyTransaction(); if (transactionSucceded) { MergeModelsFinal(); } } catch (Exception ex) { string message = string.Format("Applying delta to network model failed. {0}.", ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); applyingStarted = false; updateResult.Result = ResultType.Failed; updateResult.Message = message; } finally { if (applyingStarted && transactionSucceded) { if (newDelta.InsertOperations.Count > 0) { SaveDelta(delta); } } else { RestoreModel(); } if (updateResult.Result == ResultType.Succeeded) { string mesage = "Applying delta to network model successfully finished."; CommonTrace.WriteTrace(CommonTrace.TraceInfo, mesage); updateResult.Message = mesage; } } return(updateResult); }