org.hibernate.cache.spi.QueryKey Java Examples

The following examples show how to use org.hibernate.cache.spi.QueryKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private List assembleCachedResult(
		final QueryKey key,
		final List cached,
		boolean singleResult,
		final Type[] returnTypes,
		final SharedSessionContractImplementor session) throws HibernateException {

	final List result = new ArrayList( cached.size() );
	if ( singleResult ) {
		for ( Object aCached : cached ) {
			result.add( returnTypes[0].assemble( (Serializable) aCached, session, null ) );
		}
	}
	else {
		for ( int i = 0; i < cached.size(); i++ ) {
			result.add(
					TypeHelper.assemble( (Serializable[]) cached.get( i ), returnTypes, session, null )
			);
			if ( TRACING ) {
				logCachedResultRowDetails( returnTypes, result.get( i ) );
			}
		}
	}
	return result;
}
 
Example #2
Source File: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
default List<?> transform(QueryParameters queryParameters, QueryKey key, List<Object> result,
						  ResultTransformer resolvedTransformer) {
	if (resolvedTransformer == null) {
		return result;
	}
	else {
		CacheableResultTransformer transformer = key.getResultTransformer();
		if ( areResultSetRowsTransformedImmediately() ) {
			return transformer.retransformResults(
					result,
					getResultRowAliases(),
					queryParameters.getResultTransformer(),
					includeInResultRow()
			);
		}
		else {
			return transformer.untransformToTuples(result);
		}
	}
}
 
Example #3
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static void logCachedResultDetails(QueryKey key, Set querySpaces, Type[] returnTypes, List result) {
	if ( !TRACING ) {
		return;
	}
	LOG.trace( "key.hashCode=" + key.hashCode() );
	LOG.trace( "querySpaces=" + querySpaces );
	if ( returnTypes == null || returnTypes.length == 0 ) {
		LOG.trace(
				"Unexpected returnTypes is "
						+ ( returnTypes == null ? "null" : "empty" ) + "! result"
						+ ( result == null ? " is null" : ".size()=" + result.size() )
		);
	}
	else {
		final StringBuilder returnTypeInfo = new StringBuilder();
		for ( Type returnType : returnTypes ) {
			returnTypeInfo.append( "typename=" )
					.append( returnType.getName() )
					.append( " class=" )
					.append( returnType.getReturnedClass().getName() )
					.append( ' ' );
		}
		LOG.trace( "unexpected returnTypes is " + returnTypeInfo.toString() + "! result" );
	}
}
 
Example #4
Source File: Loader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void putResultInQueryCache(
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final Type[] resultTypes,
		final QueryResultsCache queryCache,
		final QueryKey key,
		final List result) {
	if ( session.getCacheMode().isPutEnabled() ) {
		boolean put = queryCache.put(
				key,
				result,
				key.getResultTransformer().getCachedResultTypes( resultTypes ),
				session
		);
		if ( put && factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatistics().queryCachePut( getQueryIdentifier(), queryCache.getRegion().getName() );
		}
	}
}
 
Example #5
Source File: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
default CompletionStage<List<Object>> reactiveListUsingQueryCache(
		final String sql, final String queryIdentifier,
		final SessionImplementor session,
		final QueryParameters queryParameters,
		final Set<Serializable> querySpaces,
		final Type[] resultTypes) {

	QueryResultsCache queryCache = session.getSessionFactory().getCache()
			.getQueryResultsCache( queryParameters.getCacheRegion() );

	QueryKey key = queryKey( sql, session, queryParameters );

	List<Object> cachedList = getResultFromQueryCache( session, queryParameters, querySpaces, resultTypes, queryCache, key );

	CompletionStage<List<Object>> list;
	if ( cachedList == null ) {
		list = doReactiveList( sql, queryIdentifier, session, queryParameters, key.getResultTransformer() )
				.thenApply( cachableList -> {
					putResultInQueryCache( session, queryParameters, resultTypes, queryCache, key, cachableList );
					return cachableList;
				} );
	}
	else {
		list = CompletionStages.completedFuture( cachedList );
	}

	return list.thenApply(
			result -> getResultList(
					transform( queryParameters, key, result,
							resolveResultTransformer( queryParameters.getResultTransformer() ) ),
					queryParameters.getResultTransformer()
			)
	);
}
 
Example #6
Source File: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
default QueryKey queryKey(String sql, SessionImplementor session, QueryParameters queryParameters) {
	return QueryKey.generateQueryKey(
			sql,
			queryParameters,
			FilterKey.createFilterKeys( session.getLoadQueryInfluencers().getEnabledFilters() ),
			session,
			cacheableResultTransformer( queryParameters )
	);
}
 
