forked from hibernate/hibernate-reactive
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
393 additions
and
180 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...reactive/sql/results/graph/collection/internal/ReactiveAbstractCollectionInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.sql.results.graph.collection.internal; | ||
|
||
public class ReactiveAbstractCollectionInitializer { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...org/hibernate/reactive/sql/results/graph/collection/internal/ReactiveListInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.sql.results.graph.collection.internal; | ||
|
||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
import org.hibernate.HibernateException; | ||
import org.hibernate.LockMode; | ||
import org.hibernate.collection.spi.PersistentList; | ||
import org.hibernate.engine.spi.CollectionKey; | ||
import org.hibernate.internal.log.LoggingHelper; | ||
import org.hibernate.metamodel.mapping.PluralAttributeMapping; | ||
import org.hibernate.spi.NavigablePath; | ||
import org.hibernate.sql.results.graph.AssemblerCreationState; | ||
import org.hibernate.sql.results.graph.DomainResult; | ||
import org.hibernate.sql.results.graph.DomainResultAssembler; | ||
import org.hibernate.sql.results.graph.Fetch; | ||
import org.hibernate.sql.results.graph.Initializer; | ||
import org.hibernate.sql.results.graph.InitializerData; | ||
import org.hibernate.sql.results.graph.InitializerParent; | ||
import org.hibernate.sql.results.graph.collection.internal.AbstractImmediateCollectionInitializer; | ||
import org.hibernate.sql.results.graph.collection.internal.ListInitializer; | ||
import org.hibernate.sql.results.jdbc.spi.RowProcessingState; | ||
|
||
public class ReactiveListInitializer | ||
extends AbstractImmediateCollectionInitializer<AbstractImmediateCollectionInitializer.ImmediateCollectionInitializerData> { | ||
private static final String CONCRETE_NAME = ListInitializer.class.getSimpleName(); | ||
|
||
private final DomainResultAssembler<Integer> listIndexAssembler; | ||
private final DomainResultAssembler<?> elementAssembler; | ||
|
||
private final int listIndexBase; | ||
|
||
public ReactiveListInitializer( | ||
NavigablePath navigablePath, | ||
PluralAttributeMapping attributeMapping, | ||
InitializerParent<?> parent, | ||
LockMode lockMode, | ||
DomainResult<?> collectionKeyResult, | ||
DomainResult<?> collectionValueKeyResult, | ||
boolean isResultInitializer, | ||
AssemblerCreationState creationState, | ||
Fetch listIndexFetch, | ||
Fetch elementFetch) { | ||
super( | ||
navigablePath, | ||
attributeMapping, | ||
parent, | ||
lockMode, | ||
collectionKeyResult, | ||
collectionValueKeyResult, | ||
isResultInitializer, | ||
creationState | ||
); | ||
//noinspection unchecked | ||
this.listIndexAssembler = (DomainResultAssembler<Integer>) listIndexFetch.createAssembler( | ||
this, | ||
creationState | ||
); | ||
this.elementAssembler = elementFetch.createAssembler( this, creationState ); | ||
this.listIndexBase = attributeMapping.getIndexMetadata().getListIndexBase(); | ||
} | ||
|
||
@Override | ||
protected String getSimpleConcreteImplName() { | ||
return CONCRETE_NAME; | ||
} | ||
|
||
@Override | ||
protected void forEachSubInitializer( | ||
BiConsumer<Initializer<?>, RowProcessingState> consumer, | ||
InitializerData data) { | ||
super.forEachSubInitializer( consumer, data ); | ||
final Initializer<?> initializer = elementAssembler.getInitializer(); | ||
if ( initializer != null ) { | ||
consumer.accept( initializer, data.getRowProcessingState() ); | ||
} | ||
} | ||
|
||
@Override | ||
public PersistentList<?> getCollectionInstance(ImmediateCollectionInitializerData data) { | ||
return (PersistentList<?>) super.getCollectionInstance( data ); | ||
} | ||
|
||
@Override | ||
protected void readCollectionRow( | ||
CollectionKey collectionKey, | ||
List<Object> loadingState, | ||
RowProcessingState rowProcessingState) { | ||
final Integer indexValue = listIndexAssembler.assemble( rowProcessingState ); | ||
if ( indexValue == null ) { | ||
throw new HibernateException( "Illegal null value for list index encountered while reading: " | ||
+ getCollectionAttributeMapping().getNavigableRole() ); | ||
} | ||
final Object element = elementAssembler.assemble( rowProcessingState ); | ||
if ( element == null ) { | ||
// If element is null, then NotFoundAction must be IGNORE | ||
return; | ||
} | ||
int index = indexValue; | ||
|
||
if ( listIndexBase != 0 ) { | ||
index -= listIndexBase; | ||
} | ||
|
||
for ( int i = loadingState.size(); i <= index; ++i ) { | ||
loadingState.add( i, null ); | ||
} | ||
|
||
loadingState.set( index, element ); | ||
} | ||
|
||
@Override | ||
protected void initializeSubInstancesFromParent(ImmediateCollectionInitializerData data) { | ||
final Initializer<?> initializer = elementAssembler.getInitializer(); | ||
if ( initializer != null ) { | ||
final RowProcessingState rowProcessingState = data.getRowProcessingState(); | ||
final PersistentList<?> list = getCollectionInstance( data ); | ||
assert list != null; | ||
for ( Object element : list ) { | ||
initializer.initializeInstanceFromParent( element, rowProcessingState ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void resolveInstanceSubInitializers(ImmediateCollectionInitializerData data) { | ||
final Initializer<?> initializer = elementAssembler.getInitializer(); | ||
if ( initializer != null ) { | ||
final RowProcessingState rowProcessingState = data.getRowProcessingState(); | ||
final PersistentList<?> list = getCollectionInstance( data ); | ||
assert list != null; | ||
for ( Object element : list ) { | ||
initializer.resolveInstance( element, rowProcessingState ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public DomainResultAssembler<?> getIndexAssembler() { | ||
return listIndexAssembler; | ||
} | ||
|
||
@Override | ||
public DomainResultAssembler<?> getElementAssembler() { | ||
return elementAssembler; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ReactiveListInitializer(" + LoggingHelper.toLoggableString( getNavigablePath() ) + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.