Example #1
0
  /**
   * @param tablename the table name
   * @param name the columnname
   * @param value the value
   * @return the mapped value
   */
  Object toStatementValue(Tablename tablename, String name, Object value) {
    if (isNullOrEmpty(value)) {
      return null;
    }

    final DataType dataType = catalog.getColumnMetadata(tablename, name).getType();

    // build in
    if (UDTValueMapper.isBuildInType(dataType)) {

      // enum
      if (DataTypes.isTextDataType(dataType) && Enum.class.isAssignableFrom(value.getClass())) {
        return value.toString();
      }

      // byte buffer (byte[])
      if (dataType.equals(DataType.blob()) && byte[].class.isAssignableFrom(value.getClass())) {
        return ByteBuffer.wrap((byte[]) value);
      }

      return value;

      // udt
    } else {
      return toUdtValue(
          tablename, catalog, catalog.getColumnMetadata(tablename, name).getType(), value);
    }
  }
Example #2
0
  public static void bind(
      final BoundStatement statement,
      final TableScheme scheme,
      final ColumnDefinitions meta,
      final Value<?> value) {
    final String name = value.getColumn().getName();
    if (statement.isSet(name)) {
      throw new IllegalStateException(); // FIXME: descriptive
    }

    if (value.getType() != Value.Type.BOUND) {
      throw new IllegalStateException(); // FIXME: descriptive
    }

    final DataType tableType = meta.getType(name);
    final DataType valueType = value.getColumn().getCassandraType().getDataType();

    if (!typesEquals(tableType, valueType)) {
      throw new IllegalArgumentException(
          MessageFormat.format("Unexpected types: {0} != {1}", tableType, valueType));
    }

    final Object converted = value.asCassandraValue();
    statement.setBytesUnsafe(
        name, tableType.serialize(converted, ProtocolVersion.NEWEST_SUPPORTED));
  }
  @Test
  public void testAlter() throws Exception {

    CreateTable desired =
        TableBuilder.create("test_keyspace", "test_table")
            .column("col1", "text")
            .column("col2", "bigint")
            .column("col3", "int")
            .column("col4", "text")
            .primaryKey("col1");

    TableMetadata existing = mock(TableMetadata.class);

    ColumnMetadata col1 = mock(ColumnMetadata.class);
    when(existing.getColumn(eq("col1"))).thenReturn(col1);
    when(col1.getType()).thenReturn(DataType.text());

    ColumnMetadata col2 = mock(ColumnMetadata.class);
    when(existing.getColumn(eq("col2"))).thenReturn(col2);
    when(col2.getType()).thenReturn(DataType.cint());

    List<AlterTable> statements = TableBuilder.alter(existing, desired);

    assertEquals(3, statements.size());
    assertEquals(
        "ALTER TABLE test_keyspace.test_table ALTER col2 TYPE bigint",
        statements.get(0).toString());
    assertEquals("ALTER TABLE test_keyspace.test_table ADD col3 int", statements.get(1).toString());
    assertEquals(
        "ALTER TABLE test_keyspace.test_table ADD col4 text", statements.get(2).toString());
  }
  /** Prints the sample collections that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  @SuppressWarnings("unchecked")
  public void printSampleCollections() {
    String objective = "Sample Collections";
    System.out.println(String.format("Printing %s...", objective));

    for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) {
      HashMap<DataType, Object> sampleValueMap =
          (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);

      if (dataType.getName() == DataType.Name.MAP) {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        HashMap<DataType, Object> sampleMap =
            (HashMap<DataType, Object>) sampleValueMap.get(typeArgument);

        Object mapKey = SAMPLE_DATA.get(typeArgument);
        Object mapValue = sampleMap.get(typeArgument);
        System.out.println(String.format("%1$-30s {%2$s : %3$s}", dataType, mapKey, mapValue));
      } else {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        Object sampleValue = sampleValueMap.get(typeArgument);

        System.out.println(String.format("%1$-30s %2$s", dataType, sampleValue));
      }
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }
  /** Generates the sample collections that will be used in testing */
  private static HashMap<DataType, Object> getSampleCollections() {
    HashMap<DataType, Object> sampleCollections = new HashMap<DataType, Object>();
    HashMap<DataType, Object> setAndListCollection;
    HashMap<DataType, HashMap<DataType, Object>> mapCollection;

    for (DataType.Name dataTypeName : DATA_TYPE_NON_PRIMITIVE_NAMES) {
      switch (dataTypeName) {
        case LIST:
          for (DataType typeArgument : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument)) continue;

            List<Object> list = new ArrayList<Object>();
            for (int i = 0; i < 5; i++) {
              list.add(SAMPLE_DATA.get(typeArgument));
            }

            setAndListCollection = new HashMap<DataType, Object>();
            setAndListCollection.put(typeArgument, list);
            sampleCollections.put(DataType.list(typeArgument), setAndListCollection);
          }
          break;
        case SET:
          for (DataType typeArgument : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument)) continue;

            Set<Object> set = new HashSet<Object>();
            for (int i = 0; i < 5; i++) {
              set.add(SAMPLE_DATA.get(typeArgument));
            }

            setAndListCollection = new HashMap<DataType, Object>();
            setAndListCollection.put(typeArgument, set);
            sampleCollections.put(DataType.set(typeArgument), setAndListCollection);
          }
          break;
        case MAP:
          for (DataType typeArgument1 : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument1)) continue;

            for (DataType typeArgument2 : DATA_TYPE_PRIMITIVES) {
              if (exclude(typeArgument2)) continue;

              HashMap<DataType, Object> map = new HashMap<DataType, Object>();
              map.put(typeArgument1, SAMPLE_DATA.get(typeArgument2));

              mapCollection = new HashMap<DataType, HashMap<DataType, Object>>();
              mapCollection.put(typeArgument1, map);
              sampleCollections.put(DataType.map(typeArgument1, typeArgument2), mapCollection);
            }
          }
          break;
        default:
          throw new RuntimeException("Missing handling of " + dataTypeName);
      }
    }

    return sampleCollections;
  }
Example #6
0
  public List<ListenableFuture<ResultSet>> moveTableUpdate(
      String name, ResultSet sourceResult, ColumnDefinitions defs) throws ExecutionException {
    int ncols = defs.size();

    StringBuilder body = new StringBuilder("update " + name).append(" SET ");
    boolean nb = true;
    boolean ns = true;
    List<ListenableFuture<ResultSet>> list = new ArrayList<>();
    StringBuilder spec = new StringBuilder(" WHERE ");
    for (int i = 0; i < ncols; ++i) {
      String cn = defs.getName(i);
      boolean isRowKey =
          name == null ? false : (cn.startsWith("row_") || cn.startsWith("cluster_"));
      if (isRowKey) {
        if (ns) {
          ns = false;
        } else {
          spec.append(" AND ");
        }
        spec.append(cn + " = ?");
      } else {
        if (nb) {
          nb = false;
        } else {
          body.append(", ");
        }
        DataType dt = defs.getType(i);
        if (!DataType.Name.COUNTER.equals(dt.getName())) {
          body.append(cn + " = ?");
        } else {
          body.append(cn + " = " + cn + " + ?");
        }
      }
    }
    body.append(spec.toString());
    System.out.println(body.toString());

    BoundStatement export = destination.peer.bPrepare(destination.session, body.toString());
    // Row row;
    int counter = 0;
    for (Row row : sourceResult) {
      for (int i = 0; i < ncols; ++i) {
        export.setBytesUnsafe(i, row.getBytesUnsafe(i));
      }
      list.add(destination.session.executeAsync(export));
      ++counter;
    }
    System.out.println("" + counter + " rows applied");
    return list;
  }
