Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#bytes()

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#bytes() . 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: PerRowSecondaryIndexTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexInsertAndUpdate()
{
    // create a row then test that the configured index instance was able to read the row
    Mutation rm;
    rm = new Mutation("PerRowSecondaryIndex", ByteBufferUtil.bytes("k1"));
    rm.add("Indexed1", Util.cellname("indexed"), ByteBufferUtil.bytes("foo"), 1);
    rm.apply();

    ColumnFamily indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_ROW;
    assertNotNull(indexedRow);
    assertEquals(ByteBufferUtil.bytes("foo"), indexedRow.getColumn(Util.cellname("indexed")).value());

    // update the row and verify what was indexed
    rm = new Mutation("PerRowSecondaryIndex", ByteBufferUtil.bytes("k1"));
    rm.add("Indexed1", Util.cellname("indexed"), ByteBufferUtil.bytes("bar"), 2);
    rm.apply();

    indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_ROW;
    assertNotNull(indexedRow);
    assertEquals(ByteBufferUtil.bytes("bar"), indexedRow.getColumn(Util.cellname("indexed")).value());
    assertTrue(Arrays.equals("k1".getBytes(), PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_KEY.array()));
}
 
Example 2
Source File: SelectStatementTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Test 'clustering_0 = 1' with only one clustering column
 */
@Test
public void testBuildBoundWithOneEqRestrictionsAndOneClusteringColumn() throws InvalidRequestException
{
    ByteBuffer clustering_0 = ByteBufferUtil.bytes(1);
    SingleColumnRestriction.EQ eq = new SingleColumnRestriction.EQ(toTerm(clustering_0), false);
    Restriction[] restrictions = new Restriction[] { eq };

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

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), clustering_0, EOC.END);
}
 
Example 3
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 4
Source File: AntiCompactionTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void antiCompactionSizeTest() throws ExecutionException, InterruptedException, IOException
{
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    cfs.disableAutoCompaction();
    SSTableReader s = writeFile(cfs, 1000);
    cfs.addSSTable(s);
    long origSize = s.bytesOnDisk();
    Range<Token> range = new Range<Token>(new BytesToken(ByteBufferUtil.bytes(0)), new BytesToken(ByteBufferUtil.bytes(500)));
    Collection<SSTableReader> sstables = cfs.getSSTables();
    CompactionManager.instance.performAnticompaction(cfs, Arrays.asList(range), Refs.tryRef(sstables), 12345);
    long sum = 0;
    for (SSTableReader x : cfs.getSSTables())
        sum += x.bytesOnDisk();
    assertEquals(sum, cfs.metric.liveDiskSpaceUsed.count());
    assertEquals(origSize, cfs.metric.liveDiskSpaceUsed.count(), 100000);

}
 
Example 5
Source File: RangeTombstoneTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void test7808_1() throws ExecutionException, InterruptedException
{
    Keyspace ks = Keyspace.open(KSNAME);
    ColumnFamilyStore cfs = ks.getColumnFamilyStore(CFNAME);
    cfs.metadata.gcGraceSeconds(2);

    String key = "7808_1";
    Mutation rm;
    rm = new Mutation(KSNAME, ByteBufferUtil.bytes(key));
    for (int i = 0; i < 40; i += 2)
        add(rm, i, 0);
    rm.apply();
    cfs.forceBlockingFlush();
    rm = new Mutation(KSNAME, ByteBufferUtil.bytes(key));
    ColumnFamily cf = rm.addOrGet(CFNAME);
    cf.delete(new DeletionInfo(1, 1));
    rm.apply();
    cfs.forceBlockingFlush();
    Thread.sleep(5);
    cfs.forceMajorCompaction();
}
 
Example 6
Source File: GeneratorConfig.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public GeneratorConfig(String seedStr, DistributionFactory clusteringDistributions, DistributionFactory sizeDistributions, DistributionFactory identityDistributions)
{
    this.clusteringDistributions = clusteringDistributions;
    this.sizeDistributions = sizeDistributions;
    this.identityDistributions = identityDistributions;
    ByteBuffer buf = ByteBufferUtil.bytes(seedStr);
    long[] hash = new long[2];
    MurmurHash.hash3_x64_128(buf, buf.position(), buf.remaining(), 0, hash);
    salt = hash[0];
}
 
