Search in sources :

Example 1 with Log

use of org.hibernate.reactive.logging.impl.Log in project hibernate-reactive by hibernate.

the class ReactiveCollectionUpdateAction method reactiveExecute.

@Override
public CompletionStage<Void> reactiveExecute() {
    final Serializable key = getKey();
    final SharedSessionContractImplementor session = getSession();
    final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
    final CollectionPersister corePersister = getPersister();
    final PersistentCollection collection = getCollection();
    final boolean affectedByFilters = corePersister.isAffectedByEnabledFilters(session);
    preUpdate();
    CompletionStage<Void> updateStage = CompletionStages.voidFuture();
    // And then make sure that each operations is executed in its own stage maintaining the same order as in ORM
    if (!collection.wasInitialized()) {
        // The collection should still be dirty.
        if (!collection.isDirty()) {
            throw new AssertionFailure("collection is not dirty");
        }
    // do nothing - we only need to notify the cache...
    } else if (!affectedByFilters && collection.empty()) {
        if (!emptySnapshot) {
            updateStage = updateStage.thenCompose(v -> reactivePersister.removeReactive(key, session)).thenAccept(count -> {
            /* We don't care, maybe we can log it as debug */
            });
        }
    } else if (collection.needsRecreate(corePersister)) {
        if (affectedByFilters) {
            throw LOG.cannotRecreateCollectionWhileFilterIsEnabled(collectionInfoString(corePersister, collection, key, session));
        }
        if (!emptySnapshot) {
            updateStage = updateStage.thenCompose(v -> reactivePersister.removeReactive(key, session)).thenAccept(count -> {
            /* We don't care, maybe we can log it as debug */
            });
        }
        return updateStage.thenCompose(v -> reactivePersister.recreateReactive(collection, key, session).thenAccept(ignore -> {
            session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
            evict();
            postUpdate();
            final StatisticsImplementor statistics = session.getFactory().getStatistics();
            if (statistics.isStatisticsEnabled()) {
                statistics.updateCollection(corePersister.getRole());
            }
        }));
    } else {
        updateStage = updateStage.thenCompose(v -> reactivePersister.reactiveDeleteRows(collection, key, session)).thenCompose(v -> reactivePersister.reactiveUpdateRows(collection, key, session)).thenCompose(v -> reactivePersister.reactiveInsertRows(collection, key, session));
    }
    return updateStage.thenAccept(v -> {
        session.getPersistenceContextInternal().getCollectionEntry(collection).afterAction(collection);
        evict();
        postUpdate();
        final StatisticsImplementor statistics = session.getFactory().getStatistics();
        if (statistics.isStatisticsEnabled()) {
            statistics.updateCollection(corePersister.getRole());
        }
    });
}
Also used : ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) EventListenerGroup(org.hibernate.event.service.spi.EventListenerGroup) CollectionAction(org.hibernate.action.internal.CollectionAction) AssertionFailure(org.hibernate.AssertionFailure) ReactiveExecutable(org.hibernate.reactive.engine.ReactiveExecutable) PreCollectionUpdateEventListener(org.hibernate.event.spi.PreCollectionUpdateEventListener) PreCollectionRecreateEventListener(org.hibernate.event.spi.PreCollectionRecreateEventListener) Log(org.hibernate.reactive.logging.impl.Log) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventType(org.hibernate.event.spi.EventType) PreCollectionUpdateEvent(org.hibernate.event.spi.PreCollectionUpdateEvent) MessageHelper.collectionInfoString(org.hibernate.pretty.MessageHelper.collectionInfoString) MethodHandles(java.lang.invoke.MethodHandles) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Serializable(java.io.Serializable) PreCollectionRecreateEvent(org.hibernate.event.spi.PreCollectionRecreateEvent) PostCollectionUpdateEvent(org.hibernate.event.spi.PostCollectionUpdateEvent) CompletionStage(java.util.concurrent.CompletionStage) PostCollectionRecreateEventListener(org.hibernate.event.spi.PostCollectionRecreateEventListener) PostCollectionUpdateEventListener(org.hibernate.event.spi.PostCollectionUpdateEventListener) PostCollectionRecreateEvent(org.hibernate.event.spi.PostCollectionRecreateEvent) HibernateException(org.hibernate.HibernateException) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CompletionStages(org.hibernate.reactive.util.impl.CompletionStages) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) ReactiveCollectionPersister(org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor)

Aggregations

Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 CompletionStage (java.util.concurrent.CompletionStage)1 AssertionFailure (org.hibernate.AssertionFailure)1 HibernateException (org.hibernate.HibernateException)1 CollectionAction (org.hibernate.action.internal.CollectionAction)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 EventListenerGroup (org.hibernate.event.service.spi.EventListenerGroup)1 EventType (org.hibernate.event.spi.EventType)1 PostCollectionRecreateEvent (org.hibernate.event.spi.PostCollectionRecreateEvent)1 PostCollectionRecreateEventListener (org.hibernate.event.spi.PostCollectionRecreateEventListener)1 PostCollectionUpdateEvent (org.hibernate.event.spi.PostCollectionUpdateEvent)1 PostCollectionUpdateEventListener (org.hibernate.event.spi.PostCollectionUpdateEventListener)1 PreCollectionRecreateEvent (org.hibernate.event.spi.PreCollectionRecreateEvent)1 PreCollectionRecreateEventListener (org.hibernate.event.spi.PreCollectionRecreateEventListener)1 PreCollectionUpdateEvent (org.hibernate.event.spi.PreCollectionUpdateEvent)1 PreCollectionUpdateEventListener (org.hibernate.event.spi.PreCollectionUpdateEventListener)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1 MessageHelper.collectionInfoString (org.hibernate.pretty.MessageHelper.collectionInfoString)1