org.apache.cassandra.utils.ByteBufferUtil Java Examples

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil. 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: PagingState.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static PagingState deserialize(ByteBuffer bytes)
{
    if (bytes == null)
        return null;

    try
    {
        DataInputStream in = new DataInputStream(ByteBufferUtil.inputStream(bytes));
        ByteBuffer pk = ByteBufferUtil.readWithShortLength(in);
        ByteBuffer cn = ByteBufferUtil.readWithShortLength(in);
        int remaining = in.readInt();
        return new PagingState(pk, cn, remaining);
    }
    catch (IOException e)
    {
        throw new ProtocolException("Invalid value for the paging state");
    }
}
 
Example #2
Source File: AbstractComposite.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer toByteBuffer()
{
    // This is the legacy format of composites.
    // See org.apache.cassandra.db.marshal.CompositeType for details.
    ByteBuffer result = ByteBuffer.allocate(dataSize() + 3 * size() + (isStatic() ? 2 : 0));
    if (isStatic())
        ByteBufferUtil.writeShortLength(result, CompositeType.STATIC_MARKER);

    for (int i = 0; i < size(); i++)
    {
        ByteBuffer bb = get(i);
        ByteBufferUtil.writeShortLength(result, bb.remaining());
        result.put(bb.duplicate());
        result.put((byte)0);
    }
    result.flip();
    return result;
}
 
Example #3
Source File: TypeParser.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static String stringifyUserTypeParameters(String keysace, ByteBuffer typeName, List<ByteBuffer> columnNames, List<AbstractType<?>> columnTypes)
{
    StringBuilder sb = new StringBuilder();
    sb.append('(').append(keysace).append(",").append(ByteBufferUtil.bytesToHex(typeName));

    for (int i = 0; i < columnNames.size(); i++)
    {
        sb.append(',');
        sb.append(ByteBufferUtil.bytesToHex(columnNames.get(i))).append(":");

        // omit FrozenType(...) from fields because it is currently implicit
        sb.append(columnTypes.get(i).toString(true));
    }
    sb.append(')');
    return sb.toString();
}
 
Example #4
Source File: ColumnConditionTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static boolean mapAppliesTo(ColumnCondition.CollectionBound bound, Map<ByteBuffer, ByteBuffer> conditionValues, Map<ByteBuffer, ByteBuffer> columnValues)
{
    CFMetaData cfm = CFMetaData.compile("create table foo(a int PRIMARY KEY, b map<int, int>)", "ks");
    Map<ByteBuffer, CollectionType> typeMap = new HashMap<>();
    typeMap.put(ByteBufferUtil.bytes("b"), MapType.getInstance(Int32Type.instance, Int32Type.instance, true));
    CompoundSparseCellNameType.WithCollection nameType = new CompoundSparseCellNameType.WithCollection(Collections.EMPTY_LIST, ColumnToCollectionType.getInstance(typeMap));
    ColumnDefinition definition = new ColumnDefinition(cfm, ByteBufferUtil.bytes("b"), MapType.getInstance(Int32Type.instance, Int32Type.instance, true), 0, ColumnDefinition.Kind.REGULAR);

    List<Cell> cells = new ArrayList<>(columnValues.size());
    if (columnValues != null)
    {
        for (Map.Entry<ByteBuffer, ByteBuffer> entry : columnValues.entrySet())
            cells.add(new BufferCell(nameType.create(Composites.EMPTY, definition, entry.getKey()), entry.getValue()));
    }

    return bound.mapAppliesTo(MapType.getInstance(Int32Type.instance, Int32Type.instance, true), cells.iterator(), conditionValues, bound.operator);
}
 
Example #5
Source File: SelectStatementTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Test 'clustering_1 = 1' with 2 clustering columns
 */
@Test
public void testBuildBoundWithOneEqRestrictionsAndTwoClusteringColumns() throws InvalidRequestException
{
    ByteBuffer clustering_2 = ByteBufferUtil.bytes(1);
    SingleColumnRestriction.EQ eq = new SingleColumnRestriction.EQ(toTerm(clustering_2), false);
    Restriction[] restrictions = new Restriction[] { eq, null };

    List<Composite> bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), clustering_2, EOC.START);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), clustering_2, EOC.END);
}
 
