Skip to content

Commit

Permalink
HSEARCH-3297 Use explicit dependency declaration in bridges where pos…
Browse files Browse the repository at this point in the history
…sible

That's likely to become the recommended way of implementing a bridge.
  • Loading branch information
yrodiere committed Apr 29, 2019
1 parent 1b6017c commit 7b83bb5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hibernate.search.mapper.pojo.bridge.runtime.PropertyBridgeWriteContext;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.model.PojoElementAccessor;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.integrationtest.orm.OrmUtils;
Expand Down Expand Up @@ -208,13 +207,12 @@ public void setIndexedInContaining(int indexedInContaining) {

public static class BridgeGoingThroughEntityBoundaries implements PropertyBridge {

private PojoElementAccessor<Integer> sourceFieldAccessor;
private IndexFieldReference<Integer> indexFieldReference;

@Override
public void bind(PropertyBridgeBindingContext context) {
sourceFieldAccessor = context.getBridgedElement().property( "indexedInContaining" )
.createAccessor( Integer.class );
context.getDependencies().use( "indexedInContaining" );

indexFieldReference = context.getIndexSchemaElement().field(
"indexedInContaining", f -> f.asInteger()
)
Expand All @@ -223,7 +221,8 @@ public void bind(PropertyBridgeBindingContext context) {

@Override
public void write(DocumentElement target, Object bridgedElement, PropertyBridgeWriteContext context) {
Integer value = sourceFieldAccessor.read( bridgedElement );
Contained castedBridgedElement = (Contained) bridgedElement;
Integer value = castedBridgedElement.getIndexedInContaining();
target.addValue( indexFieldReference, value );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.hibernate.search.mapper.pojo.bridge.TypeBridge;
import org.hibernate.search.mapper.pojo.bridge.binding.TypeBridgeBindingContext;
import org.hibernate.search.mapper.pojo.bridge.runtime.TypeBridgeWriteContext;
import org.hibernate.search.mapper.pojo.model.PojoElementAccessor;
import org.hibernate.search.integrationtest.showcase.library.model.Account;
import org.hibernate.search.integrationtest.showcase.library.model.Borrowal;

Expand All @@ -33,16 +32,15 @@
*/
public class AccountBorrowalSummaryBridge implements TypeBridge {

private PojoElementAccessor<Account> accountAccessor;
private IndexObjectFieldReference borrowalsObjectFieldReference;
private IndexFieldReference<Integer> shortTermBorrowalCountReference;
private IndexFieldReference<Integer> longTermBorrowalCountReference;
private IndexFieldReference<Integer> totalBorrowalCountReference;

@Override
public void bind(TypeBridgeBindingContext context) {
// TODO allow to access collections properly, and more importantly to declare dependencies on parts of collection items
accountAccessor = context.getBridgedElement().createAccessor( Account.class );
context.getDependencies()
.use( "borrowals.type" );

IndexSchemaObjectField borrowalsObjectField = context.getIndexSchemaElement().objectField( "borrowals" );
borrowalsObjectFieldReference = borrowalsObjectField.toReference();
Expand All @@ -62,12 +60,11 @@ public void bind(TypeBridgeBindingContext context) {

@Override
public void write(DocumentElement target, Object bridgedElement, TypeBridgeWriteContext context) {
Account account = accountAccessor.read( bridgedElement );
Account account = (Account) bridgedElement;
if ( account == null ) {
return;
}

// TODO this is bad, see the bind() method
List<Borrowal> borrowals = account.getBorrowals();

int shortTermBorrowalCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.hibernate.search.mapper.pojo.bridge.mapping.AnnotationBridgeBuilder;
import org.hibernate.search.mapper.pojo.bridge.mapping.BridgeBuildContext;
import org.hibernate.search.mapper.pojo.bridge.runtime.PropertyBridgeWriteContext;
import org.hibernate.search.mapper.pojo.model.PojoElementAccessor;

public class MultiKeywordStringBridge implements PropertyBridge {

Expand Down Expand Up @@ -54,7 +53,6 @@ public BeanHolder<PropertyBridge> build(BridgeBuildContext buildContext) {
private final String fieldName;
private final Pattern separatorPattern;

private PojoElementAccessor<String> sourceAccessor;
private IndexFieldReference<String> valueFieldReference;

private MultiKeywordStringBridge(Builder builder) {
Expand All @@ -64,7 +62,6 @@ private MultiKeywordStringBridge(Builder builder) {

@Override
public void bind(PropertyBridgeBindingContext context) {
sourceAccessor = context.getBridgedElement().createAccessor( String.class );
valueFieldReference = context.getIndexSchemaElement().field(
fieldName, f -> f.asString()
)
Expand All @@ -73,7 +70,7 @@ public void bind(PropertyBridgeBindingContext context) {

@Override
public void write(DocumentElement target, Object bridgedElement, PropertyBridgeWriteContext context) {
String sourceValue = sourceAccessor.read( bridgedElement );
String sourceValue = (String) bridgedElement;
if ( sourceValue != null ) {
String[] items = separatorPattern.split( sourceValue );
for ( String item : items ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void contributeDependencies(PojoIndexingDependencyCollectorPropertyNode<?
BoundContainerExtractorPath.noExtractors( propertyModel.getTypeModel() )
);
dependencyCollectorValueNode.collectDependency();

PojoIndexingDependencyCollectorTypeNode<?> dependencyCollectorTypeNode =
dependencyCollectorValueNode.type();
for ( BoundPojoModelPathValueNode<?, ?, ?> usedPath : usedPaths ) {
Expand Down

0 comments on commit 7b83bb5

Please sign in to comment.