Skip to content

Commit

Permalink
HSEARCH-3429 Allow to skip the .toProjection() call for elements of c…
Browse files Browse the repository at this point in the history
…omposite projections
  • Loading branch information
yrodiere committed Dec 12, 2018
1 parent aa6e77b commit cf01b91
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 5 deletions.
Expand Up @@ -95,6 +95,16 @@ default CompositeProjectionContext<List<?>> composite(SearchProjection<?>... pro
return composite( Function.identity(), projections );
}

/**
* Create a projection that will compose a {@link List} based on the given almost-built projections.
*
* @param terminalContexts The terminal contexts allowing to retrieve {@link SearchProjection}s.
* @return A context allowing to define the projection more precisely.
*/
default CompositeProjectionContext<List<?>> composite(SearchProjectionTerminalContext<?> ... terminalContexts) {
return composite( Function.identity(), terminalContexts );
}

/**
* Create a projection that will compose a custom object based on the given projections.
*
Expand All @@ -104,6 +114,22 @@ default CompositeProjectionContext<List<?>> composite(SearchProjection<?>... pro
*/
<T> CompositeProjectionContext<T> composite(Function<List<?>, T> transformer, SearchProjection<?>... projections);

/**
* Create a projection that will compose a custom object based on the given almost-built projections.
*
* @param transformer The function that will transform the projected element into a custom object.
* @param terminalContexts The terminal contexts allowing to retrieve {@link SearchProjection}s.
* @return A context allowing to define the projection more precisely.
*/
default <T> CompositeProjectionContext<T> composite(Function<List<?>, T> transformer,
SearchProjectionTerminalContext<?> ... terminalContexts) {
SearchProjection<?>[] projections = new SearchProjection<?>[terminalContexts.length];
for ( int i = 0; i < terminalContexts.length; i++ ) {
projections[i] = terminalContexts[i].toProjection();
}
return composite( transformer, projections );
}

/**
* Create a projection that will compose a custom object based on one given projection.
*
Expand All @@ -113,6 +139,18 @@ default CompositeProjectionContext<List<?>> composite(SearchProjection<?>... pro
*/
<P, T> CompositeProjectionContext<T> composite(Function<P, T> transformer, SearchProjection<P> projection);

/**
* Create a projection that will compose a custom object based on one almost-built projection.
*
* @param transformer The function that will transform the projected element into a custom object.
* @param terminalContext The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the element passed to the transformer.
* @return A context allowing to define the projection more precisely.
*/
default <P, T> CompositeProjectionContext<T> composite(Function<P, T> transformer, SearchProjectionTerminalContext<P> terminalContext) {
return composite( transformer, terminalContext.toProjection() );
}

/**
* Create a projection that will compose a custom object based on two given projections.
*
Expand All @@ -124,6 +162,24 @@ default CompositeProjectionContext<List<?>> composite(SearchProjection<?>... pro
<P1, P2, T> CompositeProjectionContext<T> composite(BiFunction<P1, P2, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2);

/**
* Create a projection that will compose a custom object based on two almost-built projections.
*
* @param transformer The function that will transform the projected elements into a custom object.
* @param terminalContext1 The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the first element passed to the transformer.
* @param terminalContext2 The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the second element passed to the transformer.
* @return A context allowing to define the projection more precisely.
*/
default <P1, P2, T> CompositeProjectionContext<T> composite(BiFunction<P1, P2, T> transformer,
SearchProjectionTerminalContext<P1> terminalContext1, SearchProjectionTerminalContext<P2> terminalContext2) {
return composite(
transformer,
terminalContext1.toProjection(), terminalContext2.toProjection()
);
}

/**
* Create a projection that will compose a custom object based on three given projections.
*
Expand All @@ -135,4 +191,25 @@ <P1, P2, T> CompositeProjectionContext<T> composite(BiFunction<P1, P2, T> transf
*/
<P1, P2, P3, T> CompositeProjectionContext<T> composite(TriFunction<P1, P2, P3, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2, SearchProjection<P3> projection3);

/**
* Create a projection that will compose a custom object based on three almost-built projections.
*
* @param transformer The function that will transform the projected elements into a custom object.
* @param terminalContext1 The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the first element passed to the transformer.
* @param terminalContext2 The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the second element passed to the transformer.
* @param terminalContext3 The terminal context allowing to retrieve the {@link SearchProjection}
* that will be used to produce the third element passed to the transformer.
* @return A context allowing to define the projection more precisely.
*/
default <P1, P2, P3, T> CompositeProjectionContext<T> composite(TriFunction<P1, P2, P3, T> transformer,
SearchProjectionTerminalContext<P1> terminalContext1, SearchProjectionTerminalContext<P2> terminalContext2,
SearchProjectionTerminalContext<P3> terminalContext3) {
return composite(
transformer,
terminalContext1.toProjection(), terminalContext2.toProjection(), terminalContext3.toProjection()
);
}
}
Expand Up @@ -76,7 +76,7 @@ public void setup() {
}