Example 7
Source File: SchemaLoader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static CFMetaData indexCFMD(String ksName, String cfName, final Boolean withIdxType) throws ConfigurationException
{
    CFMetaData cfm = CFMetaData.sparseCFMetaData(ksName, cfName, BytesType.instance).keyValidator(AsciiType.instance);

    ByteBuffer cName = ByteBufferUtil.bytes("birthdate");
    IndexType keys = withIdxType ? IndexType.KEYS : null;
    return cfm.addColumnDefinition(ColumnDefinition.regularDef(cfm, cName, LongType.instance, null)
                                                   .setIndex(withIdxType ? ByteBufferUtil.bytesToHex(cName) : null, keys, null));
}
 
Example 8
Source File: QueryProcessor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Column thriftify(org.apache.cassandra.db.Cell c)
{
    ByteBuffer value = (c instanceof CounterCell)
                       ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
                       : c.value();
    return new Column(c.name().toByteBuffer()).setValue(value).setTimestamp(c.timestamp());
}
 
Example 9
Source File: SchemaLoader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static CFMetaData compositeIndexCFMD(String ksName, String cfName, final Boolean withIdxType) throws ConfigurationException
{
    final CompositeType composite = CompositeType.getInstance(Arrays.asList(new AbstractType<?>[]{UTF8Type.instance, UTF8Type.instance})); 
    CFMetaData cfm = CFMetaData.sparseCFMetaData(ksName, cfName, composite);

    ByteBuffer cName = ByteBufferUtil.bytes("col1");
    IndexType idxType = withIdxType ? IndexType.COMPOSITES : null;
    return cfm.addColumnDefinition(ColumnDefinition.regularDef(cfm, cName, UTF8Type.instance, 1)
                                                   .setIndex(withIdxType ? "col1_idx" : null, idxType, Collections.<String, String>emptyMap()));
}
 
Example 10
Source File: RecoveryManager2Test.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void insertRow(String cfname, String key) 
{
    ColumnFamily cf = ArrayBackedSortedColumns.factory.create("Keyspace1", cfname);
    cf.addColumn(column("col1", "val1", 1L));
    Mutation rm = new Mutation("Keyspace1", ByteBufferUtil.bytes(key), cf);
    rm.apply();
}
 
Example 11
Source File: SSTableRewriterTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void basicTest() throws InterruptedException
{
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    cfs.truncateBlocking();
    for (int j = 0; j < 100; j ++)
    {
        ByteBuffer key = ByteBufferUtil.bytes(String.valueOf(j));
        Mutation rm = new Mutation(KEYSPACE, key);
        rm.add(CF, Util.cellname("0"), ByteBufferUtil.EMPTY_BYTE_BUFFER, j);
        rm.apply();
    }
    cfs.forceBlockingFlush();
    Set<SSTableReader> sstables = new HashSet<>(cfs.getSSTables());
    assertEquals(1, sstables.size());
    SSTableRewriter writer = new SSTableRewriter(cfs, sstables, 1000, false);
    try (AbstractCompactionStrategy.ScannerList scanners = cfs.getCompactionStrategy().getScanners(sstables);)
    {
        ISSTableScanner scanner = scanners.scanners.get(0);
        CompactionController controller = new CompactionController(cfs, sstables, cfs.gcBefore(System.currentTimeMillis()));
        writer.switchWriter(getWriter(cfs, sstables.iterator().next().descriptor.directory));
        while(scanner.hasNext())
        {
            AbstractCompactedRow row = new LazilyCompactedRow(controller, Arrays.asList(scanner.next()));
            writer.append(row);
        }
    }
    Collection<SSTableReader> newsstables = writer.finish();
    cfs.getDataTracker().markCompactedSSTablesReplaced(sstables, newsstables , OperationType.COMPACTION);
    Thread.sleep(100);
    validateCFS(cfs);
    int filecounts = assertFileCounts(sstables.iterator().next().descriptor.directory.list(), 0, 0);
    assertEquals(1, filecounts);

}
 
Example 12
Source File: ScrubTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected void fillCounterCF(ColumnFamilyStore cfs, int rowsPerSSTable) throws WriteTimeoutException
{
    for (int i = 0; i < rowsPerSSTable; i++)
    {
        String key = String.valueOf(i);
        ColumnFamily cf = ArrayBackedSortedColumns.factory.create(KEYSPACE, COUNTER_CF);
        Mutation rm = new Mutation(KEYSPACE, ByteBufferUtil.bytes(key), cf);
        rm.addCounter(COUNTER_CF, cellname("Column1"), 100);
        CounterMutation cm = new CounterMutation(rm, ConsistencyLevel.ONE);
        cm.apply();
    }

    cfs.forceBlockingFlush();
}
 
Example 13
Source File: IntToLongCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Long paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT.intValue());
}
 
