Java Code Examples for org.hibernate.internal.util.collections.ArrayHelper#countNonNull()

The following examples show how to use org.hibernate.internal.util.collections.ArrayHelper#countNonNull() . 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: ReactiveDynamicBatchingCollectionDelegator.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void initialize(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
	// first, figure out how many batchable ids we have...
	final Serializable[] batch = session.getPersistenceContextInternal()
			.getBatchFetchQueue()
			.getCollectionBatch( collectionPersister(), id, maxBatchSize );
	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		singleKeyLoader.loadCollection( session, id, collectionPersister().getKeyType() );
		return;
	}

	final Serializable[] idsToLoad = new Serializable[numberOfIds];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );

	batchLoader.doBatchedCollectionLoad( (SessionImplementor) session, idsToLoad, collectionPersister().getKeyType() );
}
 
Example 2
Source File: ReactiveDynamicBatchingCollectionDelegator.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public CompletionStage<Void> reactiveInitialize(Serializable id, SharedSessionContractImplementor session) {
	final Serializable[] batch = session.getPersistenceContextInternal()
			.getBatchFetchQueue()
			.getCollectionBatch( collectionPersister(), id, maxBatchSize );
	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		return singleKeyLoader.reactiveLoadCollection( (SessionImplementor) session, id,
				collectionPersister().getKeyType() );
	}

	final Serializable[] idsToLoad = new Serializable[numberOfIds];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );

	return batchLoader.doBatchedCollectionLoad( (SessionImplementor) session, idsToLoad,
			collectionPersister().getKeyType() );
}
 
Example 3
Source File: DynamicBatchingCollectionInitializerBuilder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void initialize(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
	// first, figure out how many batchable ids we have...
	final Serializable[] batch = session.getPersistenceContext()
			.getBatchFetchQueue()
			.getCollectionBatch( collectionPersister(), id, maxBatchSize );
	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		singleKeyLoader.loadCollection( session, id, collectionPersister().getKeyType() );
		return;
	}

	final Serializable[] idsToLoad = new Serializable[numberOfIds];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );

	batchLoader.doBatchedCollectionLoad( session, idsToLoad, collectionPersister().getKeyType() );
}
 
Example 4
Source File: ReactivePaddedBatchingEntityLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public CompletionStage<Object> load(Serializable id, Object optionalObject, SharedSessionContractImplementor session, LockOptions lockOptions, Boolean readOnly) {
	final Serializable[] batch = session.getPersistenceContextInternal()
			.getBatchFetchQueue()
			.getEntityBatch( persister(), id, batchSizes[0], persister().getEntityMode() );

	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		return loaders[batchSizes.length-1]
				.load( id, optionalObject, session )
				.thenApply( optional -> {
					if ( optional == null ) {
						// There was no entity with the specified ID. Make sure the EntityKey does not remain
						// in the batch to avoid including it in future batches that get executed.
						BatchFetchQueueHelper.removeBatchLoadableEntityKey( id, persister(), session );
					}
					return optional;
				});
	}

	// Uses the first batch-size bigger than the number of actual ids in the batch
	int indexToUse = batchSizes.length-1;
	for ( int i = 0; i < batchSizes.length-1; i++ ) {
		if ( batchSizes[i] >= numberOfIds ) {
			indexToUse = i;
		}
		else {
			break;
		}
	}

	final Serializable[] idsToLoad = new Serializable[ batchSizes[indexToUse] ];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );
	for ( int i = numberOfIds; i < batchSizes[indexToUse]; i++ ) {
		idsToLoad[i] = id;
	}

	return doBatchLoad( id, loaders[indexToUse], session, idsToLoad, optionalObject, lockOptions, readOnly );
}
 
