Skip to content

Commit

Permalink
Simplify config loading (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryannedolan authored Feb 10, 2025
1 parent 8922004 commit a15f08f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Properties;
import java.util.function.Supplier;

import org.apache.calcite.avatica.ConnectStringParser;
import org.apache.calcite.avatica.DriverVersion;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.jdbc.CalcitePrepare;
Expand Down Expand Up @@ -69,28 +70,32 @@ public Connection connect(String url, Properties props) throws SQLException {
CalciteConnection calciteConnection = (CalciteConnection) connection;
SchemaPlus rootSchema = calciteConnection.getRootSchema();

String remaining = url.substring(getConnectStringPrefix().length()).trim();
String[] catalogs = remaining.split(",");

// built-in schemas
rootSchema.add("DEFAULT", new AbstractSchema());

calciteConnection.setSchema("DEFAULT");

WrappedSchemaPlus wrappedRootSchema = new WrappedSchemaPlus(rootSchema);

// Load properties from url and from getConnection()
Properties properties = new Properties();
properties.putAll(props); // via getConnection()
properties.putAll(ConnectStringParser.parse(url.substring(getConnectStringPrefix().length())));
String[] catalogs = properties.getProperty("catalogs", "").split(",");

if (catalogs.length == 0 || catalogs[0].length() == 0) {
// load all catalogs (typical usage)
for (Catalog catalog : CatalogService.catalogs()) {
catalog.register(wrappedRootSchema, props);
catalog.register(wrappedRootSchema, properties);
}
} else {
// load specific catalogs when loaded as `jdbc:hoptimator://foo,bar`
// load specific catalogs when loaded as `jdbc:hoptimator://catalogs=foo,bar`
for (String catalog : catalogs) {
CatalogService.catalog(catalog).register(wrappedRootSchema, props);
}
}

return new HoptimatorConnection(calciteConnection, props);
return new HoptimatorConnection(calciteConnection, properties);
} catch (Exception e) {
throw new SQLException("Problem loading " + url, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void run(URI resource) throws IOException {
Quidem.Config config = Quidem.configBuilder()
.withReader(r)
.withWriter(w)
.withConnectionFactory((x, y) -> DriverManager.getConnection("jdbc:hoptimator://" + x))
.withConnectionFactory((x, y) -> DriverManager.getConnection("jdbc:hoptimator://catalogs=" + x))
.withCommandHandler(new CustomCommandHandler())
.build();
new Quidem(config).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.apache.calcite.jdbc.Driver;
import org.apache.calcite.schema.SchemaPlus;

import com.linkedin.hoptimator.util.ConfigService;


/** JDBC driver for Kafka topics. */
public class KafkaDriver extends Driver {
Expand All @@ -36,9 +34,9 @@ public Connection connect(String url, Properties props) throws SQLException {
if (!url.startsWith(getConnectStringPrefix())) {
return null;
}
// Connection string properties are given precedence over config properties
Properties properties = ConfigService.config(null);
Properties properties = new Properties();
properties.putAll(ConnectStringParser.parse(url.substring(getConnectStringPrefix().length())));
properties.putAll(props); // in case the driver is loaded via getConnection()
try {
Connection connection = super.connect(url, props);
if (connection == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public final class ConfigService {
private ConfigService() {
}

// Null namespace will default to current namespace, may not be used by some ConfigProviders.
// loadTopLevelConfigs=true loads top level configs and expands input fields as file-like properties
// loadTopLevelConfigs=false will only expand input fields as file-like properties
// Ex:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.apache.calcite.jdbc.Driver;
import org.apache.calcite.schema.SchemaPlus;

import com.linkedin.hoptimator.util.ConfigService;


/** JDBC driver for Venice stores. */
public class VeniceDriver extends Driver {
Expand All @@ -40,8 +38,9 @@ public Connection connect(String url, Properties props) throws SQLException {
return null;
}
// Connection string properties are given precedence over config properties
Properties properties = ConfigService.config(null, CONFIG_NAME);
Properties properties = new Properties();
properties.putAll(ConnectStringParser.parse(url.substring(getConnectStringPrefix().length())));
properties.putAll(props); // in case the driver is loaded via getConnection()
String cluster = properties.getProperty("cluster");
if (cluster == null) {
throw new IllegalArgumentException("Missing required cluster property. Need: jdbc:venice://cluster=...");
Expand Down

0 comments on commit a15f08f

Please sign in to comment.