Beispiel #1
0
        public IList <IObjRef> EntityToAllObjRefs(Object entity)
        {
            if (entity is IEntityMetaDataHolder)
            {
                return(EntityToAllObjRefs(entity, ((IEntityMetaDataHolder)entity).Get__EntityMetaData()));
            }
            ILoadContainer lc = (ILoadContainer)entity;

            return(EntityToAllObjRefs(entity, EntityMetaDataProvider.GetMetaData(lc.Reference.RealType)));
        }
Beispiel #2
0
        protected void ScanForInitializedObjectsIntern(Object obj, bool isDeepMerge, IList <Object> objects, IMap <Type, IList <Object> > typeToObjectsToMerge,
                                                       ISet <Object> alreadyHandledObjectsSet, IList <IObjRef> objRefs, IList <ValueHolderRef> valueHolderKeys)
        {
            if (obj == null || !alreadyHandledObjectsSet.Add(obj))
            {
                return;
            }
            if (obj is IList)
            {
                IList list = (IList)obj;
                for (int a = 0, size = list.Count; a < size; a++)
                {
                    ScanForInitializedObjectsIntern(list[a], isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys);
                }
                return;
            }
            else if (obj.GetType().IsArray)
            {
                Array array = (Array)obj;
                for (int a = array.Length; a-- > 0;)
                {
                    Object item = array.GetValue(a);
                    ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys);
                }
                return;
            }
            else if (obj is IEnumerable && !(obj is String))
            {
                foreach (Object item in (IEnumerable)obj)
                {
                    ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys);
                }
                return;
            }
            IEntityMetaData metaData = EntityMetaDataProvider.GetMetaData(obj.GetType(), true);

            if (metaData == null)
            {
                return;
            }
            IObjRef objRef = null;
            Object  id     = metaData.IdMember.GetValue(obj, false);

            if (id != null)
            {
                objRef = ObjRefFactory.CreateObjRef(metaData.EntityType, ObjRef.PRIMARY_KEY_INDEX, id, null);
                objRef.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            }
            if (!(obj is IDataObject) || ((IDataObject)obj).HasPendingChanges)
            {
                if (typeToObjectsToMerge != null)
                {
                    IList <Object> objectsToMerge = typeToObjectsToMerge.Get(metaData.EntityType);
                    if (objectsToMerge == null)
                    {
                        objectsToMerge = new List <Object>();
                        typeToObjectsToMerge.Put(metaData.EntityType, objectsToMerge);
                    }
                    objectsToMerge.Add(obj);
                }
                objects.Add(obj);
                objRefs.Add(objRef);
            }
            if (!isDeepMerge)
            {
                return;
            }
            RelationMember[] relationMembers = metaData.RelationMembers;
            if (relationMembers.Length == 0)
            {
                return;
            }
            IObjRefContainer vhc = (IObjRefContainer)obj;

            for (int relationIndex = relationMembers.Length; relationIndex-- > 0;)
            {
                if (ValueHolderState.INIT != vhc.Get__State(relationIndex))
                {
                    continue;
                }
                RelationMember relationMember = relationMembers[relationIndex];
                Object         item           = relationMember.GetValue(obj);
                if (objRef != null && item != null)
                {
                    ValueHolderRef vhk = new ValueHolderRef(objRef, relationMember, relationIndex);
                    valueHolderKeys.Add(vhk);
                }
                ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys);
            }
        }