@Test
public void compositeList() {
public void compositeList_fromSearchProjectionObjects() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<List<?>> query = searchTarget.query()
Expand All @@ -99,7 +99,29 @@ public void compositeList() {
}

@Test
public void compositeList_transformer() {
public void compositeList_fromTerminalContexts() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<List<?>> query = searchTarget.query()
.asProjection( f ->
f.composite(
f.field( indexMapping.author.relativeFieldName, String.class ),
f.field( indexMapping.title.relativeFieldName, String.class )
)
.toProjection()
)
.predicate( f -> f.matchAll().toPredicate() )
.build();

assertThat( query ).hasHitsAnyOrder(
Arrays.asList( indexMapping.author.document1Value.indexedValue, indexMapping.title.document1Value.indexedValue ),
Arrays.asList( indexMapping.author.document2Value.indexedValue, indexMapping.title.document2Value.indexedValue ),
Arrays.asList( indexMapping.author.document3Value.indexedValue, indexMapping.title.document3Value.indexedValue )
);
}

@Test
public void compositeList_transformer_fromSearchProjectionObjects() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Bi> query = searchTarget.query()
Expand All @@ -123,7 +145,29 @@ public void compositeList_transformer() {
}

@Test
public void compositeFunction() {
public void compositeList_transformer_fromTerminalContext() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Bi> query = searchTarget.query()
.asProjection( f ->
f.composite(
this::listToBook_Bi,
f.field( indexMapping.author.relativeFieldName, String.class ),
f.field( indexMapping.title.relativeFieldName, String.class )
).toProjection()
)
.predicate( f -> f.matchAll().toPredicate() )
.build();

assertThat( query ).hasHitsAnyOrder(
new Book_Bi( indexMapping.author.document1Value.indexedValue, indexMapping.title.document1Value.indexedValue ),
new Book_Bi( indexMapping.author.document2Value.indexedValue, indexMapping.title.document2Value.indexedValue ),
new Book_Bi( indexMapping.author.document3Value.indexedValue, indexMapping.title.document3Value.indexedValue )
);
}

@Test
public void compositeFunction_fromSearchProjectionObjects() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book> query = searchTarget.query()
Expand All @@ -145,7 +189,29 @@ public void compositeFunction() {
}

@Test
public void compositeBiFunction() {
public void compositeFunction_fromTerminalContext() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book> query = searchTarget.query()
.asProjection( f ->
f.composite(
Book::new,
f.field( indexMapping.title.relativeFieldName, String.class )
)
.toProjection()
)
.predicate( f -> f.matchAll().toPredicate() )
.build();

assertThat( query ).hasHitsAnyOrder(
new Book( indexMapping.title.document1Value.indexedValue ),
new Book( indexMapping.title.document2Value.indexedValue ),
new Book( indexMapping.title.document3Value.indexedValue )
);
}

@Test
public void compositeBiFunction_fromSearchProjectionObjects() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Bi> query = searchTarget.query()
Expand All @@ -169,7 +235,29 @@ public void compositeBiFunction() {
}

@Test
public void compositeTriFunction() {
public void compositeBiFunction_fromTerminalContexts() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Bi> query = searchTarget.query()
.asProjection( f ->
f.composite(
Book_Bi::new,
f.field( indexMapping.author.relativeFieldName, String.class ),
f.field( indexMapping.title.relativeFieldName, String.class )
).toProjection()
)
.predicate( f -> f.matchAll().toPredicate() )
.build();

assertThat( query ).hasHitsAnyOrder(
new Book_Bi( indexMapping.author.document1Value.indexedValue, indexMapping.title.document1Value.indexedValue ),
new Book_Bi( indexMapping.author.document2Value.indexedValue, indexMapping.title.document2Value.indexedValue ),
new Book_Bi( indexMapping.author.document3Value.indexedValue, indexMapping.title.document3Value.indexedValue )
);
}

@Test
public void compositeTriFunction_fromSearchProjectionObjects() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Tri> query = searchTarget.query()
Expand Down Expand Up @@ -197,6 +285,32 @@ public void compositeTriFunction() {
);
}

@Test
public void compositeTriFunction_fromTerminalContexts() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();

SearchQuery<Book_Tri> query = searchTarget.query()
.asProjection( f ->
f.composite(
Book_Tri::new,
f.field( indexMapping.author.relativeFieldName, String.class ),
f.field( indexMapping.title.relativeFieldName, String.class ),
f.field( indexMapping.releaseDate.relativeFieldName, LocalDate.class )
).toProjection()
)
.predicate( f -> f.matchAll().toPredicate() )
.build();

assertThat( query ).hasHitsAnyOrder(
new Book_Tri( indexMapping.author.document1Value.indexedValue, indexMapping.title.document1Value.indexedValue,
indexMapping.releaseDate.document1Value.indexedValue ),
new Book_Tri( indexMapping.author.document2Value.indexedValue, indexMapping.title.document2Value.indexedValue,
indexMapping.releaseDate.document2Value.indexedValue ),
new Book_Tri( indexMapping.author.document3Value.indexedValue, indexMapping.title.document3Value.indexedValue,
indexMapping.releaseDate.document3Value.indexedValue )
);
}

@Test
public void nestedComposite() {
StubMappingSearchTarget searchTarget = indexManager.createSearchTarget();
Expand Down

0 comments on commit cf01b91

Please sign in to comment.