From 1ef50ae179b725f87e9cd3951caa06592c99f01c Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 16 Jan 2018 11:59:51 -0500 Subject: [PATCH] Allow an init path that doesn't require a non-null Cassandra/DataStax session/cluster context. --- src/main/java/net/helenus/core/Helenus.java | 10 ++-- .../java/net/helenus/core/HelenusSession.java | 7 ++- .../net/helenus/core/SessionInitializer.java | 48 +++++++++++++++---- .../integration/core/ContextInitTest.java | 9 ++++ 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/helenus/core/Helenus.java b/src/main/java/net/helenus/core/Helenus.java index edb15e2..625c80e 100644 --- a/src/main/java/net/helenus/core/Helenus.java +++ b/src/main/java/net/helenus/core/Helenus.java @@ -33,10 +33,8 @@ import net.helenus.support.HelenusMappingException; public final class Helenus { - private static final ConcurrentMap, Object> dslCache = - new ConcurrentHashMap, Object>(); - private static final ConcurrentMap, Metadata> metadataForEntity = - new ConcurrentHashMap, Metadata>(); + private static final ConcurrentMap, Object> dslCache = new ConcurrentHashMap, Object>(); + private static final ConcurrentMap, Metadata> metadataForEntity = new ConcurrentHashMap, Metadata>(); private static final Set sessions = new HashSet(); private static volatile HelenusSettings settings = new DefaultHelenusSettings(); private static volatile HelenusSession singleton; @@ -81,6 +79,10 @@ public final class Helenus { return new SessionInitializer(session); } + public static SessionInitializer init(Session session, String keyspace) { + return new SessionInitializer(session, keyspace); + } + public static SessionInitializer init(Session session) { if (session == null) { diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index c1457ee..cb19047 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -100,7 +100,7 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab this.showCql = showCql; this.showValues = showValues; this.printStream = printStream; - this.sessionRepository = sessionRepositoryBuilder.build(); + this.sessionRepository = sessionRepositoryBuilder == null ? null : sessionRepositoryBuilder.build(); this.executor = executor; this.dropSchemaOnClose = dropSchemaOnClose; this.defaultConsistencyLevel = consistencyLevel; @@ -117,7 +117,7 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab this.valueProvider = new RowColumnValueProvider(this.sessionRepository); this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository); - this.metadata = session.getCluster().getMetadata(); + this.metadata = session == null ? null : session.getCluster().getMetadata(); } @Override @@ -794,6 +794,9 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab } public void close() { + if (session == null) { + return; + } if (session.isClosed()) { return; diff --git a/src/main/java/net/helenus/core/SessionInitializer.java b/src/main/java/net/helenus/core/SessionInitializer.java index 0cd3c7d..d573478 100644 --- a/src/main/java/net/helenus/core/SessionInitializer.java +++ b/src/main/java/net/helenus/core/SessionInitializer.java @@ -15,16 +15,28 @@ */ package net.helenus.core; -import brave.Tracer; -import com.codahale.metrics.MetricRegistry; -import com.datastax.driver.core.*; -import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.io.PrintStream; -import java.util.*; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.function.Consumer; + +import brave.Tracer; +import com.codahale.metrics.MetricRegistry; +import com.datastax.driver.core.CodecRegistry; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.TableMetadata; +import com.datastax.driver.core.UserType; +import com.google.common.util.concurrent.MoreExecutors; import net.helenus.core.cache.SessionCache; import net.helenus.core.reflect.DslExportable; import net.helenus.mapping.HelenusEntity; @@ -58,10 +70,18 @@ public final class SessionInitializer extends AbstractSessionOperations { private AutoDdl autoDdl = AutoDdl.UPDATE; private SessionCache sessionCache = null; + SessionInitializer(Session session, String keyspace) { + this.session = session; + this.usingKeyspace = keyspace; + if (session != null) { + this.sessionRepository = new SessionRepositoryBuilder(session); + } + } + SessionInitializer(Session session) { - this.session = Objects.requireNonNull(session, "empty session"); - this.usingKeyspace = session.getLoggedKeyspace(); // can be null - this.sessionRepository = new SessionRepositoryBuilder(session); + this.session = Objects.requireNonNull(session, "empty session"); + this.usingKeyspace = session.getLoggedKeyspace(); // can be null + this.sessionRepository = new SessionRepositoryBuilder(session); } @Override @@ -284,6 +304,7 @@ public final class SessionInitializer extends AbstractSessionOperations { idempotent, unitOfWorkClass, sessionCache, + statementCache, metricRegistry, zipkinTracer); } @@ -302,10 +323,17 @@ public final class SessionInitializer extends AbstractSessionOperations { } DslExportable dsl = (DslExportable) Helenus.dsl(iface); - dsl.setCassandraMetadataForHelenusSession(session.getCluster().getMetadata()); - sessionRepository.add(dsl); + if (session != null) { + dsl.setCassandraMetadataForHelenusSession(session.getCluster().getMetadata()); + } + if (sessionRepository != null) { + sessionRepository.add(dsl); + } }); + if (session == null) + return; + TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes); UserTypeOperations userTypeOps = new UserTypeOperations(this, dropUnusedColumns); diff --git a/src/test/java/net/helenus/test/integration/core/ContextInitTest.java b/src/test/java/net/helenus/test/integration/core/ContextInitTest.java index 0c8f6fd..c7c0c9a 100644 --- a/src/test/java/net/helenus/test/integration/core/ContextInitTest.java +++ b/src/test/java/net/helenus/test/integration/core/ContextInitTest.java @@ -17,6 +17,8 @@ package net.helenus.test.integration.core; import net.helenus.core.Helenus; import net.helenus.core.HelenusSession; +import net.helenus.core.SessionInitializer; +import net.helenus.core.UnitOfWork; import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest; import org.junit.Test; @@ -29,4 +31,11 @@ public class ContextInitTest extends AbstractEmbeddedCassandraTest { System.out.println("Works! " + session); } + + @Test + public void testWithNullSession() { + HelenusSession session = Helenus.init(null, "foo").get(); + UnitOfWork uow = session.begin(); + uow.abort(); + } }