Skip to content

Commit

Permalink
#43: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kovax committed Mar 24, 2019
1 parent 9b61244 commit da89c15
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 70 deletions.
5 changes: 5 additions & 0 deletions jooqdb/pom.xml
Expand Up @@ -157,6 +157,11 @@
<artifactId>argon2-jvm</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
Expand Down
Expand Up @@ -41,7 +41,7 @@ public String getName() {

@Override
public String getId() {
return "JOOQCLIENT:"+context.dialect();
return "JOOQCLIENT:"+JooqHandler.dialect;
}

@Override
Expand Down
Expand Up @@ -20,9 +20,9 @@
*/
package org.cristalise.storage.jooqdb;

import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_DOMAIN_HANDLERS;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_AUTOCOMMIT;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_DISABLE_DOMAIN_CREATE;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_DOMAIN_HANDLERS;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -56,18 +56,13 @@
*/
public class JooqClusterStorage extends TransactionalClusterStorage {

protected DSLContext context;
protected Boolean autoCommit;
protected Boolean autoCommit = Gateway.getProperties().getBoolean(JOOQ_AUTOCOMMIT, false);

protected HashMap<ClusterType, JooqHandler> jooqHandlers = new HashMap<ClusterType, JooqHandler>();
protected List<JooqDomainHandler> domainHandlers = new ArrayList<JooqDomainHandler>();

@Override
public void open(Authenticator auth) throws PersistencyException {
context = JooqHandler.connect();

autoCommit = Gateway.getProperties().getBoolean(JOOQ_AUTOCOMMIT, false);

initialiseHandlers();
}

Expand All @@ -88,6 +83,8 @@ public void initialiseHandlers() throws PersistencyException {
jooqHandlers.put(ClusterType.JOB, new JooqJobHandler());
jooqHandlers.put(ClusterType.ATTACHMENT, new JooqOutcomeAttachmentHandler());

DSLContext context = JooqHandler.connect();

for (JooqHandler handler: jooqHandlers.values()) handler.createTables(context);

try {
Expand Down Expand Up @@ -116,7 +113,7 @@ public void initialiseHandlers() throws PersistencyException {
public void close() throws PersistencyException {
Logger.msg(1, "JooqClusterStorage.close()");
try {
context.close();
JooqHandler.connect().close();
}
catch (Exception e) {
Logger.error(e);
Expand All @@ -126,17 +123,17 @@ public void close() throws PersistencyException {

@Override
public void postBoostrap() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postBoostrap(context);
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postBoostrap(JooqHandler.connect());
}

@Override
public void postStartServer() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postStartServer(context);
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postStartServer(JooqHandler.connect());
}

@Override
public void postConnect() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postConnect(context);
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postConnect(JooqHandler.connect());
}

@Override
Expand All @@ -146,6 +143,8 @@ public void begin(Object locker) {

@Override
public void commit(Object locker) throws PersistencyException {
DSLContext context = JooqHandler.connect();

for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.commit(context, locker);

if (autoCommit) {
Expand All @@ -164,7 +163,9 @@ public void commit(Object locker) throws PersistencyException {
}

@Override
public void abort(Object locker) {
public void abort(Object locker) throws PersistencyException {
DSLContext context = JooqHandler.connect();

for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.abort(context, locker);

if (autoCommit) {
Expand Down Expand Up @@ -198,17 +199,17 @@ public short queryClusterSupport(ClusterType type) {
@Override
public boolean checkQuerySupport(String language) {
String lang = language.trim().toUpperCase();
return "SQL".equals(lang) || ("SQL:"+context.dialect()).equals(lang);
return "SQL".equals(lang) || ("SQL:"+JooqHandler.dialect).equals(lang);
}

@Override
public String getName() {
return "JOOQ:"+context.dialect()+" ClusterStorage";
return "JOOQ:"+JooqHandler.dialect+" ClusterStorage";
}

@Override
public String getId() {
return "JOOQ:"+context.dialect();
return "JOOQ:"+JooqHandler.dialect;
}

@Override
Expand All @@ -221,7 +222,7 @@ public ClusterType[] getClusters(ItemPath itemPath) throws PersistencyException
ArrayList<ClusterType> result = new ArrayList<ClusterType>();

for (ClusterType type:jooqHandlers.keySet()) {
if (jooqHandlers.get(type).exists(context, itemPath.getUUID())) result.add(type);
if (jooqHandlers.get(type).exists(JooqHandler.connect(), itemPath.getUUID())) result.add(type);
}

return result.toArray(new ClusterType[0]);
Expand All @@ -248,7 +249,7 @@ public String[] getClusterContents(ItemPath itemPath, String path) throws Persis
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.getClusterContents() - uuid:"+uuid+" cluster:"+cluster+" primaryKeys"+Arrays.toString(primaryKeys));

return handler.getNextPrimaryKeys(context, uuid, primaryKeys);
return handler.getNextPrimaryKeys(JooqHandler.connect(), uuid, primaryKeys);
}
else
throw new PersistencyException("No handler found for cluster:'"+cluster+"'");
Expand All @@ -267,7 +268,7 @@ public C2KLocalObject get(ItemPath itemPath, String path) throws PersistencyExce
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.get() - uuid:"+uuid+" cluster:"+cluster+" primaryKeys:"+Arrays.toString(primaryKeys));

C2KLocalObject obj = handler.fetch(context, uuid, primaryKeys);
C2KLocalObject obj = handler.fetch(JooqHandler.connect(), uuid, primaryKeys);

if (obj == null && Logger.doLog(8)) {
Logger.warning(("JooqClusterStorage.get() - Could NOT fetch '"+itemPath+"/"+path+"'"));
Expand All @@ -292,14 +293,14 @@ public void put(ItemPath itemPath, C2KLocalObject obj, Object locker) throws Per

if (handler != null) {
Logger.msg(5, "JooqClusterStorage.put() - uuid:"+uuid+" cluster:"+cluster+" path:"+obj.getClusterPath());
handler.put(context, uuid, obj);
handler.put(JooqHandler.connect(), uuid, obj);
}
else {
throw new PersistencyException("Write is not supported for cluster:'"+cluster+"'");
}

// Trigger all registered handlers to update domain specific tables
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.put(context, uuid, obj, locker);
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.put(JooqHandler.connect(), uuid, obj, locker);
}

@Override
Expand All @@ -315,6 +316,7 @@ public void delete(ItemPath itemPath, String path, Object locker) throws Persist
String[] primaryKeys = Arrays.copyOfRange(pathArray, 1, pathArray.length);
ClusterType cluster = ClusterType.getValue(pathArray[0]);

DSLContext context = JooqHandler.connect();
JooqHandler handler = jooqHandlers.get(cluster);

if (handler != null) {
Expand Down
39 changes: 26 additions & 13 deletions jooqdb/src/main/java/org/cristalise/storage/jooqdb/JooqHandler.java
Expand Up @@ -23,7 +23,6 @@
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.using;

import java.sql.DriverManager;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
Expand All @@ -42,10 +41,12 @@
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.impl.DefaultConnectionProvider;
import org.jooq.impl.DefaultDataType;
import org.jooq.impl.SQLDataType;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public abstract class JooqHandler {
/**
* Defines the key (value:{@value}) to retrieve a string value to set JDBC URI
Expand Down Expand Up @@ -155,27 +156,39 @@ public abstract class JooqHandler {
public static final DataType<String> XML_TYPE_MYSQL = new DefaultDataType<String>(SQLDialect.MYSQL, SQLDataType.CLOB, "mediumtext", "char");
public static final DataType<byte[]> ATTACHMENT_TYPE = SQLDataType.BLOB;

public static DSLContext connect() throws PersistencyException {
String uri = Gateway.getProperties().getString(JooqHandler.JOOQ_URI);
String user = Gateway.getProperties().getString(JooqHandler.JOOQ_USER);
String pwd = Gateway.getProperties().getString(JooqHandler.JOOQ_PASSWORD);

public static final String uri = Gateway.getProperties().getString(JooqHandler.JOOQ_URI);
public static final String user = Gateway.getProperties().getString(JooqHandler.JOOQ_USER);
public static final String pwd = Gateway.getProperties().getString(JooqHandler.JOOQ_PASSWORD);
public static final Boolean autoCommit = Gateway.getProperties().getBoolean(JooqHandler.JOOQ_AUTOCOMMIT, false);
public static final SQLDialect dialect = SQLDialect.valueOf(Gateway.getProperties().getString(JooqHandler.JOOQ_DIALECT, "POSTGRES"));

private static HikariDataSource ds;

static {
if (StringUtils.isAnyBlank(uri, user, pwd)) {
throw new IllegalArgumentException("JOOQ (uri, user, password) config values must not be blank");
}

SQLDialect dialect = SQLDialect.valueOf(Gateway.getProperties().getString(JooqHandler.JOOQ_DIALECT, "POSTGRES"));
HikariConfig config = new HikariConfig();

Logger.msg(1, "JooqHandler.open() - uri:'"+uri+"' user:'"+user+"' dialect:'"+dialect+"'");
config.setJdbcUrl(uri);
config.setUsername(user);
config.setPassword(pwd);

try {
DSLContext context = using(DriverManager.getConnection(uri, user, pwd), dialect);
config.addDataSourceProperty( "cachePrepStmts", true);
config.addDataSourceProperty( "prepStmtCacheSize", "250");
config.addDataSourceProperty( "prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty( "autoCommit", autoCommit);

boolean autoCommit = Gateway.getProperties().getBoolean(JooqHandler.JOOQ_AUTOCOMMIT, false);
ds = new HikariDataSource(config);
}

((DefaultConnectionProvider)context.configuration().connectionProvider()).setAutoCommit(autoCommit);
public static DSLContext connect() throws PersistencyException {
Logger.msg(1, "JooqHandler.open() - uri:'"+uri+"' user:'"+user+"' dialect:'"+dialect+"'");

return context;
try {
return using(ds, dialect);
}
catch (Exception ex) {
Logger.error("JooqHandler could not connect to URI '"+uri+"' with user '"+user+"'");
Expand Down

0 comments on commit da89c15

Please sign in to comment.