Search in sources :

Example 6 with LocalHBaseState

use of org.apache.phoenix.hbase.index.covered.data.LocalHBaseState in project phoenix by apache.

the class TestCoveredColumnIndexCodec method testGeneratedIndexUpdates.

/**
   * Test that we get back the correct index updates for a given column group
   * @throws Exception on failure
   */
@Test
public void testGeneratedIndexUpdates() throws Exception {
    ColumnGroup group = new ColumnGroup("test-column-group");
    group.add(COLUMN_REF);
    final Result emptyState = Result.create(Collections.<Cell>emptyList());
    // setup the state we expect for the codec
    RegionCoprocessorEnvironment env = Mockito.mock(RegionCoprocessorEnvironment.class);
    Configuration conf = new Configuration(false);
    Mockito.when(env.getConfiguration()).thenReturn(conf);
    LocalHBaseState table = new SimpleTableState(emptyState);
    // make a new codec on those kvs
    CoveredColumnIndexCodec codec = CoveredColumnIndexCodec.getCodecForTesting(Arrays.asList(group));
    // start with a basic put that has some keyvalues
    Put p = new Put(PK);
    // setup the kvs to add
    List<KeyValue> kvs = new ArrayList<KeyValue>();
    byte[] v1 = Bytes.toBytes("v1");
    KeyValue kv = new KeyValue(PK, FAMILY, QUAL, 1, v1);
    kvs.add(kv);
    p.add(kv);
    byte[] v2 = Bytes.toBytes("v2");
    kv = new KeyValue(PK, Bytes.toBytes("family2"), QUAL, 1, v2);
    kvs.add(kv);
    p.add(kv);
    // check the codec for deletes it should send
    LocalTableState state = new LocalTableState(env, table, p);
    Iterable<IndexUpdate> updates = codec.getIndexDeletes(state, IndexMetaData.NULL_INDEX_META_DATA);
    assertFalse("Found index updates without any existing kvs in table!", updates.iterator().next().isValid());
    // get the updates with the pending update
    state.setCurrentTimestamp(1);
    state.addPendingUpdates(kvs);
    updates = codec.getIndexUpserts(state, IndexMetaData.NULL_INDEX_META_DATA);
    assertTrue("Didn't find index updates for pending primary table update!", updates.iterator().hasNext());
    for (IndexUpdate update : updates) {
        assertTrue("Update marked as invalid, but should be a pending index write!", update.isValid());
        Put m = (Put) update.getUpdate();
        // should just be the single update for the column reference
        byte[] expected = CoveredColumnIndexCodec.composeRowKey(PK, v1.length, Arrays.asList(toColumnEntry(v1)));
        assertArrayEquals("Didn't get expected index value", expected, m.getRow());
    }
    // then apply a delete
    Delete d = new Delete(PK, 2);
    // need to set the timestamp here, as would actually happen on the server, unlike what happens
    // with puts, where the get the constructor specified timestamp for unspecified methods.
    d.deleteFamily(FAMILY, 2);
    // setup the next batch of 'current state', basically just ripping out the current state from
    // the last round
    table = new SimpleTableState(new Result(kvs));
    state = new LocalTableState(env, table, d);
    state.setCurrentTimestamp(2);
    // check the cleanup of the current table, after the puts (mocking a 'next' update)
    updates = codec.getIndexDeletes(state, IndexMetaData.NULL_INDEX_META_DATA);
    for (IndexUpdate update : updates) {
        assertTrue("Didn't have any index cleanup, even though there is current state", update.isValid());
        Delete m = (Delete) update.getUpdate();
        // should just be the single update for the column reference
        byte[] expected = CoveredColumnIndexCodec.composeRowKey(PK, v1.length, Arrays.asList(toColumnEntry(v1)));
        assertArrayEquals("Didn't get expected index value", expected, m.getRow());
    }
    ensureNoUpdatesWhenCoveredByDelete(env, codec, kvs, d);
    // now with the delete of the columns
    d = new Delete(PK, 2);
    d.deleteColumns(FAMILY, QUAL, 2);
    ensureNoUpdatesWhenCoveredByDelete(env, codec, kvs, d);
    // this delete needs to match timestamps exactly, by contract, to have any effect
    d = new Delete(PK, 1);
    d.deleteColumn(FAMILY, QUAL, 1);
    ensureNoUpdatesWhenCoveredByDelete(env, codec, kvs, d);
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) KeyValue(org.apache.hadoop.hbase.KeyValue) Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) Put(org.apache.hadoop.hbase.client.Put) Result(org.apache.hadoop.hbase.client.Result) LocalTableState(org.apache.phoenix.hbase.index.covered.LocalTableState) RegionCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment) LocalHBaseState(org.apache.phoenix.hbase.index.covered.data.LocalHBaseState) IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) Test(org.junit.Test)

Example 7 with LocalHBaseState

use of org.apache.phoenix.hbase.index.covered.data.LocalHBaseState in project phoenix by apache.

the class TestCoveredColumnIndexCodec method ensureNoUpdatesWhenCoveredByDelete.

private void ensureNoUpdatesWhenCoveredByDelete(RegionCoprocessorEnvironment env, IndexCodec codec, List<KeyValue> currentState, Delete d) throws IOException {
    LocalHBaseState table = new SimpleTableState(new Result(currentState));
    LocalTableState state = new LocalTableState(env, table, d);
    state.setCurrentTimestamp(d.getTimeStamp());
    // now we shouldn't see anything when getting the index update
    state.addPendingUpdates(d.getFamilyMap().get(FAMILY));
    Iterable<IndexUpdate> updates = codec.getIndexUpserts(state, IndexMetaData.NULL_INDEX_META_DATA);
    for (IndexUpdate update : updates) {
        assertFalse("Had some index updates, though it should have been covered by the delete", update.isValid());
    }
}
Also used : LocalHBaseState(org.apache.phoenix.hbase.index.covered.data.LocalHBaseState) IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) Result(org.apache.hadoop.hbase.client.Result) LocalTableState(org.apache.phoenix.hbase.index.covered.LocalTableState)

Aggregations

LocalHBaseState (org.apache.phoenix.hbase.index.covered.data.LocalHBaseState)7 Put (org.apache.hadoop.hbase.client.Put)6 RegionCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment)6 Test (org.junit.Test)6 Scan (org.apache.hadoop.hbase.client.Scan)5 Region (org.apache.hadoop.hbase.regionserver.Region)5 LocalTable (org.apache.phoenix.hbase.index.covered.data.LocalTable)5 ColumnReference (org.apache.phoenix.hbase.index.covered.update.ColumnReference)5 Configuration (org.apache.hadoop.conf.Configuration)4 KeyValue (org.apache.hadoop.hbase.KeyValue)4 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)4 Scanner (org.apache.phoenix.hbase.index.scanner.Scanner)4 List (java.util.List)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Result (org.apache.hadoop.hbase.client.Result)2 IndexUpdate (org.apache.phoenix.hbase.index.covered.IndexUpdate)2 LocalTableState (org.apache.phoenix.hbase.index.covered.LocalTableState)2 ArrayList (java.util.ArrayList)1 Delete (org.apache.hadoop.hbase.client.Delete)1