Java Code Examples for org.apache.phoenix.schema.PTable#isImmutableRows()

The following examples show how to use org.apache.phoenix.schema.PTable#isImmutableRows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TephraTransactionContext.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Table getTransactionalTableWriter(PhoenixConnection connection, PTable table, Table htable, boolean isIndex) throws SQLException {
    // If we have indexes, wrap the HTable in a delegate HTable that
    // will attach the necessary index meta data in the event of a
    // rollback
    TransactionAwareHTable transactionAwareHTable;
    // Don't add immutable indexes (those are the only ones that would participate
    // during a commit), as we don't need conflict detection for these.
    if (isIndex) {
        transactionAwareHTable = new TransactionAwareHTable(htable, TxConstants.ConflictDetection.NONE);
        transactionAwareHTable.startTx(getTransaction());
    } else {
        htable = new RollbackHookHTableWrapper(htable, table, connection);
        transactionAwareHTable = new TransactionAwareHTable(htable, table.isImmutableRows() ? TxConstants.ConflictDetection.NONE : TxConstants.ConflictDetection.ROW);
        // Even for immutable, we need to do this so that an abort has the state
        // necessary to generate the rows to delete.
        this.addTransactionAware(transactionAwareHTable);
    }
    return transactionAwareHTable;
}
 
Example 2
Source File: DeleteCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private Set<PTable> getNonDisabledImmutableIndexes(TableRef tableRef) {
    PTable table = tableRef.getTable();
    if (table.isImmutableRows() && !table.getIndexes().isEmpty()) {
        Set<PTable> nonDisabledIndexes = Sets.newHashSetWithExpectedSize(table.getIndexes().size());
        for (PTable index : table.getIndexes()) {
            if (index.getIndexState() != PIndexState.DISABLE) {
                nonDisabledIndexes.add(index);
            }
        }
        return nonDisabledIndexes;
    }
    return Collections.emptySet();
}
 
Example 3
Source File: IndexMaintainer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * For client-side to serialize all IndexMaintainers for a given table
 * @param dataTable data table
 * @param ptr bytes pointer to hold returned serialized value
 * @param indexes indexes to serialize
 */
public static void serialize(PTable dataTable, ImmutableBytesWritable ptr,
        List<PTable> indexes, PhoenixConnection connection) {
    Iterator<PTable> indexesItr = nonDisabledIndexIterator(indexes.iterator());
    if ((dataTable.isImmutableRows()) || !indexesItr.hasNext()) {
        indexesItr = enabledLocalIndexIterator(indexesItr);
        if (!indexesItr.hasNext()) {
            ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
            return;
        }
    }
    int nIndexes = 0;
    int estimatedSize = dataTable.getRowKeySchema().getEstimatedByteSize() + 2;
    while (indexesItr.hasNext()) {
        nIndexes++;
        PTable index = indexesItr.next();
        estimatedSize += index.getIndexMaintainer(dataTable, connection).getEstimatedByteSize();
    }
    TrustedByteArrayOutputStream stream = new TrustedByteArrayOutputStream(estimatedSize + 1);
    DataOutput output = new DataOutputStream(stream);
    try {
        // Encode data table salting in sign of number of indexes
        WritableUtils.writeVInt(output, nIndexes * (dataTable.getBucketNum() == null ? 1 : -1));
        // Write out data row key schema once, since it's the same for all index maintainers
        dataTable.getRowKeySchema().write(output);
        indexesItr =
                dataTable.isImmutableRows() ? enabledLocalIndexIterator(indexes.iterator())
                        : nonDisabledIndexIterator(indexes.iterator());
        while (indexesItr.hasNext()) {
                indexesItr.next().getIndexMaintainer(dataTable, connection).write(output);
        }
    } catch (IOException e) {
        throw new RuntimeException(e); // Impossible
    }
    ptr.set(stream.getBuffer(), 0, stream.size());
}
 
Example 4
Source File: OmidTransactionContext.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public Table getTransactionalTableWriter(PhoenixConnection connection, PTable table, Table htable, boolean isIndex) throws SQLException {
    // When we're getting a table for writing, if the table being written to is an index,
    // write the shadow cells immediately since the only time we write to an index is
    // when we initially populate it synchronously.
    return new OmidTransactionTable(this, htable, table.isImmutableRows() || isIndex, isIndex);
}
 