Example #7
0
  static boolean isBuildInType(DataType dataType) {
    if (dataType.isCollection()) {
      for (DataType type : dataType.getTypeArguments()) {
        if (!isBuildInType(type)) {
          return false;
        }
      }
      return true;

    } else {
      return DataType.allPrimitiveTypes().contains(dataType)
          || (TupleType.class.isAssignableFrom(dataType.getClass()));
    }
  }
  @Test
  public void should_throw_exception_on_cas_error() throws Exception {
    // Given
    final AtomicReference<CASResult> atomicCASResult = new AtomicReference<>(null);
    CASResultListener listener =
        new CASResultListener() {
          @Override
          public void onCASSuccess() {}

          @Override
          public void onCASError(CASResult casResult) {
            atomicCASResult.compareAndSet(null, casResult);
          }
        };
    wrapper =
        new RegularStatementWrapper(
            CompleteBean.class,
            rs,
            new Object[] {1},
            ONE,
            Optional.fromNullable(listener),
            NO_SERIAL_CONSISTENCY);
    wrapper.invoker = invoker;
    when(rs.getQueryString()).thenReturn("UPDATE table IF name='John' SET");
    when(session.execute(rs)).thenReturn(resultSet);
    when(resultSet.one()).thenReturn(row);
    when(row.getBool(CAS_RESULT_COLUMN)).thenReturn(false);
    when(row.getColumnDefinitions()).thenReturn(columnDefinitions);

    when(columnDefinitions.iterator().hasNext()).thenReturn(true, true, false);
    Definition col1 = buildColumnDef("keyspace", "table", "[applied]", DataType.cboolean());
    Definition col2 = buildColumnDef("keyspace", "table", "name", DataType.text());
    when(columnDefinitions.iterator().next()).thenReturn(col1, col2);

    when(invoker.invokeOnRowForType(row, DataType.cboolean().asJavaClass(), "[applied]"))
        .thenReturn(false);
    when(invoker.invokeOnRowForType(row, DataType.text().asJavaClass(), "name"))
        .thenReturn("Helen");

    // When
    wrapper.execute(session);

    // Then
    verify(session).execute(rs);
    final CASResult actual = atomicCASResult.get();
    assertThat(actual).isNotNull();
    assertThat(actual.operation()).isEqualTo(UPDATE);
    assertThat(actual.currentValues())
        .contains(MapEntry.entry("[applied]", false), MapEntry.entry("name", "Helen"));
  }
  /** Helper method to stringify SAMPLE_DATA for simple insert statements */
  private static String helperStringifiedData(DataType dataType) {
    String value = SAMPLE_DATA.get(dataType).toString();

    switch (dataType.getName()) {
      case BLOB:
        value = "0xCAFE";
        break;

      case INET:
        InetAddress v1 = (InetAddress) SAMPLE_DATA.get(dataType);
        value = String.format("'%s'", v1.getHostAddress());
        break;

      case TIMESTAMP:
        Date v2 = (Date) SAMPLE_DATA.get(dataType);
        value = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(v2);
      case ASCII:
      case TEXT:
      case VARCHAR:
        value = String.format("'%s'", value);
        break;

      default:
        break;
    }

    return value;
  }
