Search in sources :

Example 1 with IntArrayCodec

use of com.datastax.driver.extras.codecs.arrays.IntArrayCodec in project gora by apache.

the class CassandraClient method registerOptionalCodecs.

private void registerOptionalCodecs() {
    // Optional Codecs for natives
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.ascii()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.bigint()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.blob()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.cboolean()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.cdouble()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.cfloat()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.cint()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.counter()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.date()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.decimal()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.inet()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.smallInt()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.time()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.timestamp()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.timeUUID()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.tinyInt()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.varint()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.varchar()));
    this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.uuid()));
    // Optional Array Codecs
    this.cluster.getConfiguration().getCodecRegistry().register(new IntArrayCodec());
    this.cluster.getConfiguration().getCodecRegistry().register(new DoubleArrayCodec());
    this.cluster.getConfiguration().getCodecRegistry().register(new FloatArrayCodec());
    this.cluster.getConfiguration().getCodecRegistry().register(new LongArrayCodec());
    this.cluster.getConfiguration().getCodecRegistry().register(new ObjectArrayCodec<>(DataType.list(DataType.varchar()), String[].class, TypeCodec.varchar()));
    // Optional Time Codecs
    this.cluster.getConfiguration().getCodecRegistry().register(new SimpleDateCodec());
    this.cluster.getConfiguration().getCodecRegistry().register(new SimpleTimestampCodec());
    for (Field field : this.mapping.getFieldList()) {
        String columnType = field.getType().toLowerCase(Locale.ENGLISH);
        // http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cql_data_types_c.html
        if (columnType.contains("list")) {
            columnType = columnType.substring(columnType.indexOf("<") + 1, columnType.indexOf(">"));
            this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.list(getTypeCodec(columnType))));
        } else if (columnType.contains("set")) {
            columnType = columnType.substring(columnType.indexOf("<") + 1, columnType.indexOf(">"));
            this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.set(getTypeCodec(columnType))));
        } else if (columnType.contains("map")) {
            String[] columnTypes = columnType.substring(columnType.indexOf("<") + 1, columnType.indexOf(">")).split(",");
            this.cluster.getConfiguration().getCodecRegistry().register(new OptionalCodec<>(TypeCodec.map(TypeCodec.set(getTypeCodec(columnTypes[0])), TypeCodec.set(getTypeCodec(columnTypes[1])))));
        }
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field) SimpleTimestampCodec(com.datastax.driver.extras.codecs.date.SimpleTimestampCodec) SimpleDateCodec(com.datastax.driver.extras.codecs.date.SimpleDateCodec) OptionalCodec(com.datastax.driver.extras.codecs.jdk8.OptionalCodec) FloatArrayCodec(com.datastax.driver.extras.codecs.arrays.FloatArrayCodec) LongArrayCodec(com.datastax.driver.extras.codecs.arrays.LongArrayCodec) DoubleArrayCodec(com.datastax.driver.extras.codecs.arrays.DoubleArrayCodec) IntArrayCodec(com.datastax.driver.extras.codecs.arrays.IntArrayCodec)

Aggregations

DoubleArrayCodec (com.datastax.driver.extras.codecs.arrays.DoubleArrayCodec)1 FloatArrayCodec (com.datastax.driver.extras.codecs.arrays.FloatArrayCodec)1 IntArrayCodec (com.datastax.driver.extras.codecs.arrays.IntArrayCodec)1 LongArrayCodec (com.datastax.driver.extras.codecs.arrays.LongArrayCodec)1 SimpleDateCodec (com.datastax.driver.extras.codecs.date.SimpleDateCodec)1 SimpleTimestampCodec (com.datastax.driver.extras.codecs.date.SimpleTimestampCodec)1 OptionalCodec (com.datastax.driver.extras.codecs.jdk8.OptionalCodec)1 Field (org.apache.gora.cassandra.bean.Field)1