Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#EMPTY_BYTE_BUFFER

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#EMPTY_BYTE_BUFFER . 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: CollectionTypeTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testListComparison()
{
    ListType<String> lt = ListType.getInstance(UTF8Type.instance, true);

    ByteBuffer[] lists = new ByteBuffer[] {
        ByteBufferUtil.EMPTY_BYTE_BUFFER,
        lt.decompose(ImmutableList.<String>of()),
        lt.decompose(ImmutableList.of("aa")),
        lt.decompose(ImmutableList.of("bb")),
        lt.decompose(ImmutableList.of("bb", "cc")),
        lt.decompose(ImmutableList.of("bb", "dd"))
    };

    for (int i = 0; i < lists.length; i++)
        assertEquals(lt.compare(lists[i], lists[i]), 0);

    for (int i = 0; i < lists.length-1; i++)
    {
        for (int j = i+1; j < lists.length; j++)
        {
            assertEquals(String.format("compare(lists[%d], lists[%d])", i, j), -1, lt.compare(lists[i], lists[j]));
            assertEquals(String.format("compare(lists[%d], lists[%d])", j, i),  1, lt.compare(lists[j], lists[i]));
        }
    }
}
 
Example 2
Source File: DoubleType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
  // Return an empty ByteBuffer for an empty string.
  if (source.isEmpty())
      return ByteBufferUtil.EMPTY_BYTE_BUFFER;

  Double d;
  try
  {
      d = Double.valueOf(source);
  }
  catch (NumberFormatException e1)
  {
      throw new MarshalException(String.format("unable to coerce '%s' to a double", source), e1);
  }

  return decompose(d);
}
 
Example 3
Source File: LongType.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    long longType;

    try
    {
        longType = Long.parseLong(source);
    }
    catch (Exception e)
    {
        throw new MarshalException(String.format("unable to make long from '%s'", source), e);
    }

    return decompose(longType);
}
 
Example 4
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 5
Source File: CollectionTypeTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapComparison()
{
    MapType<String, String> mt = MapType.getInstance(UTF8Type.instance, UTF8Type.instance, true);

    ByteBuffer[] maps = new ByteBuffer[] {
        ByteBufferUtil.EMPTY_BYTE_BUFFER,
        mt.decompose(ImmutableMap.<String, String>of()),
        mt.decompose(ImmutableMap.of("aa", "val1")),
        mt.decompose(ImmutableMap.of("aa", "val2")),
        mt.decompose(ImmutableMap.of("bb", "val1")),
        mt.decompose(ImmutableMap.of("bb", "val1", "cc", "val3")),
        mt.decompose(ImmutableMap.of("bb", "val1", "dd", "val3")),
        mt.decompose(ImmutableMap.of("bb", "val1", "dd", "val4"))
    };

    for (int i = 0; i < maps.length; i++)
        assertEquals(mt.compare(maps[i], maps[i]), 0);

    for (int i = 0; i < maps.length-1; i++)
    {
        for (int j = i+1; j < maps.length; j++)
        {
            assertEquals(String.format("compare(maps[%d], maps[%d])", i, j), mt.compare(maps[i], maps[j]), -1);
            assertEquals(String.format("compare(maps[%d], maps[%d])", j, i), mt.compare(maps[j], maps[i]), 1);
        }
    }
}
 
Example 6
Source File: TimeUUIDType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer fromStringCQL2(String source) throws MarshalException
{
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    ByteBuffer idBytes = null;

    // ffffffff-ffff-ffff-ffff-ffffffffff
    if (regexPattern.matcher(source).matches())
    {
        UUID uuid = null;
        try
        {
            uuid = UUID.fromString(source);
            idBytes = decompose(uuid);
        }
        catch (IllegalArgumentException e)
        {
            throw new MarshalException(String.format("unable to make UUID from '%s'", source), e);
        }

        if (uuid.version() != 1)
            throw new MarshalException("TimeUUID supports only version 1 UUIDs");
    }
    else
    {
        idBytes = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(TimestampSerializer.dateStringToTimestamp(source)));
    }

    return idBytes;
}
 
Example 7
Source File: CompoundSparseCellNameType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Composite makeStaticPrefix(int size)
{
    ByteBuffer[] elements = new ByteBuffer[size];
    for (int i = 0; i < size; i++)
        elements[i] = ByteBufferUtil.EMPTY_BYTE_BUFFER;

    return new CompoundComposite(elements, size, true)
    {
        @Override
        public boolean isStatic()
        {
            return true;
        }

        @Override
        public long unsharedHeapSize()
        {
            // We'll share this for a given type.
            return 0;
        }

        @Override
        public Composite copy(CFMetaData cfm, AbstractAllocator allocator)
        {
            return this;
        }
    };
}
 
Example 8
Source File: TimestampType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source) throws MarshalException
{
  // Return an empty ByteBuffer for an empty string.
  if (source.isEmpty())
      return ByteBufferUtil.EMPTY_BYTE_BUFFER;

  return ByteBufferUtil.bytes(TimestampSerializer.dateStringToTimestamp(source));
}
 