Example #10
0
  public ByteBuffer getBuffer(Object value) {
    Class<?> cls = value.getClass();
    TypeCodec codec = TypeCodec.createFor(type.getName());

    ByteBuffer b = codec.serialize(value);

    return b;
  }
 public HecubaClientManager<String> getHecubaClientManagerWithStringKeys(
     CassandraParamsBean parameters,
     HecubaConstants.CassandraClientImplementation cassandraManagerType) {
   switch (cassandraManagerType) {
     case ASTYANAX:
       return new AstyanaxBasedHecubaClientManager<String>(
           parameters, com.netflix.astyanax.serializers.StringSerializer.get());
     case HECTOR:
       return new HectorBasedHecubaClientManager<String>(
           parameters, me.prettyprint.cassandra.serializers.StringSerializer.get());
     case DATASTAX:
       return new DataStaxBasedHecubaClientManager<>(parameters, DataType.text());
     case DATASTAX_SHARED:
       return new DataStaxBasedSharedHecubaClientManager<>(parameters, DataType.text());
     default:
       throw new RuntimeException("Unhandled CassandraManagerType: " + cassandraManagerType);
   }
 }
  /** Generates the insert statements that will be used in testing */
  @SuppressWarnings("unchecked")
  private static Collection<String> getCollectionInsertStatements() {
    ArrayList<String> insertStatements = new ArrayList<String>();

    String tableName;
    String key;
    String value;
    for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) {
      HashMap<DataType, Object> sampleValueMap =
          (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);

      // Create tableName in form of: DataType_TypeArgument[_TypeArgument]
      tableName = helperGenerateTableName(dataType);

      if (dataType.getName() == DataType.Name.MAP) {
        List<DataType> typeArgument = dataType.getTypeArguments();

        key = helperStringifiedData(typeArgument.get(0));
        value = helperStringifiedData(typeArgument.get(1));

        insertStatements.add(String.format(MAP_INSERT_FORMAT, tableName, key, value));
      } else if (dataType.getName() == DataType.Name.LIST) {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        key = helperStringifiedData(typeArgument);

        // Create the value to be a list of the same 5 elements
        value = "[";
        for (int i = 0; i < 5; i++) value += key + ',';
        value = value.substring(0, value.length() - 1) + ']';

        insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value));
      } else {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        key = helperStringifiedData(typeArgument);
        value = '{' + key + '}';

        insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value));
      }
    }

    return insertStatements;
  }
  // The two following tests a really unit tests, but since the whole uses
  // CCMBridge.PerClassSingleNodeCluster, they'll still spawn a cluster even
  // you execute only them, so we keep them in the "long" group. We could
  // move them in another class but not sure where honestly (one could argue
  // that it would make more sense to move all the *other* tests to some
  // DataTypeIntegrationTest class).
  @Test(groups = "long")
  public void serializeDeserializeTest() {

    for (DataType dt : DataType.allPrimitiveTypes()) {
      if (exclude(dt)) continue;

      Object value = TestUtils.getFixedValue(dt);
      assertEquals(dt.deserialize(dt.serialize(value)), value);
    }

    try {
      DataType.bigint().serialize(4);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }

    try {
      ByteBuffer badValue = ByteBuffer.allocate(4);
      DataType.bigint().deserialize(badValue);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }
Example #14
0
  public List<ListenableFuture<ResultSet>> moveTable(String name) throws ExecutionException {

    System.out.println("inserting data to destination:" + name);
    String query = "select * from " + name;
    BoundStatement bs = source.peer.bPrepare(source.session, query);
    ResultSet rs = source.session.execute(bs);
    ColumnDefinitions defs = rs.getColumnDefinitions();
    int ncols = defs.size();
    boolean update = false;
    for (int i = 0; i < ncols; ++i) {
      DataType dt = defs.getType(i);
      if (DataType.Name.COUNTER.equals(dt.getName())) {
        update = true;
        break;
      }
    }
    if (update) {
      return moveTableUpdate(name, rs, defs);
    } else {
      return moveTableInsert(name, rs, defs);
    }
  }
  @Test
  public void should_notify_listener_on_cas_error() throws Exception {
    // Given
    wrapper =
        new RegularStatementWrapper(
            CompleteBean.class, rs, new Object[] {1}, ONE, NO_LISTENER, NO_SERIAL_CONSISTENCY);
    wrapper.invoker = invoker;
    when(rs.getQueryString()).thenReturn("INSERT INTO table IF NOT EXISTS");
    when(session.execute(rs)).thenReturn(resultSet);
    when(resultSet.one()).thenReturn(row);
    when(row.getBool(CAS_RESULT_COLUMN)).thenReturn(false);
    when(row.getColumnDefinitions()).thenReturn(columnDefinitions);

    when(columnDefinitions.iterator().hasNext()).thenReturn(true, true, false);
    Definition col1 = buildColumnDef("keyspace", "table", "[applied]", DataType.cboolean());
    Definition col2 = buildColumnDef("keyspace", "table", "id", DataType.bigint());
    when(columnDefinitions.iterator().next()).thenReturn(col1, col2);

    when(invoker.invokeOnRowForType(row, DataType.cboolean().asJavaClass(), "[applied]"))
        .thenReturn(false);
    when(invoker.invokeOnRowForType(row, DataType.bigint().asJavaClass(), "id")).thenReturn(10L);

    AchillesLightWeightTransactionException caughtEx = null;
    // When
    try {
      wrapper.execute(session);
    } catch (AchillesLightWeightTransactionException ace) {
      caughtEx = ace;
    }

    // Then
    verify(session).execute(rs);
    assertThat(caughtEx).isNotNull();
    assertThat(caughtEx.operation()).isEqualTo(INSERT);
    assertThat(caughtEx.currentValues())
        .contains(MapEntry.entry("[applied]", false), MapEntry.entry("id", 10L));
  }
  private DataType.Name validateName(DataType dataType) {

    final DataType.Name name = dataType.getName();
    switch (name) {
      case BIGINT:
      case VARINT:
      case INT:
      case DECIMAL:
      case FLOAT:
      case DOUBLE:
      case COUNTER:
        return name;
    }
    throw new IllegalArgumentException("Datatype " + dataType + " not a number");
  }
  /** Test simple statement selects for all collection data types */
  @SuppressWarnings("unchecked")
  public void collectionSelectTest() throws Throwable {
    HashMap<DataType, Object> sampleValueMap;
    String execute_string;
    DataType typeArgument1;
    DataType typeArgument2;
    Row row;
    for (DataType dataType : COLLECTION_SELECT_STATEMENTS.keySet()) {
      execute_string = COLLECTION_SELECT_STATEMENTS.get(dataType);
      row = session.execute(execute_string).one();

      sampleValueMap = (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);
      typeArgument1 = dataType.getTypeArguments().get(0);
      if (dataType.getName() == DataType.Name.MAP) {
        typeArgument2 = dataType.getTypeArguments().get(1);

        // Create a copy of the map that is being expected
        HashMap<DataType, Object> sampleMap =
            (HashMap<DataType, Object>) sampleValueMap.get(typeArgument1);
        Object mapKey = SAMPLE_DATA.get(sampleMap.keySet().iterator().next());
        Object mapValue = sampleMap.values().iterator().next();
        HashMap<Object, Object> expectedMap = new HashMap<Object, Object>();
        expectedMap.put(mapKey, mapValue);

        assertEquals(TestUtils.getValue(row, "k", typeArgument2), SAMPLE_DATA.get(typeArgument2));
        assertEquals(TestUtils.getValue(row, "v", dataType), expectedMap);
      } else {
        Object expectedValue = sampleValueMap.get(typeArgument1);

        assertEquals(TestUtils.getValue(row, "k", typeArgument1), SAMPLE_DATA.get(typeArgument1));
        assertEquals(TestUtils.getValue(row, "v", dataType), expectedValue);
      }
    }
    assertEquals(SAMPLE_COLLECTIONS.size(), 255);
    assertEquals(COLLECTION_SELECT_STATEMENTS.keySet().size(), SAMPLE_COLLECTIONS.size());
  }
  @Test(groups = "long")
  public void serializeDeserializeCollectionsTest() {

    List<String> l = Arrays.asList("foo", "bar");

    DataType dt = DataType.list(DataType.text());
    assertEquals(dt.deserialize(dt.serialize(l)), l);

    try {
      DataType.list(DataType.bigint()).serialize(l);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }
Example #19
0
  private static boolean typesEquals(DataType t1, DataType t2) {
    if (t1.isCollection() && t2.isCollection()) {
      final List<DataType> t1Args = t1.getTypeArguments();
      final List<DataType> t2Args = t2.getTypeArguments();
      if (t1Args.size() == t2Args.size()) {
        boolean eq = true;
        for (int i = 0; i < t1Args.size(); i++) {
          eq &= typesEquals(t1Args.get(i), t2Args.get(i));
        }
        return eq;
      }
    }

    final Class<?> jTableCls = t1.asJavaClass();
    final Class<?> jColumnCls = t2.asJavaClass();
    return Objects.equals(jTableCls, jColumnCls);
  }
  /** Helper method to generate table names in the form of: DataType_TypeArgument[_TypeArgument] */
  private static String helperGenerateTableName(DataType dataType) {
    String tableName = dataType.getName().toString();
    for (DataType typeArgument : dataType.getTypeArguments()) tableName += "_" + typeArgument;

    return tableName;
  }
Example #21
0
  /**
   * @param datatype the db datatype
   * @param udtValue the udt value
   * @param fieldtype1 the field 1 type
   * @param fieldtype2 the field 2 type
   * @param fieldname the fieldname
   * @return the mapped value or <code>null</code>
   */
  public <T> Object fromUdtValue(
      DataType datatype,
      UDTValue udtValue,
      Class<?> fieldtype1,
      Class<?> fieldtype2,
      String fieldname) {
    final CodecRegistry codecRegistry = getCodecRegistry();

    // build-in type
    if (isBuildInType(datatype)) {
      final TypeCodec<T> typeCodec = codecRegistry.codecFor(datatype);

      try {
        if (udtValue.isNull(fieldname)) return null;
        return typeCodec.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion);
      } catch (IllegalArgumentException ex) {
        return null;
      }

      // udt collection
    } else if (datatype.isCollection()) {

      // set
      if (DataType.Name.SET == datatype.getName()) {
        return fromUdtValues(
            datatype.getTypeArguments().get(0),
            ImmutableSet.copyOf(udtValue.getSet(fieldname, UDTValue.class)),
            fieldtype2);

        // list
      } else if (DataType.Name.LIST == datatype.getName()) {
        return fromUdtValues(
            datatype.getTypeArguments().get(0),
            ImmutableList.copyOf(udtValue.getList(fieldname, UDTValue.class)),
            fieldtype2);

        // map
      } else {
        if (isBuildInType(datatype.getTypeArguments().get(0))) {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, fieldtype1, UDTValue.class)),
              fieldtype1,
              fieldtype2);

        } else if (isBuildInType(datatype.getTypeArguments().get(1))) {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, UDTValue.class, fieldtype2)),
              fieldtype1,
              fieldtype2);

        } else {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, UDTValue.class, UDTValue.class)),
              fieldtype1,
              fieldtype2);
        }
      }

      // udt
    } else {
      return fromUdtValue(datatype, udtValue, fieldtype1);
    }
  }
