Search in sources :

Example 1 with TransactionSupport

use of com.blazebit.persistence.view.spi.TransactionSupport in project blaze-persistence by Blazebit.

the class ResetInitialStateSynchronization method afterCompletion.

@Override
@SuppressWarnings("unchecked")
public void afterCompletion(int status) {
    if (status != Status.STATUS_COMMITTED) {
        Map<EntityViewProxy, ViewTransition> objects;
        if (listenerManager.hasPostRollbackListeners()) {
            objects = new IdentityHashMap<>();
        } else {
            objects = null;
        }
        if (coalescedInitialStates != null) {
            for (int i = 0; i < coalescedInitialStates.size(); i += 2) {
                Object[] initialState = coalescedInitialStates.get(i);
                Object[] originalInitialState = coalescedInitialStates.get(i + 1);
                for (int j = 0; j < initialState.length; j++) {
                    initialState[j] = originalInitialState[j];
                }
            }
        }
        if (coalescedRecordingActions != null) {
            for (int i = 0; i < coalescedRecordingActions.size(); i += 4) {
                Object collectionReference = coalescedRecordingActions.get(i);
                Object actionList = coalescedRecordingActions.get(i + 1);
                Map<Object, Object> added = (Map<Object, Object>) coalescedRecordingActions.get(i + 2);
                Map<Object, Object> removed = (Map<Object, Object>) coalescedRecordingActions.get(i + 3);
                if (collectionReference instanceof RecordingCollection<?, ?>) {
                    RecordingCollection<Collection<Object>, Object> collection = (RecordingCollection<Collection<Object>, Object>) collectionReference;
                    collection.setActions((List<CollectionAction<Collection<Object>>>) (List<?>) actionList, added, removed);
                    collection.$$_markDirty(-1);
                } else {
                    Map<Object, Object> addedElements = (Map<Object, Object>) coalescedRecordingActions.get(i + 4);
                    Map<Object, Object> removedElements = (Map<Object, Object>) coalescedRecordingActions.get(i + 5);
                    RecordingMap<Map<Object, Object>, Object, Object> collection = (RecordingMap<Map<Object, Object>, Object, Object>) collectionReference;
                    collection.setActions((List<MapAction<Map<Object, Object>>>) (List<?>) actionList, added, removed, addedElements, removedElements);
                    collection.$$_markDirty(-1);
                    i += 2;
                }
            }
        }
        if (persistedViews != null) {
            for (int i = 0; i < persistedViews.size(); i += 7) {
                MutableStateTrackable view = (MutableStateTrackable) persistedViews.get(i);
                Object newObject = persistedViews.get(i + 1);
                view.$$_setIsNew(true);
                Object id = persistedViews.get(i + 2);
                DirtyTracker parent = (DirtyTracker) persistedViews.get(i + 3);
                int parentIndex = (int) persistedViews.get(i + 4);
                List<Object> readOnlyParents = (List<Object>) persistedViews.get(i + 5);
                if (id != NO_ID_MARKER) {
                    view.$$_setId(id);
                }
                if (parent != null) {
                    // This is not needed as this will happen when rolling back collection actions already
                    if (!(parent instanceof RecordingCollection<?, ?> || parent instanceof RecordingMap<?, ?, ?>)) {
                        parent.$$_replaceAttribute(newObject, parentIndex, view);
                    }
                    for (int j = 0; j < readOnlyParents.size(); j += 2) {
                        DirtyTracker readOnlyParent = (DirtyTracker) readOnlyParents.get(j);
                        int readOnlyParentIndex = (int) readOnlyParents.get(j + 1);
                        readOnlyParent.$$_replaceAttribute(newObject, readOnlyParentIndex, view);
                    }
                }
                view.$$_setDirty((long[]) persistedViews.get(i + 6));
                if (objects != null) {
                    objects.put(view, ViewTransition.PERSIST);
                }
            }
        }
        if (updatedViews != null) {
            for (int i = 0; i < updatedViews.size(); i += 2) {
                MutableStateTrackable view = (MutableStateTrackable) updatedViews.get(i);
                view.$$_setDirty((long[]) updatedViews.get(i + 1));
                if (objects != null) {
                    objects.put(view, ViewTransition.UPDATE);
                }
            }
        }
        if (removedViews != null) {
            for (int i = 0; i < removedViews.size(); i += 4) {
                EntityViewProxy view = (EntityViewProxy) removedViews.get(i);
                if (view instanceof MutableStateTrackable) {
                    MutableStateTrackable removedView = (MutableStateTrackable) view;
                    DirtyTracker parent = (DirtyTracker) removedViews.get(i + 1);
                    if (parent != null) {
                        removedView.$$_setParent(parent, (Integer) removedViews.get(i + 2));
                    }
                    long[] dirtyArray = (long[]) removedViews.get(i + 3);
                    if (dirtyArray != null) {
                        removedView.$$_setDirty(dirtyArray);
                    }
                }
                if (objects != null) {
                    objects.put(view, ViewTransition.REMOVE);
                }
            }
        }
        if (versionedViews != null) {
            for (int i = 0; i < versionedViews.size(); i += 2) {
                MutableStateTrackable view = (MutableStateTrackable) versionedViews.get(i);
                view.$$_setVersion(versionedViews.get(i + 1));
            }
        }
        if (objects != null) {
            PostRollbackInvoker postRollbackInvoker = new PostRollbackInvoker(updateContext, listenerManager, objects);
            TransactionSupport txSupport = updateContext.getEntityViewManager().getService(TransactionSupport.class);
            if (txSupport == null) {
                TransactionAccess transactionAccess = updateContext.getTransactionAccess();
                if (transactionAccess instanceof SynchronizationRegistry) {
                    transactionAccess = ((SynchronizationRegistry) transactionAccess).getTransactionAccess();
                }
                if (transactionAccess instanceof TransactionSupport) {
                    ((TransactionSupport) transactionAccess).transactional(postRollbackInvoker);
                } else {
                    // Log warning that querying doesn't work because we can't run in a new transaction?
                    postRollbackInvoker.run();
                }
            } else {
                txSupport.transactional(postRollbackInvoker);
            }
        }
    } else {
        if (listenerManager.hasPostCommitListeners()) {
            if (persistedViews != null) {
                for (int i = 0; i < persistedViews.size(); i += 7) {
                    listenerManager.invokePostCommit(updateContext, (MutableStateTrackable) persistedViews.get(i), ViewTransition.PERSIST);
                }
            }
            if (updatedViews != null) {
                for (int i = 0; i < updatedViews.size(); i += 2) {
                    listenerManager.invokePostCommit(updateContext, (MutableStateTrackable) updatedViews.get(i), ViewTransition.UPDATE);
                }
            }
            if (removedViews != null) {
                for (int i = 0; i < removedViews.size(); i += 4) {
                    listenerManager.invokePostCommit(updateContext, (EntityViewProxy) removedViews.get(i), ViewTransition.REMOVE);
                }
            }
        }
    }
}
Also used : MutableStateTrackable(com.blazebit.persistence.view.spi.type.MutableStateTrackable) ViewTransition(com.blazebit.persistence.view.ViewTransition) RecordingCollection(com.blazebit.persistence.view.impl.collection.RecordingCollection) TransactionSupport(com.blazebit.persistence.view.spi.TransactionSupport) EntityViewProxy(com.blazebit.persistence.view.spi.type.EntityViewProxy) ArrayList(java.util.ArrayList) List(java.util.List) DirtyTracker(com.blazebit.persistence.view.spi.type.DirtyTracker) RecordingMap(com.blazebit.persistence.view.impl.collection.RecordingMap) MapAction(com.blazebit.persistence.view.impl.collection.MapAction) SynchronizationRegistry(com.blazebit.persistence.view.impl.tx.SynchronizationRegistry) TransactionAccess(com.blazebit.persistence.view.spi.TransactionAccess) CollectionAction(com.blazebit.persistence.view.impl.collection.CollectionAction) RecordingCollection(com.blazebit.persistence.view.impl.collection.RecordingCollection) Collection(java.util.Collection) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) RecordingMap(com.blazebit.persistence.view.impl.collection.RecordingMap)

Aggregations

ViewTransition (com.blazebit.persistence.view.ViewTransition)1 CollectionAction (com.blazebit.persistence.view.impl.collection.CollectionAction)1 MapAction (com.blazebit.persistence.view.impl.collection.MapAction)1 RecordingCollection (com.blazebit.persistence.view.impl.collection.RecordingCollection)1 RecordingMap (com.blazebit.persistence.view.impl.collection.RecordingMap)1 SynchronizationRegistry (com.blazebit.persistence.view.impl.tx.SynchronizationRegistry)1 TransactionAccess (com.blazebit.persistence.view.spi.TransactionAccess)1 TransactionSupport (com.blazebit.persistence.view.spi.TransactionSupport)1 DirtyTracker (com.blazebit.persistence.view.spi.type.DirtyTracker)1 EntityViewProxy (com.blazebit.persistence.view.spi.type.EntityViewProxy)1 MutableStateTrackable (com.blazebit.persistence.view.spi.type.MutableStateTrackable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1