Example 14
Source File: AbstractTextSerializer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public ByteBuffer serialize(String value)
{
    return ByteBufferUtil.bytes(value, charset);
}
 
Example 15
Source File: WordCount.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public int run(String[] args) throws Exception
{
    String outputReducerType = "filesystem";
    if (args != null && args[0].startsWith(OUTPUT_REDUCER_VAR))
    {
        String[] s = args[0].split("=");
        if (s != null && s.length == 2)
            outputReducerType = s[1];
    }
    logger.info("output reducer type: " + outputReducerType);

    // use a smaller page size that doesn't divide the row count evenly to exercise the paging logic better
    ConfigHelper.setRangeBatchSize(getConf(), 99);

    for (int i = 0; i < WordCountSetup.TEST_COUNT; i++)
    {
        String columnName = "text" + i;

        Job job = new Job(getConf(), "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);

        if (outputReducerType.equalsIgnoreCase("filesystem"))
        {
            job.setCombinerClass(ReducerToFilesystem.class);
            job.setReducerClass(ReducerToFilesystem.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX + i));
        }
        else
        {
            job.setReducerClass(ReducerToCassandra.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            job.setOutputKeyClass(ByteBuffer.class);
            job.setOutputValueClass(List.class);

            job.setOutputFormatClass(ColumnFamilyOutputFormat.class);

            ConfigHelper.setOutputColumnFamily(job.getConfiguration(), KEYSPACE, OUTPUT_COLUMN_FAMILY);
            job.getConfiguration().set(CONF_COLUMN_NAME, "sum");
        }

        job.setInputFormatClass(ColumnFamilyInputFormat.class);

        ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160");
        ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setInputPartitioner(job.getConfiguration(), "Murmur3Partitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY);
        SlicePredicate predicate = new SlicePredicate().setColumn_names(Arrays.asList(ByteBufferUtil.bytes(columnName)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        if (i == 4)
        {
            IndexExpression expr = new IndexExpression(ByteBufferUtil.bytes("int4"), IndexOperator.EQ, ByteBufferUtil.bytes(0));
            ConfigHelper.setInputRange(job.getConfiguration(), Arrays.asList(expr));
        }

        if (i == 5)
        {
            // this will cause the predicate to be ignored in favor of scanning everything as a wide row
            ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY, true);
        }

        ConfigHelper.setOutputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setOutputPartitioner(job.getConfiguration(), "Murmur3Partitioner");

        job.waitForCompletion(true);
    }
    return 0;
}
 
Example 16
Source File: ColumnIdentifier.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public ColumnIdentifier(String rawText, boolean keepCase)
{
    this.text = keepCase ? rawText : rawText.toLowerCase(Locale.US);
    this.bytes = ByteBufferUtil.bytes(this.text);
}
 
Example 17
Source File: TestRingCache.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * usage: java -cp <configpath> org.apache.cassandra.client.TestRingCache [keyspace row-id-prefix row-id-int]
 * to test a single keyspace/row, use the parameters. row-id-prefix and row-id-int are appended together to form a
 * single row id.  If you supply now parameters, 'Keyspace1' is assumed and will check 9 rows ('row1' through 'row9').
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Throwable
{
    int minRow;
    int maxRow;
    String rowPrefix, keyspace = "Keyspace1";

    if (args.length > 0)
    {
        keyspace = args[0];
        rowPrefix = args[1];
        minRow = Integer.parseInt(args[2]);
        maxRow = minRow + 1;
    }
    else
    {
        minRow = 1;
        maxRow = 10;
        rowPrefix = "row";
    }

    TestRingCache tester = new TestRingCache(keyspace);

    for (int nRows = minRow; nRows < maxRow; nRows++)
    {
        ByteBuffer row = ByteBufferUtil.bytes((rowPrefix + nRows));
        ColumnPath col = new ColumnPath("Standard1").setSuper_column((ByteBuffer)null).setColumn("col1".getBytes());
        ColumnParent parent = new ColumnParent("Standard1").setSuper_column((ByteBuffer)null);

        Collection<InetAddress> endpoints = tester.ringCache.getEndpoint(row);
        InetAddress firstEndpoint = endpoints.iterator().next();
        System.out.printf("hosts with key %s : %s; choose %s%n",
                          new String(row.array()), StringUtils.join(endpoints, ","), firstEndpoint);

        // now, read the row back directly from the host owning the row locally
        tester.setup(firstEndpoint.getHostAddress(), DatabaseDescriptor.getRpcPort());
        tester.thriftClient.set_keyspace(keyspace);
        tester.thriftClient.insert(row, parent, new Column(ByteBufferUtil.bytes("col1")).setValue(ByteBufferUtil.bytes("val1")).setTimestamp(1), ConsistencyLevel.ONE);
        Column column = tester.thriftClient.get(row, col, ConsistencyLevel.ONE).column;
        System.out.println("read row " + new String(row.array()) + " " + new String(column.name.array()) + ":" + new String(column.value.array()) + ":" + column.timestamp);
    }

    System.exit(1);
}
 
Example 18
Source File: SelectStatementTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Test multi-column slice restrictions (e.g '(clustering_0, clustering_1) > (1, 2)') with only one clustering
 * column
 */
@Test
public void testBuildBoundWithMultiSliceRestrictionsWithTwoClusteringColumn() throws InvalidRequestException
{
    ByteBuffer value1 = ByteBufferUtil.bytes(1);
    ByteBuffer value2 = ByteBufferUtil.bytes(2);

    // (clustering_0, clustering1) > (1, 2)
    MultiColumnRestriction.Slice slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.GT, toMultiItemTerminal(value1, value2));
    Restriction[] restrictions = new Restriction[] { slice, slice };

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

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertEmptyComposite(bounds.get(0));

    // (clustering_0, clustering1) >= (1, 2)
    slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.GTE, toMultiItemTerminal(value1, value2));
    restrictions = new Restriction[] { slice, slice };

    bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.NONE);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertEmptyComposite(bounds.get(0));

    // (clustering_0, clustering1) <= (1, 2)
    slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.LTE, toMultiItemTerminal(value1, value2));
    restrictions = new Restriction[] { slice, slice };

    bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertEmptyComposite(bounds.get(0));

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.END);

    // (clustering_0, clustering1) < (1, 2)
    slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.LT, toMultiItemTerminal(value1, value2));
    restrictions = new Restriction[] { slice, slice };

    bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertEmptyComposite(bounds.get(0));

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.START);

    // (clustering_0, clustering1) > (1, 2) AND (clustering_0) < (2)
    slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.GT, toMultiItemTerminal(value1, value2));
    slice.setBound(Operator.LT, toMultiItemTerminal(value2));
    restrictions = new Restriction[] { slice, slice };

    bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.END);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value2, EOC.START);

    // (clustering_0, clustering1) >= (1, 2) AND (clustering_0, clustering1) <= (2, 1)
    slice = new MultiColumnRestriction.Slice(false);
    slice.setBound(Operator.GTE, toMultiItemTerminal(value1, value2));
    slice.setBound(Operator.LTE, toMultiItemTerminal(value2, value1));
    restrictions = new Restriction[] { slice, slice };

    bounds = executeBuildBound(restrictions, Bound.START);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value1, value2, EOC.NONE);

    bounds = executeBuildBound(restrictions, Bound.END);
    assertEquals(1, bounds.size());
    assertComposite(bounds.get(0), value2, value1, EOC.END);
}
 
