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);
}
}
}
}
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;
}
};
}
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;
}
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;
}
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();
}
}
}
Aggregations