Example #6
Source File: IndexSummaryTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEmptyKey() throws Exception
{
    IPartitioner p = new RandomPartitioner();
    try (IndexSummaryBuilder builder = new IndexSummaryBuilder(1, 1, BASE_SAMPLING_LEVEL))
    {
        builder.maybeAddEntry(p.decorateKey(ByteBufferUtil.EMPTY_BYTE_BUFFER), 0);
        IndexSummary summary = builder.build(p);
        assertEquals(1, summary.size());
        assertEquals(0, summary.getPosition(0));
        assertArrayEquals(new byte[0], summary.getKey(0));

        DataOutputBuffer dos = new DataOutputBuffer();
        IndexSummary.serializer.serialize(summary, dos, false);
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dos.toByteArray()));
        IndexSummary loaded = IndexSummary.serializer.deserialize(dis, p, false, 1, 1);

        assertEquals(1, loaded.size());
        assertEquals(summary.getPosition(0), loaded.getPosition(0));
        assertArrayEquals(summary.getKey(0), summary.getKey(0));
        summary.close();
        loaded.close();
    }
}
 
Example #7
Source File: AbstractCompositeType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public List<CompositeComponent> deconstruct( ByteBuffer bytes )
{
    List<CompositeComponent> list = new ArrayList<CompositeComponent>();

    ByteBuffer bb = bytes.duplicate();
    readIsStatic(bb);
    int i = 0;

    while (bb.remaining() > 0)
    {
        AbstractType comparator = getComparator(i, bb);
        ByteBuffer value = ByteBufferUtil.readBytesWithShortLength(bb);

        list.add( new CompositeComponent(comparator,value) );

        byte b = bb.get(); // Ignore; not relevant here
        ++i;
    }
    return list;
}
 
Example #8
Source File: AbstractSimpleCellNameType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void maybeReadNext() throws IOException
{
    if (next != null)
        return;

    int length = in.readShort() & 0xFFFF;
    // Note that empty is ok because it marks the end of row
    if (length == 0)
    {
        next = ByteBufferUtil.EMPTY_BYTE_BUFFER;
        return;
    }

    byte[] b = new byte[length];
    in.readFully(b);
    next = ByteBuffer.wrap(b);
}
 
Example #9
Source File: BatchlogManager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private List<Mutation> replayingMutations() throws IOException
{
    DataInputStream in = new DataInputStream(ByteBufferUtil.inputStream(data));
    int size = in.readInt();
    List<Mutation> mutations = new ArrayList<>(size);
    for (int i = 0; i < size; i++)
    {
        Mutation mutation = Mutation.serializer.deserialize(in, version);

        // Remove CFs that have been truncated since. writtenAt and SystemTable#getTruncatedAt() both return millis.
        // We don't abort the replay entirely b/c this can be considered a success (truncated is same as delivered then
        // truncated.
        for (UUID cfId : mutation.getColumnFamilyIds())
            if (writtenAt <= SystemKeyspace.getTruncatedAt(cfId))
                mutation = mutation.without(cfId);

        if (!mutation.isEmpty())
            mutations.add(mutation);
    }
    return mutations;
}
 
Example #10
Source File: TypeParser.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static String stringifyCollectionsParameters(Map<ByteBuffer, ? extends CollectionType> collections)
{
    StringBuilder sb = new StringBuilder();
    sb.append('(');
    boolean first = true;
    for (Map.Entry<ByteBuffer, ? extends CollectionType> entry : collections.entrySet())
    {
        if (!first)
            sb.append(',');

        first = false;
        sb.append(ByteBufferUtil.bytesToHex(entry.getKey())).append(":");
        sb.append(entry.getValue());
    }
    sb.append(')');
    return sb.toString();
}
 
