Search in sources :

Example 1 with ValueGetter

use of org.apache.phoenix.hbase.index.ValueGetter in project phoenix by apache.

the class DataTableLocalIndexRegionScanner method getLocalIndexCellsFromDataTable.

private void getLocalIndexCellsFromDataTable(List<Cell> dataTableResults, List<Cell> localIndexResults) throws IOException {
    if (!dataTableResults.isEmpty()) {
        result.setKeyValues(dataTableResults);
        for (IndexMaintainer maintainer : indexMaintainers) {
            result.getKey(ptr);
            ValueGetter valueGetter = maintainer.createGetterFromKeyValues(ImmutableBytesPtr.copyBytesIfNecessary(ptr), dataTableResults);
            List<Cell> list = maintainer.buildUpdateMutation(kvBuilder, valueGetter, ptr, dataTableResults.get(0).getTimestamp(), startKey, endKey).getFamilyCellMap().get(localIndexFamily);
            if (list != null) {
                localIndexResults.addAll(list);
            }
        }
    }
}
Also used : IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) Cell(org.apache.hadoop.hbase.Cell) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter)

Example 2 with ValueGetter

use of org.apache.phoenix.hbase.index.ValueGetter in project phoenix by apache.

the class IndexMaintainer method createGetterFromKeyValues.

public ValueGetter createGetterFromKeyValues(final byte[] rowKey, Collection<? extends Cell> pendingUpdates) {
    final Map<ColumnReference, ImmutableBytesPtr> valueMap = Maps.newHashMapWithExpectedSize(pendingUpdates.size());
    for (Cell kv : pendingUpdates) {
        // create new pointers to each part of the kv
        ImmutableBytesPtr value = new ImmutableBytesPtr(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
        valueMap.put(new ColumnReference(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()), value);
    }
    return new ValueGetter() {

        @Override
        public ImmutableBytesWritable getLatestValue(ColumnReference ref) {
            if (ref.equals(dataEmptyKeyValueRef))
                return null;
            return valueMap.get(ref);
        }

        @Override
        public byte[] getRowKey() {
            return rowKey;
        }
    };
}
Also used : ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Cell(org.apache.hadoop.hbase.Cell) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter)

Example 3 with ValueGetter

use of org.apache.phoenix.hbase.index.ValueGetter in project phoenix by apache.

the class PhoenixIndexCodec method getIndexUpserts.

@Override
public Iterable<IndexUpdate> getIndexUpserts(TableState state, IndexMetaData context) throws IOException {
    PhoenixIndexMetaData metaData = (PhoenixIndexMetaData) context;
    List<IndexMaintainer> indexMaintainers = metaData.getIndexMaintainers();
    if (indexMaintainers.get(0).isRowDeleted(state.getPendingUpdate())) {
        return Collections.emptyList();
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    ptr.set(state.getCurrentRowKey());
    List<IndexUpdate> indexUpdates = Lists.newArrayList();
    for (IndexMaintainer maintainer : indexMaintainers) {
        Pair<ValueGetter, IndexUpdate> statePair = state.getIndexUpdateState(maintainer.getAllColumns(), metaData.ignoreNewerMutations(), false, context);
        ValueGetter valueGetter = statePair.getFirst();
        IndexUpdate indexUpdate = statePair.getSecond();
        indexUpdate.setTable(maintainer.isLocalIndex() ? state.getEnvironment().getRegion().getTableDesc().getName() : maintainer.getIndexTableName());
        Put put = maintainer.buildUpdateMutation(KV_BUILDER, valueGetter, ptr, state.getCurrentTimestamp(), env.getRegion().getRegionInfo().getStartKey(), env.getRegion().getRegionInfo().getEndKey());
        indexUpdate.setUpdate(put);
        indexUpdates.add(indexUpdate);
    }
    return indexUpdates;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) Put(org.apache.hadoop.hbase.client.Put) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter)

Example 4 with ValueGetter

use of org.apache.phoenix.hbase.index.ValueGetter in project phoenix by apache.

the class PhoenixIndexCodec method getIndexDeletes.

@Override
public Iterable<IndexUpdate> getIndexDeletes(TableState state, IndexMetaData context) throws IOException {
    PhoenixIndexMetaData metaData = (PhoenixIndexMetaData) context;
    List<IndexMaintainer> indexMaintainers = metaData.getIndexMaintainers();
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    ptr.set(state.getCurrentRowKey());
    List<IndexUpdate> indexUpdates = Lists.newArrayList();
    for (IndexMaintainer maintainer : indexMaintainers) {
        // For transactional tables, we use an index maintainer
        // to aid in rollback if there's a KeyValue column in the index. The alternative would be
        // to hold on to all uncommitted index row keys (even ones already sent to HBase) on the
        // client side.
        Set<ColumnReference> cols = Sets.newHashSet(maintainer.getAllColumns());
        cols.add(new ColumnReference(indexMaintainers.get(0).getDataEmptyKeyValueCF(), indexMaintainers.get(0).getEmptyKeyValueQualifier()));
        Pair<ValueGetter, IndexUpdate> statePair = state.getIndexUpdateState(cols, metaData.ignoreNewerMutations(), true, context);
        ValueGetter valueGetter = statePair.getFirst();
        if (valueGetter != null) {
            IndexUpdate indexUpdate = statePair.getSecond();
            indexUpdate.setTable(maintainer.isLocalIndex() ? state.getEnvironment().getRegion().getTableDesc().getName() : maintainer.getIndexTableName());
            Delete delete = maintainer.buildDeleteMutation(KV_BUILDER, valueGetter, ptr, state.getPendingUpdate(), state.getCurrentTimestamp(), env.getRegion().getRegionInfo().getStartKey(), env.getRegion().getRegionInfo().getEndKey());
            indexUpdate.setUpdate(delete);
            indexUpdates.add(indexUpdate);
        }
    }
    return indexUpdates;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) IndexUpdate(org.apache.phoenix.hbase.index.covered.IndexUpdate) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter)

