Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3709 Document routing key bridges
- Loading branch information
Showing
11 changed files
with
336 additions
and
48 deletions.
There are no files selected for viewing
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
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
14 changes: 14 additions & 0 deletions
14
.../hibernate/search/documentation/mapper/orm/bridge/routingkeybridge/ormcontext/MyData.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,14 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.routingkeybridge.ormcontext; | ||
|
||
public enum MyData { | ||
|
||
INDEXED, | ||
PROJECTED; | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ibernate/search/documentation/mapper/orm/bridge/routingkeybridge/ormcontext/MyEntity.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,29 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.routingkeybridge.ormcontext; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.RoutingKeyBinderRef; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.RoutingKeyBinding; | ||
|
||
@Entity | ||
@Indexed | ||
@RoutingKeyBinding(binder = @RoutingKeyBinderRef(type = MyEntityRoutingKeyBinder.class)) | ||
public class MyEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...documentation/mapper/orm/bridge/routingkeybridge/ormcontext/MyEntityRoutingKeyBinder.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,49 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.routingkeybridge.ormcontext; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.search.mapper.orm.HibernateOrmExtension; | ||
import org.hibernate.search.mapper.pojo.bridge.RoutingKeyBridge; | ||
import org.hibernate.search.mapper.pojo.bridge.binding.RoutingKeyBindingContext; | ||
import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.RoutingKeyBinder; | ||
import org.hibernate.search.mapper.pojo.bridge.runtime.RoutingKeyBridgeToRoutingKeyContext; | ||
|
||
public class MyEntityRoutingKeyBinder implements RoutingKeyBinder { | ||
|
||
@Override | ||
public void bind(RoutingKeyBindingContext context) { | ||
context.dependencies() | ||
.useRootOnly(); | ||
|
||
context.bridge( new Bridge() ); | ||
} | ||
|
||
//tag::include[] | ||
private static class Bridge implements RoutingKeyBridge { | ||
|
||
@Override | ||
public String toRoutingKey(String tenantIdentifier, Object entityIdentifier, | ||
Object bridgedElement, RoutingKeyBridgeToRoutingKeyContext context) { | ||
Session session = context.extension( HibernateOrmExtension.get() ) // <1> | ||
.session(); // <2> | ||
String routingKey; | ||
// ... do something with the session ... | ||
//end::include[] | ||
/* | ||
* I don't know what to do with the session here, | ||
* so I'm just going to extract data from it. | ||
* This is silly, but at least it allows us to check the session was successfully retrieved. | ||
*/ | ||
MyData dataFromSession = (MyData) session.getProperties().get( "test.data.indexed" ); | ||
routingKey = dataFromSession.name(); | ||
//tag::include[] | ||
return routingKey; | ||
} | ||
} | ||
//end::include[] | ||
} |
81 changes: 81 additions & 0 deletions
81
...mentation/mapper/orm/bridge/routingkeybridge/ormcontext/RoutingKeyBridgeOrmContextIT.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,81 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.routingkeybridge.ormcontext; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.hibernate.search.documentation.testsupport.BackendConfigurations; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.orm.automaticindexing.session.AutomaticIndexingSynchronizationStrategyNames; | ||
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings; | ||
import org.hibernate.search.mapper.orm.session.SearchSession; | ||
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendConfiguration; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class RoutingKeyBridgeOrmContextIT { | ||
|
||
private static final int SHARD_COUNT = 4; | ||
|
||
@Parameterized.Parameters(name = "{0}") | ||
public static Object[] backendSetups() { | ||
return BackendConfigurations.hashBasedSharding( SHARD_COUNT ).toArray(); | ||
} | ||
|
||
@Rule | ||
public OrmSetupHelper setupHelper; | ||
|
||
private EntityManagerFactory entityManagerFactory; | ||
|
||
public RoutingKeyBridgeOrmContextIT(BackendConfiguration backendConfiguration) { | ||
this.setupHelper = OrmSetupHelper.withSingleBackend( backendConfiguration ); | ||
} | ||
|
||
@Before | ||
public void setup() { | ||
entityManagerFactory = setupHelper.start() | ||
.withProperty( | ||
HibernateOrmMapperSettings.AUTOMATIC_INDEXING_SYNCHRONIZATION_STRATEGY, | ||
AutomaticIndexingSynchronizationStrategyNames.SYNC | ||
) | ||
.setup( MyEntity.class ); | ||
} | ||
|
||
@Test | ||
public void smoke() { | ||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
// See MyDataPropertyBinder | ||
entityManager.setProperty( "test.data.indexed", MyData.INDEXED ); | ||
|
||
MyEntity myEntity = new MyEntity(); | ||
entityManager.persist( myEntity ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
SearchSession searchSession = Search.session( entityManager ); | ||
|
||
List<MyEntity> result = searchSession.search( MyEntity.class ) | ||
.where( f -> f.matchAll() ) | ||
.routing( "INDEXED" ) | ||
.fetchHits( 20 ); | ||
|
||
assertThat( result ).hasSize( 1 ); | ||
} ); | ||
} | ||
|
||
} |
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.