Example #11
Source File: SSTableReaderTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/** see CASSANDRA-5407 */
@Test
public void testGetScannerForNoIntersectingRanges()
{
    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamilyStore store = keyspace.getColumnFamilyStore("Standard1");
    ByteBuffer key = ByteBufferUtil.bytes(String.valueOf("k1"));
    Mutation rm = new Mutation("Keyspace1", key);
    rm.add("Standard1", cellname("xyz"), ByteBufferUtil.bytes("abc"), 0);
    rm.apply();
    store.forceBlockingFlush();
    boolean foundScanner = false;
    for (SSTableReader s : store.getSSTables())
    {
        ISSTableScanner scanner = s.getScanner(new Range<Token>(t(0), t(1), s.partitioner), null);
        scanner.next(); // throws exception pre 5407
        foundScanner = true;
    }
    assertTrue(foundScanner);
}
 
Example #12
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
// create two sstables, and verify that we only deserialize data from the most recent one
public void testTimeSortedQuery()
{
    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("Standard1");
    cfs.truncateBlocking();

    Mutation rm;
    rm = new Mutation("Keyspace1", ByteBufferUtil.bytes("key1"));
    rm.add("Standard1", cellname("Column1"), ByteBufferUtil.bytes("asdf"), 0);
    rm.apply();
    cfs.forceBlockingFlush();

    rm = new Mutation("Keyspace1", ByteBufferUtil.bytes("key1"));
    rm.add("Standard1", cellname("Column1"), ByteBufferUtil.bytes("asdf"), 1);
    rm.apply();
    cfs.forceBlockingFlush();

    cfs.getRecentSSTablesPerReadHistogram(); // resets counts
    cfs.getColumnFamily(Util.namesQueryFilter(cfs, Util.dk("key1"), "Column1"));
    assertEquals(1, cfs.getRecentSSTablesPerReadHistogram()[0]);
}
 
Example #13
Source File: BytesConversionFcts.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Function makeFromBlobFunction(final AbstractType<?> toType)
{
    final String name = "blobas" + toType.asCQL3Type();
    return new AbstractFunction(name, toType, BytesType.instance)
    {
        public ByteBuffer execute(List<ByteBuffer> parameters) throws InvalidRequestException
        {
            ByteBuffer val = parameters.get(0);
            try
            {
                if (val != null)
                    toType.validate(val);
                return val;
            }
            catch (MarshalException e)
            {
                throw new InvalidRequestException(String.format("In call to function %s, value 0x%s is not a valid binary representation for type %s",
                                                                name, ByteBufferUtil.bytesToHex(val), toType.asCQL3Type()));
            }
        }
    };
}
 
Example #14
Source File: ScrubTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Tests CASSANDRA-6892 (key aliases being used improperly for validation)
 */
@Test
public void testColumnNameEqualToDefaultKeyAlias() throws ExecutionException, InterruptedException
{
    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("UUIDKeys");

    ColumnFamily cf = ArrayBackedSortedColumns.factory.create("Keyspace1", "UUIDKeys");
    cf.addColumn(column(CFMetaData.DEFAULT_KEY_ALIAS, "not a uuid", 1L));
    Mutation mutation = new Mutation("Keyspace1", ByteBufferUtil.bytes(UUIDGen.getTimeUUID()), cf);
    mutation.applyUnsafe();
    cfs.forceBlockingFlush();
    CompactionManager.instance.performScrub(cfs, false);

    assertEquals(1, cfs.getSSTables().size());
}
 
Example #15
Source File: KeyspaceTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSliceNoMatch() throws Throwable
{
    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamily cf = ArrayBackedSortedColumns.factory.create("Keyspace1", "Standard2");
    cf.addColumn(column("col1", "val1", 1));
    Mutation rm = new Mutation("Keyspace1", ByteBufferUtil.bytes("row1000"), cf);
    rm.apply();

    validateGetSliceNoMatch(keyspace);
    keyspace.getColumnFamilyStore("Standard2").forceBlockingFlush();
    validateGetSliceNoMatch(keyspace);

    Collection<SSTableReader> ssTables = keyspace.getColumnFamilyStore("Standard2").getSSTables();
    assertEquals(1, ssTables.size());
    ssTables.iterator().next().forceFilterFailures();
    validateGetSliceNoMatch(keyspace);
}
 
