Skip to content

Commit

Permalink
#91 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Jul 28, 2015
1 parent 82273b5 commit 018f09f
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,17 @@ class UpdateEventInfo {
private final Object id;
private final int eventType;
private final Map<String, String> hints;
private final EntityProvider entityProvider;

public UpdateEventInfo(Class<?> entityClass, Object id, int eventType) {
this( entityClass, id, eventType, Collections.emptyMap() );
}

public UpdateEventInfo(Class<?> entityClass, Object id, int eventType, Map<String, String> hints) {
this( entityClass, id, eventType, hints, null );
}

public UpdateEventInfo(
Class<?> entityClass,
Object id,
int eventType,
Map<String, String> hints,
EntityProvider entityProvider) {
super();
this.entityClass = entityClass;
this.id = id;
this.eventType = eventType;
this.hints = hints;
this.entityProvider = entityProvider;
}

/**
Expand Down Expand Up @@ -80,16 +69,6 @@ public Map<String, String> getHints() {
return hints;
}

/**
* the entity provider that should be used to handle this event. this is only needed for native event access
* and is null for the trigger event system
*
* @return
*/
public EntityProvider getEntityProvider() {
return entityProvider;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
Expand All @@ -110,12 +89,7 @@ public boolean equals(Object o) {
if ( id != null ? !id.equals( that.id ) : that.id != null ) {
return false;
}
if ( hints != null ? !hints.equals( that.hints ) : that.hints != null ) {
return false;
}
return !(entityProvider != null ?
!entityProvider.equals( that.entityProvider ) :
that.entityProvider != null);
return !(hints != null ? !hints.equals( that.hints ) : that.hints != null);

}

Expand All @@ -125,7 +99,6 @@ public int hashCode() {
result = 31 * result + (id != null ? id.hashCode() : 0);
result = 31 * result + eventType;
result = 31 * result + (hints != null ? hints.hashCode() : 0);
result = 31 * result + (entityProvider != null ? entityProvider.hashCode() : 0);
return result;
}

Expand All @@ -136,7 +109,6 @@ public String toString() {
", id=" + id +
", eventType=" + eventType +
", hints=" + hints +
", entityProvider=" + entityProvider +
'}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
package org.hibernate.search.genericjpa.db.events.index.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -81,26 +79,10 @@ public IndexUpdater(
}

public void updateEvent(List<UpdateEventInfo> updateInfos) {
Map<EntityProvider, List<UpdateEventInfo>> perEntityProvider = new HashMap<>();
List<UpdateEventInfo> infoWithoutEntityProvider = new ArrayList<>();
for ( UpdateEventInfo info : updateInfos ) {
EntityProvider entityProvider = info.getEntityProvider();
if ( entityProvider == null ) {
infoWithoutEntityProvider.add( info );
}
else {
perEntityProvider.computeIfAbsent( entityProvider, _1 -> new ArrayList<>() )
.add(
info
);
}
}
for ( Map.Entry<EntityProvider, List<UpdateEventInfo>> entry : perEntityProvider.entrySet() ) {
this.updateEvent( entry.getValue(), entry.getKey() );
}
if ( infoWithoutEntityProvider.size() > 0 ) {
this.updateEvent( infoWithoutEntityProvider, this.entityProvider );
if(updateInfos.size() == 0) {
return;
}
this.updateEvent( updateInfos, this.entityProvider );
}

public void updateEvent(List<UpdateEventInfo> updateInfos, EntityProvider provider) {
Expand Down Expand Up @@ -204,7 +186,7 @@ public void delete(
Object id,
EntityProvider entityProvider,
Transaction tx) {
this.indexWrapper.delete( entityClass, inIndexOf, id, entityProvider , tx );
this.indexWrapper.delete( entityClass, inIndexOf, id, entityProvider, tx );
}

public void update(Object entity, Transaction tx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package org.hibernate.search.genericjpa.db.events.eclipselink.impl;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.persistence.descriptors.DescriptorEvent;
Expand All @@ -24,21 +26,16 @@
import org.eclipse.persistence.sessions.UnitOfWork;

import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.search.backend.spi.SingularTermDeletionQuery;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.StringBridge;
import org.hibernate.search.engine.ProjectionConstants;
import org.hibernate.search.engine.metadata.impl.DocumentFieldMetadata;
import org.hibernate.search.genericjpa.JPASearchFactoryController;
import org.hibernate.search.genericjpa.db.EventType;
import org.hibernate.search.genericjpa.db.events.UpdateConsumer;
import org.hibernate.search.genericjpa.db.events.index.impl.IndexUpdater;
import org.hibernate.search.genericjpa.entity.EntityProvider;
import org.hibernate.search.genericjpa.events.impl.SynchronizedUpdateSource;
import org.hibernate.search.genericjpa.exception.AssertionFailure;
import org.hibernate.search.genericjpa.exception.SearchException;
import org.hibernate.search.genericjpa.factory.Transaction;
import org.hibernate.search.genericjpa.factory.impl.SubClassSupportInstanceInitializer;
import org.hibernate.search.genericjpa.metadata.impl.RehashedTypeMetadata;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.spi.InstanceInitializer;

/**
Expand All @@ -59,10 +56,13 @@ public class EclipseLinkUpdateSource implements SynchronizedUpdateSource {
private final Map<Class<?>, RehashedTypeMetadata> rehashedTypeMetadataPerIndexRoot;
private final Map<Class<?>, List<Class<?>>> containedInIndexOf;

private List<UpdateConsumer> updateConsumers = new ArrayList<>();

final DescriptorEventAspect descriptorEventAspect;
final SessionEventAspect sessionEventAspect;

private final ConcurrentHashMap<Session, Transaction> transactions = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Transaction, List<UpdateConsumer.UpdateEventInfo>> updateEventInfos = new ConcurrentHashMap<>();

public EclipseLinkUpdateSource(
IndexUpdater indexUpdater,
Expand All @@ -77,29 +77,79 @@ public EclipseLinkUpdateSource(
this.containedInIndexOf = containedInIndexOf;
}

@Override
public void setUpdateConsumers(List<UpdateConsumer> updateConsumers) {
this.updateConsumers = updateConsumers;
}

private void notify(List<UpdateConsumer.UpdateEventInfo> updateEventInfos) {
if ( updateEventInfos.size() > 0 ) {
for ( UpdateConsumer updateConsumer : EclipseLinkUpdateSource.this.updateConsumers ) {
try {
updateConsumer.updateEvent( updateEventInfos );
}
catch (Exception e) {
LOGGER.log( Level.WARNING, "Exception while notifying updateConsumers", e );
}
}
}
}

private Object getId(Class<?> entityClass, Object entity) {
List<Class<?>> inIndexOf = EclipseLinkUpdateSource.this.containedInIndexOf.get( entityClass );
if ( inIndexOf.size() > 0 ) {
//hack, but works
RehashedTypeMetadata metadata = EclipseLinkUpdateSource.this.rehashedTypeMetadataPerIndexRoot.get(
inIndexOf.get( 0 )
);
XProperty idProperty = metadata.getIdPropertyAccessorForType().get( entityClass );
Object id = idProperty.invoke( entity );
return id;
}
return null;
}

private class DescriptorEventAspect extends DescriptorEventAdapter {

@Override
public void postInsert(DescriptorEvent event) {
Object entity = event.getObject();
Class<?> entityClass = INSTANCE_INITIALIZER.getClass( entity );
Object id = EclipseLinkUpdateSource.this.getId( entityClass, entity );
if ( id == null ) {
throw new SearchException( "id was null for " + entity );
}
if ( EclipseLinkUpdateSource.this.indexRelevantEntities.contains( entityClass ) ) {
LOGGER.fine( "Insert Event for " + entity );
Session session = event.getSession();
if ( session.isUnitOfWork() ) {
session = ((UnitOfWork) session).getParent();
}
Transaction tx = EclipseLinkUpdateSource.this.transactions.get( session );
if(tx == null) {
if ( tx == null ) {
tx = new Transaction();
try {
EclipseLinkUpdateSource.this.indexUpdater.index( entity, tx );
tx.commit();
} catch(Exception e) {
EclipseLinkUpdateSource.this.notify(
Collections.singletonList(
new UpdateConsumer.UpdateEventInfo(
entityClass, id, EventType.INSERT
)
)
);
}
catch (Exception e) {
tx.rollback();
}
} else {
}
else {
EclipseLinkUpdateSource.this.indexUpdater.index( entity, tx );
EclipseLinkUpdateSource.this.updateEventInfos.get( tx ).add(
new UpdateConsumer.UpdateEventInfo(
entityClass, id, EventType.INSERT
)
);
}
}
}
Expand All @@ -108,23 +158,41 @@ public void postInsert(DescriptorEvent event) {
public void postUpdate(DescriptorEvent event) {
Object entity = event.getObject();
Class<?> entityClass = INSTANCE_INITIALIZER.getClass( entity );
Object id = EclipseLinkUpdateSource.this.getId( entityClass, entity );
if ( id == null ) {
throw new SearchException( "id was null for " + entity );
}
if ( EclipseLinkUpdateSource.this.indexRelevantEntities.contains( entityClass ) ) {
LOGGER.fine( "Update Event for " + entity );
Session session = event.getSession();
if ( session.isUnitOfWork() ) {
session = ((UnitOfWork) session).getParent();
}
Transaction tx = EclipseLinkUpdateSource.this.transactions.get( session );
if(tx == null) {
if ( tx == null ) {
tx = new Transaction();
try {
EclipseLinkUpdateSource.this.indexUpdater.update( entity, tx );
tx.commit();
} catch(Exception e) {
EclipseLinkUpdateSource.this.notify(
Collections.singletonList(
new UpdateConsumer.UpdateEventInfo(
entityClass, id, EventType.UPDATE
)
)
);
}
catch (Exception e) {
tx.rollback();
}
} else {
}
else {
EclipseLinkUpdateSource.this.indexUpdater.update( entity, tx );
EclipseLinkUpdateSource.this.updateEventInfos.get( tx ).add(
new UpdateConsumer.UpdateEventInfo(
entityClass, id, EventType.UPDATE
)
);
}
}
}
Expand All @@ -148,19 +216,24 @@ public void postDelete(DescriptorEvent event) {
final Session session = tmp;
Transaction tx = EclipseLinkUpdateSource.this.transactions.get( session );
boolean createdOwnTx = false;
if(tx == null) {
UpdateConsumer.UpdateEventInfo updateEventInfo = null;
if ( tx == null ) {
tx = new Transaction();
createdOwnTx = true;
}
try {
List<Class<?>> inIndexOf = EclipseLinkUpdateSource.this.containedInIndexOf.get( entityClass );
if ( inIndexOf.size() > 0 ) {
//hack, but works
RehashedTypeMetadata metadata = EclipseLinkUpdateSource.this.rehashedTypeMetadataPerIndexRoot.get(
inIndexOf.get( 0 )
);
RehashedTypeMetadata metadata = EclipseLinkUpdateSource.this.rehashedTypeMetadataPerIndexRoot
.get(
inIndexOf.get( 0 )
);
XProperty idProperty = metadata.getIdPropertyAccessorForType().get( entityClass );
Object id = idProperty.invoke( entity );
updateEventInfo = new UpdateConsumer.UpdateEventInfo(
entityClass, id, EventType.DELETE
);
EclipseLinkUpdateSource.this.indexUpdater.delete(
entityClass, inIndexOf, id, new EntityProvider() {

Expand Down Expand Up @@ -190,11 +263,22 @@ public void close() throws IOException {
}, tx
);
}
if(createdOwnTx) {
if ( createdOwnTx ) {
tx.commit();
if ( updateEventInfo != null ) {
EclipseLinkUpdateSource.this.notify( Collections.singletonList( updateEventInfo ) );
}
}
} catch(Exception e) {
if(createdOwnTx) {
else {
if ( updateEventInfo != null ) {
EclipseLinkUpdateSource.this.updateEventInfos.get( tx ).add(
updateEventInfo
);
}
}
}
catch (Exception e) {
if ( createdOwnTx ) {
tx.rollback();
}
throw e;
Expand All @@ -216,6 +300,7 @@ public void postBeginTransaction(SessionEvent event) {
else {
tx = new Transaction();
EclipseLinkUpdateSource.this.transactions.put( session, tx );
EclipseLinkUpdateSource.this.updateEventInfos.put( tx, new ArrayList<>() );
}
}

Expand All @@ -225,6 +310,11 @@ public void postCommitTransaction(SessionEvent event) {
Transaction tx = EclipseLinkUpdateSource.this.transactions.get( session );
if ( tx != null && tx.isTransactionInProgress() ) {
tx.commit();
List<UpdateConsumer.UpdateEventInfo> updateEventInfos = EclipseLinkUpdateSource.this.updateEventInfos
.remove(
tx
);
EclipseLinkUpdateSource.this.notify( updateEventInfos );
EclipseLinkUpdateSource.this.transactions.remove( session );
}
else {
Expand All @@ -240,6 +330,7 @@ public void postRollbackTransaction(SessionEvent event) {
Transaction tx = EclipseLinkUpdateSource.this.transactions.get( session );
if ( tx != null && tx.isTransactionInProgress() ) {
tx.rollback();
EclipseLinkUpdateSource.this.updateEventInfos.remove( tx );
EclipseLinkUpdateSource.this.transactions.remove( session );
}
else {
Expand All @@ -258,6 +349,7 @@ public void postLogout(SessionEvent event) {
"rolling back transaction because session logged out..."
);
tx.rollback();
EclipseLinkUpdateSource.this.updateEventInfos.remove( tx );
EclipseLinkUpdateSource.this.transactions.remove( session );
}
}
Expand Down
Loading

0 comments on commit 018f09f

Please sign in to comment.