Search in sources :

Example 21 with IndexMaintainer

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);
                }
            }
        }
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) Cell(org.apache.hadoop.hbase.Cell) Put(org.apache.hadoop.hbase.client.Put) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter)

Example 22 with IndexMaintainer

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);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) SQLException(java.sql.SQLException) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) PTable(org.apache.phoenix.schema.PTable)

Example 23 with IndexMaintainer

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);
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) SQLException(java.sql.SQLException) Mutation(org.apache.hadoop.hbase.client.Mutation) IOException(java.io.IOException)

Example 24 with IndexMaintainer

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);
    }
}
Also used : SQLException(java.sql.SQLException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) ArrayList(java.util.ArrayList) List(java.util.List) Mutation(org.apache.hadoop.hbase.client.Mutation) Cell(org.apache.hadoop.hbase.Cell) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference)

Example 25 with IndexMaintainer

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();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Aggregations

IndexMaintainer (org.apache.phoenix.index.IndexMaintainer)25 ColumnReference (org.apache.phoenix.hbase.index.covered.update.ColumnReference)13 PTable (org.apache.phoenix.schema.PTable)12 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)10 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)9 IOException (java.io.IOException)8 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)8 Cell (org.apache.hadoop.hbase.Cell)7 Mutation (org.apache.hadoop.hbase.client.Mutation)7 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 ValueGetter (org.apache.phoenix.hbase.index.ValueGetter)6 List (java.util.List)5 Delete (org.apache.hadoop.hbase.client.Delete)5 Put (org.apache.hadoop.hbase.client.Put)5 Scan (org.apache.hadoop.hbase.client.Scan)5 Pair (org.apache.hadoop.hbase.util.Pair)5 PColumn (org.apache.phoenix.schema.PColumn)4 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3