Example #1
0
        private async ValueTask RemapEntityKeys(KeyMapping keyMapping, bool isAsync, CancellationToken token = default)
        {
            if (keyMapping.Map.Count == 0)
            {
                return;
            }
            using (Activate()) {
                if (!LazyKeyGenerationIsEnabled)
                {
                    await Persist(PersistReason.RemapEntityKeys, isAsync, token).ConfigureAwait(false);

                    Invalidate();
                }
                if (IsDebugEventLoggingEnabled)
                {
                    OrmLog.Debug(Strings.LogSessionXRemappingEntityKeys, this);
                }

                foreach (var entityState in EntityChangeRegistry.GetItems(PersistenceState.New))
                {
                    var key         = entityState.Key;
                    var remappedKey = keyMapping.TryRemapKey(key);
                    if (remappedKey != key)
                    {
                        entityState.RemapKey(remappedKey);
                    }
                    EntityStateCache.Add(entityState);
                }
                ProcessChangesOfEntitySets(entitySetState => entitySetState.RemapKeys(keyMapping));
                EntityEvents.RemapKeys(keyMapping);
            }
        }
Example #2
0
 internal void RemapEntityKeys(KeyMapping keyMapping)
 {
     if (keyMapping.Map.Count == 0)
     {
         return;
     }
     using (Activate()) {
         if (!LazyKeyGenerationIsEnabled)
         {
             Persist(PersistReason.RemapEntityKeys);
             Invalidate();
         }
         OrmLog.Debug(Strings.LogSessionXRemappingEntityKeys, this);
         foreach (var entityState in EntityChangeRegistry.GetItems(PersistenceState.New))
         {
             var key         = entityState.Key;
             var remappedKey = keyMapping.TryRemapKey(key);
             if (remappedKey != key)
             {
                 entityState.RemapKey(remappedKey);
             }
             EntityStateCache.Add(entityState);
         }
         ProcessChangesOfEntitySets(entitySetState => entitySetState.RemapKeys(keyMapping));
         EntityEvents.RemapKeys(keyMapping);
     }
 }
Example #3
0
        /// <inheritdoc/>
        public KeyMapping Replay(Session session)
        {
            if (session.Operations.IsRegisteringOperation)
            {
                throw new InvalidOperationException(Strings.ExRunningOperationRegistrationMustBeFinished);
            }

            var         executionContext     = new OperationExecutionContext(session);
            bool        isSystemOperationLog = LogType == OperationLogType.SystemOperationLog;
            KeyMapping  keyMapping           = null;
            Transaction transaction          = null;

            using (session.Activate()) {
                using (isSystemOperationLog ? session.OpenSystemLogicOnlyRegion() : null)
                    using (var tx = session.OpenTransaction(TransactionOpenMode.New)) {
                        foreach (var operation in operations)
                        {
                            operation.Prepare(executionContext);
                        }

                        session.Query.Many <Entity>(executionContext.KeysToPrefetch).Run();

                        foreach (var operation in operations)
                        {
                            var identifierToKey = new Dictionary <string, Key>();
                            var handler         = new EventHandler <OperationCompletedEventArgs>((sender, e) => {
                                foreach (var pair in e.Operation.IdentifiedEntities)
                                {
                                    identifierToKey.Add(pair.Key, pair.Value);
                                }
                            });

                            session.Operations.OutermostOperationCompleted += handler;
                            try {
                                operation.Execute(executionContext);
                            }
                            finally {
                                session.Operations.OutermostOperationCompleted -= handler;
                            }

                            foreach (var pair in operation.IdentifiedEntities)
                            {
                                string identifier = pair.Key;
                                var    oldKey     = pair.Value;
                                var    newKey     = identifierToKey.GetValueOrDefault(identifier);
                                if (newKey != null)
                                {
                                    executionContext.AddKeyMapping(oldKey, newKey);
                                }
                            }
                        }

                        keyMapping = new KeyMapping(executionContext.KeyMapping);

                        tx.Complete();
                    }
                return(keyMapping);
            }
        }
        /// <summary>
        /// Remaps the event keys in accordance with specified <paramref name="keyMapping"/>.
        /// </summary>
        /// <param name="keyMapping">The key mapping.</param>
        public void RemapKeys(KeyMapping keyMapping)
        {
            if (subscribers == null || subscribers.Count == 0)
            {
                return;
            }
            var copy = new Dictionary <Triplet <Key, FieldInfo, object>, Delegate>(subscribers);

            subscribers.Clear();
            foreach (var kvp in copy)
            {
                var triplet    = kvp.Key;
                var subscriber = kvp.Value;
                if (subscriber == null) // Strange, but there is report is can be null: http://goo.gl/W6xo
                {
                    continue;
                }
                var key = keyMapping.TryRemapKey(triplet.First);
                AddSubscriber(key, triplet.Second, triplet.Third, kvp.Value);
            }
        }
Example #5
0
 internal void RemapEntityKeys(KeyMapping keyMapping) =>
 RemapEntityKeys(keyMapping, false).GetAwaiter().GetResult();