Example 5 with ValueGetter

use of org.apache.phoenix.hbase.index.ValueGetter in project phoenix by apache.

the class IndexMaintainerTest method testIndexRowKeyBuilding.

private void testIndexRowKeyBuilding(String schemaName, String tableName, String dataColumns, String pk, String indexColumns, Object[] values, String includeColumns, String dataProps, String indexProps, KeyValueBuilder builder) throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    String fullTableName = SchemaUtil.getTableName(SchemaUtil.normalizeIdentifier(schemaName), SchemaUtil.normalizeIdentifier(tableName));
    String fullIndexName = SchemaUtil.getTableName(SchemaUtil.normalizeIdentifier(schemaName), SchemaUtil.normalizeIdentifier("idx"));
    conn.createStatement().execute("CREATE TABLE " + fullTableName + "(" + dataColumns + " CONSTRAINT pk PRIMARY KEY (" + pk + "))  " + (dataProps.isEmpty() ? "" : dataProps));
    try {
        conn.createStatement().execute("CREATE INDEX idx ON " + fullTableName + "(" + indexColumns + ") " + (includeColumns.isEmpty() ? "" : "INCLUDE (" + includeColumns + ") ") + (indexProps.isEmpty() ? "" : indexProps));
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName));
        PTable index = pconn.getTable(new PTableKey(pconn.getTenantId(), fullIndexName));
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        table.getIndexMaintainers(ptr, pconn);
        List<IndexMaintainer> c1 = IndexMaintainer.deserialize(ptr, builder, true);
        assertEquals(1, c1.size());
        IndexMaintainer im1 = c1.get(0);
        StringBuilder buf = new StringBuilder("UPSERT INTO " + fullTableName + " VALUES(");
        for (int i = 0; i < values.length; i++) {
            buf.append("?,");
        }
        buf.setCharAt(buf.length() - 1, ')');
        PreparedStatement stmt = conn.prepareStatement(buf.toString());
        for (int i = 0; i < values.length; i++) {
            stmt.setObject(i + 1, values[i]);
        }
        stmt.execute();
        Iterator<Pair<byte[], List<KeyValue>>> iterator = PhoenixRuntime.getUncommittedDataIterator(conn);
        List<KeyValue> dataKeyValues = iterator.next().getSecond();
        Map<ColumnReference, byte[]> valueMap = Maps.newHashMapWithExpectedSize(dataKeyValues.size());
        byte[] row = dataKeyValues.get(0).getRow();
        ImmutableBytesWritable rowKeyPtr = new ImmutableBytesWritable(row);
        Put dataMutation = new Put(rowKeyPtr.copyBytes());
        for (KeyValue kv : dataKeyValues) {
            valueMap.put(new ColumnReference(kv.getFamily(), kv.getQualifier()), kv.getValue());
            dataMutation.add(kv);
        }
        ValueGetter valueGetter = newValueGetter(row, valueMap);
        List<Mutation> indexMutations = IndexTestUtil.generateIndexData(index, table, dataMutation, ptr, builder);
        assertEquals(1, indexMutations.size());
        assertTrue(indexMutations.get(0) instanceof Put);
        Mutation indexMutation = indexMutations.get(0);
        ImmutableBytesWritable indexKeyPtr = new ImmutableBytesWritable(indexMutation.getRow());
        ptr.set(rowKeyPtr.get(), rowKeyPtr.getOffset(), rowKeyPtr.getLength());
        byte[] mutablelndexRowKey = im1.buildRowKey(valueGetter, ptr, null, null);
        byte[] immutableIndexRowKey = indexKeyPtr.copyBytes();
        assertArrayEquals(immutableIndexRowKey, mutablelndexRowKey);
        for (ColumnReference ref : im1.getCoveredColumns()) {
            valueMap.get(ref);
        }
        byte[] dataRowKey = im1.buildDataRowKey(indexKeyPtr, null);
        assertArrayEquals(dataRowKey, dataKeyValues.get(0).getRow());
    } finally {
        try {
            conn.createStatement().execute("DROP TABLE " + fullTableName);
        } finally {
            conn.close();
        }
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) KeyValue(org.apache.hadoop.hbase.KeyValue) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PreparedStatement(java.sql.PreparedStatement) PTable(org.apache.phoenix.schema.PTable) Put(org.apache.hadoop.hbase.client.Put) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter) Mutation(org.apache.hadoop.hbase.client.Mutation) PTableKey(org.apache.phoenix.schema.PTableKey) Pair(org.apache.hadoop.hbase.util.Pair) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference)

Aggregations

ValueGetter (org.apache.phoenix.hbase.index.ValueGetter)8 ColumnReference (org.apache.phoenix.hbase.index.covered.update.ColumnReference)5 Cell (org.apache.hadoop.hbase.Cell)4 Put (org.apache.hadoop.hbase.client.Put)4 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)4 Mutation (org.apache.hadoop.hbase.client.Mutation)3 IndexMaintainer (org.apache.phoenix.index.IndexMaintainer)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 KeyValue (org.apache.hadoop.hbase.KeyValue)2 Delete (org.apache.hadoop.hbase.client.Delete)2 Pair (org.apache.hadoop.hbase.util.Pair)2 IndexUpdate (org.apache.phoenix.hbase.index.covered.IndexUpdate)2 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)2 PTable (org.apache.phoenix.schema.PTable)2 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Configuration (org.apache.hadoop.conf.Configuration)1