From 9a6f797364b0c2f743d9f5dc1ed8015fe1f609a2 Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Wed, 27 May 2020 12:36:46 -0500 Subject: [PATCH] WIP on cqld4 --- .../cqld4/config/CQLD4OptionsMapper.java | 88 +++++++++++++++++-- .../statements/core/CQLSessionCache.java | 20 ++--- 2 files changed, 88 insertions(+), 20 deletions(-) diff --git a/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/config/CQLD4OptionsMapper.java b/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/config/CQLD4OptionsMapper.java index bf8d06aad..8756050ff 100644 --- a/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/config/CQLD4OptionsMapper.java +++ b/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/config/CQLD4OptionsMapper.java @@ -1,16 +1,92 @@ package io.nosqlbench.activitytype.cqld4.config; +import com.datastax.oss.driver.api.core.config.DriverOption; +import com.datastax.oss.driver.api.core.config.OptionsMap; import com.datastax.oss.driver.api.core.config.TypedDriverOption; +import com.datastax.oss.driver.api.core.data.CqlDuration; -import java.lang.reflect.Field; -import java.util.List; -import java.util.stream.StreamSupport; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.time.*; +import java.util.UUID; public class CQLD4OptionsMapper { - public List> findOptions() { - Field[] fields = TypedDriverOption.class.getFields(); - ... + public static void apply(OptionsMap optionsMap, String name, String value) { + for (TypedDriverOption builtin : TypedDriverOption.builtInValues()) { + DriverOption rawOption = builtin.getRawOption(); + String path = rawOption.getPath(); + if (name.equals(path)) { + Class rawType = builtin.getExpectedType().getRawType(); + Object convertedValue = adaptTypeValue(value, rawType, name); + TypedDriverOption option = (TypedDriverOption) builtin; + optionsMap.put(option, convertedValue); + return; + } + } + + throw new RuntimeException("Driver option " + name + " was not found in the available options."); } + + private static Object adaptTypeValue(String value, Class rawOption, String optionName) { + switch (rawOption.getCanonicalName()) { + case "java.lang.Boolean": + return Boolean.parseBoolean(value); + case "java.lang.Byte": + return Byte.parseByte(value); + case "java.lang.Double": + return Double.parseDouble(value); + case "java.lang.Float": + return Float.parseFloat(value); + case "java.lang.Integer": + return Integer.parseInt(value); + case "java.lang.Long": + return Long.parseLong(value); + case "java.lang.Short": + return Short.parseShort(value); + case "java.time.Instant": + return Instant.parse(value); + case "java.time.ZonedDateTime": + return ZonedDateTime.parse(value); + case "java.time.LocalDate": + return LocalDate.parse(value); + case "java.time.LocalTime": + return LocalTime.parse(value); + case "java.nio.ByteBuffer": + return ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8)); // What else to do here? + case "java.lang.String": + return value; + case "java.math.BigInteger": + return new BigInteger(value); + case "java.math.BigDecimal": + return new BigDecimal(value); + case "java.util.UUID": + return UUID.fromString(value); + case "java.net.InetAddress": + try { + return InetAddress.getByName(value); + } catch (UnknownHostException e) { + throw new RuntimeException(e); + } + case "com.datastax.oss.driver.api.core.data.CqlDuration": + return CqlDuration.from(value); + case "java.time.Duration:": + return Duration.parse(value); + default: +// These appear to be valid types, but there is no record of them used in driver configuration, +// nor a convenient way to convert them directly from known type and string value without invoking +// connected metadata machinery from an active session. +// case "com.datastax.oss.driver.api.core.data.TupleValue": +// case "com.datastax.oss.driver.api.core.data.UdtValue": + + throw new RuntimeException("The type converter for driver option named " + optionName + " was not " + + "found, or is unimplemented. Please file an issue at nosqlbench.io"); + } + } + } diff --git a/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/statements/core/CQLSessionCache.java b/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/statements/core/CQLSessionCache.java index 2fe58c66d..bd985958f 100644 --- a/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/statements/core/CQLSessionCache.java +++ b/driver-cqld4/src/main/java/io/nosqlbench/activitytype/cqld4/statements/core/CQLSessionCache.java @@ -8,6 +8,7 @@ import com.datastax.oss.driver.api.core.metadata.EndPoint; import com.datastax.oss.driver.api.core.retry.RetryPolicy; import com.datastax.oss.driver.api.core.session.Session; import com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy; +import com.datastax.oss.driver.internal.core.config.map.MapBasedDriverConfigLoader; import com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader; import com.datastax.oss.driver.internal.core.retry.DefaultRetryPolicy; import com.typesafe.config.ConfigFactory; @@ -83,8 +84,8 @@ public class CQLSessionCache implements Shutdownable { // TODO: Figure out how to layer configs with the new TypeSafe Config layer in the Datastax Java Driver // TODO: Or give up and bulk import options into the map, because the config API is a labyrinth -// -// CqlSessionBuilder builder = CqlSession.builder(); + + CqlSessionBuilder builder = CqlSession.builder(); // // OptionsMap optionsMap = new OptionsMap(); // @@ -92,19 +93,10 @@ public class CQLSessionCache implements Shutdownable { // DriverConfigLoader cl = DriverConfigLoader.fromMap(defaults); // DriverConfig cfg = cl.getInitialConfig(); - OptionsMap optionsMap = new OptionsMap(); - optionsMap.put(TypedDriverOption.CONTINUOUS_PAGING_TIMEOUT_OTHER_PAGES,) - OptionsMap source = OptionsMap.driverDefaults(); - optionsMap.put() - - DriverConfigLoader alldefaults = DriverConfigLoader.fromMap(source); - - alldefaults. - DriverConfigLoader.fromMap() - - builder.withConfigLoader(DriverConfigLoader.fromMap().) - + OptionsMap optionsMap = OptionsMap.driverDefaults(); + builder.withConfigLoader(new MapBasedDriverConfigLoader()) + builder.withConfigLoader(optionsMap); Optional scb = activityDef.getParams().getOptionalString("secureconnectbundle")