Skip to content

Commit

Permalink
#37 hibernate-search-standalone formatted right
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Apr 29, 2015
1 parent 1781743 commit 9d8923b
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package com.github.hotware.hsearch.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.hibernate.search.query.engine.spi.HSQuery;

import com.github.hotware.hsearch.dto.DtoDescriptor.DtoDescription;

/**
* projection utility class to automatically convert from projections back to a DtoObject. The projection to use is
* specified via {@link com.github.hotware.hsearch.dto.annotations.DtoField}(s)
*
* @author Martin
*/
public class DtoQueryExecutor {

private final Map<Class<?>, DtoDescription> dtoDescriptions;
private final DtoDescriptor dtoDescriptor;

public DtoQueryExecutor() {
this.dtoDescriptions = new HashMap<>();
this.dtoDescriptor = new DtoDescriptorImpl();
}

public <T> List<T> executeHSQuery(HSQuery hsQuery, Class<T> clazz) {
return this.executeHSQuery( hsQuery, clazz, DtoDescription.DEFAULT_PROFILE );
}

public <T> List<T> executeHSQuery(HSQuery hsQuery, Class<T> returnedType, String profile) {

DtoDescription desc = this.dtoDescriptions.computeIfAbsent( returnedType, (clazz_) -> {
return this.dtoDescriptor.getDtoDescription( clazz_ );
} );
String[] projectedFieldsBefore = hsQuery.getProjectedFields();
try {
List<String> projection = new ArrayList<>();
List<java.lang.reflect.Field> fields = new ArrayList<>();
desc.getFieldDescriptionsForProfile( profile ).forEach( (fd) -> {
projection.add( fd.getFieldName() );
fields.add( fd.getField() );
} );
hsQuery.projection( projection.toArray( new String[0] ) );

List<T> ret;
{
hsQuery.getTimeoutManager().start();

ret = hsQuery.queryEntityInfos().stream().map( (entityInfo) -> {
try {
T val = returnedType.newInstance();
Object[] projectedValues = entityInfo.getProjection();
for ( int i = 0; i < projection.size(); i++ ) {
java.lang.reflect.Field field = fields.get( i );
field.set( val, projectedValues[i] );
}
return val;
}
catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException( e );
}
} ).collect( Collectors.toList() );

hsQuery.getTimeoutManager().stop();
}
return ret;
}
finally {
hsQuery.projection( projectedFieldsBefore );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
/**
* Hibernate-Search is no object storage. All hits found on the Index have a original representation. This interface
* provides means to retrieve these when executing a {@link com.github.hotware.hsearch.query.HSearchQuery}
*
*
* @author Martin Braun
*/
public interface EntityProvider extends Closeable {

public Object get(Class<?> entityClass, Object id);
Object get(Class<?> entityClass, Object id);

/**
* ATTENTION: ORDER IS NOT PRESERVED!
*/
@SuppressWarnings("rawtypes")
public List getBatch(Class<?> entityClass, List<Object> id);
List getBatch(Class<?> entityClass, List<Object> id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
package com.github.hotware.hsearch.entity;

/**
* @author Martin
* @author Martin Braun
*/
public interface ReusableEntityProvider extends EntityProvider {

public void close();
void close();

public void open();
void open();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,71 @@ public interface SearchFactory extends Closeable {
/**
* provides means for getting vanilla Lucene IndexReaders. use this if you need direct index access because the
* SearchFactory doesn't allow the things you want to do
*
*
* @return IndexReaderAccessor for this Searchfactory
*/
public IndexReaderAccessor getIndexReaderAccessor();
IndexReaderAccessor getIndexReaderAccessor();

/**
* @return Set of all indexed Entities' classes.
*/
public Set<Class<?>> getIndexedEntities();
Set<Class<?>> getIndexedEntities();

/**
* @return
*/
public QueryContextBuilder buildQueryBuilder();
QueryContextBuilder buildQueryBuilder();

public void optimize();
void optimize();

public void optimize(Class<?> entityClass);
void optimize(Class<?> entityClass);

public Statistics getStatistics();
Statistics getStatistics();

public void purgeAll(Class<?> entityClass, TransactionContext tc);
void purgeAll(Class<?> entityClass, TransactionContext tc);

public default void purgeAll(Class<?> entityClass) {
default void purgeAll(Class<?> entityClass) {
Transaction tc = new Transaction();
this.purgeAll( entityClass, tc );
tc.end();
}

public HSearchQuery createQuery(Query query, Class<?>... targetedEntities);
HSearchQuery createQuery(Query query, Class<?>... targetedEntities);

public FilterCachingStrategy getFilterCachingStrategy();
FilterCachingStrategy getFilterCachingStrategy();

public Analyzer getAnalyzer(String name);
Analyzer getAnalyzer(String name);

public Analyzer getAnalyzer(Class<?> entityClass);
Analyzer getAnalyzer(Class<?> entityClass);

public void purge(Class<?> entityClass, Serializable id, TransactionContext tc);
void purge(Class<?> entityClass, Serializable id, TransactionContext tc);

public default void purge(Class<?> entityClass, Serializable id) {
default void purge(Class<?> entityClass, Serializable id) {
Transaction tc = new Transaction();
this.purge( entityClass, id, tc );
tc.end();
}

public void purge(Iterable<?> entities, TransactionContext tc);
void purge(Iterable<?> entities, TransactionContext tc);

public default void purge(Object entity, TransactionContext tc) {
default void purge(Object entity, TransactionContext tc) {
this.purge( Arrays.asList( entity ), tc );
}

public default void purge(Iterable<?> entities) {
default void purge(Iterable<?> entities) {
Transaction tc = new Transaction();
this.purge( entities, tc );
tc.end();
}

public default void purge(Object entity) {
default void purge(Object entity) {
this.purge( Arrays.asList( entity ) );
}

/**
* this first queries for all matching documents and then deletes them by their id
*/
public default void purge(Class<?> entityClass, Query query) {
default void purge(Class<?> entityClass, Query query) {
Transaction tc = new Transaction();
this.purge( entityClass, query, tc );
tc.end();
Expand All @@ -100,55 +100,55 @@ public default void purge(Class<?> entityClass, Query query) {
* this first queries for all matching documents and then deletes them by their id
*/
@Deprecated
public void purge(Class<?> entityClass, Query query, TransactionContext tc);
void purge(Class<?> entityClass, Query query, TransactionContext tc);

// same names

public void index(Iterable<?> entities, TransactionContext tc);
void index(Iterable<?> entities, TransactionContext tc);

public default void index(Object entity, TransactionContext tc) {
default void index(Object entity, TransactionContext tc) {
this.index( Arrays.asList( entity ), tc );
}

public default void index(Iterable<?> entities) {
default void index(Iterable<?> entities) {
Transaction tc = new Transaction();
this.index( entities, tc );
tc.end();
}

public default void index(Object entity) {
default void index(Object entity) {
this.index( Arrays.asList( entity ) );
}

public void update(Iterable<?> entities, TransactionContext tc);
void update(Iterable<?> entities, TransactionContext tc);

public default void update(Object entity, TransactionContext tc) {
default void update(Object entity, TransactionContext tc) {
this.update( Arrays.asList( entity ), tc );
}

public default void update(Iterable<?> entities) {
default void update(Iterable<?> entities) {
Transaction tc = new Transaction();
this.update( entities, tc );
tc.end();
}

public default void update(Object entity) {
default void update(Object entity) {
this.update( Arrays.asList( entity ) );
}

public void delete(Iterable<?> entities, TransactionContext tc);
void delete(Iterable<?> entities, TransactionContext tc);

public default void delete(Object entity, TransactionContext tc) {
default void delete(Object entity, TransactionContext tc) {
this.delete( Arrays.asList( entity ), tc );
}

public default void delete(Iterable<?> entities) {
default void delete(Iterable<?> entities) {
Transaction tc = new Transaction();
this.delete( entities, tc );
tc.end();
}

public default void delete(Object entity) {
default void delete(Object entity) {
this.delete( Arrays.asList( entity ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

import org.hibernate.search.cfg.spi.SearchConfiguration;
import org.hibernate.search.engine.integration.impl.ExtendedSearchIntegrator;
import org.hibernate.search.exception.AssertionFailure;
import org.hibernate.search.spi.SearchIntegrator;
import org.hibernate.search.spi.SearchIntegratorBuilder;

public final class SearchFactoryFactory {

private SearchFactoryFactory() {
throw new AssertionError( "can't touch this!" );
throw new AssertionFailure( "can't touch this!" );
}

public static SearchFactory createSearchFactory(SearchConfiguration searchConfiguration, Collection<Class<?>> classes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
import org.hibernate.search.query.engine.spi.HSQuery;
import org.hibernate.search.stat.Statistics;

import com.github.hotware.hsearch.dto.HibernateSearchQueryExecutor;
import com.github.hotware.hsearch.dto.DtoQueryExecutor;
import com.github.hotware.hsearch.query.HSearchQuery;
import com.github.hotware.hsearch.query.HSearchQueryImpl;
import com.github.hotware.hsearch.transaction.TransactionContext;

public class SearchFactoryImpl implements SearchFactory {

private final ExtendedSearchIntegrator searchIntegrator;
private final HibernateSearchQueryExecutor queryExec;
private final DtoQueryExecutor queryExec;

public SearchFactoryImpl(ExtendedSearchIntegrator searchIntegrator) {
super();
this.searchIntegrator = searchIntegrator;
this.queryExec = new HibernateSearchQueryExecutor();
this.queryExec = new DtoQueryExecutor();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* <br>
* all methods not related to getting Classes out of this are delegated to
* {@link org.hibernate.search.impl.SimpleInitializer}
*
*
* @author Martin
*/
public class SubClassSupportInstanceInitializer implements InstanceInitializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.search.engine.metadata.impl.EmbeddedTypeMetadata;
import org.hibernate.search.engine.metadata.impl.PropertyMetadata;
import org.hibernate.search.engine.metadata.impl.TypeMetadata;
import org.hibernate.search.exception.AssertionFailure;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;

/**
Expand Down Expand Up @@ -74,7 +75,7 @@ private boolean handlePropertyMetadata(TypeMetadata original, RehashedTypeMetada
} ).add( documentFieldMetadata.getName() );
rehashed.idPropertyNameForType.put( type, propertyMetadata.getPropertyAccessorName() );
if ( rehashed.documentFieldMetadataForIdFieldName.containsKey( documentFieldMetadata.getName() ) ) {
throw new AssertionError( "field handled twice!" );
throw new AssertionFailure( "field handled twice!" );
}
rehashed.documentFieldMetadataForIdFieldName.put( documentFieldMetadata.getName(), documentFieldMetadata );
SingularTermDeletionQuery.Type deletionQueryType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hibernate.search.engine.metadata.impl.MetadataProvider;
import org.hibernate.search.engine.service.impl.StandardServiceManager;
import org.hibernate.search.engine.service.spi.ServiceManager;
import org.hibernate.search.exception.AssertionFailure;
import org.hibernate.search.exception.ErrorHandler;
import org.hibernate.search.exception.impl.LogErrorHandler;
import org.hibernate.search.indexes.impl.IndexManagerHolder;
Expand All @@ -34,7 +35,7 @@
public class MetadataUtil {

private MetadataUtil() {
throw new AssertionError( "can't touch this!" );
throw new AssertionFailure( "can't touch this!" );
}

public static MetadataProvider getMetadataProvider(SearchConfiguration searchConfiguration) {
Expand Down Expand Up @@ -85,7 +86,7 @@ public static Map<Class<?>, String> calculateIdProperties(List<RehashedTypeMetad

/**
* calculates the Entity-Classes that are relevant for the indexes represented by the rehashedTypeMetadatas
*
*
* @return all Entity-Classes found in the rehashedTypeMetadatas
*/
public static Set<Class<?>> calculateIndexRelevantEntities(List<RehashedTypeMetadata> rehashedTypeMetadatas) {
Expand Down
Loading

0 comments on commit 9d8923b

Please sign in to comment.