Skip to content

Commit

Permalink
HSEARCH-4875 Simplify Search/ORM query adapters for ORM 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and marko-bekhta committed Sep 25, 2023
1 parent 50e854b commit 3cf0052
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.spi.AppliedGraph;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.internal.QueryOptionsImpl;
import org.hibernate.query.spi.AbstractQuery;
import org.hibernate.query.spi.MutableQueryOptions;
import org.hibernate.query.spi.ParameterMetadataImplementor;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.spi.QueryParameterBinding;
Expand Down Expand Up @@ -61,15 +61,12 @@ public static <R> HibernateOrmSearchQueryAdapter<R> create(SearchQuery<R> query)

private final SearchQueryImplementor<R> delegate;

private final SessionImplementor sessionImplementor;
private final MutableEntityLoadingOptions loadingOptions;
private final QueryOptionsImpl queryOptions = new QueryOptionsImpl();

HibernateOrmSearchQueryAdapter(SearchQueryImplementor<R> delegate, SessionImplementor sessionImplementor,
MutableEntityLoadingOptions loadingOptions) {
super( sessionImplementor );
this.delegate = delegate;
this.sessionImplementor = sessionImplementor;
this.loadingOptions = loadingOptions;
}

Expand Down Expand Up @@ -111,11 +108,6 @@ public String getQueryString() {
return delegate.queryString();
}

@Override
public QueryOptionsImpl getQueryOptions() {
return queryOptions;
}

@Override
public HibernateOrmSearchQueryAdapter<R> setHint(String hintName, Object value) {
switch ( hintName ) {
Expand Down Expand Up @@ -163,18 +155,11 @@ protected ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode) {
throw log.canOnlyUseScrollWithScrollModeForwardsOnly( scrollMode );
}

extractQueryOptions();

int chunkSize = loadingOptions.fetchSize();
return new HibernateOrmSearchScrollableResultsAdapter<>( delegate.scroll( chunkSize ), getMaxResults(),
Function.identity() );
}

@Override
public SharedSessionContractImplementor getSession() {
return sessionImplementor;
}

@Override
protected List<R> doList() {
// Do not use getMaxRows()/getFirstRow() directly, they return weird values to comply with the JPA spec
Expand All @@ -191,17 +176,22 @@ protected void beforeQuery() {
}

private void extractQueryOptions() {
Integer queryFetchSize = getQueryOptions().getFetchSize();
MutableQueryOptions queryOptions = getQueryOptions();
Integer queryFetchSize = queryOptions.getFetchSize();
if ( queryFetchSize != null ) {
loadingOptions.fetchSize( queryFetchSize );
}
Integer queryTimeout = getQueryOptions().getTimeout();
Integer queryTimeout = queryOptions.getTimeout();
if ( queryTimeout != null ) {
delegate.failAfter( queryTimeout, TimeUnit.SECONDS );
}
EntityGraphHint<?> entityGraphHint = null;
if ( queryOptions.getGraph() != null ) {
entityGraphHint = new EntityGraphHint<>( queryOptions.getGraph(), queryOptions.getSemantic() );
if ( hasAppliedGraph( queryOptions ) ) {
AppliedGraph appliedGraph = queryOptions.getAppliedGraph();
RootGraph<?> graph = appliedGraph.getGraph();
if ( graph != null ) {
entityGraphHint = new EntityGraphHint<>( graph, appliedGraph.getSemantic() );
}
}
loadingOptions.entityGraphHint( entityGraphHint, true );
}
Expand Down Expand Up @@ -302,7 +292,7 @@ public int executeUpdate() throws HibernateException {

@Override
protected int doExecuteUpdate() {
throw new UnsupportedOperationException( "executeUpdate is not supported in Hibernate Search queries" );
throw new UnsupportedOperationException( "executeUpdate() is not supported in Hibernate Search queries" );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import org.hibernate.ScrollMode;
import org.hibernate.TypeMismatchException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.internal.QueryOptionsImpl;
import org.hibernate.query.spi.AbstractQuery;
import org.hibernate.query.spi.MutableQueryOptions;
import org.hibernate.query.spi.ParameterMetadataImplementor;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.spi.QueryParameterBinding;
Expand Down Expand Up @@ -73,8 +72,6 @@ public class FullTextQueryImpl extends AbstractQuery implements FullTextQuery {

private final HSQuery hSearchQuery;

private final SessionImplementor sessionImplementor;
private final QueryOptionsImpl queryOptions = new QueryOptionsImpl();
//initialized at 0 since we don't expect to use hints at this stage
private final Map<String, Object> hints = new HashMap<String, Object>( 0 );

Expand All @@ -100,7 +97,6 @@ public FullTextQueryImpl(Query luceneQuery, SessionImplementor sessionImplemento
V5MigrationSearchSession<SearchLoadingOptionsStep> searchSession,
Class<?>... entities) {
super( sessionImplementor );
this.sessionImplementor = sessionImplementor;
this.searchSession = searchSession;
this.hSearchQuery = searchIntegrator.createHSQuery( luceneQuery, searchSession,
loadingOptionsContributor, entities );
Expand All @@ -124,7 +120,6 @@ public ScrollableResultsImplementor scroll() {

@Override
protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) {
extractQueryOptions();
SearchScroll<?> scroll = hSearchQuery.scroll( fetchSize != null ? fetchSize : 100 );
Integer maxResults = hSearchQuery.maxResults();
return new HibernateOrmSearchScrollableResultsAdapter<>( scroll,
Expand Down Expand Up @@ -159,15 +154,16 @@ protected void beforeQuery() {
}

private void extractQueryOptions() {
Integer limit = getQueryOptions().getLimit().getMaxRows();
MutableQueryOptions queryOptions = getQueryOptions();
Integer limit = queryOptions.getLimit().getMaxRows();
hSearchQuery.maxResults( limit );
Integer offset = getQueryOptions().getLimit().getFirstRow();
Integer offset = queryOptions.getLimit().getFirstRow();
hSearchQuery.firstResult( offset == null ? 0 : offset );
Integer queryFetchSize = getQueryOptions().getFetchSize();
Integer queryFetchSize = queryOptions.getFetchSize();
if ( queryFetchSize != null ) {
fetchSize = queryFetchSize;
}
Integer queryTimeout = getQueryOptions().getTimeout();
Integer queryTimeout = queryOptions.getTimeout();
if ( queryTimeout != null ) {
hSearchQuery.failAfter( queryTimeout, TimeUnit.SECONDS );
}
Expand Down Expand Up @@ -391,7 +387,7 @@ public int executeUpdate() throws HibernateException {

@Override
protected int doExecuteUpdate() {
throw new UnsupportedOperationException( "executeUpdate is not supported in Hibernate Search queries" );
throw new UnsupportedOperationException( "executeUpdate() is not supported in Hibernate Search queries" );
}

@Override
Expand Down Expand Up @@ -447,16 +443,6 @@ public String getQueryString() {
return hSearchQuery.getQueryString();
}

@Override
public QueryOptionsImpl getQueryOptions() {
return queryOptions;
}

@Override
public SharedSessionContractImplementor getSession() {
return sessionImplementor;
}

@Override
public String toString() {
return "FullTextQueryImpl(" + getQueryString() + ")";
Expand Down

0 comments on commit 3cf0052

Please sign in to comment.