Example 5
Source File: ReactivePaddedBatchingCollectionInitializer.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public CompletionStage<Void> reactiveInitialize(Serializable id, SharedSessionContractImplementor session) {
	final Serializable[] batch = session.getPersistenceContextInternal()
			.getBatchFetchQueue()
			.getCollectionBatch( persister, id, batchSizes[0] );
	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		loaders[batchSizes.length-1].loadCollection( session, id, persister.getKeyType() );
		return CompletionStages.nullFuture();
	}

	// Uses the first batch-size bigger than the number of actual ids in the batch
	int indexToUse = batchSizes.length-1;
	for ( int i = 0; i < batchSizes.length-1; i++ ) {
		if ( batchSizes[i] >= numberOfIds ) {
			indexToUse = i;
		}
		else {
			break;
		}
	}

	final Serializable[] idsToLoad = new Serializable[ batchSizes[indexToUse] ];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );
	for ( int i = numberOfIds; i < batchSizes[indexToUse]; i++ ) {
		idsToLoad[i] = id;
	}

	return loaders[indexToUse].reactiveLoadCollectionBatch(
			(SessionImplementor) session,
			idsToLoad,
			persister.getKeyType()
	);
}
 
Example 6
Source File: DynamicBatchingEntityLoaderBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object load(
		Serializable id,
		Object optionalObject,
		SharedSessionContractImplementor session,
		LockOptions lockOptions) {
	final Serializable[] batch = session.getPersistenceContext()
			.getBatchFetchQueue()
			.getEntityBatch( persister(), id, maxBatchSize, persister().getEntityMode() );

	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		final Object result =  singleKeyLoader.load( id, optionalObject, session );
		if ( result == null ) {
			// There was no entity with the specified ID. Make sure the EntityKey does not remain
			// in the batch to avoid including it in future batches that get executed.
			BatchFetchQueueHelper.removeBatchLoadableEntityKey( id, persister(), session );
		}
		return result;
	}

	final Serializable[] idsToLoad = new Serializable[numberOfIds];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );

	if ( log.isDebugEnabled() ) {
		log.debugf( "Batch loading entity: %s", MessageHelper.infoString( persister(), idsToLoad, session.getFactory() ) );
	}

	QueryParameters qp = buildQueryParameters( id, idsToLoad, optionalObject, lockOptions );
	List results = dynamicLoader.doEntityBatchFetch( session, qp, idsToLoad );

	// The EntityKey for any entity that is not found will remain in the batch.
	// Explicitly remove the EntityKeys for entities that were not found to
	// avoid including them in future batches that get executed.
	BatchFetchQueueHelper.removeNotFoundBatchLoadableEntityKeys( idsToLoad, results, persister(), session );

	return getObjectFromList( results, id, session );
}
 
Example 7
Source File: PaddedBatchingEntityLoaderBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object load(Serializable id, Object optionalObject, SharedSessionContractImplementor session, LockOptions lockOptions) {
	final Serializable[] batch = session.getPersistenceContext()
			.getBatchFetchQueue()
			.getEntityBatch( persister(), id, batchSizes[0], persister().getEntityMode() );

	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		final Object result =  ( (UniqueEntityLoader) loaders[batchSizes.length-1] ).load( id, optionalObject, session );
		if ( result == null ) {
			// There was no entity with the specified ID. Make sure the EntityKey does not remain
			// in the batch to avoid including it in future batches that get executed.
			BatchFetchQueueHelper.removeBatchLoadableEntityKey( id, persister(), session );
		}
		return result;
	}

	// Uses the first batch-size bigger than the number of actual ids in the batch
	int indexToUse = batchSizes.length-1;
	for ( int i = 0; i < batchSizes.length-1; i++ ) {
		if ( batchSizes[i] >= numberOfIds ) {
			indexToUse = i;
		}
		else {
			break;
		}
	}

	final Serializable[] idsToLoad = new Serializable[ batchSizes[indexToUse] ];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );
	for ( int i = numberOfIds; i < batchSizes[indexToUse]; i++ ) {
		idsToLoad[i] = id;
	}

	return doBatchLoad( id, loaders[indexToUse], session, idsToLoad, optionalObject, lockOptions );
}
 