Example 5
Source File: DeleteCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean isMaintainedOnClient(PTable table) {
    // Test for not being local (rather than being GLOBAL) so that this doesn't fail
    // when tested with our projected table.
    return (table.getIndexType() != IndexType.LOCAL && (table.isTransactional() || table.isImmutableRows())) ||
           (table.getIndexType() == IndexType.LOCAL && (table.isTransactional() &&
            table.getTransactionProvider().getTransactionProvider().isUnsupported(Feature.MAINTAIN_LOCAL_INDEX_ON_SERVER) ) );
}
 
Example 6
Source File: IndexMaintainer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void serializeServerMaintainedIndexes(PTable dataTable, ImmutableBytesWritable ptr,
        List<PTable> indexes, PhoenixConnection connection) {
    Iterator<PTable> indexesItr = Collections.emptyListIterator();
    boolean onlyLocalIndexes = dataTable.isImmutableRows() || dataTable.isTransactional();
    if (onlyLocalIndexes) {
        if (!dataTable.isTransactional()
                || !dataTable.getTransactionProvider().getTransactionProvider().isUnsupported(Feature.MAINTAIN_LOCAL_INDEX_ON_SERVER)) {
            indexesItr = maintainedLocalIndexes(indexes.iterator());
        }
    } else {
        indexesItr = maintainedIndexes(indexes.iterator());
    }

    serialize(dataTable, ptr, Lists.newArrayList(indexesItr), connection);
}
 
Example 7
Source File: IndexUpgradeTool.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private int executeTool(Connection conn,
        ConnectionQueryServices queryServices,
        Configuration conf) {
    ArrayList<String> immutableList = new ArrayList<>();
    ArrayList<String> mutableList = new ArrayList<>();
    for (Map.Entry<String, HashSet<String>> entry :tablesAndIndexes.entrySet()) {
        String dataTableFullName = entry.getKey();
        try {
            PTable dataTable = PhoenixRuntime.getTableNoCache(conn, dataTableFullName);
            if (dataTable.isImmutableRows()) {
                //add to list where immutable tables are processed in a different function
                immutableList.add(dataTableFullName);
            } else {
                mutableList.add(dataTableFullName);
            }
        } catch (SQLException e) {
            LOGGER.severe("Something went wrong while getting the PTable "
                    + dataTableFullName + " " + e);
            return -1;
        }
    }
    long startWaitTime = executeToolForImmutableTables(queryServices, immutableList);
    executeToolForMutableTables(conn, queryServices, conf, mutableList);
    enableImmutableTables(queryServices, immutableList, startWaitTime);
    rebuildIndexes(conn, conf, immutableList);
    if (hasFailure) {
        return -1;
    } else {
        return 0;
    }
}
 
Example 8
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static List<PTable> getClientMaintainedIndexes(PTable table) {
    Iterator<PTable> indexIterator = // Only maintain tables with immutable rows through this client-side mechanism
            (table.isTransactional() && table.getTransactionProvider().getTransactionProvider().isUnsupported(Feature.MAINTAIN_LOCAL_INDEX_ON_SERVER)) ?
                     IndexMaintainer.maintainedIndexes(table.getIndexes().iterator()) :
                         (table.isImmutableRows() || table.isTransactional()) ?
                            IndexMaintainer.maintainedGlobalIndexes(table.getIndexes().iterator()) :
                                Collections.<PTable>emptyIterator();
    return Lists.newArrayList(indexIterator);
}
 