Example #16
Source File: SimpleSliceReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public SimpleSliceReader(SSTableReader sstable, RowIndexEntry indexEntry, FileDataInput input, Composite finishColumn)
{
    Tracing.trace("Seeking to partition beginning in data file");
    this.finishColumn = finishColumn;
    this.comparator = sstable.metadata.comparator;
    try
    {
        if (input == null)
        {
            this.file = sstable.getFileDataInput(indexEntry.position);
            this.needsClosing = true;
        }
        else
        {
            this.file = input;
            input.seek(indexEntry.position);
            this.needsClosing = false;
        }

        // Skip key and data size
        ByteBufferUtil.skipShortLength(file);

        emptyColumnFamily = ArrayBackedSortedColumns.factory.create(sstable.metadata);
        emptyColumnFamily.delete(DeletionTime.serializer.deserialize(file));
        atomIterator = emptyColumnFamily.metadata().getOnDiskIterator(file, sstable.descriptor.version);
    }
    catch (IOException e)
    {
        sstable.markSuspect();
        throw new CorruptSSTableException(e, sstable.getFilename());
    }
}
 
Example #17
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void insert(ByteBuffer key, ColumnParent column_parent, Column column, ConsistencyLevel consistency_level)
throws InvalidRequestException, UnavailableException, TimedOutException
{
    if (startSessionIfRequested())
    {
        Map<String, String> traceParameters = ImmutableMap.of("key", ByteBufferUtil.bytesToHex(key),
                                                              "column_parent", column_parent.toString(),
                                                              "column", column.toString(),
                                                              "consistency_level", consistency_level.name());
        Tracing.instance.begin("insert", traceParameters);
    }
    else
    {
        logger.debug("insert");
    }

    try
    {
        internal_insert(key, column_parent, column, consistency_level);
    }
    catch (RequestValidationException e)
    {
        throw ThriftConversion.toThrift(e);
    }
    finally
    {
        Tracing.instance.stopSession();
    }
}
 
Example #18
Source File: ColumnMapperBlobTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueByteBuffer() {
    ColumnMapperBlob mapper = new ColumnMapperBlob();
    ByteBuffer bb = ByteBufferUtil.hexToBytes("f1");
    String parsed = mapper.indexValue("test", bb);
    Assert.assertEquals("f1", parsed);
}
 
Example #19
Source File: AbstractSimpleCellNameType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Inline
static int compareUnsigned(Composite c1, Composite c2)
{
    ByteBuffer b1 = c1.toByteBuffer();
    ByteBuffer b2 = c2.toByteBuffer();
    return ByteBufferUtil.compareUnsigned(b1, b2);
}
 
Example #20
Source File: SchemaLoader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected void insertData(String keyspace, String columnFamily, int offset, int numberOfRows)
{
    for (int i = offset; i < offset + numberOfRows; i++)
    {
        ByteBuffer key = ByteBufferUtil.bytes("key" + i);
        Mutation mutation = new Mutation(keyspace, key);
        mutation.add(columnFamily, Util.cellname("col" + i), ByteBufferUtil.bytes("val" + i), System.currentTimeMillis());
        mutation.applyUnsafe();
    }
}
 
Example #21
Source File: BufferedRandomAccessFileTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testClose() throws IOException
{
    final SequentialWriter w = createTempFile("brafClose");

    byte[] data = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE + 20);

    w.write(data);
    w.close(); // will flush

    final RandomAccessReader r = RandomAccessReader.open(new File(w.getPath()));

    r.close(); // closing to test read after close

    expectException(new Callable<Object>()
    {
        public Object call()
        {
            return r.read();
        }
    }, AssertionError.class);

    expectException(new Callable<Object>()
    {
        public Object call() throws IOException
        {
            w.write(generateByteArray(1));
            return null;
        }
    }, ClosedChannelException.class);

    try (RandomAccessReader copy = RandomAccessReader.open(new File(r.getPath())))
    {
        ByteBuffer contents = copy.readBytes((int) copy.length());

        assertEquals(contents.limit(), data.length);
        assertEquals(ByteBufferUtil.compare(contents, data), 0);
    }
}
 