/**
 * Tests DataType class to ensure data sent in is the same as data received All tests are executed
 * via a Simple Statements Counters are the only datatype not tested within the entirety of the
 * suite. There is, however, an isolated test case that needs to be implemented. All statements and
 * sample data is easily exportable via the print_*() methods.
 */
public class DataTypeTest extends CCMBridge.PerClassSingleNodeCluster {

  private static final Set<DataType> DATA_TYPE_PRIMITIVES = DataType.allPrimitiveTypes();
  private static final Set<DataType.Name> DATA_TYPE_NON_PRIMITIVE_NAMES =
      EnumSet.of(DataType.Name.MAP, DataType.Name.SET, DataType.Name.LIST);

  private static final String PRIMITIVE_INSERT_FORMAT =
      "INSERT INTO %1$s (k, v) VALUES (%2$s, %2$s);";
  private static final String BASIC_SELECT_FORMAT = "SELECT k, v FROM %1$s;";

  private static final String COLLECTION_INSERT_FORMAT =
      "INSERT INTO %1$s (k, v) VALUES (%2$s, %3$s);";
  private static final String MAP_INSERT_FORMAT =
      "INSERT INTO %1$s (k, v) VALUES (%3$s, {%2$s: %3$s});";

  private static final HashMap<DataType, Object> SAMPLE_DATA = getSampleData();
  private static final HashMap<DataType, Object> SAMPLE_COLLECTIONS = getSampleCollections();

  private static final Collection<String> PRIMITIVE_INSERT_STATEMENTS =
      getPrimitiveInsertStatements();
  private static final HashMap<DataType, String> PRIMITIVE_SELECT_STATEMENTS =
      getPrimitiveSelectStatements();

  private static final Collection<String> COLLECTION_INSERT_STATEMENTS =
      getCollectionInsertStatements();
  private static final HashMap<DataType, String> COLLECTION_SELECT_STATEMENTS =
      getCollectionSelectStatements();

  private static boolean exclude(DataType t) {
    return t.getName() == DataType.Name.COUNTER;
  }

