use of org.apache.phoenix.index.IndexMaintainer in project phoenix by apache.
the class DataTableLocalIndexRegionScanner method addMutations.
private void addMutations(List<Cell> dataTableResults) 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);
Put put = null;
Delete del = null;
for (Cell cell : list) {
if (KeyValue.Type.codeToType(cell.getTypeByte()) == KeyValue.Type.Put) {
if (put == null) {
put = new Put(CellUtil.cloneRow(cell));
mutationList.add(put);
}
put.add(cell);
} else {
if (del == null) {
del = new Delete(CellUtil.cloneRow(cell));
mutationList.add(del);
}
del.addDeleteMarker(cell);
}
}
}
}
}
use of org.apache.phoenix.index.IndexMaintainer in project phoenix by apache.
the class IndexHalfStoreFileReaderGenerator method getRepairScanner.
/**
* @param env
* @param store Local Index store
* @param scan
* @param scanType
* @param earliestPutTs
* @param request
* @return StoreScanner for new Local Index data for a passed store and Null if repair is not possible
* @throws IOException
*/
private InternalScanner getRepairScanner(RegionCoprocessorEnvironment env, Store store) throws IOException {
// List<KeyValueScanner> scannersForStoreFiles = Lists.newArrayListWithExpectedSize(store.getStorefilesCount());
Scan scan = new Scan();
scan.setMaxVersions(store.getFamily().getMaxVersions());
for (Store s : env.getRegion().getStores()) {
if (!IndexUtil.isLocalIndexStore(s)) {
scan.addFamily(s.getFamily().getName());
}
}
try {
PhoenixConnection conn = QueryUtil.getConnectionOnServer(env.getConfiguration()).unwrap(PhoenixConnection.class);
PTable dataPTable = IndexUtil.getPDataTable(conn, env.getRegion().getTableDesc());
final List<IndexMaintainer> maintainers = Lists.newArrayListWithExpectedSize(dataPTable.getIndexes().size());
for (PTable index : dataPTable.getIndexes()) {
if (index.getIndexType() == IndexType.LOCAL) {
maintainers.add(index.getIndexMaintainer(dataPTable, conn));
}
}
return new DataTableLocalIndexRegionScanner(env.getRegion().getScanner(scan), env.getRegion(), maintainers, store.getFamily().getName(), env.getConfiguration());
} catch (ClassNotFoundException | SQLException e) {
throw new IOException(e);
}
}
use of org.apache.phoenix.index.IndexMaintainer in project phoenix by apache.
the class IndexUtil method generateDeleteIndexData.
public static List<Delete> generateDeleteIndexData(final PTable table, PTable index, List<Delete> dataMutations, ImmutableBytesWritable ptr, final KeyValueBuilder kvBuilder, PhoenixConnection connection) throws SQLException {
try {
IndexMaintainer maintainer = index.getIndexMaintainer(table, connection);
List<Delete> indexMutations = Lists.newArrayListWithExpectedSize(dataMutations.size());
for (final Mutation dataMutation : dataMutations) {
long ts = MetaDataUtil.getClientTimeStamp(dataMutation);
ptr.set(dataMutation.getRow());
byte[] regionStartKey = null;
byte[] regionEndkey = null;
if (maintainer.isLocalIndex()) {
HRegionLocation tableRegionLocation = connection.getQueryServices().getTableRegionLocation(table.getPhysicalName().getBytes(), dataMutation.getRow());
regionStartKey = tableRegionLocation.getRegionInfo().getStartKey();
regionEndkey = tableRegionLocation.getRegionInfo().getEndKey();
}
Delete delete = maintainer.buildDeleteMutation(kvBuilder, null, ptr, Collections.<KeyValue>emptyList(), ts, regionStartKey, regionEndkey);
delete.setAttribute(PhoenixTransactionContext.TX_ROLLBACK_ATTRIBUTE_KEY, dataMutation.getAttribute(PhoenixTransactionContext.TX_ROLLBACK_ATTRIBUTE_KEY));
indexMutations.add(delete);
}
return indexMutations;
} catch (IOException e) {
throw new SQLException(e);
}
}
use of org.apache.phoenix.index.IndexMaintainer in project phoenix by apache.
the class IndexUtil method generateIndexData.
public static List<Mutation> generateIndexData(final PTable table, PTable index, final MultiRowMutationState multiRowMutationState, List<Mutation> dataMutations, final KeyValueBuilder kvBuilder, PhoenixConnection connection) throws SQLException {
try {
final ImmutableBytesPtr ptr = new ImmutableBytesPtr();
IndexMaintainer maintainer = index.getIndexMaintainer(table, connection);
List<Mutation> indexMutations = Lists.newArrayListWithExpectedSize(dataMutations.size());
for (final Mutation dataMutation : dataMutations) {
long ts = MetaDataUtil.getClientTimeStamp(dataMutation);
ptr.set(dataMutation.getRow());
/*
* We only need to generate the additional mutations for a Put for immutable indexes.
* Deletes of rows are handled by running a re-written query against the index table,
* and Deletes of column values should never be necessary, as you should never be
* updating an existing row.
*/
if (dataMutation instanceof Put) {
ValueGetter valueGetter = new ValueGetter() {
@Override
public byte[] getRowKey() {
return dataMutation.getRow();
}
@Override
public ImmutableBytesWritable getLatestValue(ColumnReference ref, long ts) {
// maintainer to always treat this Put as a new row.
if (isEmptyKeyValue(table, ref)) {
return null;
}
byte[] family = ref.getFamily();
byte[] qualifier = ref.getQualifier();
Map<byte[], List<Cell>> familyMap = dataMutation.getFamilyCellMap();
List<Cell> kvs = familyMap.get(family);
if (kvs == null) {
return null;
}
for (Cell kv : kvs) {
if (Bytes.compareTo(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), family, 0, family.length) == 0 && Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), qualifier, 0, qualifier.length) == 0) {
ImmutableBytesPtr ptr = new ImmutableBytesPtr();
kvBuilder.getValueAsPtr(kv, ptr);
return ptr;
}
}
return null;
}
};
byte[] regionStartKey = null;
byte[] regionEndkey = null;
if (maintainer.isLocalIndex()) {
HRegionLocation tableRegionLocation = connection.getQueryServices().getTableRegionLocation(table.getPhysicalName().getBytes(), dataMutation.getRow());
regionStartKey = tableRegionLocation.getRegionInfo().getStartKey();
regionEndkey = tableRegionLocation.getRegionInfo().getEndKey();
}
indexMutations.add(maintainer.buildUpdateMutation(kvBuilder, valueGetter, ptr, ts, regionStartKey, regionEndkey));
}
}
return indexMutations;
} catch (IOException e) {
throw new SQLException(e);
}
}
use of org.apache.phoenix.index.IndexMaintainer in project phoenix by apache.
the class NonTxIndexBuilderTest method getTestIndexMaintainer.
private IndexMaintainer getTestIndexMaintainer() throws Exception {
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
// disable column encoding, makes debugging easier
props.put(QueryServices.DEFAULT_COLUMN_ENCODED_BYTES_ATRRIB, "0");
Connection conn = DriverManager.getConnection(getUrl(), props);
try {
conn.setAutoCommit(true);
conn.createStatement().execute(TEST_TABLE_DDL);
conn.createStatement().execute(TEST_TABLE_INDEX_DDL);
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), TEST_TABLE_STRING));
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
table.getIndexMaintainers(ptr, pconn);
List<IndexMaintainer> indexMaintainerList = IndexMaintainer.deserialize(ptr, GenericKeyValueBuilder.INSTANCE, true);
assertEquals(1, indexMaintainerList.size());
IndexMaintainer indexMaintainer = indexMaintainerList.get(0);
return indexMaintainer;
} finally {
conn.close();
}
}
Aggregations