Search in sources :

Example 6 with IndexUpdate

use of org.apache.phoenix.hbase.index.covered.IndexUpdate 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 IndexUpdate

use of org.apache.phoenix.hbase.index.covered.IndexUpdate 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)

Example 8 with IndexUpdate

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

the class PhoenixTransactionalIndexer method generatePuts.

private boolean generatePuts(PhoenixIndexMetaData indexMetaData, Collection<Pair<Mutation, byte[]>> indexUpdates, TxTableState state) throws IOException {
    state.applyMutation();
    Iterable<IndexUpdate> puts = codec.getIndexUpserts(state, indexMetaData);
    boolean validPut = false;
    for (IndexUpdate put : puts) {
        if (put.isValid()) {
            indexUpdates.add(new Pair<Mutation, byte[]>(put.getUpdate(), put.getTableName()));
            validPut = true;
        }
    }
    return validPut;
}
Also used : IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) Mutation(org.apache.hadoop.hbase.client.Mutation) MultiMutation(org.apache.phoenix.hbase.index.MultiMutation)

Example 9 with IndexUpdate

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

the class PhoenixTransactionalIndexer method generateDeletes.

private void generateDeletes(PhoenixIndexMetaData indexMetaData, Collection<Pair<Mutation, byte[]>> indexUpdates, byte[] attribValue, TxTableState state) throws IOException {
    Iterable<IndexUpdate> deletes = codec.getIndexDeletes(state, indexMetaData);
    for (IndexUpdate delete : deletes) {
        if (delete.isValid()) {
            delete.getUpdate().setAttribute(TxConstants.TX_ROLLBACK_ATTRIBUTE_KEY, attribValue);
            indexUpdates.add(new Pair<Mutation, byte[]>(delete.getUpdate(), delete.getTableName()));
        }
    }
}
Also used : IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) Mutation(org.apache.hadoop.hbase.client.Mutation) MultiMutation(org.apache.phoenix.hbase.index.MultiMutation)

Example 10 with IndexUpdate

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

the class CoveredColumnIndexCodec method getIndexUpdateForGroup.

/**
     * @param group
     * @param state
     * @return the update that should be made to the table
     */
private IndexUpdate getIndexUpdateForGroup(ColumnGroup group, TableState state, IndexMetaData indexMetaData) {
    List<CoveredColumn> refs = group.getColumns();
    try {
        Pair<Scanner, IndexUpdate> stateInfo = ((LocalTableState) state).getIndexedColumnsTableState(refs, false, false, indexMetaData);
        Scanner kvs = stateInfo.getFirst();
        Pair<Integer, List<ColumnEntry>> columns = getNextEntries(refs, kvs, state.getCurrentRowKey());
        // make sure we close the scanner
        kvs.close();
        if (columns.getFirst().intValue() == 0) {
            return stateInfo.getSecond();
        }
        // have all the column entries, so just turn it into a Delete for the row
        // convert the entries to the needed values
        byte[] rowKey = composeRowKey(state.getCurrentRowKey(), columns.getFirst(), columns.getSecond());
        Put p = new Put(rowKey, state.getCurrentTimestamp());
        // add the columns to the put
        addColumnsToPut(p, columns.getSecond());
        // update the index info
        IndexUpdate update = stateInfo.getSecond();
        update.setTable(Bytes.toBytes(group.getTable()));
        update.setUpdate(p);
        return update;
    } catch (IOException e) {
        throw new RuntimeException("Unexpected exception when getting state for columns: " + refs);
    }
}
Also used : Scanner(org.apache.phoenix.hbase.index.scanner.Scanner) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) LocalTableState(org.apache.phoenix.hbase.index.covered.LocalTableState) IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IndexUpdate (org.apache.phoenix.hbase.index.covered.IndexUpdate)11 ArrayList (java.util.ArrayList)4 Mutation (org.apache.hadoop.hbase.client.Mutation)4 MultiMutation (org.apache.phoenix.hbase.index.MultiMutation)4 LocalTableState (org.apache.phoenix.hbase.index.covered.LocalTableState)4 Delete (org.apache.hadoop.hbase.client.Delete)3 Put (org.apache.hadoop.hbase.client.Put)3 IOException (java.io.IOException)2 List (java.util.List)2 Result (org.apache.hadoop.hbase.client.Result)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2 ValueGetter (org.apache.phoenix.hbase.index.ValueGetter)2 LocalHBaseState (org.apache.phoenix.hbase.index.covered.data.LocalHBaseState)2 Scanner (org.apache.phoenix.hbase.index.scanner.Scanner)2 Configuration (org.apache.hadoop.conf.Configuration)1 KeyValue (org.apache.hadoop.hbase.KeyValue)1 RegionCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment)1 ColumnReference (org.apache.phoenix.hbase.index.covered.update.ColumnReference)1 Test (org.junit.Test)1