Example #22
Source File: CollectionType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source)
{
    try
    {
        return ByteBufferUtil.hexToBytes(source);
    }
    catch (NumberFormatException e)
    {
        throw new MarshalException(String.format("cannot parse '%s' as hex bytes", source), e);
    }
}
 
Example #23
Source File: WordCount.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Mutation getMutation(Text word, int sum)
{
    org.apache.cassandra.thrift.Column c = new org.apache.cassandra.thrift.Column();
    c.setName(Arrays.copyOf(word.getBytes(), word.getLength()));
    c.setValue(ByteBufferUtil.bytes(sum));
    c.setTimestamp(System.currentTimeMillis());

    Mutation m = new Mutation();
    m.setColumn_or_supercolumn(new ColumnOrSuperColumn());
    m.column_or_supercolumn.setColumn(c);
    return m;
}
 
Example #24
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void remove_counter(ByteBuffer key, ColumnPath path, ConsistencyLevel consistency_level)
throws InvalidRequestException, UnavailableException, TimedOutException, TException
{
    if (startSessionIfRequested())
    {
        Map<String, String> traceParameters = ImmutableMap.of("key", ByteBufferUtil.bytesToHex(key),
                                                              "column_path", path.toString(),
                                                              "consistency_level", consistency_level.name());
        Tracing.instance.begin("remove_counter", traceParameters);
    }
    else
    {
        logger.debug("remove_counter");
    }

    try
    {
        internal_remove(key, path, System.currentTimeMillis(), consistency_level, true);
    }
    catch (RequestValidationException e)
    {
        throw ThriftConversion.toThrift(e);
    }
    finally
    {
        Tracing.instance.stopSession();
    }
}
 
Example #25
Source File: AbstractCassandraStorage.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/** set the value to the position of the tuple */
protected void setTupleValue(Tuple pair, int position, Object value) throws ExecException
{
   if (value instanceof BigInteger)
       pair.set(position, ((BigInteger) value).intValue());
   else if (value instanceof ByteBuffer)
       pair.set(position, new DataByteArray(ByteBufferUtil.getArray((ByteBuffer) value)));
   else if (value instanceof UUID)
       pair.set(position, new DataByteArray(UUIDGen.decompose((java.util.UUID) value)));
   else if (value instanceof Date)
       pair.set(position, TimestampType.instance.decompose((Date) value).getLong());
   else
       pair.set(position, value);
}
 
Example #26
Source File: Util.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static void addMutation(Mutation rm, String columnFamilyName, String superColumnName, long columnName, String value, long timestamp)
{
    CellName cname = superColumnName == null
                   ? CellNames.simpleDense(getBytes(columnName))
                   : CellNames.compositeDense(ByteBufferUtil.bytes(superColumnName), getBytes(columnName));
    rm.add(columnFamilyName, cname, ByteBufferUtil.bytes(value), timestamp);
}
 
Example #27
Source File: DoubleToDecimalCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Double paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT);
}
 
Example #28
Source File: PagingState.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer serialize()
{
    try
    {
        DataOutputBuffer out = new DataOutputBuffer(serializedSize());
        ByteBufferUtil.writeWithShortLength(partitionKey, out);
        ByteBufferUtil.writeWithShortLength(cellName, out);
        out.writeInt(remaining);
        return out.asByteBuffer();
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #29
Source File: PrepareResponse.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void serialize(PrepareResponse response, DataOutputPlus out, int version) throws IOException
{
    out.writeBoolean(response.promised);
    ByteBufferUtil.writeWithShortLength(response.inProgressCommit.key, out);
    UUIDSerializer.serializer.serialize(response.inProgressCommit.ballot, out, version);
    ColumnFamily.serializer.serialize(response.inProgressCommit.update, out, version);
    UUIDSerializer.serializer.serialize(response.mostRecentCommit.ballot, out, version);
    ColumnFamily.serializer.serialize(response.mostRecentCommit.update, out, version);
}
 
Example #30
Source File: BigDecimalToBigintCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramByteBuffer == null) {
		return null;

	}
	// always duplicate the ByteBuffer instance before consuming it!
	Long value = ByteBufferUtil.toLong(paramByteBuffer.duplicate());
	return new BigDecimal(value);
}