Skip to content

Commit

Permalink
#91 refactorings and Hibernate ORM events implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Jul 28, 2015
1 parent 84b83f0 commit 9e034e6
Show file tree
Hide file tree
Showing 19 changed files with 1,161 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ public void updateEvent(List<UpdateEventInfo> updateInfos, EntityProvider provid
break;
}
case EventType.DELETE: {
IndexUpdater.this.indexWrapper.delete( entityClass, inIndexOf, id, tx );
IndexUpdater.this.indexWrapper.delete(
entityClass, inIndexOf, id, this.entityProvider,
tx
);
break;
}
default: {
Expand Down Expand Up @@ -195,13 +198,35 @@ public void updateEvent(List<UpdateEventInfo> updateInfos, EntityProvider provid

}

public void delete(
Class<?> entityClass,
List<Class<?>> inIndexOf,
Object id,
EntityProvider entityProvider,
Transaction tx) {
this.indexWrapper.delete( entityClass, inIndexOf, id, entityProvider , tx );
}

public void update(Object entity, Transaction tx) {
this.indexWrapper.update( entity, tx );
}

public void index(Object entity, Transaction tx) {
this.indexWrapper.update( entity, tx );
}

public void close() {
this.exec.shutdown();
}

public interface IndexWrapper {

void delete(Class<?> entityClass, List<Class<?>> inIndexOf, Object id, Transaction tx);
void delete(
Class<?> entityClass,
List<Class<?>> inIndexOf,
Object id,
EntityProvider entityProvider,
Transaction tx);

void update(Object entity, Transaction tx);

Expand All @@ -218,7 +243,12 @@ public DefaultIndexWrapper(ExtendedSearchIntegrator searchIntegrator) {
}

@Override
public void delete(Class<?> entityClass, List<Class<?>> inIndexOf, Object id, Transaction tx) {
public void delete(
Class<?> entityClass,
List<Class<?>> inIndexOf,
Object id,
EntityProvider entityProvider,
Transaction tx) {
for ( Class<?> indexClass : inIndexOf ) {
RehashedTypeMetadata metadata = IndexUpdater.this.metadataForIndexRoot.get( indexClass );
List<String> fields = metadata.getIdFieldNamesForType().get( entityClass );
Expand Down Expand Up @@ -271,7 +301,7 @@ public void delete(Class<?> entityClass, List<Class<?>> inIndexOf, Object id, Tr
).maxResults( HSQUERY_BATCH )
.queryEntityInfos() ) {
Serializable originalId = (Serializable) entityInfo.getProjection()[0];
Object original = IndexUpdater.this.entityProvider.get( indexClass, originalId );
Object original = entityProvider.get( indexClass, originalId );
if ( original != null ) {
this.update( original, tx );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hibernate.search.genericjpa.db.events.UpdateConsumer.UpdateEventInfo;
import org.hibernate.search.genericjpa.db.events.index.impl.IndexUpdater;
import org.hibernate.search.genericjpa.db.events.index.impl.IndexUpdater.IndexWrapper;
import org.hibernate.search.genericjpa.entity.EntityProvider;
import org.hibernate.search.genericjpa.entity.ReusableEntityProvider;
import org.hibernate.search.genericjpa.factory.StandaloneSearchConfiguration;
import org.hibernate.search.genericjpa.factory.Transaction;
Expand Down Expand Up @@ -102,7 +103,12 @@ public void testWithoutIndex() {
IndexWrapper indexWrapper = new IndexWrapper() {

@Override
public void delete(Class<?> entityClass, List<Class<?>> inIndexOf, Object id, Transaction tx) {
public void delete(
Class<?> entityClass,
List<Class<?>> inIndexOf,
Object id,
EntityProvider entityProvider,
Transaction tx) {
Object obj = IndexUpdaterTest.this.obj( entityClass );
System.out.println( entityClass );
System.out.println( updateInfoSet );
Expand Down
8 changes: 7 additions & 1 deletion jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<scope>provided</scope>
</dependency>


<!-- code gen -->
<dependency>
<groupId>com.squareup</groupId>
Expand All @@ -105,7 +112,6 @@
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
4 changes: 4 additions & 0 deletions jpa/src/main/java/org/hibernate/search/genericjpa/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hibernate.search.genericjpa.annotations.CustomUpdateEntityProvider;
import org.hibernate.search.genericjpa.annotations.InIndex;
import org.hibernate.search.genericjpa.db.events.eclipselink.impl.EclipseLinkSynchronizedUpdateSourceProvider;
import org.hibernate.search.genericjpa.db.events.hibernate.impl.HibernateSynchronizedUpdateSourceProvider;
import org.hibernate.search.genericjpa.db.events.triggers.TriggerSQLStringSource;
import org.hibernate.search.genericjpa.entity.EntityManagerEntityProvider;
import org.hibernate.search.genericjpa.exception.SearchException;
Expand Down Expand Up @@ -178,6 +179,9 @@ else if ( "manual-updates".equals( type ) ) {
else if ( "eclipselink".equals( type ) ) {
synchronizedUpdateSourceProvider = new EclipseLinkSynchronizedUpdateSourceProvider();
}
else if ( "hibernate".equals( type ) ) {
synchronizedUpdateSourceProvider = new HibernateSynchronizedUpdateSourceProvider();
}
else {
throw new SearchException( "unrecognized " + SEARCH_FACTORY_TYPE_KEY + ": " + type );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.hibernate.search.genericjpa.db.events.eclipselink.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.AssertionFailure;
import org.hibernate.search.genericjpa.entity.ReusableEntityProvider;

/**
* Created by Martin on 28.07.2015.
*/
public class DummyReusableEntityProvider implements ReusableEntityProvider {

@Override
public void close() {
throw new AssertionFailure( "should not have been used" );
}

@Override
public void open() {
throw new AssertionFailure( "should not have been used" );
}

@Override
public Object get(Class<?> entityClass, Object id, Map<String, String> hints) {
throw new AssertionFailure( "should not have been used" );
}

@Override
public List getBatch(Class<?> entityClass, List<Object> id, Map<String, String> hints) {
throw new AssertionFailure( "should not have been used" );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.sessions.Session;

import org.hibernate.search.engine.integration.impl.ExtendedSearchIntegrator;
import org.hibernate.search.genericjpa.JPASearchFactoryController;
import org.hibernate.search.genericjpa.db.events.index.impl.IndexUpdater;
import org.hibernate.search.genericjpa.entity.ReusableEntityProvider;
import org.hibernate.search.genericjpa.events.impl.SynchronizedUpdateSource;
import org.hibernate.search.genericjpa.impl.JPASearchFactoryAdapter;
import org.hibernate.search.genericjpa.impl.SynchronizedUpdateSourceProvider;
import org.hibernate.search.genericjpa.metadata.impl.RehashedTypeMetadata;

Expand All @@ -28,7 +32,7 @@ public class EclipseLinkSynchronizedUpdateSourceProvider implements Synchronized

@Override
public SynchronizedUpdateSource getUpdateSource(
JPASearchFactoryController searchFactoryController,
ExtendedSearchIntegrator searchIntegrator,
Map<Class<?>, RehashedTypeMetadata> rehashedTypeMetadataPerIndexRoot,
Map<Class<?>, List<Class<?>>> containedInIndexOf,
Properties properties,
Expand All @@ -39,8 +43,15 @@ public SynchronizedUpdateSource getUpdateSource(
try {
Session session = entityManager.getServerSession();

IndexUpdater indexUpdater = new IndexUpdater(
rehashedTypeMetadataPerIndexRoot,
containedInIndexOf,
new DummyReusableEntityProvider(),
searchIntegrator
);

EclipseLinkUpdateSource eclipseLinkUpdateSource = new EclipseLinkUpdateSource(
searchFactoryController,
indexUpdater,
indexRelevantEntities,
rehashedTypeMetadataPerIndexRoot,
containedInIndexOf
Expand Down
Loading

0 comments on commit 9e034e6

Please sign in to comment.