Beispiel #3
0
        public virtual ICUDResult MergeDeep(Object obj, MergeHandle handle)
        {
            ICache cache = handle.Cache;

            if (cache == null && CacheFactory != null)
            {
                cache        = CacheFactory.Create(CacheFactoryDirective.NoDCE, false, false, "MergeController.ORIGINAL");
                handle.Cache = cache;
            }
            IMap <Type, IList <Object> > typeToObjectsToMerge = null;

            Type[] entityPersistOrder = EntityMetaDataProvider.GetEntityPersistOrder();
            if (entityPersistOrder != null && entityPersistOrder.Length > 0)
            {
                typeToObjectsToMerge = new HashMap <Type, IList <Object> >();
            }
            List <IObjRef>        objRefs                = new List <IObjRef>();
            List <ValueHolderRef> valueHolderKeys        = new List <ValueHolderRef>();
            IList <Object>        objectsToMerge         = ScanForInitializedObjects(obj, handle.IsDeepMerge, typeToObjectsToMerge, objRefs, valueHolderKeys);
            IList <Object>        eagerlyLoadedOriginals = null;

            // Load all requested object originals in one roundtrip
            if (objRefs.Count > 0)
            {
                eagerlyLoadedOriginals = cache.GetObjects(objRefs, CacheDirective.ReturnMisses);
                for (int a = eagerlyLoadedOriginals.Count; a-- > 0;)
                {
                    IObjRef existingOri = objRefs[a];
                    if (eagerlyLoadedOriginals[a] == null && existingOri != null && existingOri.Id != null)
                    {
                        // Cache miss for an entity we want to merge. This is an OptimisticLock-State
                        throw new OptimisticLockException(null, null, existingOri);
                    }
                }
                List <IObjRef> objRefsOfVhks = new List <IObjRef>(valueHolderKeys.Count);
                for (int a = 0, size = valueHolderKeys.Count; a < size; a++)
                {
                    objRefsOfVhks.Add(valueHolderKeys[a].ObjRef);
                }
                IList <Object> objectsOfVhks = cache.GetObjects(objRefsOfVhks, CacheDirective.FailEarly | CacheDirective.ReturnMisses);
                for (int a = valueHolderKeys.Count; a-- > 0;)
                {
                    IObjRefContainer objectOfVhk = (IObjRefContainer)objectsOfVhks[a];
                    if (objectOfVhk == null)
                    {
                        continue;
                    }
                    ValueHolderRef valueHolderRef = valueHolderKeys[a];
                    if (ValueHolderState.INIT != objectOfVhk.Get__State(valueHolderRef.RelationIndex))
                    {
                        DirectValueHolderRef vhcKey = new DirectValueHolderRef(objectOfVhk, valueHolderRef.Member);
                        handle.PendingValueHolders.Add(vhcKey);
                    }
                }
            }
            if (typeToObjectsToMerge != null)
            {
                foreach (Type orderedEntityType in entityPersistOrder)
                {
                    IList <Object> objectsToMergeOfOrderedType = typeToObjectsToMerge.Remove(orderedEntityType);
                    if (objectsToMergeOfOrderedType == null)
                    {
                        continue;
                    }
                    MergeDeepStart(objectsToMergeOfOrderedType, handle);
                }
                foreach (Entry <Type, IList <Object> > entry in typeToObjectsToMerge)
                {
                    IList <Object> objectsToMergeOfUnorderedType = entry.Value;
                    MergeDeepStart(objectsToMergeOfUnorderedType, handle);
                }
            }
            else if (objectsToMerge.Count > 0)
            {
                MergeDeepStart(objectsToMerge, handle);
            }
            return(CUDResultHelper.CreateCUDResult(handle));
        }