  /** Generates the table definitions that will be used in testing */
  @Override
  protected Collection<String> getTableDefinitions() {
    ArrayList<String> tableDefinitions = new ArrayList<String>();

    // Create primitive data type definitions
    for (DataType dataType : DATA_TYPE_PRIMITIVES) {
      if (exclude(dataType)) continue;

      tableDefinitions.add(
          String.format("CREATE TABLE %1$s (k %2$s PRIMARY KEY, v %1$s)", dataType, dataType));
    }

    // Create collection data type definitions
    for (DataType.Name dataTypeName : DATA_TYPE_NON_PRIMITIVE_NAMES) {
      // Create MAP data type definitions
      if (dataTypeName == DataType.Name.MAP) {
        for (DataType typeArgument1 : DATA_TYPE_PRIMITIVES) {
          if (exclude(typeArgument1)) continue;

          for (DataType typeArgument2 : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument2)) continue;

            tableDefinitions.add(
                String.format(
                    "CREATE TABLE %1$s_%2$s_%3$s (k %3$s PRIMARY KEY, v %1$s<%2$s, %3$s>)",
                    dataTypeName, typeArgument1, typeArgument2));
          }
        }
        // Create SET and LIST data type definitions
      } else {
        for (DataType typeArgument : DATA_TYPE_PRIMITIVES) {
          if (exclude(typeArgument)) continue;

          tableDefinitions.add(
              String.format(
                  "CREATE TABLE %1$s_%2$s (k %2$s PRIMARY KEY, v %1$s<%2$s>)",
                  dataTypeName, typeArgument));
        }
      }
    }

    return tableDefinitions;
  }

  /** Generates the sample data that will be used in testing */
  private static HashMap<DataType, Object> getSampleData() {
    HashMap<DataType, Object> sampleData = new HashMap<DataType, Object>();

    for (DataType dataType : DATA_TYPE_PRIMITIVES) {
      switch (dataType.getName()) {
        case ASCII:
          sampleData.put(dataType, new String("ascii"));
          break;
        case BIGINT:
          sampleData.put(dataType, Long.MAX_VALUE);
          break;
        case BLOB:
          ByteBuffer bb = ByteBuffer.allocate(58);
          bb.putShort((short) 0xCAFE);
          bb.flip();
          sampleData.put(dataType, bb);
          break;
        case BOOLEAN:
          sampleData.put(dataType, Boolean.TRUE);
          break;
        case COUNTER:
          // Not supported in an insert statement
          break;
        case DECIMAL:
          sampleData.put(dataType, new BigDecimal("12.3E+7"));
          break;
        case DOUBLE:
          sampleData.put(dataType, Double.MAX_VALUE);
          break;
        case FLOAT:
          sampleData.put(dataType, Float.MAX_VALUE);
          break;
        case INET:
          try {
            sampleData.put(dataType, InetAddress.getByName("123.123.123.123"));
          } catch (java.net.UnknownHostException e) {
          }
          break;
        case INT:
          sampleData.put(dataType, Integer.MAX_VALUE);
          break;
        case TEXT:
          sampleData.put(dataType, new String("text"));
          break;
        case TIMESTAMP:
          sampleData.put(dataType, new Date(872835240000L));
          break;
        case TIMEUUID:
          sampleData.put(dataType, UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"));
          break;
        case UUID:
          sampleData.put(dataType, UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"));
          break;
        case VARCHAR:
          sampleData.put(dataType, new String("varchar"));
          break;
        case VARINT:
          sampleData.put(dataType, new BigInteger(Integer.toString(Integer.MAX_VALUE) + "000"));
          break;
        default:
          throw new RuntimeException("Missing handling of " + dataType);
      }
    }

    return sampleData;
  }

  /** Generates the sample collections that will be used in testing */
  private static HashMap<DataType, Object> getSampleCollections() {
    HashMap<DataType, Object> sampleCollections = new HashMap<DataType, Object>();
    HashMap<DataType, Object> setAndListCollection;
    HashMap<DataType, HashMap<DataType, Object>> mapCollection;

    for (DataType.Name dataTypeName : DATA_TYPE_NON_PRIMITIVE_NAMES) {
      switch (dataTypeName) {
        case LIST:
          for (DataType typeArgument : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument)) continue;

            List<Object> list = new ArrayList<Object>();
            for (int i = 0; i < 5; i++) {
              list.add(SAMPLE_DATA.get(typeArgument));
            }

            setAndListCollection = new HashMap<DataType, Object>();
            setAndListCollection.put(typeArgument, list);
            sampleCollections.put(DataType.list(typeArgument), setAndListCollection);
          }
          break;
        case SET:
          for (DataType typeArgument : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument)) continue;

            Set<Object> set = new HashSet<Object>();
            for (int i = 0; i < 5; i++) {
              set.add(SAMPLE_DATA.get(typeArgument));
            }

            setAndListCollection = new HashMap<DataType, Object>();
            setAndListCollection.put(typeArgument, set);
            sampleCollections.put(DataType.set(typeArgument), setAndListCollection);
          }
          break;
        case MAP:
          for (DataType typeArgument1 : DATA_TYPE_PRIMITIVES) {
            if (exclude(typeArgument1)) continue;

            for (DataType typeArgument2 : DATA_TYPE_PRIMITIVES) {
              if (exclude(typeArgument2)) continue;

              HashMap<DataType, Object> map = new HashMap<DataType, Object>();
              map.put(typeArgument1, SAMPLE_DATA.get(typeArgument2));

              mapCollection = new HashMap<DataType, HashMap<DataType, Object>>();
              mapCollection.put(typeArgument1, map);
              sampleCollections.put(DataType.map(typeArgument1, typeArgument2), mapCollection);
            }
          }
          break;
        default:
          throw new RuntimeException("Missing handling of " + dataTypeName);
      }
    }

    return sampleCollections;
  }

  /** Helper method to stringify SAMPLE_DATA for simple insert statements */
  private static String helperStringifiedData(DataType dataType) {
    String value = SAMPLE_DATA.get(dataType).toString();

    switch (dataType.getName()) {
      case BLOB:
        value = "0xCAFE";
        break;

      case INET:
        InetAddress v1 = (InetAddress) SAMPLE_DATA.get(dataType);
        value = String.format("'%s'", v1.getHostAddress());
        break;

      case TIMESTAMP:
        Date v2 = (Date) SAMPLE_DATA.get(dataType);
        value = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(v2);
      case ASCII:
      case TEXT:
      case VARCHAR:
        value = String.format("'%s'", value);
        break;

      default:
        break;
    }

    return value;
  }

  /** Generates the insert statements that will be used in testing */
  private static Collection<String> getPrimitiveInsertStatements() {
    ArrayList<String> insertStatements = new ArrayList<String>();

    for (DataType dataType : SAMPLE_DATA.keySet()) {
      String value = helperStringifiedData(dataType);
      insertStatements.add(String.format(PRIMITIVE_INSERT_FORMAT, dataType, value));
    }

    return insertStatements;
  }

  /** Generates the select statements that will be used in testing */
  private static HashMap<DataType, String> getPrimitiveSelectStatements() {
    HashMap<DataType, String> selectStatements = new HashMap<DataType, String>();

    for (DataType dataType : SAMPLE_DATA.keySet()) {
      selectStatements.put(dataType, String.format(BASIC_SELECT_FORMAT, dataType));
    }

    return selectStatements;
  }

  /** Helper method to generate table names in the form of: DataType_TypeArgument[_TypeArgument] */
  private static String helperGenerateTableName(DataType dataType) {
    String tableName = dataType.getName().toString();
    for (DataType typeArgument : dataType.getTypeArguments()) tableName += "_" + typeArgument;

    return tableName;
  }

  /** Generates the insert statements that will be used in testing */
  @SuppressWarnings("unchecked")
  private static Collection<String> getCollectionInsertStatements() {
    ArrayList<String> insertStatements = new ArrayList<String>();

    String tableName;
    String key;
    String value;
    for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) {
      HashMap<DataType, Object> sampleValueMap =
          (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);

      // Create tableName in form of: DataType_TypeArgument[_TypeArgument]
      tableName = helperGenerateTableName(dataType);

      if (dataType.getName() == DataType.Name.MAP) {
        List<DataType> typeArgument = dataType.getTypeArguments();

        key = helperStringifiedData(typeArgument.get(0));
        value = helperStringifiedData(typeArgument.get(1));

        insertStatements.add(String.format(MAP_INSERT_FORMAT, tableName, key, value));
      } else if (dataType.getName() == DataType.Name.LIST) {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        key = helperStringifiedData(typeArgument);

        // Create the value to be a list of the same 5 elements
        value = "[";
        for (int i = 0; i < 5; i++) value += key + ',';
        value = value.substring(0, value.length() - 1) + ']';

        insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value));
      } else {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        key = helperStringifiedData(typeArgument);
        value = '{' + key + '}';

        insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value));
      }
    }

    return insertStatements;
  }

  /** Generates the select statements that will be used in testing */
  private static HashMap<DataType, String> getCollectionSelectStatements() {
    HashMap<DataType, String> selectStatements = new HashMap<DataType, String>();

    String tableName;
    for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) {
      tableName = helperGenerateTableName(dataType);
      selectStatements.put(dataType, String.format(BASIC_SELECT_FORMAT, tableName));
    }

    return selectStatements;
  }

  /** Test simple statement inserts for all primitive data types */
  public void primitiveInsertTest() throws Throwable {
    ResultSet rs;
    for (String execute_string : PRIMITIVE_INSERT_STATEMENTS) {
      rs = session.execute(execute_string);
      assertTrue(rs.isExhausted());
    }
    assertEquals(SAMPLE_DATA.size(), 15);
    assertEquals(PRIMITIVE_INSERT_STATEMENTS.size(), SAMPLE_DATA.size());
  }

  /** Validate simple statement selects for all primitive data types */
  public void primitiveSelectTest() throws Throwable {
    String execute_string;
    Object value;
    Row row;
    for (DataType dataType : PRIMITIVE_SELECT_STATEMENTS.keySet()) {
      execute_string = PRIMITIVE_SELECT_STATEMENTS.get(dataType);
      row = session.execute(execute_string).one();

      value = SAMPLE_DATA.get(dataType);
      assertEquals(TestUtils.getValue(row, "k", dataType), value);
      assertEquals(TestUtils.getValue(row, "v", dataType), value);
    }
    assertEquals(SAMPLE_DATA.size(), 15);
    assertEquals(PRIMITIVE_SELECT_STATEMENTS.keySet().size(), SAMPLE_DATA.size());
  }

  /** Test simple statement inserts and selects for all primitive data types */
  @Test(groups = "long")
  public void primitiveTests() throws Throwable {
    primitiveInsertTest();
    primitiveSelectTest();
  }

  /** Test simple statement inserts for all collection data types */
  public void collectionInsertTest() throws Throwable {
    ResultSet rs;
    for (String execute_string : COLLECTION_INSERT_STATEMENTS) {
      rs = session.execute(execute_string);
      assertTrue(rs.isExhausted());
    }
    assertEquals(SAMPLE_COLLECTIONS.size(), 255);
    assertEquals(COLLECTION_INSERT_STATEMENTS.size(), SAMPLE_COLLECTIONS.size());
  }

  /** Test simple statement selects for all collection data types */
  @SuppressWarnings("unchecked")
  public void collectionSelectTest() throws Throwable {
    HashMap<DataType, Object> sampleValueMap;
    String execute_string;
    DataType typeArgument1;
    DataType typeArgument2;
    Row row;
    for (DataType dataType : COLLECTION_SELECT_STATEMENTS.keySet()) {
      execute_string = COLLECTION_SELECT_STATEMENTS.get(dataType);
      row = session.execute(execute_string).one();

      sampleValueMap = (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);
      typeArgument1 = dataType.getTypeArguments().get(0);
      if (dataType.getName() == DataType.Name.MAP) {
        typeArgument2 = dataType.getTypeArguments().get(1);

        // Create a copy of the map that is being expected
        HashMap<DataType, Object> sampleMap =
            (HashMap<DataType, Object>) sampleValueMap.get(typeArgument1);
        Object mapKey = SAMPLE_DATA.get(sampleMap.keySet().iterator().next());
        Object mapValue = sampleMap.values().iterator().next();
        HashMap<Object, Object> expectedMap = new HashMap<Object, Object>();
        expectedMap.put(mapKey, mapValue);

        assertEquals(TestUtils.getValue(row, "k", typeArgument2), SAMPLE_DATA.get(typeArgument2));
        assertEquals(TestUtils.getValue(row, "v", dataType), expectedMap);
      } else {
        Object expectedValue = sampleValueMap.get(typeArgument1);

        assertEquals(TestUtils.getValue(row, "k", typeArgument1), SAMPLE_DATA.get(typeArgument1));
        assertEquals(TestUtils.getValue(row, "v", dataType), expectedValue);
      }
    }
    assertEquals(SAMPLE_COLLECTIONS.size(), 255);
    assertEquals(COLLECTION_SELECT_STATEMENTS.keySet().size(), SAMPLE_COLLECTIONS.size());
  }

  /** Test simple statement inserts and selects for all collection data types */
  @Test(groups = "long")
  public void collectionTest() throws Throwable {
    collectionInsertTest();
    collectionSelectTest();
  }

  // The two following tests a really unit tests, but since the whole uses
  // CCMBridge.PerClassSingleNodeCluster, they'll still spawn a cluster even
  // you execute only them, so we keep them in the "long" group. We could
  // move them in another class but not sure where honestly (one could argue
  // that it would make more sense to move all the *other* tests to some
  // DataTypeIntegrationTest class).
  @Test(groups = "long")
  public void serializeDeserializeTest() {

    for (DataType dt : DataType.allPrimitiveTypes()) {
      if (exclude(dt)) continue;

      Object value = TestUtils.getFixedValue(dt);
      assertEquals(dt.deserialize(dt.serialize(value)), value);
    }

    try {
      DataType.bigint().serialize(4);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }

    try {
      ByteBuffer badValue = ByteBuffer.allocate(4);
      DataType.bigint().deserialize(badValue);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }

  @Test(groups = "long")
  public void serializeDeserializeCollectionsTest() {

    List<String> l = Arrays.asList("foo", "bar");

    DataType dt = DataType.list(DataType.text());
    assertEquals(dt.deserialize(dt.serialize(l)), l);

    try {
      DataType.list(DataType.bigint()).serialize(l);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }

  /** Prints the table definitions that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printTableDefinitions() {
    String objective = "Table Definitions";
    System.out.println(String.format("Printing %s...", objective));

    // Prints the full list of table definitions
    for (String definition : getTableDefinitions()) {
      System.out.println(definition);
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the sample data that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printSampleData() {
    String objective = "Sample Data";
    System.out.println(String.format("Printing %s...", objective));

    for (DataType dataType : SAMPLE_DATA.keySet()) {
      Object sampleValue = SAMPLE_DATA.get(dataType);
      System.out.println(String.format("%1$-10s %2$s", dataType, sampleValue));
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the sample collections that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  @SuppressWarnings("unchecked")
  public void printSampleCollections() {
    String objective = "Sample Collections";
    System.out.println(String.format("Printing %s...", objective));

    for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) {
      HashMap<DataType, Object> sampleValueMap =
          (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType);

      if (dataType.getName() == DataType.Name.MAP) {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        HashMap<DataType, Object> sampleMap =
            (HashMap<DataType, Object>) sampleValueMap.get(typeArgument);

        Object mapKey = SAMPLE_DATA.get(typeArgument);
        Object mapValue = sampleMap.get(typeArgument);
        System.out.println(String.format("%1$-30s {%2$s : %3$s}", dataType, mapKey, mapValue));
      } else {
        DataType typeArgument = sampleValueMap.keySet().iterator().next();
        Object sampleValue = sampleValueMap.get(typeArgument);

        System.out.println(String.format("%1$-30s %2$s", dataType, sampleValue));
      }
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the simple insert statements that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printPrimitiveInsertStatements() {
    String objective = "Primitive Insert Statements";
    System.out.println(String.format("Printing %s...", objective));

    for (String execute_string : PRIMITIVE_INSERT_STATEMENTS) {
      System.out.println(execute_string);
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the simple select statements that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printPrimitiveSelectStatements() {
    String objective = "Primitive Select Statements";
    System.out.println(String.format("Printing %s...", objective));

    for (String execute_string : PRIMITIVE_SELECT_STATEMENTS.values()) {
      System.out.println(execute_string);
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the simple insert statements that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printCollectionInsertStatements() {
    String objective = "Collection Insert Statements";
    System.out.println(String.format("Printing %s...", objective));

    for (String execute_string : COLLECTION_INSERT_STATEMENTS) {
      System.out.println(execute_string);
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }

  /** Prints the simple insert statements that will be used in testing (for exporting purposes) */
  @Test(groups = "doc")
  public void printCollectionSelectStatements() {
    String objective = "Collection Select Statements";
    System.out.println(String.format("Printing %s...", objective));

    for (String execute_string : COLLECTION_SELECT_STATEMENTS.values()) {
      System.out.println(execute_string);
    }

    System.out.println(String.format("\nEnd of %s\n\n", objective));
  }
}
Example #23
0
  @SuppressWarnings("unchecked")
  public Object toUdtValue(
      Tablename tablename, MetadataCatalog catalog, DataType datatype, Object value) {

    // build-in type (will not be converted)
    if (isBuildInType(datatype)) {
      return value;

      // udt collection
    } else if (datatype.isCollection()) {

      // set
      if (DataType.Name.SET == datatype.getName()) {
        final DataType elementDataType = datatype.getTypeArguments().get(0);

        final Set<Object> udt = Sets.newHashSet();
        if (value != null) {
          for (Object element : (Set<Object>) value) {
            udt.add(toUdtValue(tablename, catalog, elementDataType, element));
          }
        }

        return ImmutableSet.copyOf(udt);

        // list
      } else if (DataType.Name.LIST == datatype.getName()) {
        final DataType elementDataType = datatype.getTypeArguments().get(0);

        final List<Object> udt = Lists.newArrayList();
        if (value != null) {
          for (Object element : (List<Object>) value) {
            udt.add(toUdtValue(tablename, catalog, elementDataType, element));
          }
        }

        return ImmutableList.copyOf(udt);

        // map
      } else {
        final DataType keyDataType = datatype.getTypeArguments().get(0);
        final DataType valueDataType = datatype.getTypeArguments().get(1);

        final Map<Object, Object> udt = Maps.newHashMap();
        if (value != null) {
          for (Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
            udt.put(
                toUdtValue(tablename, catalog, keyDataType, entry.getKey()),
                toUdtValue(tablename, catalog, valueDataType, entry.getValue()));
          }
        }
        return ImmutableMap.copyOf(udt);
      }

      // udt
    } else {
      if (value == null) {
        return value;

      } else {
        final UserType usertype =
            catalog.getUserType(tablename, ((UserType) datatype).getTypeName());
        final UDTValue udtValue = usertype.newValue();

        for (Entry<String, Optional<Object>> entry :
            beanMapper.toValues(value, ImmutableSet.<String>of()).entrySet()) {
          if (!entry.getValue().isPresent()) {
            // return null;
            udtValue.setToNull(entry.getKey());
            continue;
          }

          final DataType fieldType = usertype.getFieldType(entry.getKey());
          Object vl = entry.getValue().get();

          if (!isBuildInType(usertype.getFieldType(entry.getKey()))) {
            vl = toUdtValue(tablename, catalog, fieldType, vl);
          }

          final String key = entry.getKey();
          udtValue.setBytesUnsafe(key, serialize(fieldType, vl));
        }

        return udtValue;
      }
    }
  }
Example #24
0
  private Object getValue(Row row, ColumnMapping mapping, Definition def) {
    Object value = null;

    if (DataType.text().equals(mapping.type)) {
      value = row.getString(def.getName());

    } else if (DataType.blob().equals(mapping.type)) {
      value = row.getBytes(def.getName());

    } else if (DataType.map(DataType.text(), DataType.text()).equals(mapping.type)) {
      value = row.getMap(def.getName(), String.class, String.class);

    } else if (DataType.cint().equals(mapping.type)) {
      value = row.getInt(def.getName());
    } else if (DataType.bigint().equals(mapping.type)) {
      value = row.getLong(def.getName());
    } else if (DataType.timestamp().equals(mapping.type)) {
      value = row.getDate(def.getName());
    } else if (DataType.blob().equals(mapping.type)) {
      value = row.getBytes(def.getName());
    } else if (DataType.cdouble().equals(mapping.type)) {
      value = row.getBytes(def.getName());
    } else if (DataType.cfloat().equals(mapping.type)) {
      value = row.getFloat(def.getName());
    } else if (DataType.inet().equals(mapping.type)) {
      value = row.getInet(def.getName());
    } else if (DataType.cboolean().equals(mapping.type)) {
      value = row.getBool(def.getName());
    } else if (DataType.uuid().equals(mapping.type)) {
      value = row.getUUID(def.getName());
    }
    // what if value is null? primitives won't like this
    return value;
  }
Example #25
0
  protected void discoverBeanMethods() {
    // break this off?
    Class<?> ft = field.getType();
    if (String.class.equals(ft)) {
      type = DataType.text();
    } else if (int.class.equals(ft)) {
      type = DataType.cint();
    } else if (Integer.class.equals(ft)) {
      type = DataType.cint();
    } else if (int.class.equals(ft)) {
      type = DataType.cint();
    } else if (Long.class.equals(ft)) {
      type = DataType.bigint();
    } else if (long.class.equals(ft)) {
      type = DataType.bigint();
    } else if (Date.class.equals(ft)) {
      type = DataType.timestamp();
    } else if (ByteBuffer.class.equals(ft)) {
      type = DataType.blob();
    } else if (Double.class.equals(ft)) {
      type = DataType.cdouble();
    } else if (double.class.equals(ft)) {
      type = DataType.cdouble();
    } else if (Float.class.equals(ft)) {
      type = DataType.cfloat();
    } else if (float.class.equals(ft)) {
      type = DataType.cfloat();
    } else if (Boolean.class.equals(ft)) {
      type = DataType.cboolean();
    }
    // how to differentiate type tuuid and uuid
    else if (UUID.class.equals(ft)) {
      type = DataType.uuid();
    }
    // java.sql.timestamp is not directly supported
    // remove it for now
    /*else if (Timestamp.class.equals(ft)) {
    	type = DataType.timestamp();
    }*/ else if (Map.class.isAssignableFrom(ft)) {
      type = DataType.map(DataType.text(), DataType.text());
    }

    // getter and setter find
    try {
      String getMeth =
          "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
      String setMeth =
          "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
      getter = field.getDeclaringClass().getDeclaredMethod(getMeth);
      setter = field.getDeclaringClass().getDeclaredMethod(setMeth, field.getType());
    } catch (Exception e) {
      // TODO some useful stuff: warning of non-conformance to java bean conventions
    }
  }
 private static boolean exclude(DataType t) {
   return t.getName() == DataType.Name.COUNTER;
 }
  @SuppressWarnings("unchecked")
  @Override
  public <P> Getter<GettableByIndexData, P> newGetter(
      Type target, DatastaxColumnKey key, ColumnDefinition<?, ?> columnDefinition) {
    Class<?> targetClass = TypeHelper.toClass(target);
    if (Date.class.equals(targetClass)) {
      return (Getter<GettableByIndexData, P>) new DatastaxDateGetter(key.getIndex());
    }

    if (boolean.class.equals(targetClass) || Boolean.class.equals(targetClass)) {
      return (Getter<GettableByIndexData, P>) new DatastaxBooleanGetter(key.getIndex());
    }

    if (InetAddress.class.equals(targetClass)) {
      return (Getter<GettableByIndexData, P>) new DatastaxInetAddressGetter(key.getIndex());
    }

    if (TupleValue.class.equals(targetClass)) {
      return (Getter<GettableByIndexData, P>) new DatastaxTupleValueGetter(key.getIndex());
    }

    if (Collection.class.isAssignableFrom(targetClass)) {

      Type elementType = TypeHelper.getComponentTypeOfListOrArray(target);
      Class<?> dataTypeClass = Object.class;
      Class<?> dataTypeElt = null;
      DataType dtElt = null;
      if (key.getDataType() != null) {
        DataType dataType = key.getDataType();
        dataTypeClass = dataType.asJavaClass();
        if (dataType.isCollection()) {
          dtElt = key.getDataType().getTypeArguments().get(0);
          dataTypeElt = dtElt.asJavaClass();
        }
      } else {
        dataTypeElt = TypeHelper.toClass(elementType);
      }

      if (dataTypeElt != null) {
        if (TypeHelper.areEquals(elementType, dataTypeElt)) {
          if (Set.class.equals(dataTypeClass)) {
            if (targetClass.isAssignableFrom(dataTypeClass)) {
              return new DatastaxSetGetter(key.getIndex(), TypeHelper.toClass(elementType));
            }
          }
          if (List.class.equals(dataTypeClass)) {
            if (targetClass.isAssignableFrom(dataTypeClass)) {
              return new DatastaxListGetter(key.getIndex(), TypeHelper.toClass(elementType));
            }
          }
        } else {
          Converter<?, ?> converter = getConverter(elementType, dataTypeElt, dtElt);

          if (converter != null) {
            if (Set.class.equals(dataTypeClass)) {
              if (targetClass.isAssignableFrom(dataTypeClass)) {
                return new DatastaxSetWithConverterGetter(key.getIndex(), dataTypeElt, converter);
              }
            }
            if (List.class.equals(dataTypeClass)) {
              if (targetClass.isAssignableFrom(dataTypeClass)) {
                return new DatastaxListWithConverterGetter(key.getIndex(), dataTypeElt, converter);
              }
            }
          }
        }
      }
    }
    if (Map.class.equals(targetClass)) {
      Tuple2<Type, Type> keyValueTypeOfMap = TypeHelper.getKeyValueTypeOfMap(target);

      Class<?> dtKeyType = null;
      Class<?> dtValueType = null;
      DataType dtKey = null;
      DataType dtValue = null;
      if (key.getDataType() != null) {
        List<DataType> typeArguments = key.getDataType().getTypeArguments();
        if (typeArguments.size() == 2) {
          dtKey = typeArguments.get(0);
          dtKeyType = dtKey.asJavaClass();
          dtValue = typeArguments.get(1);
          dtValueType = dtValue.asJavaClass();
        }
      } else {
        dtKeyType = TypeHelper.toClass(keyValueTypeOfMap.first());
        dtValueType = TypeHelper.toClass(keyValueTypeOfMap.second());
      }
      if (dtKeyType != null && dtValueType != null) {
        if (TypeHelper.areEquals(keyValueTypeOfMap.first(), dtKeyType)
            && TypeHelper.areEquals(keyValueTypeOfMap.second(), dtValueType)) {
          return new DatastaxMapGetter(
              key.getIndex(),
              TypeHelper.toClass(keyValueTypeOfMap.first()),
              TypeHelper.toClass(keyValueTypeOfMap.second()));
        } else {
          Converter<?, ?> keyConverter = getConverter(keyValueTypeOfMap.first(), dtKeyType, dtKey);
          Converter<?, ?> valueConverter =
              getConverter(keyValueTypeOfMap.second(), dtValueType, dtValue);

          if (keyConverter != null && valueConverter != null) {
            return new DatastaxMapWithConverterGetter(
                key.getIndex(), dtKeyType, dtValueType, keyConverter, valueConverter);
          }
        }
      }
    }

    if (Tuples.isTuple(target)) {
      if (key.getDataType() != null && key.getDataType() instanceof TupleType) {
        TupleType tt = (TupleType) key.getDataType();

        List<DataType> typeArguments = tt.getTypeArguments();

        TypeVariable<? extends Class<?>>[] typeParameters = targetClass.getTypeParameters();

        if (typeArguments.size() <= typeParameters.length) {
          return (Getter<GettableByIndexData, P>)
              DatastaxTupleGetter.newInstance(datastaxMapperFactory, target, tt, key.getIndex());
        }
      }
    }

    if (TypeHelper.isEnum(target)) {
      final Getter<GettableByIndexData, ? extends Enum> getter =
          enumGetter(key, TypeHelper.toClass(target));
      if (getter != null) {
        return (Getter<GettableByIndexData, P>) getter;
      }
    }

    final GetterFactory<GettableByIndexData, DatastaxColumnKey> rowGetterFactory =
        getterFactories.get(targetClass);

    if (rowGetterFactory != null) {
      return rowGetterFactory.newGetter(target, key, columnDefinition);
    }

    final Getter<GettableByIndexData, P> getter =
        jodaTimeGetterFactory.newGetter(target, key, columnDefinition);

    if (getter != null) {
      return getter;
    }

    if (key.getDataType() != null && key.getDataType() instanceof UserType) {
      UserType ut = (UserType) key.getDataType();
      return (Getter<GettableByIndexData, P>)
          DatastaxUDTGetter.newInstance(datastaxMapperFactory, target, ut, key.getIndex());
    }

    return null;
  }
  /** Generates the sample data that will be used in testing */
  private static HashMap<DataType, Object> getSampleData() {
    HashMap<DataType, Object> sampleData = new HashMap<DataType, Object>();

    for (DataType dataType : DATA_TYPE_PRIMITIVES) {
      switch (dataType.getName()) {
        case ASCII:
          sampleData.put(dataType, new String("ascii"));
          break;
        case BIGINT:
          sampleData.put(dataType, Long.MAX_VALUE);
          break;
        case BLOB:
          ByteBuffer bb = ByteBuffer.allocate(58);
          bb.putShort((short) 0xCAFE);
          bb.flip();
          sampleData.put(dataType, bb);
          break;
        case BOOLEAN:
          sampleData.put(dataType, Boolean.TRUE);
          break;
        case COUNTER:
          // Not supported in an insert statement
          break;
        case DECIMAL:
          sampleData.put(dataType, new BigDecimal("12.3E+7"));
          break;
        case DOUBLE:
          sampleData.put(dataType, Double.MAX_VALUE);
          break;
        case FLOAT:
          sampleData.put(dataType, Float.MAX_VALUE);
          break;
        case INET:
          try {
            sampleData.put(dataType, InetAddress.getByName("123.123.123.123"));
          } catch (java.net.UnknownHostException e) {
          }
          break;
        case INT:
          sampleData.put(dataType, Integer.MAX_VALUE);
          break;
        case TEXT:
          sampleData.put(dataType, new String("text"));
          break;
        case TIMESTAMP:
          sampleData.put(dataType, new Date(872835240000L));
          break;
        case TIMEUUID:
          sampleData.put(dataType, UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"));
          break;
        case UUID:
          sampleData.put(dataType, UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"));
          break;
        case VARCHAR:
          sampleData.put(dataType, new String("varchar"));
          break;
        case VARINT:
          sampleData.put(dataType, new BigInteger(Integer.toString(Integer.MAX_VALUE) + "000"));
          break;
        default:
          throw new RuntimeException("Missing handling of " + dataType);
      }
    }

    return sampleData;
  }