Ejemplo n.º 1
0
        public UpdateResult ApplyDelta(Delta delta)
        {
            bool         applyingStarted = false;
            UpdateResult updateResult    = new UpdateResult();

            try
            {
                CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Applying  delta to network model.");

                Dictionary <short, int> typesCounters = GetCounters();
                Dictionary <long, long> globalIdPairs = new Dictionary <long, long>();
                delta.FixNegativeToPositiveIds(ref typesCounters, ref globalIdPairs);
                updateResult.GlobalIdPairs = globalIdPairs;
                delta.SortOperations();

                applyingStarted = true;

                foreach (ResourceDescription rd in delta.InsertOperations)
                {
                    InsertEntity(rd);
                }

                foreach (ResourceDescription rd in delta.UpdateOperations)
                {
                    UpdateEntity(rd);
                }

                foreach (ResourceDescription rd in delta.DeleteOperations)
                {
                    DeleteEntity(rd);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("Applying delta to network model failed. {0}.", ex.Message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                updateResult.Result  = ResultType.Failed;
                updateResult.Message = message;
            }
            finally
            {
                //if (applyingStarted)
                //{
                //	SaveDelta(delta);
                //}

                deltaToCommit = delta;


                if (updateResult.Result == ResultType.Succeeded)
                {
                    string mesage = "Applying delta to network model successfully finished.";
                    CommonTrace.WriteTrace(CommonTrace.TraceInfo, mesage);
                    updateResult.Message = mesage;
                }
            }

            return(updateResult);
        }
Ejemplo n.º 2
0
        public UpdateResult ApplyDelta(Delta delta)
        {
            bool         ValideDelta  = false;
            UpdateResult updateResult = new UpdateResult();

            try
            {
                LogHelper.Log(LogTarget.File, LogService.NMSNetworkModel, " INFO - NetworkModel.cs - Applying  delta to network model.");

                Dictionary <short, int> typesCounters = GetCounters();
                Dictionary <long, long> globalIdPairs = new Dictionary <long, long>();
                delta.FixNegativeToPositiveIds(ref typesCounters, ref globalIdPairs);
                updateResult.GlobalIdPairs = globalIdPairs;
                delta.SortOperations();

                ValideDelta = smartContainer.ApplayDelta(delta);
            }
            catch (Exception ex)
            {
                string message = string.Format("Applying delta to network model failed. {0}.", ex.Message);
                LogHelper.Log(LogTarget.File, LogService.NMSNetworkModel, " ERROR - NetworkModel.cs - " + message);

                updateResult.Result  = ResultType.Failed;
                updateResult.Message = message;
            }
            finally
            {
                if (ValideDelta)
                {
                    smartContainer.SaveDelta(delta);
                }

                if (updateResult.Result == ResultType.Succeeded)
                {
                    string message = "Applying delta to network model successfully finished.";
                    LogHelper.Log(LogTarget.File, LogService.NMSNetworkModel, " INFO - NetworkModel.cs - " + message);
                    updateResult.Message = message;
                }
            }

            return(updateResult);
        }
Ejemplo n.º 3
0
        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);
        }
        public async Task <UpdateResult> ApplyDelta(Delta delta)
        {
            bool         applyingStarted = false;
            UpdateResult updateResult    = new UpdateResult();

            try
            {
                networkDataModel = new Dictionary <DMSType, Container>();
                Logger.LogInformation("Applying  delta to network model.");
                var deltaDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <long, Delta> >(this.deltasDictionaryName);

                using (var tx = stateManager.CreateTransaction())
                {
                    var count = await deltaDictionary.GetCountAsync(tx);

                    if (await deltaDictionary.GetCountAsync(tx) > 0)
                    {
                        await deltaDictionary.ClearAsync();

                        await tx.CommitAsync();
                    }
                }

                Dictionary <short, int> typesCounters = GetCounters();
                Dictionary <long, long> globalIdPairs = new Dictionary <long, long>();
                delta.FixNegativeToPositiveIds(ref typesCounters, ref globalIdPairs);
                updateResult.GlobalIdPairs = globalIdPairs;
                delta.SortOperations();

                applyingStarted = true;

                foreach (ResourceDescription rd in delta.InsertOperations)
                {
                    InsertEntity(rd);
                }

                foreach (ResourceDescription rd in delta.UpdateOperations)
                {
                    UpdateEntity(rd);
                }

                foreach (ResourceDescription rd in delta.DeleteOperations)
                {
                    DeleteEntity(rd);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("Applying delta to network model failed. {0}.", ex.Message);
                Logger.LogError(message);

                updateResult.Result  = ResultType.Failed;
                updateResult.Message = message;
            }
            finally
            {
                if (applyingStarted)
                {
                    try
                    {
                        await SaveDelta(delta);
                    }
                    catch (Exception e)
                    {
                        string message = $"Error on save delta. Error message = {e.Message}";
                        Logger.LogError(message);
                    }
                }

                if (updateResult.Result == ResultType.Succeeded)
                {
                    string mesage = "Applying delta to network model successfully finished.";
                    Logger.LogError(mesage);
                    updateResult.Message = mesage;
                }
            }

            return(updateResult);
        }