Example 9
Source File: Composites.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer get(int i)
{
    if (i > 0)
        throw new IndexOutOfBoundsException();

    return ByteBufferUtil.EMPTY_BYTE_BUFFER;
}
 
Example 10
Source File: ReadResponse.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void serialize(ReadResponse response, DataOutputPlus out, int version) throws IOException
{
    out.writeInt(response.isDigestQuery() ? response.digest().remaining() : 0);
    ByteBuffer buffer = response.isDigestQuery() ? response.digest() : ByteBufferUtil.EMPTY_BYTE_BUFFER;
    out.write(buffer);
    out.writeBoolean(response.isDigestQuery());
    if (!response.isDigestQuery())
        Row.serializer.serialize(response.row(), out, version);
}
 
Example 11
Source File: ClusteringKeyMapper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected final CellName extractClusteringKey(CellName cellName) {
    int numClusteringColumns = metadata.clusteringColumns().size();
    ByteBuffer[] components = new ByteBuffer[numClusteringColumns + 1];
    for (int i = 0; i < numClusteringColumns; i++) {
        components[i] = cellName.get(i);
    }
    components[numClusteringColumns] = ByteBufferUtil.EMPTY_BYTE_BUFFER;
    return cellNameType.makeCellName((Object[])components);
}
 
Example 12
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
private static Expression expressionForNot(AbstractType<?> validator, ByteBuffer lower, ByteBuffer upper, Iterable<ByteBuffer> terms)
{
    Expression expression = new Expression(ByteBufferUtil.EMPTY_BYTE_BUFFER, validator);
    expression.setOp(Expression.Op.RANGE);
    expression.setLower(new Expression.Bound(lower, true));
    expression.setUpper(new Expression.Bound(upper, true));
    for (ByteBuffer term : terms)
        expression.add(IndexOperator.NOT_EQ, term);
    return expression;

}
 
Example 13
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
private static Expression expressionFor(long lower, boolean lowerInclusive, long upper, boolean upperInclusive)
{
    Expression expression = new Expression(ByteBufferUtil.EMPTY_BYTE_BUFFER, LongType.instance);
    expression.add(lowerInclusive ? IndexOperator.GTE : IndexOperator.GT, LongType.instance.decompose(lower));
    expression.add(upperInclusive ? IndexOperator.LTE : IndexOperator.LT, LongType.instance.decompose(upper));
    return expression;
}
 
Example 14
Source File: CliClient.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void executeCount(Tree statement)
        throws TException, InvalidRequestException, UnavailableException, TimedOutException
{
    if (!CliMain.isConnected() || !hasKeySpace())
        return;

    Tree columnFamilySpec = statement.getChild(0);

    String columnFamily = CliCompiler.getColumnFamily(columnFamilySpec, currentCfDefs());
    int columnSpecCnt = CliCompiler.numColumnSpecifiers(columnFamilySpec);

    ColumnParent colParent = new ColumnParent(columnFamily).setSuper_column((ByteBuffer) null);

    if (columnSpecCnt != 0)
    {
        Tree columnTree = columnFamilySpec.getChild(2);

        byte[] superColumn = (columnTree.getType() == CliParser.FUNCTION_CALL)
                              ? convertValueByFunction(columnTree, null, null).array()
                              : columnNameAsByteArray(CliCompiler.getColumn(columnFamilySpec, 0), columnFamily);

        colParent = new ColumnParent(columnFamily).setSuper_column(superColumn);
    }

    SliceRange range = new SliceRange(ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, Integer.MAX_VALUE);
    SlicePredicate predicate = new SlicePredicate().setColumn_names(null).setSlice_range(range);

    int count = thriftClient.get_count(getKeyAsBytes(columnFamily, columnFamilySpec.getChild(1)), colParent, predicate, consistencyLevel);
    sessionState.out.printf("%d cells%n", count);
}
 
Example 15
Source File: EmptySerializer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public ByteBuffer serialize(Void value)
{
    return ByteBufferUtil.EMPTY_BYTE_BUFFER;
}
 
Example 16
Source File: PagingState.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public PagingState(ByteBuffer partitionKey, ByteBuffer cellName, int remaining)
{
    this.partitionKey = partitionKey == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : partitionKey;
    this.cellName = cellName == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : cellName;
    this.remaining = remaining;
}
 
Example 17
Source File: LocalPartitioner.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public LocalToken getMinimumToken()
{
    return new LocalToken(comparator, ByteBufferUtil.EMPTY_BYTE_BUFFER);
}
 
Example 18
Source File: BufferCell.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
BufferCell(CellName name)
{
    this(name, ByteBufferUtil.EMPTY_BYTE_BUFFER);
}
 
Example 19
Source File: IntegerSerializer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public ByteBuffer serialize(BigInteger value)
{
    return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBuffer.wrap(value.toByteArray());
}
 
Example 20
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 4 votes vote down vote up
private static Expression expressionFor(AbstractType<?> validator, ByteBuffer term)
{
    Expression expression = new Expression(ByteBufferUtil.EMPTY_BYTE_BUFFER, validator);
    expression.add(IndexOperator.EQ, term);
    return expression;
}