Example #7
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private CacheItem getCachedData(QueryKey key, SharedSessionContractImplementor session) {
	CacheItem cachedItem = null;
	try {
		session.getEventListenerManager().cacheGetStart();
		cachedItem = (CacheItem) cacheRegion.getFromCache( key, session );
	}
	finally {
		session.getEventListenerManager().cacheGetEnd( cachedItem != null );
	}
	return cachedItem;
}
 
Example #8
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List get(
		QueryKey key,
		Set<Serializable> spaces,
		final Type[] returnTypes,
		SharedSessionContractImplementor session) {
	return get(
			key,
			QuerySpacesHelper.INSTANCE.toStringArray( spaces ),
			returnTypes,
			session
	);
}
 
Example #9
Source File: CustomLoader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@link #resultTypes} can be overridden by {@link #autoDiscoverTypes(ResultSet)},
 * *after* {@link #list(SharedSessionContractImplementor, QueryParameters)} has already been called.  It's a bit of a
 * chicken-and-the-egg issue since {@link #autoDiscoverTypes(ResultSet)} needs the {@link ResultSet}.
 * <p/>
 * As a hacky workaround, overriden here to provide the {@link #resultTypes}.
 *
 * see HHH-3051
 */
@Override
protected void putResultInQueryCache(
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final Type[] resultTypes,
		final QueryResultsCache queryCache,
		final QueryKey key,
		final List result) {
	super.putResultInQueryCache( session, queryParameters, this.resultTypes, queryCache, key, result );
}
 
Example #10
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private QueryKey generateQueryKey(
		SharedSessionContractImplementor session,
		QueryParameters queryParameters) {
	return QueryKey.generateQueryKey(
			getSQLString(),
			queryParameters,
			FilterKey.createFilterKeys( session.getLoadQueryInfluencers().getEnabledFilters() ),
			session,
			createCacheableResultTransformer( queryParameters )
	);
}
 
Example #11
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private List listUsingQueryCache(
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final Set<Serializable> querySpaces,
		final Type[] resultTypes) {

	QueryResultsCache queryCache = factory.getCache().getQueryResultsCache( queryParameters.getCacheRegion() );

	QueryKey key = generateQueryKey( session, queryParameters );

	if ( querySpaces == null || querySpaces.size() == 0 ) {
		LOG.tracev( "Unexpected querySpaces is {0}", ( querySpaces == null ? querySpaces : "empty" ) );
	}
	else {
		LOG.tracev( "querySpaces is {0}", querySpaces );
	}

	List result = getResultFromQueryCache(
			session,
			queryParameters,
			querySpaces,
			resultTypes,
			queryCache,
			key
	);

	if ( result == null ) {
		result = doList( session, queryParameters, key.getResultTransformer() );

		putResultInQueryCache(
				session,
				queryParameters,
				resultTypes,
				queryCache,
				key,
				result
		);
	}

	ResultTransformer resolvedTransformer = resolveResultTransformer( queryParameters.getResultTransformer() );
	if ( resolvedTransformer != null ) {
		result = (
				areResultSetRowsTransformedImmediately() ?
						key.getResultTransformer().retransformResults(
								result,
								getResultRowAliases(),
								queryParameters.getResultTransformer(),
								includeInResultRow()
						) :
						key.getResultTransformer().untransformToTuples(
								result
						)
		);
	}

	return getResultList( result, queryParameters.getResultTransformer() );
}
 
Example #12
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public List get(
		final QueryKey key,
		final String[] spaces,
		final Type[] returnTypes,
		final SharedSessionContractImplementor session) {
	if ( DEBUGGING ) {
		LOG.debugf( "Checking cached query results in region: %s", cacheRegion.getName() );
	}

	final CacheItem cacheItem = getCachedData( key, session );
	if ( cacheItem == null ) {
		if ( DEBUGGING ) {
			LOG.debug( "Query results were not found in cache" );
		}
		return null;
	}

	if ( !timestampsCache.isUpToDate( spaces, cacheItem.timestamp, session ) ) {
		if ( DEBUGGING ) {
			LOG.debug( "Cached query results were not up-to-date" );
		}
		return null;
	}

	if ( DEBUGGING ) {
		LOG.debug( "Returning cached query results" );
	}

	final boolean singleResult = returnTypes.length == 1;
	for ( int i = 0; i < cacheItem.results.size(); i++ ) {
		if ( singleResult ) {
			returnTypes[0].beforeAssemble( (Serializable) cacheItem.results.get( i ), session );
		}
		else {
			TypeHelper.beforeAssemble( (Serializable[]) cacheItem.results.get( i ), returnTypes, session );
		}
	}

	return assembleCachedResult( key, cacheItem.results, singleResult, returnTypes, session );
}
 
Example #13
Source File: QueryResultsCacheImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public boolean put(
		final QueryKey key,
		final List results,
		final Type[] returnTypes,
		final SharedSessionContractImplementor session) throws HibernateException {
	if ( DEBUGGING ) {
		LOG.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), session.getTransactionStartTimestamp() );
	}

	final List resultsCopy = CollectionHelper.arrayList( results.size() );

	final boolean isSingleResult = returnTypes.length == 1;
	for ( Object aResult : results ) {
		final Serializable resultRowForCache;
		if ( isSingleResult ) {
			resultRowForCache = returnTypes[0].disassemble( aResult, session, null );
		}
		else {
			resultRowForCache = TypeHelper.disassemble( (Object[]) aResult, returnTypes, null, session, null );
		}
		resultsCopy.add( resultRowForCache );
		if ( TRACING ) {
			logCachedResultRowDetails( returnTypes, aResult );
		}
	}

	if ( TRACING ) {
		logCachedResultDetails( key, null, returnTypes, resultsCopy );
	}

	final CacheItem cacheItem = new CacheItem(
			session.getTransactionStartTimestamp(),
			resultsCopy
	);

	try {
		session.getEventListenerManager().cachePutStart();
		cacheRegion.putIntoCache( key, cacheItem, session );
	}
	finally {
		session.getEventListenerManager().cachePutEnd();
	}

	return true;
}
 
Example #14
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private List getResultFromQueryCache(
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final Set<Serializable> querySpaces,
		final Type[] resultTypes,
		final QueryResultsCache queryCache,
		final QueryKey key) {
	List result = null;

	if ( session.getCacheMode().isGetEnabled() ) {
		boolean isImmutableNaturalKeyLookup =
				queryParameters.isNaturalKeyLookup() &&
						resultTypes.length == 1 &&
						resultTypes[0].isEntityType() &&
						getEntityPersister( EntityType.class.cast( resultTypes[0] ) )
								.getEntityMetamodel()
								.hasImmutableNaturalId();

		final PersistenceContext persistenceContext = session.getPersistenceContext();
		boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
		if ( queryParameters.isReadOnlyInitialized() ) {
			// The read-only/modifiable mode for the query was explicitly set.
			// Temporarily set the default read-only/modifiable setting to the query's setting.
			persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
		}
		else {
			// The read-only/modifiable setting for the query was not initialized.
			// Use the default read-only/modifiable from the persistence context instead.
			queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
		}
		try {
			result = queryCache.get(
					key,
					querySpaces,
					key.getResultTransformer().getCachedResultTypes( resultTypes ),
					session
			);
		}
		finally {
			persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
		}

		if ( factory.getStatistics().isStatisticsEnabled() ) {
			if ( result == null ) {
				factory.getStatistics().queryCacheMiss( getQueryIdentifier(), queryCache.getRegion().getName() );
			}
			else {
				factory.getStatistics().queryCacheHit( getQueryIdentifier(), queryCache.getRegion().getName() );
			}
		}
	}

	return result;
}
 
Example #15
Source File: ReactiveCustomLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override @SuppressWarnings("unchecked")
public List<Object> getResultFromQueryCache(SessionImplementor session, QueryParameters queryParameters, Set<Serializable> querySpaces, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key) {
	return super.getResultFromQueryCache(session, queryParameters, querySpaces, resultTypes, queryCache, key);
}
 
Example #16
Source File: ReactiveQueryLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void putResultInQueryCache(SessionImplementor session, QueryParameters queryParameters, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key, List<Object> cachableList) {
	super.putResultInQueryCache(session, queryParameters, resultTypes, queryCache, key, cachableList);
}
 
Example #17
Source File: ReactiveQueryLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override @SuppressWarnings("unchecked")
public List<Object> getResultFromQueryCache(SessionImplementor session, QueryParameters queryParameters, Set<Serializable> querySpaces, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key) {
	return super.getResultFromQueryCache(session, queryParameters, querySpaces, resultTypes, queryCache, key);
}
 
Example #18
Source File: ReactiveCustomLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void putResultInQueryCache(SessionImplementor session, QueryParameters queryParameters, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key, List<Object> cachableList) {
	super.putResultInQueryCache(session, queryParameters, resultTypes, queryCache, key, cachableList);
}
 
Example #19
Source File: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 votes vote down vote up
void putResultInQueryCache(SessionImplementor session, QueryParameters queryParameters, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key, List<Object> cachableList); 
Example #20
Source File: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 votes vote down vote up
List<Object> getResultFromQueryCache(SessionImplementor session, QueryParameters queryParameters, Set<Serializable> querySpaces, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key);