Allow an init path that doesn't require a non-null Cassandra/DataStax session/cluster context.

This commit is contained in:
Greg Burd 2018-01-16 11:59:51 -05:00
parent 26c67e391a
commit 1ef50ae179
4 changed files with 58 additions and 16 deletions

View file

@ -33,10 +33,8 @@ import net.helenus.support.HelenusMappingException;
public final class Helenus {
private static final ConcurrentMap<Class<?>, Object> dslCache =
new ConcurrentHashMap<Class<?>, Object>();
private static final ConcurrentMap<Class<?>, Metadata> metadataForEntity =
new ConcurrentHashMap<Class<?>, Metadata>();
private static final ConcurrentMap<Class<?>, Object> dslCache = new ConcurrentHashMap<Class<?>, Object>();
private static final ConcurrentMap<Class<?>, Metadata> metadataForEntity = new ConcurrentHashMap<Class<?>, Metadata>();
private static final Set<HelenusSession> sessions = new HashSet<HelenusSession>();
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) {

View file

@ -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;

View file

@ -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);

View file

@ -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();
}
}