Example 9
Source File: PhoenixMetadata.java    From presto with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> getTableProperties(ConnectorSession session, JdbcTableHandle handle)
{
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();

    try (PhoenixConnection connection = phoenixClient.getConnection(JdbcIdentity.from(session));
            HBaseAdmin admin = connection.getQueryServices().getAdmin()) {
        String schemaName = toPhoenixSchemaName(Optional.ofNullable(handle.getSchemaName())).orElse(null);
        PTable table = getTable(connection, SchemaUtil.getTableName(schemaName, handle.getTableName()));

        boolean salted = table.getBucketNum() != null;
        StringJoiner joiner = new StringJoiner(",");
        List<PColumn> pkColumns = table.getPKColumns();
        for (PColumn pkColumn : pkColumns.subList(salted ? 1 : 0, pkColumns.size())) {
            joiner.add(pkColumn.getName().getString());
        }
        properties.put(PhoenixTableProperties.ROWKEYS, joiner.toString());

        if (table.getBucketNum() != null) {
            properties.put(PhoenixTableProperties.SALT_BUCKETS, table.getBucketNum());
        }
        if (table.isWALDisabled()) {
            properties.put(PhoenixTableProperties.DISABLE_WAL, table.isWALDisabled());
        }
        if (table.isImmutableRows()) {
            properties.put(PhoenixTableProperties.IMMUTABLE_ROWS, table.isImmutableRows());
        }

        String defaultFamilyName = QueryConstants.DEFAULT_COLUMN_FAMILY;
        if (table.getDefaultFamilyName() != null) {
            defaultFamilyName = table.getDefaultFamilyName().getString();
            properties.put(PhoenixTableProperties.DEFAULT_COLUMN_FAMILY, defaultFamilyName);
        }

        HTableDescriptor tableDesc = admin.getTableDescriptor(table.getPhysicalName().getBytes());

        HColumnDescriptor[] columnFamilies = tableDesc.getColumnFamilies();
        for (HColumnDescriptor columnFamily : columnFamilies) {
            if (columnFamily.getNameAsString().equals(defaultFamilyName)) {
                if (!"NONE".equals(columnFamily.getBloomFilterType().toString())) {
                    properties.put(PhoenixTableProperties.BLOOMFILTER, columnFamily.getBloomFilterType().toString());
                }
                if (columnFamily.getMaxVersions() != 1) {
                    properties.put(PhoenixTableProperties.VERSIONS, columnFamily.getMaxVersions());
                }
                if (columnFamily.getMinVersions() > 0) {
                    properties.put(PhoenixTableProperties.MIN_VERSIONS, columnFamily.getMinVersions());
                }
                if (!columnFamily.getCompression().toString().equals("NONE")) {
                    properties.put(PhoenixTableProperties.COMPRESSION, columnFamily.getCompression().toString());
                }
                if (columnFamily.getTimeToLive() < FOREVER) {
                    properties.put(PhoenixTableProperties.TTL, columnFamily.getTimeToLive());
                }
                break;
            }
        }
    }
    catch (IOException | SQLException e) {
        throw new PrestoException(PHOENIX_METADATA_ERROR, "Couldn't get Phoenix table properties", e);
    }
    return properties.build();
}
 
Example 10
Source File: MutationState.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether indexes were added to mutated tables while the transaction was in progress.
 * 
 * @return true if indexes were added and false otherwise.
 * @throws SQLException
 */
private boolean shouldResubmitTransaction(Set<TableRef> txTableRefs) throws SQLException {
    if (LOGGER.isInfoEnabled()) LOGGER.info("Checking for index updates as of " + getInitialWritePointer());
    MetaDataClient client = new MetaDataClient(connection);
    PMetaData cache = connection.getMetaDataCache();
    boolean addedAnyIndexes = false;
    boolean allImmutableTables = !txTableRefs.isEmpty();
    for (TableRef tableRef : txTableRefs) {
        PTable dataTable = tableRef.getTable();
        List<PTable> oldIndexes;
        PTableRef ptableRef = cache.getTableRef(dataTable.getKey());
        oldIndexes = ptableRef.getTable().getIndexes();
        // Always check at server for metadata change, as it's possible that the table is configured to not check
        // for metadata changes
        // but in this case, the tx manager is telling us it's likely that there has been a change.
        MetaDataMutationResult result = client.updateCache(dataTable.getTenantId(), dataTable.getSchemaName()
                .getString(), dataTable.getTableName().getString(), true);
        long timestamp = TransactionUtil.getResolvedTime(connection, result);
        tableRef.setTimeStamp(timestamp);
        PTable updatedDataTable = result.getTable();
        if (updatedDataTable == null) { throw new TableNotFoundException(dataTable.getSchemaName().getString(),
                dataTable.getTableName().getString()); }
        allImmutableTables &= updatedDataTable.isImmutableRows();
        tableRef.setTable(updatedDataTable);
        if (!addedAnyIndexes) {
            // TODO: in theory we should do a deep equals check here, as it's possible
            // that an index was dropped and recreated with the same name but different
            // indexed/covered columns.
            addedAnyIndexes = (!oldIndexes.equals(updatedDataTable.getIndexes()));
            if (LOGGER.isInfoEnabled())
                LOGGER.info((addedAnyIndexes ? "Updates " : "No updates ") + "as of " + timestamp + " to "
                        + updatedDataTable.getName().getString() + " with indexes " + updatedDataTable.getIndexes());
        }
    }
    if (LOGGER.isInfoEnabled())
        LOGGER.info((addedAnyIndexes ? "Updates " : "No updates ") + "to indexes as of " + getInitialWritePointer()
                + " over " + (allImmutableTables ? " all immutable tables" : " some mutable tables"));
    // If all tables are immutable, we know the conflict we got was due to our DDL/DML fence.
    // If any indexes were added, then the conflict might be due to DDL/DML fence.
    return allImmutableTables || addedAnyIndexes;
}