Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions plugin/trino-raptor-legacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>dbpool</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>discovery</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>http-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,20 @@
*/
package io.trino.plugin.raptor.legacy.metadata;

import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.airlift.dbpool.H2EmbeddedDataSourceModule;
import io.airlift.dbpool.MySqlDataSource;
import io.airlift.dbpool.MySqlDataSourceConfig;
import io.airlift.discovery.client.ServiceDescriptor;
import io.airlift.discovery.client.testing.StaticServiceSelector;
import io.trino.plugin.raptor.legacy.util.DaoSupplier;
import org.jdbi.v3.core.ConnectionFactory;
import org.jdbi.v3.core.Jdbi;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.sql.DataSource;

import java.lang.reflect.Type;
import java.sql.DriverManager;

import static com.google.common.base.Preconditions.checkState;
import static io.airlift.configuration.ConditionalModule.conditionalModule;
import static io.airlift.configuration.ConfigBinder.configBinder;
import static io.airlift.discovery.client.ServiceDescriptor.serviceDescriptor;
import static java.util.Objects.requireNonNull;

public class DatabaseMetadataModule
extends AbstractConfigurationAwareModule
Expand All @@ -54,93 +37,63 @@ protected void setup(Binder ignored)
install(conditionalModule(
DatabaseConfig.class,
config -> "mysql".equals(config.getDatabaseType()),
binder -> {
binder.install(new MySqlDataSourceModule());
bindDaoSupplier(binder, ShardDao.class, MySqlShardDao.class);
}));
new MySqlModule()));

install(conditionalModule(
DatabaseConfig.class,
config -> "h2".equals(config.getDatabaseType()),
binder -> {
binder.install(new H2EmbeddedDataSourceModule("metadata", ForMetadata.class));
bindDaoSupplier(binder, ShardDao.class, H2ShardDao.class);
}));
new H2Module()));
}

@ForMetadata
@Singleton
@Provides
public ConnectionFactory createConnectionFactory(@ForMetadata DataSource dataSource)
{
return dataSource::getConnection;
}

public static <B, T extends B> void bindDaoSupplier(Binder binder, Class<B> baseType, Class<T> type)
{
binder.bind(daoSupplierTypeToken(baseType))
.toProvider(new DaoSupplierProvider<>(type))
.in(Scopes.SINGLETON);
}

@SuppressWarnings("unchecked")
private static <T> TypeLiteral<DaoSupplier<? extends T>> daoSupplierTypeToken(Class<T> type)
{
Type javaType = new TypeToken<DaoSupplier<T>>() {}
.where(new TypeParameter<>() {}, TypeToken.of(type))
.getType();
return (TypeLiteral<DaoSupplier<? extends T>>) TypeLiteral.get(javaType);
}

private static class DaoSupplierProvider<T>
implements Provider<DaoSupplier<T>>
private static class MySqlModule
implements Module
{
private final Class<T> type;
private Injector injector;

public DaoSupplierProvider(Class<T> type)
@Override
public void configure(Binder binder)
{
this.type = requireNonNull(type, "type is null");
configBinder(binder).bindConfig(JdbcDatabaseConfig.class);
}

@Inject
public void setInjector(Injector injector)
@Provides
@Singleton
@ForMetadata
public static ConnectionFactory createConnectionFactory(JdbcDatabaseConfig config)
{
this.injector = injector;
String url = config.getUrl();
return () -> DriverManager.getConnection(url);
}

@Override
public DaoSupplier<T> get()
@Provides
@Singleton
public static DaoSupplier<ShardDao> createShardDaoSupplier(@ForMetadata Jdbi dbi)
{
checkState(injector != null, "injector was not set");
Jdbi dbi = injector.getInstance(Key.get(Jdbi.class, ForMetadata.class));
return new DaoSupplier<>(dbi, type);
return new DaoSupplier<>(dbi, MySqlShardDao.class);
}
}

private static class MySqlDataSourceModule
private static class H2Module
implements Module
{
@Override
public void configure(Binder binder)
{
configBinder(binder).bindConfig(JdbcDatabaseConfig.class);
configBinder(binder).bindConfig(MySqlDataSourceConfig.class, ForMetadata.class, "metadata");
configBinder(binder).bindConfigDefaults(MySqlDataSourceConfig.class, ForMetadata.class, config -> {
config.setMaxConnections(100);
config.setDefaultFetchSize(1000);
});
configBinder(binder).bindConfig(H2DatabaseConfig.class);
}

@ForMetadata
@Provides
@Singleton
@ForMetadata
public static ConnectionFactory createConnectionFactory(H2DatabaseConfig config)
{
String url = "jdbc:h2:" + config.getFilename();
return () -> DriverManager.getConnection(url);
}

@Provides
DataSource createDataSource(JdbcDatabaseConfig config, @ForMetadata MySqlDataSourceConfig mysqlConfig)
@Singleton
public static DaoSupplier<ShardDao> createShardDaoSupplier(@ForMetadata Jdbi dbi)
{
ServiceDescriptor descriptor = serviceDescriptor("mysql")
.addProperty("jdbc", config.getUrl())
.build();
return new MySqlDataSource(new StaticServiceSelector(descriptor), mysqlConfig);
return new DaoSupplier<>(dbi, H2ShardDao.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.raptor.legacy.metadata;

import io.airlift.configuration.Config;

import javax.validation.constraints.NotNull;

public class H2DatabaseConfig
{
private String filename;

@NotNull
public String getFilename()
{
return filename;
}

@Config("metadata.db.filename")
public H2DatabaseConfig setFilename(String filename)
{
this.filename = filename;
return this;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public static DistributedQueryRunner createRaptorQueryRunner(
Map<String, String> raptorProperties = ImmutableMap.<String, String>builder()
.putAll(extraRaptorProperties)
.put("metadata.db.type", "h2")
.put("metadata.db.connections.max", "100")
.put("metadata.db.filename", new File(baseDir, "db").getAbsolutePath())
.put("storage.data-directory", new File(baseDir, "data").getAbsolutePath())
.put("storage.max-shard-rows", "2000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;

public class TestJdbcDatabaseConfig
public class TestH2DatabaseConfig
{
@Test
public void testDefaults()
{
assertRecordedDefaults(recordDefaults(JdbcDatabaseConfig.class)
.setUrl(null));
assertRecordedDefaults(recordDefaults(H2DatabaseConfig.class)
.setFilename(null));
}

@Test
public void testExplicitPropertyMappings()
{
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put("metadata.db.url", "jdbc:test://example.net/test")
.put("metadata.db.filename", "/tmp/db")
.buildOrThrow();

JdbcDatabaseConfig expected = new JdbcDatabaseConfig()
.setUrl("jdbc:test://example.net/test");
H2DatabaseConfig expected = new H2DatabaseConfig()
.setFilename("/tmp/db");

assertFullMapping(properties, expected);
}
Expand Down