Skip to content

Commit

Permalink
HSEARCH-3566 Test reindexing when an association mentioned in an Inde…
Browse files Browse the repository at this point in the history
…xingDependency.derivedFrom is updated
  • Loading branch information
yrodiere committed Apr 24, 2019
1 parent a050346 commit 9fd99a9
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 33 deletions.
Expand Up @@ -71,7 +71,8 @@
* </li>
* <li>
* the second token defines how the association between TContaining and TContained is indexed:
* indexed-embedded, non-indexed-embedded, indexed-embbedded with ReindexOnUpdate.NO.
* indexed-embedded, non-indexed-embedded, indexed-embbedded with ReindexOnUpdate.NO,
* indexed as a side effect of being used in a cross-entity derived property.
* </li>
* <li>
* the third token (if any) defines which type of value is being updated/replaced:
Expand Down Expand Up @@ -630,8 +631,13 @@ public void indirectValueUpdate_indexedEmbedded_containedDerivedValue_indexed()
backendMock.verifyExpectationsMet();
}

/**
* Test that updating a property in an entity
* when this property is the last component of a path in a @IndexingDependency.derivedFrom attribute
* does trigger reindexing of the indexed entity.
*/
@Test
public void indirectValueUpdate_indexedEmbedded_crossEntityDerivedValue_indexed() {
public void indirectValueUpdate_usedInCrossEntityDerivedProperty_crossEntityDerivedValue_indexed() {
OrmUtils.withinTransaction( sessionFactory, session -> {
TIndexed entity1 = primitives.newIndexed( 1 );

Expand All @@ -642,8 +648,8 @@ public void indirectValueUpdate_indexedEmbedded_crossEntityDerivedValue_indexed(
TContained contained1 = primitives.newContained( 4 );
primitives.setFieldUsedInCrossEntityDerivedField1( contained1, "field1_initialValue" );
primitives.setFieldUsedInCrossEntityDerivedField2( contained1, "field2_initialValue" );
primitives.setContainedIndexedEmbeddedSingle( containingEntity1, contained1 );
primitives.setContainingAsIndexedEmbeddedSingle( contained1, containingEntity1 );
primitives.setContainedUsedInCrossEntityDerivedPropertySingle( containingEntity1, contained1 );
primitives.setContainingAsUsedInCrossEntityDerivedPropertySingle( contained1, containingEntity1 );

session.persist( contained1 );
session.persist( containingEntity1 );
Expand All @@ -656,9 +662,6 @@ public void indirectValueUpdate_indexedEmbedded_crossEntityDerivedValue_indexed(
"crossEntityDerivedField",
"field1_initialValue field2_initialValue"
)
.objectField( "containedIndexedEmbedded", b3 -> b3
.field( "indexedField", null )
)
)
)
.preparedThenExecuted();
Expand All @@ -677,9 +680,6 @@ public void indirectValueUpdate_indexedEmbedded_crossEntityDerivedValue_indexed(
"crossEntityDerivedField",
"field1_updatedValue field2_initialValue"
)
.objectField( "containedIndexedEmbedded", b3 -> b3
.field( "indexedField", null )
)
)
)
.preparedThenExecuted();
Expand All @@ -698,9 +698,6 @@ public void indirectValueUpdate_indexedEmbedded_crossEntityDerivedValue_indexed(
"crossEntityDerivedField",
"field1_updatedValue field2_updatedValue"
)
.objectField( "containedIndexedEmbedded", b3 -> b3
.field( "indexedField", null )
)
)
)
.preparedThenExecuted();
Expand Down Expand Up @@ -915,6 +912,10 @@ interface AssociationModelPrimitives<

void setContainingAsIndexedEmbeddedNoReindexOnUpdateSingle(TContained contained, TContaining containing);

void setContainedUsedInCrossEntityDerivedPropertySingle(TContaining containing, TContained contained);

void setContainingAsUsedInCrossEntityDerivedPropertySingle(TContained contained, TContaining containing);

void setIndexedField(TContained contained, String value);

void setNonIndexedField(TContained contained, String value);
Expand Down
Expand Up @@ -1034,6 +1034,22 @@ default void setContainingAsIndexedEmbeddedNoReindexOnUpdateSingle(TContained co
addContaining( containingAssociation, containing );
}