Example 19
Source File: ReadMessageTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoCommitLog() throws Exception
{
    Mutation rm = new Mutation("Keyspace1", ByteBufferUtil.bytes("row"));
    rm.add("Standard1", Util.cellname("commit1"), ByteBufferUtil.bytes("abcd"), 0);
    rm.apply();

    rm = new Mutation("NoCommitlogSpace", ByteBufferUtil.bytes("row"));
    rm.add("Standard1", Util.cellname("commit2"), ByteBufferUtil.bytes("abcd"), 0);
    rm.apply();

    boolean commitLogMessageFound = false;
    boolean noCommitLogMessageFound = false;

    File commitLogDir = new File(DatabaseDescriptor.getCommitLogLocation());

    byte[] commitBytes = "commit".getBytes("UTF-8");

    for(String filename : commitLogDir.list())
    {
        BufferedInputStream is = null;
        try
        {
            is = new BufferedInputStream(new FileInputStream(commitLogDir.getAbsolutePath()+File.separator+filename));

            if (!isEmptyCommitLog(is))
            {
                while (findPatternInStream(commitBytes, is))
                {
                    char c = (char)is.read();

                    if (c == '1')
                        commitLogMessageFound = true;
                    else if (c == '2')
                        noCommitLogMessageFound = true;
                }
            }
        }
        finally
        {
            if (is != null)
                is.close();
        }
    }

    assertTrue(commitLogMessageFound);
    assertFalse(noCommitLogMessageFound);
}
 
Example 20
Source File: BufferCounterUpdateCell.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public BufferCounterUpdateCell(CellName name, long value, long timestamp)
{
    this(name, ByteBufferUtil.bytes(value), timestamp);
}