org.apache.cassandra.db.marshal.AsciiType Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.AsciiType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap() {
    // map from ASCII to Double
    // test non-frozen
    DataType mapType = DataType.map(DataType.ascii(), DataType.cdouble());
    AbstractType<?> convertedType = CassandraTypeConverter.convert(mapType);

    MapType<?, ?> expectedType = MapType.getInstance(AsciiType.instance, DoubleType.instance, true);
    Assert.assertEquals(expectedType, convertedType);

    // test frozen
    mapType = DataType.map(DataType.ascii(), DataType.cdouble(), true);
    convertedType = CassandraTypeConverter.convert(mapType);
    expectedType = MapType.getInstance(AsciiType.instance, DoubleType.instance, false);
    Assert.assertEquals(expectedType, convertedType);
    Assert.assertTrue("Expected convertType to be frozen", convertedType.isFrozenCollection());
}
 
Example #2
Source File: SSTableExportTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsciiKeyValidator() throws IOException, ParseException
{
    File tempSS = tempSSTableFile("Keyspace1", "AsciiKeys");
    ColumnFamily cfamily = ArrayBackedSortedColumns.factory.create("Keyspace1", "AsciiKeys");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2, ActiveRepairService.UNREPAIRED_SSTABLE);

    // Add a row
    cfamily.addColumn(column("column", "value", 1L));
    writer.append(Util.dk("key", AsciiType.instance), cfamily);

    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("CFWithAsciiKeys", ".json");
    SSTableExport.export(reader,
                         new PrintStream(tempJson.getPath()),
                         new String[0],
                         CFMetaData.sparseCFMetaData("Keyspace1", "AsciiKeys", BytesType.instance));

    JSONArray json = (JSONArray)JSONValue.parseWithException(new FileReader(tempJson));
    assertEquals(1, json.size());

    JSONObject row = (JSONObject)json.get(0);
    // check row key
    assertEquals("key", row.get("key"));
}
 
Example #3
Source File: Term.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the typed value, serialized to a ByteBuffer.
 *
 * @return a ByteBuffer of the value.
 * @throws InvalidRequestException if unable to coerce the string to its type.
 */
public ByteBuffer getByteBuffer() throws InvalidRequestException
{
    switch (type)
    {
        case STRING:
            return AsciiType.instance.fromString(text);
        case INTEGER:
            return IntegerType.instance.fromString(text);
        case UUID:
            // we specifically want the Lexical class here, not "UUIDType," because we're supposed to have
            // a uuid-shaped string here, and UUIDType also accepts integer or date strings (and turns them into version 1 uuids).
            return LexicalUUIDType.instance.fromString(text);
        case FLOAT:
          return FloatType.instance.fromString(text);
    }

    // FIXME: handle scenario that should never happen
    return null;
}
 
Example #4
Source File: SSTableWriter.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialization. Creates target directory if needed and establishes 
 * the writer
 * 
 * @throws Exception if a problem occurs
 */
public void init() throws Exception {
  File directory = new File(this.directory);

  if (!directory.exists()) {
    directory.mkdir();
  }
  try {
  	//TODO set parameter for null
    writer = new SSTableSimpleUnsortedWriter(directory, null, keyspace,
        columnFamily, AsciiType.instance, null, bufferSize);
  } catch (Throwable t) {
    throw new KettleException(
        "Failed to create SSTableSimpleUnsortedWriter", t);
  }
}
 
Example #5
Source File: BlobPlacementFactory.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Placement newPlacement(String placement) throws ConnectionException {
    String[] parsed = PlacementUtil.parsePlacement(placement);
    String keyspaceName = parsed[0];
    String cfPrefix = parsed[1];

    CassandraKeyspace keyspace = _keyspaceMap.get(keyspaceName);
    if (keyspace == null) {
        throw new UnknownPlacementException(format(
                "Placement string refers to unknown or non-local Cassandra keyspace: %s", keyspaceName), placement);
    }

    KeyspaceDefinition keyspaceDef = keyspace.getAstyanaxKeyspace().describeKeyspace();
    ColumnFamily<ByteBuffer,Composite> columnFamily = getColumnFamily(keyspaceDef, cfPrefix, "blob", placement,
            new SpecificCompositeSerializer(CompositeType.getInstance(Arrays.<AbstractType<?>>asList(
                    AsciiType.instance, IntegerType.instance))));

    return new BlobPlacement(placement, keyspace, columnFamily);
}
 
Example #6
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleType() {
    List<AbstractType<?>> innerAbstractTypes = new ArrayList<>(2);
    innerAbstractTypes.add(AsciiType.instance);
    innerAbstractTypes.add(ShortType.instance);
    TupleType tupleType = new TupleType(innerAbstractTypes);

    String sourceTupleString = "foo:1";
    ByteBuffer serializedTuple = tupleType.fromString(sourceTupleString);

    Object deserializedTuple = CassandraTypeDeserializer.deserialize(tupleType, serializedTuple);
    Schema tupleSchema = CassandraTypeDeserializer.getSchemaBuilder(tupleType).build();
    Struct expectedTuple = new Struct(tupleSchema)
            .put("field1", "foo")
            .put("field2", (short) 1);

    Assert.assertEquals(expectedTuple, deserializedTuple);
}
 