@Override
default void setContainedUsedInCrossEntityDerivedPropertySingle(TContaining containing,
TContained contained) {
TContainedAssociation containedAssociation = getContainedUsedInCrossEntityDerivedProperty( containing );
clearContained( containedAssociation );
addContained( containedAssociation, contained );
}

@Override
default void setContainingAsUsedInCrossEntityDerivedPropertySingle(TContained contained,
TContaining containing) {
TContainingAssociation containingAssociation = getContainingAsUsedInCrossEntityDerivedProperty( contained );
clearContaining( containingAssociation );
addContaining( containingAssociation, containing );
}

TContainedAssociation newContainedAssociation(TContainedAssociation original);

void addContained(TContainedAssociation association, TContained contained);
Expand Down Expand Up @@ -1065,6 +1081,10 @@ default void setContainingAsIndexedEmbeddedNoReindexOnUpdateSingle(TContained co
void setContainedIndexedEmbeddedNoReindexOnUpdate(TContaining containing, TContainedAssociation association);

TContainingAssociation getContainingAsIndexedEmbeddedNoReindexOnUpdate(TContained contained);

TContainedAssociation getContainedUsedInCrossEntityDerivedProperty(TContaining containing);

TContainingAssociation getContainingAsUsedInCrossEntityDerivedProperty(TContained contained);
}

}
Expand Up @@ -184,6 +184,17 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate(Co
return containedEntity.getContainingAsIndexedEmbeddedNoReindexOnUpdate();
}

@Override
public List<ContainedEntity> getContainedUsedInCrossEntityDerivedProperty(
ContainingEntity containingEntity) {
return containingEntity.getContainedUsedInCrossEntityDerivedProperty();
}

@Override
public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty(ContainedEntity containedEntity) {
return containedEntity.getContainingAsUsedInCrossEntityDerivedProperty();
}

