Skip to content

Commit

Permalink
Simpler property cache using SmallryeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Malandril committed Jul 14, 2024
1 parent 90d7baa commit 80627ae
Showing 1 changed file with 15 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -42,6 +41,7 @@
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.SmallRyeConfig;

@BuildSteps(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
public class DevServicesDatasourceProcessor {
Expand All @@ -51,8 +51,7 @@ public class DevServicesDatasourceProcessor {

// list of devservices properties we should not check for restart
// see issue #30390
private static final Set<String> EXCLUDED_PROPERTIES = Set.of("quarkus.datasource.devservices.enabled",
"quarkus.datasource.devservices.reuse");
private static final Set<String> EXCLUDED_PROPERTIES = Set.of("quarkus.datasource.devservices.enabled");

static volatile List<RunningDevService> databases;

Expand All @@ -79,24 +78,21 @@ DevServicesDatasourceResultBuildItem launchDatabases(
//if not and the DB's have already started we just return
if (databases != null) {
boolean restartRequired = false;
Map<String, String> newDatasourceConfigs = ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getOptionalValues("quarkus.datasource",
String.class, String.class)
.orElse(Map.of());
for (Map.Entry<String, String> entry : cachedProperties.entrySet()) {
String value = ConfigProvider.getConfig()
.getOptionalValue(entry.getKey(), String.class)
.orElse(null);
if (value == null) {
value = getValueWithOrWithoutQuotes(entry.getKey(),
dataSourcesBuildTimeConfig.dataSources().keySet());
}
if (!Objects.equals(entry.getValue(), trim(value))) {
String newValue = newDatasourceConfigs.get(entry.getKey());
if (!Objects.equals(entry.getValue(), trim(newValue))) {
restartRequired = true;
break;
}
}
if (!restartRequired) {
//devservices properties may have been added
for (var name : ConfigProvider.getConfig().getPropertyNames()) {
if (name.startsWith("quarkus.datasource.") && name.contains(".devservices.")
&& !cachedProperties.containsKey(name) && !EXCLUDED_PROPERTIES.contains(name)) {
for (var name : newDatasourceConfigs.keySet()) {
if (!cachedProperties.containsKey(name)) {
restartRequired = true;
break;
}
Expand Down Expand Up @@ -178,41 +174,16 @@ public void run() {
closeBuildItem.addCloseTask(closeTask, true);
}
databases = runningDevServices;
cachedProperties = propertiesMap;
cachedProperties = ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getOptionalValues("quarkus.datasource", String.class, String.class)
.orElse(Map.of());
for (RunningDevService database : databases) {
devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
}
return new DevServicesDatasourceResultBuildItem(results);
}

/**
* Gets the value of the given key trying to quote the datasource if unquoted, and unquote it if quoted
*
* @param key key to get
* @param datasources datasources to try to quote and unqote
* @return the value or else otherwise
*/
private static String getValueWithOrWithoutQuotes(String key, Collection<String> datasources) {
for (String datasource : datasources) {
if (!DataSourceUtil.isDefault(datasource) && key.contains(datasource)) {
String quoted = ".\"" + datasource + "\".";
String unquoted = "." + datasource + ".";
if (key.contains(quoted)) {
String entryWithoutQuote = key.replace(quoted, unquoted);
return ConfigProvider.getConfig()
.getOptionalValue(entryWithoutQuote, String.class)
.orElse(null);
} else {
String entryWithQuotes = key.replace(unquoted, quoted);
return ConfigProvider.getConfig()
.getOptionalValue(entryWithQuotes, String.class)
.orElse(null);
}
}
}
return null;
}

private String trim(String optional) {
if (optional == null) {
return null;
Expand Down

0 comments on commit 80627ae

Please sign in to comment.