Example #7
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapType() {
    Map<String, Double> expectedMap = new HashMap<>();
    expectedMap.put("foo", 1D);
    expectedMap.put("bar", 50D);

    // non-frozen
    MapType<String, Double> nonFrozenMapType = MapType.getInstance(AsciiType.instance, DoubleType.instance, true);
    ByteBuffer serializedMap = nonFrozenMapType.decompose(expectedMap);
    Object deserializedMap = CassandraTypeDeserializer.deserialize(nonFrozenMapType, serializedMap);
    Assert.assertEquals(expectedMap, deserializedMap);

    // frozen
    MapType<String, Double> frozenMapType = MapType.getInstance(AsciiType.instance, DoubleType.instance, false);
    serializedMap = frozenMapType.decompose(expectedMap);
    deserializedMap = CassandraTypeDeserializer.deserialize(frozenMapType, serializedMap);
    Assert.assertEquals(expectedMap, deserializedMap);
}
 
Example #8
Source File: View.java    From sasi with Apache License 2.0 5 votes vote down vote up
public View(ColumnIndex index, AbstractType<?> keyValidator,
            Collection<SSTableIndex> currentView,
            Collection<SSTableReader> oldSSTables,
            Set<SSTableIndex> newIndexes)
{
    Map<Descriptor, SSTableIndex> newView = new HashMap<>();

    AbstractType<?> validator = index.getValidator();
    TermTree.Builder termTreeBuilder = (validator instanceof AsciiType || validator instanceof UTF8Type)
                                        ? new PrefixTermTree.Builder(index.getMode().mode, validator)
                                        : new RangeTermTree.Builder(index.getMode().mode, validator);

    List<Interval<ByteBuffer, SSTableIndex>> keyIntervals = new ArrayList<>();
    for (SSTableIndex sstableIndex : Iterables.concat(currentView, newIndexes))
    {
        SSTableReader sstable = sstableIndex.getSSTable();
        if (oldSSTables.contains(sstable) || sstable.isMarkedCompacted() || newView.containsKey(sstable.descriptor))
        {
            sstableIndex.release();
            continue;
        }

        newView.put(sstable.descriptor, sstableIndex);

        termTreeBuilder.add(sstableIndex);
        keyIntervals.add(Interval.create(sstableIndex.minKey(), sstableIndex.maxKey(), sstableIndex));
    }

    this.view = newView;
    this.termTree = termTreeBuilder.build();
    this.keyIntervalTree = IntervalTree.build(keyIntervals, keyValidator);

    if (keyIntervalTree.intervalCount() != termTree.intervalCount())
        throw new IllegalStateException(String.format("mismatched sizes for intervals tree for keys vs terms: %d != %d", keyIntervalTree.intervalCount(), termTree.intervalCount()));
}
 
Example #9
Source File: GeoShapeMapper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new {@link GeoShapeMapper}.
 */
@JsonCreator
public GeoShapeMapper(@JsonProperty("max_levels") Integer maxLevels) {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance});
    this.maxLevels = maxLevels == null ? DEFAULT_MAX_LEVELS : maxLevels;
    this.grid = new GeohashPrefixTree(spatialContext, this.maxLevels);
}
 
Example #10
Source File: CFMetaDataTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testThriftConversion() throws Exception
{
    CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName())
                             .setComment("Test comment")
                             .setColumn_metadata(columnDefs)
                             .setKeyspace(KEYSPACE)
                             .setName(COLUMN_FAMILY);

    // convert Thrift to CFMetaData
    CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef);

    CfDef thriftCfDef = new CfDef();
    thriftCfDef.keyspace = KEYSPACE;
    thriftCfDef.name = COLUMN_FAMILY;
    thriftCfDef.default_validation_class = cfDef.default_validation_class;
    thriftCfDef.comment = cfDef.comment;
    thriftCfDef.column_metadata = new ArrayList<ColumnDef>();
    for (ColumnDef columnDef : columnDefs)
    {
        ColumnDef c = new ColumnDef();
        c.name = ByteBufferUtil.clone(columnDef.name);
        c.validation_class = columnDef.getValidation_class();
        c.index_name = columnDef.getIndex_name();
        c.index_type = IndexType.KEYS;
        thriftCfDef.column_metadata.add(c);
    }

    CfDef converted = cfMetaData.toThrift();

    assertEquals(thriftCfDef.keyspace, converted.keyspace);
    assertEquals(thriftCfDef.name, converted.name);
    assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class);
    assertEquals(thriftCfDef.comment, converted.comment);
    assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata));
}
 