@Override
public void setIndexedField(ContainedEntity containedEntity, String value) {
containedEntity.setIndexedField( value );
Expand Down Expand Up @@ -271,6 +282,10 @@ public static class ContainingEntity {
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.NO)
private List<ContainedEntity> containedIndexedEmbeddedNoReindexOnUpdate = new ArrayList<>();

@ManyToMany
@JoinTable(name = "indexed_containedUsedInCrossEntityDerivedProperty")
private List<ContainedEntity> containedUsedInCrossEntityDerivedProperty = new ArrayList<>();

public Integer getId() {
return id;
}
Expand Down Expand Up @@ -320,21 +335,30 @@ public void setContainedIndexedEmbeddedNoReindexOnUpdate(
this.containedIndexedEmbeddedNoReindexOnUpdate = containedIndexedEmbeddedNoReindexOnUpdate;
}

public List<ContainedEntity> getContainedUsedInCrossEntityDerivedProperty() {
return containedUsedInCrossEntityDerivedProperty;
}

public void setContainedUsedInCrossEntityDerivedProperty(
List<ContainedEntity> containedUsedInCrossEntityDerivedProperty) {
this.containedUsedInCrossEntityDerivedProperty = containedUsedInCrossEntityDerivedProperty;
}

@Transient
@GenericField
@IndexingDependency(derivedFrom = {
@ObjectPath({
@PropertyValue(propertyName = "containedIndexedEmbedded"),
@PropertyValue(propertyName = "containedUsedInCrossEntityDerivedProperty"),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField1")
}),
@ObjectPath({
@PropertyValue(propertyName = "containedIndexedEmbedded"),
@PropertyValue(propertyName = "containedUsedInCrossEntityDerivedProperty"),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField2")
})
})
public Optional<String> getCrossEntityDerivedField() {
return computeDerived(
containedIndexedEmbedded.stream().flatMap( c -> Stream.of(
containedUsedInCrossEntityDerivedProperty.stream().flatMap( c -> Stream.of(
c.getFieldUsedInCrossEntityDerivedField1(),
c.getFieldUsedInCrossEntityDerivedField2()
) )
Expand Down Expand Up @@ -368,6 +392,10 @@ public static class ContainedEntity {
@OrderBy("id asc") // Make sure the iteration order is predictable
private List<ContainingEntity> containingAsIndexedEmbeddedNoReindexOnUpdate = new ArrayList<>();

@ManyToMany(mappedBy = "containedUsedInCrossEntityDerivedProperty")
@OrderBy("id asc") // Make sure the iteration order is predictable
private List<ContainingEntity> containingAsUsedInCrossEntityDerivedProperty = new ArrayList<>();

@Basic
@GenericField
private String indexedField;
Expand Down Expand Up @@ -418,6 +446,10 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate()
return containingAsIndexedEmbeddedNoReindexOnUpdate;
}

public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty() {
return containingAsUsedInCrossEntityDerivedProperty;
}

public String getIndexedField() {
return indexedField;
}
Expand Down
Expand Up @@ -192,6 +192,17 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate(Co
return containedEntity.getContainingAsIndexedEmbeddedNoReindexOnUpdate();
}

@Override
public Map<ContainedEntity, String> getContainedUsedInCrossEntityDerivedProperty(
ContainingEntity containingEntity) {
return containingEntity.getContainedUsedInCrossEntityDerivedProperty();
}

@Override
public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty(ContainedEntity containedEntity) {
return containedEntity.getContainingAsUsedInCrossEntityDerivedProperty();
}

@Override
public void setIndexedField(ContainedEntity containedEntity, String value) {
containedEntity.setIndexedField( value );
Expand Down Expand Up @@ -306,6 +317,16 @@ public static class ContainingEntity {
)
private Map<ContainedEntity, String> containedIndexedEmbeddedNoReindexOnUpdate = new LinkedHashMap<>();

@ElementCollection
@JoinTable(
name = "indexed_containedUsedInCrossEntityDerivedProperty",
joinColumns = @JoinColumn(name = "mapHolder")
)
@MapKeyJoinColumn(name = "map_key")
@Column(name = "value")
@OrderBy("map_key asc") // Forces Hibernate ORM to use a LinkedHashMap; we make sure to insert entries in the correct order
private Map<ContainedEntity, String> containedUsedInCrossEntityDerivedProperty = new LinkedHashMap<>();

public Integer getId() {
return id;
}
Expand Down Expand Up @@ -355,27 +376,36 @@ public void setContainedIndexedEmbeddedNoReindexOnUpdate(
this.containedIndexedEmbeddedNoReindexOnUpdate = containedIndexedEmbeddedNoReindexOnUpdate;
}

public Map<ContainedEntity, String> getContainedUsedInCrossEntityDerivedProperty() {
return containedUsedInCrossEntityDerivedProperty;
}

public void setContainedUsedInCrossEntityDerivedProperty(
Map<ContainedEntity, String> containedUsedInCrossEntityDerivedProperty) {
this.containedUsedInCrossEntityDerivedProperty = containedUsedInCrossEntityDerivedProperty;
}

@Transient
@GenericField
@IndexingDependency(derivedFrom = {
@ObjectPath({
@PropertyValue(
propertyName = "containedIndexedEmbedded",
propertyName = "containedUsedInCrossEntityDerivedProperty",
extractors = @ContainerExtractorRef(BuiltinContainerExtractor.MAP_KEY)
),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField1")
}),
@ObjectPath({
@PropertyValue(
propertyName = "containedIndexedEmbedded",
propertyName = "containedUsedInCrossEntityDerivedProperty",
extractors = @ContainerExtractorRef(BuiltinContainerExtractor.MAP_KEY)
),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField2")
})
})
public Optional<String> getCrossEntityDerivedField() {
return computeDerived(
containedIndexedEmbedded.keySet().stream().flatMap( c -> Stream.of(
containedUsedInCrossEntityDerivedProperty.keySet().stream().flatMap( c -> Stream.of(
c.getFieldUsedInCrossEntityDerivedField1(),
c.getFieldUsedInCrossEntityDerivedField2()
) )
Expand Down Expand Up @@ -439,6 +469,21 @@ public static class ContainedEntity {
)
private List<ContainingEntity> containingAsIndexedEmbeddedNoReindexOnUpdate = new ArrayList<>();

/*
* No mappedBy here, same reasons as above.
*/
@ManyToMany
@OrderBy("id asc") // Make sure the iteration order is predictable
@AssociationInverseSide(
inversePath = @ObjectPath(
@PropertyValue(
propertyName = "containedUsedInCrossEntityDerivedProperty",
extractors = @ContainerExtractorRef(BuiltinContainerExtractor.MAP_KEY)
)
)
)
private List<ContainingEntity> containingAsUsedInCrossEntityDerivedProperty = new ArrayList<>();

@Basic
@GenericField
private String indexedField;
Expand Down Expand Up @@ -489,6 +534,10 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate()
return containingAsIndexedEmbeddedNoReindexOnUpdate;
}

public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty() {
return containingAsUsedInCrossEntityDerivedProperty;
}

public String getIndexedField() {
return indexedField;
}
Expand Down
Expand Up @@ -188,6 +188,17 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate(Co
return containedEntity.getContainingAsIndexedEmbeddedNoReindexOnUpdate();
}

@Override
public Map<String, ContainedEntity> getContainedUsedInCrossEntityDerivedProperty(
ContainingEntity containingEntity) {
return containingEntity.getContainedUsedInCrossEntityDerivedProperty();
}

@Override
public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty(ContainedEntity containedEntity) {
return containedEntity.getContainingAsUsedInCrossEntityDerivedProperty();
}

@Override
public void setIndexedField(ContainedEntity containedEntity, String value) {
containedEntity.setIndexedField( value );
Expand Down Expand Up @@ -293,6 +304,16 @@ public static class ContainingEntity {
@OrderBy("id asc") // Forces Hibernate ORM to use a LinkedHashMap; we make sure to insert entries in the correct order
private Map<String, ContainedEntity> containedIndexedEmbeddedNoReindexOnUpdate = new LinkedHashMap<>();

@ManyToMany
@JoinTable(
name = "indexed_containedUsedInCrossEntityDerivedProperty",
joinColumns = @JoinColumn(name = "mapHolder"),
inverseJoinColumns = @JoinColumn(name = "value")
)
@MapKeyColumn(name = "map_key")
@OrderBy("id asc") // Forces Hibernate ORM to use a LinkedHashMap; we make sure to insert entries in the correct order
private Map<String, ContainedEntity> containedUsedInCrossEntityDerivedProperty = new LinkedHashMap<>();

public Integer getId() {
return id;
}
Expand Down Expand Up @@ -342,21 +363,30 @@ public void setContainedIndexedEmbeddedNoReindexOnUpdate(
this.containedIndexedEmbeddedNoReindexOnUpdate = containedIndexedEmbeddedNoReindexOnUpdate;
}

public Map<String, ContainedEntity> getContainedUsedInCrossEntityDerivedProperty() {
return containedUsedInCrossEntityDerivedProperty;
}

public void setContainedUsedInCrossEntityDerivedProperty(
Map<String, ContainedEntity> containedUsedInCrossEntityDerivedProperty) {
this.containedUsedInCrossEntityDerivedProperty = containedUsedInCrossEntityDerivedProperty;
}

@Transient
@GenericField
@IndexingDependency(derivedFrom = {
@ObjectPath({
@PropertyValue(propertyName = "containedIndexedEmbedded"),
@PropertyValue(propertyName = "containedUsedInCrossEntityDerivedProperty"),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField1")
}),
@ObjectPath({
@PropertyValue(propertyName = "containedIndexedEmbedded"),
@PropertyValue(propertyName = "containedUsedInCrossEntityDerivedProperty"),
@PropertyValue(propertyName = "fieldUsedInCrossEntityDerivedField2")
})
})
public Optional<String> getCrossEntityDerivedField() {
return computeDerived(
containedIndexedEmbedded.values().stream().flatMap( c -> Stream.of(
containedUsedInCrossEntityDerivedProperty.values().stream().flatMap( c -> Stream.of(
c.getFieldUsedInCrossEntityDerivedField1(),
c.getFieldUsedInCrossEntityDerivedField2()
) )
Expand Down Expand Up @@ -390,6 +420,10 @@ public static class ContainedEntity {
@OrderBy("id asc") // Make sure the iteration order is predictable
private List<ContainingEntity> containingAsIndexedEmbeddedNoReindexOnUpdate = new ArrayList<>();

@ManyToMany(mappedBy = "containedUsedInCrossEntityDerivedProperty")
@OrderBy("id asc") // Make sure the iteration order is predictable
private List<ContainingEntity> containingAsUsedInCrossEntityDerivedProperty = new ArrayList<>();

@Basic
@GenericField
private String indexedField;
Expand Down Expand Up @@ -440,6 +474,10 @@ public List<ContainingEntity> getContainingAsIndexedEmbeddedNoReindexOnUpdate()
return containingAsIndexedEmbeddedNoReindexOnUpdate;
}

public List<ContainingEntity> getContainingAsUsedInCrossEntityDerivedProperty() {
return containingAsUsedInCrossEntityDerivedProperty;
}

public String getIndexedField() {
return indexedField;
}
Expand Down

0 comments on commit 9fd99a9

Please sign in to comment.