Example 8
Source File: PaddedBatchingCollectionInitializerBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void initialize(Serializable id, SharedSessionContractImplementor session)	throws HibernateException {
	final Serializable[] batch = session.getPersistenceContext()
			.getBatchFetchQueue()
			.getCollectionBatch( collectionPersister(), id, batchSizes[0] );
	final int numberOfIds = ArrayHelper.countNonNull( batch );
	if ( numberOfIds <= 1 ) {
		loaders[batchSizes.length-1].loadCollection( session, id, collectionPersister().getKeyType() );
		return;
	}

	// Uses the first batch-size bigger than the number of actual ids in the batch
	int indexToUse = batchSizes.length-1;
	for ( int i = 0; i < batchSizes.length-1; i++ ) {
		if ( batchSizes[i] >= numberOfIds ) {
			indexToUse = i;
		}
		else {
			break;
		}
	}

	final Serializable[] idsToLoad = new Serializable[ batchSizes[indexToUse] ];
	System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );
	for ( int i = numberOfIds; i < batchSizes[indexToUse]; i++ ) {
		idsToLoad[i] = id;
	}

	loaders[indexToUse].loadCollectionBatch( session, idsToLoad, collectionPersister().getKeyType() );
}
 
Example 9
Source File: AbstractCollectionReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected AbstractCollectionReference(
		ExpandingCollectionQuerySpace collectionQuerySpace,
		PropertyPath propertyPath,
		boolean shouldIncludeJoins) {
	this.collectionQuerySpace = collectionQuerySpace;
	this.propertyPath = propertyPath;

	this.allowElementJoin = shouldIncludeJoins;

	// Currently we can only allow a join for the collection index if all of the following are true:
	// - collection element joins are allowed;
	// - index is an EntityType;
	// - index values are not "formulas" (e.g., a @MapKey index is translated into "formula" value(s)).
	// Hibernate cannot currently support eager joining of associations within a component (@Embeddable) as an index.
	if ( shouldIncludeJoins &&
			collectionQuerySpace.getCollectionPersister().hasIndex() &&
			collectionQuerySpace.getCollectionPersister().getIndexType().isEntityType()  ) {
		final String[] indexFormulas =
				( (QueryableCollection) collectionQuerySpace.getCollectionPersister() ).getIndexFormulas();
		final int nNonNullFormulas = ArrayHelper.countNonNull( indexFormulas );
		this.allowIndexJoin = nNonNullFormulas == 0;
	}
	else {
		this.allowIndexJoin = false;
	}

	// All other fields must be initialized before building this.index and this.element.
	this.index = buildIndexGraph();
	this.element = buildElementGraph();
}
 
Example 10
Source File: ReactiveDynamicBatchingEntityDelegator.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
	public CompletionStage<Object> load(
			Serializable id,
			Object optionalObject,
			SharedSessionContractImplementor session,
			LockOptions lockOptions,
			Boolean readOnly) {
		final Serializable[] batch = session.getPersistenceContextInternal()
				.getBatchFetchQueue()
				.getEntityBatch( persister(), id, maxBatchSize, persister().getEntityMode() );

		final int numberOfIds = ArrayHelper.countNonNull( batch );
		if ( numberOfIds <= 1 ) {
			final Object result =  singleKeyLoader.load( id, optionalObject, session );
			if ( result == null ) {
				// There was no entity with the specified ID. Make sure the EntityKey does not remain
				// in the batch to avoid including it in future batches that get executed.
				BatchFetchQueueHelper.removeBatchLoadableEntityKey( id, persister(), session );
			}
			return CompletionStages.completedFuture(result);
		}

		final Serializable[] idsToLoad = new Serializable[numberOfIds];
		System.arraycopy( batch, 0, idsToLoad, 0, numberOfIds );

//		if ( log.isDebugEnabled() ) {
//			log.debugf( "Batch loading entity: %s", MessageHelper.infoString( persister(), idsToLoad, session.getFactory() ) );
//		}

		QueryParameters qp = buildQueryParameters( id, idsToLoad, optionalObject, lockOptions, false );

		return dynamicLoader.doEntityBatchFetch( (SessionImplementor) session, qp, idsToLoad )
				.thenApply( results -> {
					// The EntityKey for any entity that is not found will remain in the batch.
					// Explicitly remove the EntityKeys for entities that were not found to
					// avoid including them in future batches that get executed.
					BatchFetchQueueHelper.removeNotFoundBatchLoadableEntityKeys( idsToLoad, results, persister(), session );

					return getObjectFromList(results, id, session);
				} );
	}