Example #11
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Test
public void testAscii() {
    DataType asciiType = DataType.ascii();
    AbstractType<?> convertedType = CassandraTypeConverter.convert(asciiType);

    AsciiType expectedType = AsciiType.instance;

    Assert.assertEquals(expectedType, convertedType);
}
 
Example #12
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Test
public void testUserType() {
    // this is slightly complicated, so we're testing in two parts:
    // first, explicitly test for schema correctness
    ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
    List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("durationField".getBytes(Charset.defaultCharset()))));
    // testing duration to make sure that recursive deserialization works correctly
    List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
    expectedFieldTypes.add(AsciiType.instance);
    expectedFieldTypes.add(DoubleType.instance);
    expectedFieldTypes.add(DurationType.instance);
    UserType userType = new UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            true);

    Schema userSchema = CassandraTypeDeserializer.getSchemaBuilder(userType).build();

    long expectedNanoDuration = (30 + 2) * ChronoUnit.DAYS.getDuration().toNanos() + 3;

    Struct expectedUserTypeData = new Struct(userSchema)
            .put("asciiField", "foobar")
            .put("doubleField", 1.5d)
            .put("durationField", expectedNanoDuration);

    Map<String, Object> jsonObject = new HashMap<>(3);
    jsonObject.put("\"asciiField\"", "foobar");
    jsonObject.put("\"doubleField\"", 1.5d);
    jsonObject.put("\"durationField\"", DurationType.instance.getSerializer().toString(Duration.newInstance(1, 2, 3)));
    Term userTypeObject = userType.fromJSONObject(jsonObject);

    ByteBuffer buffer = userTypeObject.bindAndGet(QueryOptions.DEFAULT);

    ByteBuffer serializedUserTypeObject = userType.decompose(buffer);

    Object deserializedUserTypeObject = CassandraTypeDeserializer.deserialize(userType, serializedUserTypeObject);

    Assert.assertEquals(expectedUserTypeData, deserializedUserTypeObject);
}
 
Example #13
Source File: ColumnIndex.java    From sasi with Apache License 2.0 4 votes vote down vote up
public boolean isLiteral()
{
    AbstractType<?> validator = getValidator();
    return isIndexed() ? mode.isLiteral : (validator instanceof UTF8Type || validator instanceof AsciiType);
}
 
Example #14
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Test
public void testUdt() {
    // we can't create a UserType directly, and there isn't really a good way to make one some other way, so...
    // mock it!
    UserType userType = Mockito.mock(UserType.class);
    Mockito.when(userType.getName()).thenReturn(DataType.Name.UDT);
    Mockito.when(userType.getTypeName()).thenReturn("FooType");
    Mockito.when(userType.getKeyspace()).thenReturn("barspace");
    List<String> fieldNames = new ArrayList<>();
    fieldNames.add("asciiField");
    fieldNames.add("doubleField");
    Mockito.when(userType.getFieldNames()).thenReturn(fieldNames);
    Mockito.when(userType.getFieldType("asciiField")).thenReturn(DataType.ascii());
    Mockito.when(userType.getFieldType("doubleField")).thenReturn(DataType.cdouble());
    Mockito.when(userType.isFrozen()).thenReturn(false, true); // cheaty way to test non-frozen and then frozen path.

    ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
    List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
    List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
    expectedFieldTypes.add(AsciiType.instance);
    expectedFieldTypes.add(DoubleType.instance);

    // non-frozen
    org.apache.cassandra.db.marshal.UserType expectedAbstractType = new org.apache.cassandra.db.marshal.UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            true);
    AbstractType<?> convertedType = CassandraTypeConverter.convert(userType);
    Assert.assertEquals(expectedAbstractType, convertedType);

    // frozen
    expectedAbstractType = new org.apache.cassandra.db.marshal.UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            false);
    convertedType = CassandraTypeConverter.convert(userType);
    Assert.assertEquals(expectedAbstractType, convertedType);
}
 
Example #15
Source File: ColumnMapperBlob.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link ColumnMapperBlob}.
 */
@JsonCreator
public ColumnMapperBlob() {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance, BytesType.instance}, new AbstractType[]{});
}
 
Example #16
Source File: ColumnMapperInet.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link ColumnMapperInet}.
 */
@JsonCreator
public ColumnMapperInet() {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance, InetAddressType.instance},
          new AbstractType[]{});
}
 
Example #17
Source File: ColumnMapperBoolean.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link ColumnMapperBlob}.
 */
@JsonCreator
public ColumnMapperBoolean() {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance, BooleanType.instance}, new AbstractType[]{});
}
 
Example #18
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 3 votes vote down vote up
@Test
public void testAscii() {
    String expectedAscii = "some text";

    ByteBuffer serializedAscii = AsciiType.instance.decompose(expectedAscii);

    Object deserializedAscii = CassandraTypeDeserializer.deserialize(AsciiType.instance, serializedAscii);

    Assert.assertEquals("some text", deserializedAscii);
}