Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: io-jdbc #235

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions core/src/test/java/cz/o2/proxima/util/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2017-2020 O2 Czech Republic, a.s.
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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 cz.o2.proxima.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import static org.junit.Assert.assertTrue;

public class TestUtils {
/**
* Check if object is serializable
*
* @param object Object to check
* @throws IOException
*/
public static void assertSerializable(Object object) throws IOException {

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(buffer)) {
oos.writeObject(object);
}
assertTrue(
String.format("Class '%s' isn't serializable.", object.getClass().getName()),
buffer.toByteArray().length > 0);
}
}
1 change: 1 addition & 0 deletions direct/io-jdbc/license-header-spotless.txt
1 change: 1 addition & 0 deletions direct/io-jdbc/license-header.txt
127 changes: 127 additions & 0 deletions direct/io-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!--

Copyright 2017-2020 O2 Czech Republic, a.s.

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>cz.o2.proxima</groupId>
<artifactId>proxima-direct</artifactId>
<version>0.3.0-rc1</version>
</parent>

<artifactId>proxima-direct-io-jdbc</artifactId>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
<configuration>
<artifactSet>
<excludes>
<!-- exclude proxima and core dependencies -->
<exclude>cz.o2.proxima:proxima-core</exclude>
<exclude>cz.o2.proxima:proxima-direct-core</exclude>
</excludes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.google.common.</pattern>
<shadedPattern>${coreShade}.com.google.common.</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>cz.o2.proxima</groupId>
<artifactId>proxima-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>cz.o2.proxima</groupId>
<artifactId>proxima-direct-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.1</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>cz.o2.proxima</groupId>
<artifactId>proxima-core</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2017-2020 O2 Czech Republic, a.s.
*
* 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 cz.o2.proxima.direct.jdbc;

import cz.o2.proxima.repository.AttributeDescriptor;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

public interface Converter<T> extends Serializable {
default void setup() {}

String getKeyFromResult(ResultSet result) throws SQLException;

default long getTimestampFromResult(ResultSet result) throws SQLException {
return System.currentTimeMillis();
}

Object attributeValue(ResultSet resultSet, AttributeDescriptor<T> attr) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Copyright 2017-2020 O2 Czech Republic, a.s.
*
* 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 cz.o2.proxima.direct.jdbc;

import com.google.common.annotations.VisibleForTesting;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import cz.o2.proxima.annotations.Experimental;
import cz.o2.proxima.direct.core.AttributeWriterBase;
import cz.o2.proxima.direct.core.Context;
import cz.o2.proxima.direct.core.DataAccessor;
import cz.o2.proxima.direct.randomaccess.RandomAccessReader;
import cz.o2.proxima.repository.EntityDescriptor;
import cz.o2.proxima.storage.AbstractStorage;
import cz.o2.proxima.util.Classpath;
import java.net.URI;
import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Experimental(value = "Need real use case")
@Slf4j
public class JdbcDataAccessor extends AbstractStorage implements DataAccessor {
static final String JDBC_URI_STORAGE_PREFIX = "jdbc://";
static final String JDBC_DRIVER_CFG = "driverClassName";
static final String JDBC_USERNAME_CFG = "username";

@SuppressWarnings("squid:S2068")
static final String JDBC_PASSWORD_CFG = "password";

static final String JDBC_SQL_QUERY_FACTORY_CFG = "sqlQueryFactory";
static final String JDBC_RESULT_CONVERTER_CFG = "converter";

private final String jdbcUri;
private final String jdbcUsername;
private final String jdbcPassword;
private final String jdbcDriver;

private final EntityDescriptor entityDescriptor;
private final URI uri;

private final SqlStatementFactory sqlStatementFactory;

@Getter private final Converter<?> resultConverter;

private transient HikariDataSource dataSource;

protected JdbcDataAccessor(EntityDescriptor entityDesc, URI uri, Map<String, Object> cfg) {
super(entityDesc, uri);
this.jdbcUri = uri.toString().substring(JDBC_URI_STORAGE_PREFIX.length());
this.entityDescriptor = entityDesc;
this.uri = uri;

jdbcDriver = cfg.getOrDefault(JDBC_DRIVER_CFG, "").toString();
jdbcUsername = cfg.getOrDefault(JDBC_USERNAME_CFG, "").toString();
jdbcPassword = cfg.getOrDefault(JDBC_PASSWORD_CFG, "").toString();

if (!cfg.containsKey(JDBC_SQL_QUERY_FACTORY_CFG)) {
log.error("Missing configuration param {}.", JDBC_URI_STORAGE_PREFIX);
throw new IllegalStateException(
String.format("Missing configuration param %s", JDBC_SQL_QUERY_FACTORY_CFG));
} else {
log.info(
"Using '{}' as {}.", cfg.get(JDBC_SQL_QUERY_FACTORY_CFG), JDBC_SQL_QUERY_FACTORY_CFG);
sqlStatementFactory =
Classpath.newInstance(
cfg.get(JDBC_SQL_QUERY_FACTORY_CFG).toString(), SqlStatementFactory.class);
try {
sqlStatementFactory.setup(entityDesc, uri, this.getDataSource());
} catch (SQLException e) {
log.error(
"Unable to setup {} from class {}.",
JDBC_SQL_QUERY_FACTORY_CFG,
cfg.get(JDBC_SQL_QUERY_FACTORY_CFG),
e);
throw new IllegalStateException(e.getMessage(), e);
}
}

if (!cfg.containsKey(JDBC_RESULT_CONVERTER_CFG)) {
log.error("Missing configuration param {}.", JDBC_RESULT_CONVERTER_CFG);
throw new IllegalStateException(
String.format("Missing configuration param %s", JDBC_RESULT_CONVERTER_CFG));
} else {
log.info("Using '{}' as SqlStatementFactory.", cfg.get(JDBC_RESULT_CONVERTER_CFG));
resultConverter =
Classpath.newInstance(cfg.get(JDBC_RESULT_CONVERTER_CFG).toString(), Converter.class);
resultConverter.setup();
}
}

@Override
public Optional<AttributeWriterBase> getWriter(Context context) {
return Optional.of(newWriter());
}

@Override
public Optional<RandomAccessReader> getRandomAccessReader(Context context) {
return Optional.of(newRandomAccessReader());
}

@VisibleForTesting
AttributeWriterBase newWriter() {
return new JdbcOnlineAttributeWriter(this, this.sqlStatementFactory, entityDescriptor, uri);
}

@VisibleForTesting
RandomAccessReader newRandomAccessReader() {
return new JdbcOnlineAttributeReader(this, this.sqlStatementFactory, entityDescriptor, uri);
}

HikariDataSource getDataSource() {
if (dataSource == null) {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setPoolName(
String.format("jdbc-pool-%s", this.getEntityDescriptor().getName()));

if (!jdbcDriver.isEmpty()) {
dataSourceConfig.setDataSourceClassName(jdbcDriver);
}
log.info("Creating JDBC storage from url: {}", jdbcUri);
dataSourceConfig.setJdbcUrl(jdbcUri);
if (!jdbcUsername.isEmpty()) {
dataSourceConfig.setUsername(jdbcUsername);
}
if (!jdbcPassword.isEmpty()) {
dataSourceConfig.setPassword(jdbcPassword);
}
dataSource = new HikariDataSource(dataSourceConfig);
}
return dataSource;
}
}
Loading