Skip to content

Commit

Permalink
Merge pull request #32854 from yrodiere/unnamed-key
Browse files Browse the repository at this point in the history
Use `@WithUnnamedKey` in Hibernate Search extension config
  • Loading branch information
gsmet authored Jul 4, 2023
2 parents 60da8ba + 21f56c4 commit eaaedd3
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@

import java.util.Map;

import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-search-orm")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public interface HibernateSearchOutboxPollingBuildTimeConfig {

/**
* Configuration for the default persistence unit.
*/
@WithParentName
HibernateSearchOutboxPollingBuildTimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Configuration for additional named persistence units.
* Configuration for persistence units.
*/
@ConfigDocSection
@ConfigDocMapKey("persistence-unit-name")
@WithParentName
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@ConfigDocMapKey("persistence-unit-name")
Map<String, HibernateSearchOutboxPollingBuildTimeConfigPersistenceUnit> persistenceUnits();

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.search.mapper.orm.coordination.outboxpolling.cfg.HibernateOrmMapperOutboxPollingSettings;

import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeInitListener;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationStaticInitListener;
import io.quarkus.hibernate.search.orm.coordination.outboxpolling.runtime.HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit.AgentsConfig;
Expand All @@ -21,19 +20,15 @@ public class HibernateSearchOutboxPollingRecorder {

public HibernateOrmIntegrationStaticInitListener createStaticInitListener(
HibernateSearchOutboxPollingBuildTimeConfig buildTimeConfig, String persistenceUnitName) {
HibernateSearchOutboxPollingBuildTimeConfigPersistenceUnit puConfig = PersistenceUnitUtil
.isDefaultPersistenceUnit(persistenceUnitName)
? buildTimeConfig.defaultPersistenceUnit()
: buildTimeConfig.persistenceUnits().get(persistenceUnitName);
HibernateSearchOutboxPollingBuildTimeConfigPersistenceUnit puConfig = buildTimeConfig.persistenceUnits()
.get(persistenceUnitName);
return new StaticInitListener(puConfig);
}

public HibernateOrmIntegrationRuntimeInitListener createRuntimeInitListener(
HibernateSearchOutboxPollingRuntimeConfig runtimeConfig, String persistenceUnitName) {
HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit puConfig = PersistenceUnitUtil
.isDefaultPersistenceUnit(persistenceUnitName)
? runtimeConfig.defaultPersistenceUnit()
: runtimeConfig.persistenceUnits().get(persistenceUnitName);
HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit puConfig = runtimeConfig.persistenceUnits()
.get(persistenceUnitName);
return new RuntimeInitListener(puConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

import java.util.Map;

import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-search-orm")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface HibernateSearchOutboxPollingRuntimeConfig {

/**
* Configuration for the default persistence unit.
* Configuration for persistence units.
*/
@WithParentName
HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Configuration for additional named persistence units.
*/
@ConfigDocSection
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@ConfigDocMapKey("persistence-unit-name")
@WithParentName
Map<String, HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit> persistenceUnits();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ public interface HibernateSearchOutboxPollingRuntimeConfigPersistenceUnit {
CoordinationConfig coordination();

@ConfigGroup
public interface CoordinationConfig {
interface CoordinationConfig {

/**
* Default config
* Default configuration.
*/
@ConfigDocSection
@WithParentName
AgentsConfig defaults();

/**
* Per-tenant config
* Per-tenant configuration overrides.
*/
@ConfigDocSection
@ConfigDocMapKey("tenant-id")
Expand All @@ -40,7 +39,7 @@ public interface CoordinationConfig {
}

@ConfigGroup
public interface AgentsConfig {
interface AgentsConfig {

/**
* Configuration for the event processor agent.
Expand All @@ -55,7 +54,7 @@ public interface AgentsConfig {
}

@ConfigGroup
public interface EventProcessorConfig {
interface EventProcessorConfig {

// @formatter:off
/**
Expand Down Expand Up @@ -209,7 +208,7 @@ public interface EventProcessorConfig {
}

@ConfigGroup
public interface EventProcessorShardsConfig {
interface EventProcessorShardsConfig {

// @formatter:off
/**
Expand Down Expand Up @@ -257,7 +256,7 @@ public interface EventProcessorShardsConfig {
}

@ConfigGroup
public interface MassIndexerConfig {
interface MassIndexerConfig {

// @formatter:off
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void build(HibernateSearchElasticsearchRecorder recorder,
index);

Map<String, HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit> configByPU = buildTimeConfig
.getAllPersistenceUnitConfigsAsMap();
.persistenceUnits();

for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
Collection<AnnotationInstance> indexedAnnotationsForPU = new ArrayList<>();
Expand Down Expand Up @@ -293,7 +293,7 @@ private void processPersistenceUnitBuildTimeConfig(

Set<String> propertyKeysWithNoVersion = new LinkedHashSet<>();
Map<String, ElasticsearchBackendBuildTimeConfig> backends = buildTimeConfig != null
? buildTimeConfig.getAllBackendConfigsAsMap()
? buildTimeConfig.backends()
: Collections.emptyMap();

Set<String> allBackendNames = new LinkedHashSet<>(configuredPersistenceUnit.getBackendNamesForIndexedEntities());
Expand Down Expand Up @@ -379,13 +379,15 @@ private void registerReflectionForGson(BuildProducer<ReflectiveClassBuildItem> r

@BuildStep(onlyIfNot = IsNormal.class)
DevservicesElasticsearchBuildItem devServices(HibernateSearchElasticsearchBuildTimeConfig buildTimeConfig) {
if (buildTimeConfig.defaultPersistenceUnit() == null) {
var defaultPUConfig = buildTimeConfig.persistenceUnits().get(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME);
if (defaultPUConfig == null) {
// Currently we only start dev-services for the default backend of the default persistence unit.
// See https://github.com/quarkusio/quarkus/issues/24011
return null;
}
if (buildTimeConfig.defaultPersistenceUnit().defaultBackend() == null
|| !buildTimeConfig.defaultPersistenceUnit().defaultBackend().version().isPresent()) {
var defaultPUDefaultBackendConfig = defaultPUConfig.backends().get(null);
if (defaultPUDefaultBackendConfig == null
|| !defaultPUDefaultBackendConfig.version().isPresent()) {
// If the version is not set, the default backend is not in use.
return null;
}
Expand All @@ -395,7 +397,7 @@ DevservicesElasticsearchBuildItem devServices(HibernateSearchElasticsearchBuildT
// If Hibernate Search is deactivated, we don't want to trigger dev services.
return null;
}
ElasticsearchVersion version = buildTimeConfig.defaultPersistenceUnit().defaultBackend().version().get();
ElasticsearchVersion version = defaultPUDefaultBackendConfig.version().get();
String hostsPropertyKey = backendPropertyKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, null, null,
"hosts");
return new DevservicesElasticsearchBuildItem(hostsPropertyKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static <T> void addBackendConfig(BiConsumer<String, Object> propertyColle
T value,
Function<T, Boolean> shouldBeAdded, Function<T, ?> getValue) {
if (shouldBeAdded.apply(value)) {
propertyCollector.accept(BackendSettings.backendKey(backendName, configPath), getValue.apply(value));
addBackendConfig(propertyCollector, backendName, configPath, getValue.apply(value));
}
}

Expand All @@ -62,8 +62,7 @@ public static <T> void addBackendIndexConfig(BiConsumer<String, Object> property
propertyCollector.accept(
IndexSettings.indexKey(backendName, indexName, configPath), getValue.apply(value));
} else {
propertyCollector.accept(
BackendSettings.backendKey(backendName, configPath), getValue.apply(value));
addBackendConfig(propertyCollector, backendName, configPath, getValue.apply(value));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.quarkus.hibernate.search.orm.elasticsearch.runtime;

import java.util.Map;
import java.util.TreeMap;

import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-search-orm")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
Expand All @@ -29,46 +28,11 @@ public interface HibernateSearchElasticsearchBuildTimeConfig {
boolean enabled();

/**
* Configuration for the default persistence unit.
* Configuration for persistence units.
*/
@WithParentName
HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Configuration for additional named persistence units.
*/
@ConfigDocSection
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@ConfigDocMapKey("persistence-unit-name")
@WithParentName
Map<String, HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit> persistenceUnits();

default Map<String, HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit> getAllPersistenceUnitConfigsAsMap() {
Map<String, HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit> map = new TreeMap<>();
HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit defaultPersistenceUnit = defaultPersistenceUnit();

if (defaultPersistenceUnit != null) {
map.put(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, defaultPersistenceUnit);
}
map.putAll(persistenceUnits());
return map;
}

default HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit getPersistenceUnitConfig(String persistenceUnitName) {
if (persistenceUnitName == null) {
throw new IllegalArgumentException("Persistence unit name may not be null");
}

if (PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME.equals(persistenceUnitName)) {
return defaultPersistenceUnit();
}

HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit persistenceUnitConfig = persistenceUnits()
.get(persistenceUnitName);

if (persistenceUnitConfig == null) {
throw new IllegalStateException("No persistence unit config exists for name " + persistenceUnitName);
}

return persistenceUnitConfig;
}
}
Loading

0 comments on commit eaaedd3

Please sign in to comment.