Beispiel #4
0
        protected ICUDResult ApplyIntern(ICUDResult cudResult, bool checkBaseState, IncrementalMergeState incrementalState)
        {
            ICache stateCache = incrementalState.GetStateCache();
            IList <IChangeContainer> allChanges   = cudResult.AllChanges;
            IList <Object>           originalRefs = cudResult.GetOriginalRefs();
            IList <Object>           allObjects   = GetAllExistingObjectsFromCache(stateCache, allChanges);
            List <Object>            hardRefs     = new List <Object>();

            hardRefs.Add(allObjects); // add list as item intended. adding each item of the source is NOT needed

            List <IObjRef> toFetchFromCache             = new List <IObjRef>();
            List <DirectValueHolderRef>      toPrefetch = new List <DirectValueHolderRef>();
            List <IBackgroundWorkerDelegate> runnables  = new List <IBackgroundWorkerDelegate>();

            IEntityFactory entityFactory = this.EntityFactory;

            IdentityHashMap <IObjRef, StateEntry> newObjRefToStateEntryMap        = new IdentityHashMap <IObjRef, StateEntry>();
            IdentityHashMap <IChangeContainer, IChangeContainer> alreadyClonedMap = new IdentityHashMap <IChangeContainer, IChangeContainer>();

            List <IChangeContainer> newAllChanges = new List <IChangeContainer>(allChanges.Count);

            for (int a = 0, size = allChanges.Count; a < size; a++)
            {
                IChangeContainer changeContainer = allChanges[a];
                Object           originalEntity  = originalRefs[a];

                StateEntry stateEntry = incrementalState.entityToStateMap.Get(originalEntity);

                IChangeContainer newChangeContainer;
                if (changeContainer is CreateContainer)
                {
                    newChangeContainer = new CreateContainer();
                }
                else if (changeContainer is UpdateContainer)
                {
                    newChangeContainer = new UpdateContainer();
                }
                else
                {
                    newChangeContainer = new DeleteContainer();
                }
                newAllChanges.Add(newChangeContainer);
                alreadyClonedMap.Put(changeContainer, newChangeContainer);

                if (!(changeContainer is CreateContainer))
                {
                    Object stateCacheEntity2 = allObjects[a];
                    stateEntry = incrementalState.entityToStateMap.Get(stateCacheEntity2);
                    if (stateEntry == null)
                    {
                        stateEntry = new StateEntry(stateCacheEntity2, changeContainer.Reference, incrementalState.entityToStateMap.Count + 1);

                        incrementalState.entityToStateMap.Put(stateCacheEntity2, stateEntry);
                        incrementalState.objRefToStateMap.Put(stateEntry.objRef, stateEntry);
                    }
                    // delete & update do not need further handling
                    continue;
                }
                Type realType = changeContainer.Reference.RealType;

                Object stateCacheEntity;
                if (stateEntry == null)
                {
                    stateCacheEntity = entityFactory.CreateEntity(realType);

                    DirectObjRef directObjRef = new DirectObjRef(realType, stateCacheEntity);
                    directObjRef.CreateContainerIndex = a;

                    stateEntry = new StateEntry(stateCacheEntity, directObjRef, incrementalState.entityToStateMap.Count + 1);

                    incrementalState.entityToStateMap.Put(stateCacheEntity, stateEntry);
                    incrementalState.objRefToStateMap.Put(stateEntry.objRef, stateEntry);
                    newObjRefToStateEntryMap.Put(changeContainer.Reference, stateEntry);
                }
                else
                {
                    stateCacheEntity = stateEntry.entity;
                }
                allObjects[a] = stateCacheEntity;
            }
            cloneStateTL.Value = new CloneState(newObjRefToStateEntryMap, incrementalState);
            try
            {
                for (int a = allChanges.Count; a-- > 0;)
                {
                    IChangeContainer changeContainer = allChanges[a];
                    IObjRefContainer entity          = (IObjRefContainer)allObjects[a];

                    changeContainer = FillClonedChangeContainer(changeContainer, alreadyClonedMap);

                    IPrimitiveUpdateItem[] puis;
                    IRelationUpdateItem[]  ruis;
                    if (changeContainer is CreateContainer)
                    {
                        CreateContainer createContainer = (CreateContainer)changeContainer;
                        puis = createContainer.Primitives;
                        ruis = createContainer.Relations;
                    }
                    else if (changeContainer is UpdateContainer)
                    {
                        UpdateContainer updateContainer = (UpdateContainer)changeContainer;
                        puis = updateContainer.Primitives;
                        ruis = updateContainer.Relations;
                    }
                    else
                    {
                        ((IDataObject)entity).ToBeDeleted = true;
                        continue;
                    }
                    IEntityMetaData metaData = ((IEntityMetaDataHolder)entity).Get__EntityMetaData();
                    ApplyPrimitiveUpdateItems(entity, puis, metaData);

                    if (ruis != null)
                    {
                        bool isUpdate = changeContainer is UpdateContainer;
                        foreach (IRelationUpdateItem rui in ruis)
                        {
                            ApplyRelationUpdateItem(entity, rui, isUpdate, metaData, toPrefetch, toFetchFromCache, checkBaseState, runnables);
                        }
                    }
                }
                while (toPrefetch.Count > 0 || toFetchFromCache.Count > 0 || runnables.Count > 0)
                {
                    if (toPrefetch.Count > 0)
                    {
                        PrefetchHelper.Prefetch(toPrefetch);
                        toPrefetch.Clear();
                    }
                    if (toFetchFromCache.Count > 0)
                    {
                        IList <Object> fetchedObjects = stateCache.GetObjects(toFetchFromCache, CacheDirective.None);
                        hardRefs.Add(fetchedObjects); // add list as item intended. adding each item of the source is NOT needed
                        toFetchFromCache.Clear();
                    }
                    IBackgroundWorkerDelegate[] runnableArray = runnables.ToArray();
                    runnables.Clear();
                    foreach (IBackgroundWorkerDelegate runnable in runnableArray)
                    {
                        runnable();
                    }
                }
                List <Object> newObjects = new List <Object>(allObjects.Count);
                List <DirectValueHolderRef> changedRelationRefs = new List <DirectValueHolderRef>();
                for (int a = allObjects.Count; a-- > 0;)
                {
                    IChangeContainer      newChange = newAllChanges[a];
                    IRelationUpdateItem[] ruis      = null;
                    Object entity = allObjects[a];
                    if (newChange is CreateContainer)
                    {
                        newObjects.Add(entity);
                        ruis = ((CreateContainer)newChange).Relations;
                    }
                    else if (newChange is UpdateContainer)
                    {
                        ruis = ((UpdateContainer)newChange).Relations;
                    }
                    if (ruis == null)
                    {
                        continue;
                    }
                    IEntityMetaData metaData = EntityMetaDataProvider.GetMetaData(entity.GetType());
                    foreach (IRelationUpdateItem rui in ruis)
                    {
                        Member member = metaData.GetMemberByName(rui.MemberName);
                        changedRelationRefs.Add(new DirectValueHolderRef((IObjRefContainer)entity, (RelationMember)member));
                    }
                }
                if (newObjects.Count > 0)
                {
                    ((IWritableCache)stateCache).Put(newObjects);
                }
                if (changedRelationRefs.Count > 0)
                {
                    PrefetchHelper.Prefetch(changedRelationRefs);
                }
                return(new CUDResult(newAllChanges, allObjects));
            }
            finally
            {
                cloneStateTL.Value = null;
            }
        }