public final Config database(final String database) { return database(Optional.of(database)); }
public final Config password(final String password) { return password(Optional.of(password)); }
@Override public final DataSource<PreparedStatement, Row> get() { final Pool<Connection> pool = LockFreePool.create(createConnection, config.poolMaxSize(), config.poolMaxWaiters(), config.connectionTimeout(), config.poolValidationInterval(), config.scheduler()); return new PooledDataSource(pool, config); } }
final String dataSourceSupplierClass = getRequiredProperty(prefix, properties, "dataSourceSupplierClass"); final String host = getRequiredProperty(prefix, properties, "host"); final int port = getRequiredProperty(prefix, properties, "port", Integer::parseInt); final String user = getRequiredProperty(prefix, properties, "user"); Config config = Config.create(dataSourceSupplierClass, host, port, user); config = config.charset(getProperty(prefix, properties, "charset", Charset::forName) .orElse(Charset.defaultCharset())); config = config.password(getProperty(prefix, properties, "password")); config = config.database(getProperty(prefix, properties, "database")); config = config.poolMaxSize(getProperty(prefix, properties, "poolMaxSize", Integer::parseInt)); config = config.poolMaxWaiters(getProperty(prefix, properties, "poolMaxWaiters", Integer::parseInt)); config = config.poolValidationInterval(getProperty(prefix, properties, "poolValidationIntervalSeconds", s -> Duration.ofSeconds(Long.parseLong(s)))); config = config.connectionTimeout(getProperty(prefix, properties, "connectionTimeoutSeconds", s -> Duration.ofSeconds(Long.parseLong(s)))); config = config.queryTimeout(getProperty(prefix, properties, "queryTimeoutSeconds", s -> Duration.ofSeconds(Long.parseLong(s)))); config = config.encodingClasses(getProperty(prefix, properties, "encodingClasses") .map(k -> Stream.of(k.split(",")).filter(s -> !s.isEmpty()).collect(Collectors.toSet()))); config = config.ssl(getProperty(prefix, properties, "ssl.mode", SSL.Mode::valueOf).map(sslMode -> { final SSL ssl = SSL.create(sslMode);
@Override protected Supplier<Future<Connection>> createConnectionSupplier(final Config config, final Supplier<Future<NettyChannel>> channelSupplier) { final EncodingRegistry encoding = new EncodingRegistry(config.loadCustomEncodings(), config.charset()); final Marshallers marshallers = new Marshallers(encoding); final Unmarshallers unmarshallers = new Unmarshallers(config.charset()); final QueryResultExchange queryResultExchange = new QueryResultExchange(encoding, unmarshallers); final InitSSLExchange initSSLExchange = new InitSSLExchange(marshallers, unmarshallers); final StartupExchange startup = new StartupExchange(marshallers, unmarshallers); return () -> { final PrepareStatementExchange preparedStatementCache = new PrepareStatementExchange(marshallers, unmarshallers); final ExtendedExchange extendedExchange = new ExtendedExchange(marshallers, unmarshallers, preparedStatementCache); return channelSupplier.get().flatMap(channel -> initSSLExchange.apply(config.ssl()).run(channel) .flatMap(ssl -> initSSLHandler.apply(config.host(), config.port(), ssl, channel)) .flatMap(v -> startup.apply(config.charset(), config.user(), config.password(), config.database()) .run(channel) .map(backendKeyData -> new io.trane.ndbc.postgres.Connection( channel, marshallers, config.queryTimeout(), config.scheduler(), channelSupplier, backendKeyData, new SimpleQueryExchange(queryResultExchange, marshallers, unmarshallers), new SimpleExecuteExchange(marshallers, unmarshallers), new ExtendedQueryExchange(queryResultExchange, extendedExchange), new ExtendedQueryStreamExchange(marshallers, unmarshallers, preparedStatementCache, queryResultExchange), new ExtendedExecuteExchange(extendedExchange, unmarshallers))))); }; } }
@Override public final DataSource<PreparedStatement, Row> get() { log.info("Starting embedded postgres " + version + " on port " + config.port()); Paths.get(System.getProperty("user.home"), ".ndbc", "embedded_postgres")); final String password = config.password().orElseGet(() -> { throw new UnsupportedOperationException("Embedded postgres requires a password"); }); postgres.start(cached, config.host(), config.port(), config.database().orElse(EmbeddedPostgres.DEFAULT_DB_NAME), config.user(), password, DEFAULT_ADD_PARAMS); } catch (final IOException e) { throw new RuntimeException(e); final DataSource<PreparedStatement, Row> underlying = DataSource.fromConfig(config.embedded(Optional.empty())); return new ProxyDataSource<PreparedStatement, Row>(underlying) { @Override
@Override protected Supplier<Future<Connection>> createConnectionSupplier(final Config config, final Supplier<Future<NettyChannel>> channelSupplier) { final EncodingRegistry encoding = new EncodingRegistry(config.loadCustomEncodings(), config.charset()); final Unmarshallers unmarshallers = new Unmarshallers(config.charset(), encoding); final TerminatorExchange terminatorExchange = new TerminatorExchange(unmarshallers); final Marshallers marshallers = new Marshallers(encoding, config.charset()); final SimpleQueryExchange simpleQueryExchange = new SimpleQueryExchange(marshallers, new ResultSetExchange(unmarshallers, false)); final StartupExchange startup = new StartupExchange(simpleQueryExchange, marshallers, unmarshallers, .apply(config.user(), config.password(), config.database(), "utf8").run(channel).map(connectionId -> { return new io.trane.ndbc.mysql.Connection(channel, connectionId, marshallers, config.queryTimeout(), config.scheduler(), simpleQueryExchange, simpleExecuteExchange, extendedQueryExchange, extendedQueryStreamExchange, extendedExecuteExchange, this); }));
public Netty4DataSourceSupplier(final Config config, final Function<BufferReader, Optional<BufferReader>> transformBufferReader) { this.config = config; final ChannelSupplier channelSupplier = new ChannelSupplier( new NioEventLoopGroup(config.nioThreads().orElse(0), new DefaultThreadFactory("ndbc-netty4", true)), config.host(), config.port(), config.charset(), transformBufferReader); this.createConnection = createConnectionSupplier(config, channelSupplier); }
public final Config embedded(final String supplierClass) { return embedded(supplierClass, Optional.empty()); }
/** * Creates the data source based on a `Config` instance. * * @param config * the config * @return the data source instance */ @SuppressWarnings("unchecked") public static DataSource<PreparedStatement, Row> fromConfig(final Config config) { return config.embedded().map(embedded -> { try { final Supplier<DataSource<PreparedStatement, Row>> supplier = (Supplier<DataSource<PreparedStatement, Row>>) Class .forName(embedded.supplierClass) .getConstructor(Config.class, Optional.class).newInstance(config, embedded.version); return supplier.get(); } catch (final Exception e) { throw new NdbcException("Can't load DataSource supplier for: " + embedded, e); } }).orElseGet(() -> { try { final Supplier<DataSource<PreparedStatement, Row>> supplier = (Supplier<DataSource<PreparedStatement, Row>>) Class .forName(config.dataSourceSupplierClass()) .getConstructor(Config.class).newInstance(config); return supplier.get(); } catch (final Exception e) { throw new NdbcException("Can't load DataSource supplier: " + config.dataSourceSupplierClass(), e); } }); }
public final Config queryTimeout(final Duration queryTimeout) { return queryTimeout(Optional.of(queryTimeout)); }
public final Config connectionTimeout(final Duration connectionTimeout) { return connectionTimeout(Optional.of(connectionTimeout)); }
@Override public DataSource<PreparedStatement, Row> get() { log.info("Starting embedded mysql " + version + " on port " + config.port()); final String password = config.password().orElseGet(() -> { throw new UnsupportedOperationException("Embedded mysql requires a password"); }); final DownloadConfig downloadConfig = DownloadConfig.aDownloadConfig() .withCacheDir(Paths.get(System.getProperty("user.home"), ".ndbc", "embedded_mysql").toString()) .build(); final MysqldConfig mysqldConfig = MysqldConfig.aMysqldConfig(version) .withPort(config.port()) .withUser(config.user(), password) .build(); final Builder builder = EmbeddedMysql.anEmbeddedMysql(mysqldConfig, downloadConfig); config.database().ifPresent(db -> builder.addSchema(SchemaConfig.aSchemaConfig(db).build())); final EmbeddedMysql mysql = builder.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> mysql.stop())); log.info("mysql " + version + " started"); DataSource<PreparedStatement, Row> underlying = DataSource.fromConfig(config.embedded(Optional.empty())); return new ProxyDataSource<PreparedStatement, Row>(underlying) { @Override public Config config() { return EmbeddedSupplier.this.config; } }; }
public final Config embedded(final Embedded embedded) { return embedded(Optional.of(embedded)); }
public final Config embedded(final String supplierClass, final Optional<String> version) { return embedded(Embedded.create(supplierClass, version)); }