@Test
  public void testGetSliceFromLarge() throws Throwable {
    // tests slicing against 1000 columns in an sstable
    Table table = Table.open("Keyspace1");
    ColumnFamilyStore cfStore = table.getColumnFamilyStore("Standard1");
    String key = "row3";
    RowMutation rm = new RowMutation("Keyspace1", key);
    ColumnFamily cf = ColumnFamily.create("Keyspace1", "Standard1");
    for (int i = 1000; i < 2000; i++) cf.addColumn(column("col" + i, ("v" + i), 1L));
    rm.add(cf);
    rm.apply();
    cfStore.forceBlockingFlush();

    validateSliceLarge(cfStore);
    // compact so we have a big row with more than the minimum index count
    if (cfStore.getSSTables().size() > 1) {
      CompactionManager.instance.submitMajor(cfStore).get();
    }
    SSTableReader sstable = cfStore.getSSTables().iterator().next();
    DecoratedKey decKey = sstable.getPartitioner().decorateKey(key);
    SSTable.PositionSize info = sstable.getPosition(decKey);
    BufferedRandomAccessFile file = new BufferedRandomAccessFile(sstable.getFilename(), "r");
    file.seek(info.position);
    assert file.readUTF().equals(key);
    file.readInt();
    IndexHelper.skipBloomFilter(file);
    ArrayList<IndexHelper.IndexInfo> indexes = IndexHelper.deserializeIndex(file);
    assert indexes.size() > 2;
    validateSliceLarge(cfStore);
  }
  private void validateGetSliceNoMatch(Table table) throws IOException {
    ColumnFamilyStore cfStore = table.getColumnFamilyStore("Standard2");
    ColumnFamily cf;

    // key before the rows that exists
    cf =
        cfStore.getColumnFamily(
            "a",
            new QueryPath("Standard2"),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            1);
    assertColumns(cf);

    // key after the rows that exist
    cf =
        cfStore.getColumnFamily(
            "z",
            new QueryPath("Standard2"),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            1);
    assertColumns(cf);
  }
  @Test
  public void testGetRowSliceByRange() throws Throwable {
    String key = TEST_KEY + "slicerow";
    Table table = Table.open("Keyspace1");
    ColumnFamilyStore cfStore = table.getColumnFamilyStore("Standard1");
    RowMutation rm = new RowMutation("Keyspace1", key);
    ColumnFamily cf = ColumnFamily.create("Keyspace1", "Standard1");
    // First write "a", "b", "c"
    cf.addColumn(column("a", "val1", 1L));
    cf.addColumn(column("b", "val2", 1L));
    cf.addColumn(column("c", "val3", 1L));
    rm.add(cf);
    rm.apply();

    cf =
        cfStore.getColumnFamily(
            key, new QueryPath("Standard1"), "b".getBytes(), "c".getBytes(), false, 100);
    assertEquals(2, cf.getColumnCount());

    cf =
        cfStore.getColumnFamily(
            key, new QueryPath("Standard1"), "b".getBytes(), "b".getBytes(), false, 100);
    assertEquals(1, cf.getColumnCount());

    cf =
        cfStore.getColumnFamily(
            key, new QueryPath("Standard1"), "b".getBytes(), "c".getBytes(), false, 1);
    assertEquals(1, cf.getColumnCount());

    cf =
        cfStore.getColumnFamily(
            key, new QueryPath("Standard1"), "c".getBytes(), "b".getBytes(), false, 1);
    assertNull(cf);
  }
  @Test
  public void testGetSliceFromAdvanced() throws Throwable {
    // tests slicing against data from one row spread across two sstables
    final Table table = Table.open("Keyspace1");
    final ColumnFamilyStore cfStore = table.getColumnFamilyStore("Standard1");
    final String ROW = "row2";

    RowMutation rm = new RowMutation("Keyspace1", ROW);
    ColumnFamily cf = ColumnFamily.create("Keyspace1", "Standard1");
    cf.addColumn(column("col1", "val1", 1L));
    cf.addColumn(column("col2", "val2", 1L));
    cf.addColumn(column("col3", "val3", 1L));
    cf.addColumn(column("col4", "val4", 1L));
    cf.addColumn(column("col5", "val5", 1L));
    cf.addColumn(column("col6", "val6", 1L));
    rm.add(cf);
    rm.apply();
    cfStore.forceBlockingFlush();

    rm = new RowMutation("Keyspace1", ROW);
    cf = ColumnFamily.create("Keyspace1", "Standard1");
    cf.addColumn(column("col1", "valx", 2L));
    cf.addColumn(column("col2", "valx", 2L));
    cf.addColumn(column("col3", "valx", 2L));
    rm.add(cf);
    rm.apply();

    Runnable verify =
        new WrappedRunnable() {
          public void runMayThrow() throws Exception {
            ColumnFamily cf;

            cf =
                cfStore.getColumnFamily(
                    ROW,
                    new QueryPath("Standard1"),
                    "col2".getBytes(),
                    ArrayUtils.EMPTY_BYTE_ARRAY,
                    false,
                    3);
            assertColumns(cf, "col2", "col3", "col4");
            assertEquals(new String(cf.getColumn("col2".getBytes()).value()), "valx");
            assertEquals(new String(cf.getColumn("col3".getBytes()).value()), "valx");
            assertEquals(new String(cf.getColumn("col4".getBytes()).value()), "val4");
          }
        };

    reTest(table.getColumnFamilyStore("Standard1"), verify);
  }
  private void validateSliceLarge(ColumnFamilyStore cfStore) throws IOException {
    String key = "row3";
    ColumnFamily cf;
    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col1000".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            3);
    assertColumns(cf, "col1000", "col1001", "col1002");
    assertEquals(new String(cf.getColumn("col1000".getBytes()).value()), "v1000");
    assertEquals(new String(cf.getColumn("col1001".getBytes()).value()), "v1001");
    assertEquals(new String(cf.getColumn("col1002".getBytes()).value()), "v1002");

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col1195".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            3);
    assertColumns(cf, "col1195", "col1196", "col1197");
    assertEquals(new String(cf.getColumn("col1195".getBytes()).value()), "v1195");
    assertEquals(new String(cf.getColumn("col1196".getBytes()).value()), "v1196");
    assertEquals(new String(cf.getColumn("col1197".getBytes()).value()), "v1197");

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col1996".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            true,
            1000);
    IColumn[] columns = cf.getSortedColumns().toArray(new IColumn[0]);
    for (int i = 1000; i < 1996; i++) {
      String expectedName = "col" + i;
      IColumn column = columns[i - 1000];
      assert Arrays.equals(column.name(), expectedName.getBytes())
          : cfStore.getComparator().getString(column.name()) + " is not " + expectedName;
      assert Arrays.equals(column.value(), ("v" + i).getBytes());
    }

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col1990".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            3);
    assertColumns(cf, "col1990", "col1991", "col1992");
    assertEquals(new String(cf.getColumn("col1990".getBytes()).value()), "v1990");
    assertEquals(new String(cf.getColumn("col1991".getBytes()).value()), "v1991");
    assertEquals(new String(cf.getColumn("col1992".getBytes()).value()), "v1992");

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            ArrayUtils.EMPTY_BYTE_ARRAY,
            true,
            3);
    assertColumns(cf, "col1997", "col1998", "col1999");
    assertEquals(new String(cf.getColumn("col1999".getBytes()).value()), "v1999");
    assertEquals(new String(cf.getColumn("col1998".getBytes()).value()), "v1998");
    assertEquals(new String(cf.getColumn("col1997".getBytes()).value()), "v1997");

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col9000".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            true,
            3);
    assertColumns(cf, "col1997", "col1998", "col1999");

    cf =
        cfStore.getColumnFamily(
            key,
            new QueryPath("Standard1"),
            "col9000".getBytes(),
            ArrayUtils.EMPTY_BYTE_ARRAY,
            false,
            3);
    assertColumns(cf);
  }
 public static void reTest(ColumnFamilyStore cfs, Runnable verify) throws Exception {
   verify.run();
   cfs.forceBlockingFlush();
   verify.run();
 }