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

The following examples show how to use org.apache.phoenix.schema.PTable#isMultiTenant() . 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: IndexUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[][] getViewConstants(PTable dataTable) {
    if (dataTable.getType() != PTableType.VIEW && dataTable.getType() != PTableType.PROJECTED) return null;
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    List<byte[]> viewConstants = new ArrayList<byte[]>();
    List<PColumn> dataPkColumns = dataTable.getPKColumns();
    for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
        PColumn dataPKColumn = dataPkColumns.get(i);
        if (dataPKColumn.getViewConstant() != null) {
            if (IndexUtil.getViewConstantValue(dataPKColumn, ptr)) {
                viewConstants.add(ByteUtil.copyKeyBytesIfNecessary(ptr));
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return viewConstants.isEmpty() ? null : viewConstants
            .toArray(new byte[viewConstants.size()][]);
}
 
Example 2
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static List<PColumn> getPkColumns(PTable ptable, Connection conn) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 3
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static SelectStatement prependTenantAndViewConstants(PTable table, SelectStatement select, String tenantId, Set<PColumn> addViewColumns, boolean useServerTimestamp) {
    if ((!table.isMultiTenant() || tenantId == null) && table.getViewIndexId() == null && addViewColumns.isEmpty() && !useServerTimestamp) {
        return select;
    }
    List<AliasedNode> selectNodes = newArrayListWithCapacity(select.getSelect().size() + 1 + addViewColumns.size());
    if (table.getViewIndexId() != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(table.getViewIndexId())));
    }
    if (table.isMultiTenant() && tenantId != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(tenantId)));
    }
    selectNodes.addAll(select.getSelect());
    for (PColumn column : addViewColumns) {
        byte[] byteValue = column.getViewConstant();
        Object value = column.getDataType().toObject(byteValue, 0, byteValue.length-1);
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(value)));
    }
    if (useServerTimestamp) {
        PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
        selectNodes.add(new AliasedNode(null, getNodeForRowTimestampColumn(rowTimestampCol)));
    }
    return SelectStatement.create(select, selectNodes);
}
 
Example 4
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Decode a byte array value back into the Object values of the
 * primary key constraint. If the connection and table are both
 * tenant-specific, the tenant ID column is not expected to have
 * been encoded and will not appear in the returned values.
 * @param conn an open connection
 * @param name the full table name
 * @param encodedValue the value that was encoded with {@link #encodePK(Connection, String, Object[])}
 * @return the Object values encoded in the byte array value
 * @throws SQLException
 */
@Deprecated
public static Object[] decodePK(Connection conn, String name, byte[] value) throws SQLException {
    PTable table = getTable(conn, name);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
    int nValues = table.getPKColumns().size() - offset;
    RowKeySchema schema = table.getRowKeySchema();
    Object[] values = new Object[nValues];
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    schema.iterator(value, ptr);
    int i = 0;
    int fieldIdx = offset;
    while (i < nValues && schema.next(ptr, fieldIdx, value.length) != null) {
        values[i] = schema.getField(fieldIdx).getDataType().toObject(ptr);
        i++;
        fieldIdx++;
    }
    return values;
}
 
Example 5
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 6
Source File: ExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Called by visitor to resolve a column expression node into a column reference.
 * Derived classes may use this as a hook to trap all column resolves.
 * @param node a column expression node
 * @return a resolved ColumnRef
 * @throws SQLException if the column expression node does not refer to a known/unambiguous column
 */
protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
    ColumnRef ref = null;
    try {
        ref = context.getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    } catch (ColumnNotFoundException e) {
        // Rather than not use a local index when a column not contained by it is referenced, we
        // join back to the data table in our coprocessor since this is a relatively cheap
        // operation given that we know the join is local.
        if (context.getCurrentTable().getTable().getIndexType() == IndexType.LOCAL) {
            try {
                return new LocalIndexDataColumnRef(context, node.getName());
            } catch (ColumnFamilyNotFoundException c) {
                throw e;
            }
        } else {
            throw e;
        }
    }
    PTable table = ref.getTable();
    int pkPosition = ref.getPKSlotPosition();
    // Disallow explicit reference to salting column, tenant ID column, and index ID column
    if (pkPosition >= 0) {
        boolean isSalted = table.getBucketNum() != null;
        boolean isMultiTenant = context.getConnection().getTenantId() != null && table.isMultiTenant();
        boolean isSharedViewIndex = table.getViewIndexId() != null;
        int minPosition = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
        if (pkPosition < minPosition) {
            throw new ColumnNotFoundException(table.getSchemaName().getString(), table.getTableName().getString(), null, ref.getColumn().getName().getString());
        }
    }
    return ref;
}
 
Example 7
Source File: IndexExpressionParseNodeRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public IndexExpressionParseNodeRewriter(PTable index, String alias, PhoenixConnection connection, Map<String, UDFParseNode> udfParseNodes) throws SQLException {
      indexedParseNodeToColumnParseNodeMap = Maps.newHashMapWithExpectedSize(index.getColumns().size());
      NamedTableNode tableNode = NamedTableNode.create(alias,
              TableName.create(index.getParentSchemaName().getString(), index.getParentTableName().getString()),
              Collections.<ColumnDef> emptyList());
      ColumnResolver dataResolver = FromCompiler.getResolver(tableNode, connection, udfParseNodes);
      StatementContext context = new StatementContext(new PhoenixStatement(connection), dataResolver);
      IndexStatementRewriter rewriter = new IndexStatementRewriter(dataResolver, null, true);
      ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
      int indexPosOffset = (index.getBucketNum() == null ? 0 : 1) + (index.isMultiTenant() ? 1 : 0) + (index.getViewIndexId() == null ? 0 : 1);
      List<PColumn> pkColumns = index.getPKColumns();
for (int i=indexPosOffset; i<pkColumns.size(); ++i) {
      	PColumn column = pkColumns.get(i);
      	String expressionStr = IndexUtil.getIndexColumnExpressionStr(column);
          ParseNode expressionParseNode  = SQLParser.parseCondition(expressionStr);
          String colName = "\"" + column.getName().getString() + "\"";
          Expression dataExpression = expressionParseNode.accept(expressionCompiler);
          PDataType expressionDataType = dataExpression.getDataType();
          ParseNode indexedParseNode = expressionParseNode.accept(rewriter);
          PDataType indexColType = IndexUtil.getIndexColumnDataType(dataExpression.isNullable(), expressionDataType);
          ParseNode columnParseNode = new ColumnParseNode(alias!=null ? TableName.create(null, alias) : null, colName, null);
          if ( indexColType != expressionDataType) {
              columnParseNode = NODE_FACTORY.cast(columnParseNode, expressionDataType, null, null);
          }
          indexedParseNodeToColumnParseNodeMap.put(indexedParseNode, columnParseNode);
      }
  }
 
Example 8
Source File: BaseResultIterators.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private int computeColumnsInCommon() {
    PTable dataTable;
    if ((dataTable=dataPlan.getTableRef().getTable()).getBucketNum() != null) { // unable to compute prefix range for salted data table
        return 0;
    }

    PTable table = getTable();
    int nColumnsOffset = dataTable.isMultiTenant() ? 1 :0;
    int nColumnsInCommon = nColumnsOffset;
    List<PColumn> dataPKColumns = dataTable.getPKColumns();
    List<PColumn> indexPKColumns = table.getPKColumns();
    int nIndexPKColumns = indexPKColumns.size();
    int nDataPKColumns = dataPKColumns.size();
    // Skip INDEX_ID and tenant ID columns
    for (int i = 1 + nColumnsInCommon; i < nIndexPKColumns; i++) {
        PColumn indexColumn = indexPKColumns.get(i);
        String indexColumnName = indexColumn.getName().getString();
        String cf = IndexUtil.getDataColumnFamilyName(indexColumnName);
        if (cf.length() != 0) {
            break;
        }
        if (i > nDataPKColumns) {
            break;
        }
        PColumn dataColumn = dataPKColumns.get(i-1);
        String dataColumnName = dataColumn.getName().getString();
        // Ensure both name and type are the same. Because of the restrictions we have
        // on PK column types (namely that you can only have a fixed width nullable
        // column as your last column), the type check is more of a sanity check
        // since it wouldn't make sense to have an index with every column in common.
        if (indexColumn.getDataType() == dataColumn.getDataType() 
                && dataColumnName.equals(IndexUtil.getDataColumnName(indexColumnName))) {
            nColumnsInCommon++;
            continue;
        }
        break;
    }
    return nColumnsInCommon;
}
 
Example 9
Source File: ExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Called by visitor to resolve a column expression node into a column reference.
 * Derived classes may use this as a hook to trap all column resolves.
 * @param node a column expression node
 * @return a resolved ColumnRef
 * @throws SQLException if the column expression node does not refer to a known/unambiguous column
 */
protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
    ColumnRef ref = null;
    try {
        ref = context.getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    } catch (ColumnNotFoundException e) {
        // Rather than not use a local index when a column not contained by it is referenced, we
        // join back to the data table in our coprocessor since this is a relatively cheap
        // operation given that we know the join is local.
        if (context.getCurrentTable().getTable().getIndexType() == IndexType.LOCAL) {
            try {
                return new LocalIndexDataColumnRef(context, context.getCurrentTable(), node.getName());
            } catch (ColumnFamilyNotFoundException c) {
                throw e;
            }
        } else {
            throw e;
        }
    }
    PTable table = ref.getTable();
    int pkPosition = ref.getPKSlotPosition();
    // Disallow explicit reference to salting column, tenant ID column, and index ID column
    if (pkPosition >= 0) {
        boolean isSalted = table.getBucketNum() != null;
        boolean isMultiTenant = context.getConnection().getTenantId() != null && table.isMultiTenant();
        boolean isSharedViewIndex = table.getViewIndexId() != null;
        int minPosition = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
        if (pkPosition < minPosition) {
            throw new ColumnNotFoundException(table.getSchemaName().getString(), table.getTableName().getString(), null, ref.getColumn().getName().getString());
        }
    }
    return ref;
}
 
Example 10
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 11
Source File: ExpressionUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * For table with salted/multiTenant/viewIndexId,some leading rowkey columns should be skipped.
 * @param table
 * @param phoenixConnection
 * @return
 */
public static int getRowKeyColumnOffset(PTable table, PhoenixConnection phoenixConnection) {
    boolean isSalted = table.getBucketNum() != null;
    boolean isMultiTenant = phoenixConnection.getTenantId() != null && table.isMultiTenant();
    boolean isSharedViewIndex = table.getViewIndexId() != null;
    return (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
}
 
Example 12
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Encode the primary key values from the table as a byte array. The values must
 * be in the same order as the primary key constraint. If the connection and
 * table are both tenant-specific, the tenant ID column must not be present in
 * the values.
 * @param conn an open connection
 * @param fullTableName the full table name
 * @param values the values of the primary key columns ordered in the same order
 *  as the primary key constraint
 * @return the encoded byte array
 * @throws SQLException if the table cannot be found or the incorrect number of
 *  of values are provided
 * @see #decodePK(Connection, String, byte[]) to decode the byte[] back to the
 *  values
 */
@Deprecated
public static byte[] encodePK(Connection conn, String fullTableName, Object[] values) throws SQLException {
    PTable table = getTable(conn, fullTableName);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
    List<PColumn> pkColumns = table.getPKColumns();
    if (pkColumns.size() - offset != values.length) {
        throw new SQLException("Expected " + (pkColumns.size() - offset) + " but got " + values.length);
    }
    PDataType type = null;
    TrustedByteArrayOutputStream output = new TrustedByteArrayOutputStream(table.getRowKeySchema().getEstimatedValueLength());
    try {
        for (int i = offset; i < pkColumns.size(); i++) {
            if (type != null && !type.isFixedWidth()) {
                output.write(QueryConstants.SEPARATOR_BYTE);
            }
            type = pkColumns.get(i).getDataType();

            //for fixed width data types like CHAR and BINARY, we need to pad values to be of max length.
            Object paddedObj = type.pad(values[i - offset], pkColumns.get(i).getMaxLength());
            byte[] value = type.toBytes(paddedObj);
            output.write(value);
        }
        return output.toByteArray();
    } finally {
        try {
            output.close();
        } catch (IOException e) {
            throw new RuntimeException(e); // Impossible
        }
    }
}
 
Example 13
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void serializeViewConstantsIntoScan(Scan scan, PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            byte[][] viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
            serializeViewConstantsIntoScan(viewConstants, scan);
        }
    }
}
 
Example 14
Source File: IndexExpressionParseNodeRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public IndexExpressionParseNodeRewriter(PTable index, PhoenixConnection connection) throws SQLException {
      indexedParseNodeToColumnParseNodeMap = Maps.newHashMapWithExpectedSize(index.getColumns().size());
      NamedTableNode tableNode = NamedTableNode.create(null,
              TableName.create(index.getParentSchemaName().getString(), index.getParentTableName().getString()),
              Collections.<ColumnDef> emptyList());
      ColumnResolver dataResolver = FromCompiler.getResolver(tableNode, connection);
      StatementContext context = new StatementContext(new PhoenixStatement(connection), dataResolver);
      IndexStatementRewriter rewriter = new IndexStatementRewriter(dataResolver, null);
      ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
      ColumnParseNodeVisitor columnParseNodeVisitor = new ColumnParseNodeVisitor();
      int indexPosOffset = (index.getBucketNum() == null ? 0 : 1) + (index.isMultiTenant() ? 1 : 0) + (index.getViewIndexId() == null ? 0 : 1);
      List<PColumn> pkColumns = index.getPKColumns();
for (int i=indexPosOffset; i<pkColumns.size(); ++i) {
      	PColumn column = pkColumns.get(i);
      	String expressionStr = IndexUtil.getIndexColumnExpressionStr(column);
          ParseNode expressionParseNode  = SQLParser.parseCondition(expressionStr);
          columnParseNodeVisitor.reset();
          expressionParseNode.accept(columnParseNodeVisitor);
          String colName = column.getName().getString();
          if (columnParseNodeVisitor.isParseNodeCaseSensitive()) {
              // force column name to be case sensitive name by surround with double quotes
              colName = "\"" + colName + "\"";
          }
          
          Expression dataExpression = expressionParseNode.accept(expressionCompiler);
          PDataType expressionDataType = dataExpression.getDataType();
          ParseNode indexedParseNode = expressionParseNode.accept(rewriter);
          PDataType indexColType = IndexUtil.getIndexColumnDataType(dataExpression.isNullable(), expressionDataType);
          ParseNode columnParseNode = new ColumnParseNode(null, colName, null);
          if ( indexColType != expressionDataType) {
              columnParseNode = NODE_FACTORY.cast(columnParseNode, expressionDataType, null, null);
          }
          indexedParseNodeToColumnParseNodeMap.put(indexedParseNode, columnParseNode);
      }
  }
 
Example 15
Source File: TrackOrderPreservingExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
TrackOrderPreservingExpressionCompiler(StatementContext context, GroupBy groupBy, int expectedEntrySize, Ordering ordering, TupleProjector tupleProjector) {
    super(context, groupBy);
    PTable table = context.getResolver().getTables().get(0).getTable();
    boolean isSalted = table.getBucketNum() != null;
    boolean isMultiTenant = context.getConnection().getTenantId() != null && table.isMultiTenant();
    boolean isSharedViewIndex = table.getViewIndexId() != null;
    // TODO: util for this offset, as it's computed in numerous places
    positionOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
    entries = Lists.newArrayListWithExpectedSize(expectedEntrySize);
    this.ordering = ordering;
    this.tupleProjector = tupleProjector;
}
 
Example 16
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static int getMinPKOffset(PTable table, PName tenantId) {
    // In SELECT *, don't include tenant column or index ID column for tenant connection
    int posOffset = table.getBucketNum() == null ? 0 : 1;
    if (table.isMultiTenant() && tenantId != null) {
        posOffset++;
    }
    if (table.getViewIndexId() != null) {
        posOffset++;
    }
    return posOffset;
}
 
Example 17
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 18
Source File: MetaDataEndpointImpl.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * @param tableName parent table's name
 * Looks for whether child views exist for the table specified by table.
 * TODO: should we pass a timestamp here?
 */
private TableViewFinderResult findChildViews(HRegion region, byte[] tenantId, PTable table) throws IOException {
    byte[] schemaName = table.getSchemaName().getBytes();
    byte[] tableName = table.getTableName().getBytes();
    boolean isMultiTenant = table.isMultiTenant();
    Scan scan = new Scan();
    // If the table is multi-tenant, we need to check across all tenant_ids,
    // so we can't constrain the row key. Otherwise, any views would have
    // the same tenantId.
    if (!isMultiTenant) {
        byte[] startRow = ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY);
        byte[] stopRow = ByteUtil.nextKey(startRow);
        scan.setStartRow(startRow);
        scan.setStopRow(stopRow);
    }
    SingleColumnValueFilter linkFilter = new SingleColumnValueFilter(TABLE_FAMILY_BYTES, LINK_TYPE_BYTES, CompareOp.EQUAL, PHYSICAL_TABLE_BYTES);
    linkFilter.setFilterIfMissing(true);
    byte[] suffix = ByteUtil.concat(QueryConstants.SEPARATOR_BYTE_ARRAY, SchemaUtil.getTableNameAsBytes(schemaName, tableName));
    SuffixFilter rowFilter = new SuffixFilter(suffix);
    Filter filter = new FilterList(linkFilter, rowFilter);
    scan.setFilter(filter);
    scan.addColumn(TABLE_FAMILY_BYTES, LINK_TYPE_BYTES);
    // Original region-only scanner modified due to PHOENIX-1208
    // RegionScanner scanner = region.getScanner(scan);
    // The following *should* work, but doesn't due to HBASE-11837
    // TableName systemCatalogTableName = region.getTableDesc().getTableName();
    // HTableInterface hTable = env.getTable(systemCatalogTableName);
    // These deprecated calls work around the issue
    HTableInterface hTable = ServerUtil.getHTableForCoprocessorScan(env, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
    try {
        boolean allViewsInCurrentRegion = true;
        int numOfChildViews = 0;
        List<Result> results = Lists.newArrayList();
        ResultScanner scanner = hTable.getScanner(scan);
        try {
            for (Result result = scanner.next(); (result != null); result = scanner.next()) {
                numOfChildViews++;
                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
                ResultTuple resultTuple = new ResultTuple(result);
                resultTuple.getKey(ptr);
                byte[] key = ptr.copyBytes();
                if (checkTableKeyInRegion(key, region) != null) {
                    allViewsInCurrentRegion = false;
                }
                results.add(result);
            }
            TableViewFinderResult tableViewFinderResult = new TableViewFinderResult(results);
            if (numOfChildViews > 0 && !allViewsInCurrentRegion) {
                tableViewFinderResult.setAllViewsNotInSingleRegion();
            }
            return tableViewFinderResult;
        } finally {
                scanner.close();
        }
    } finally {
        hTable.close();
    }
}
 
Example 19
Source File: IndexMetaDataCacheClient.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static ServerCache setMetaDataOnMutations(PhoenixConnection connection, PTable table, List<? extends Mutation> mutations,
        ImmutableBytesWritable indexMetaDataPtr) throws SQLException {
    final byte[] tenantIdBytes;
    if (table.isMultiTenant()) {
        tenantIdBytes = connection.getTenantId() == null ? null : ScanUtil.getTenantIdBytes(
                table.getRowKeySchema(), table.getBucketNum() != null, connection.getTenantId(),
                table.getViewIndexId() != null);
    } else {
        tenantIdBytes = connection.getTenantId() == null ? null : connection.getTenantId().getBytes();
    }
    ServerCache cache = null;
    byte[] attribValue = null;
    byte[] uuidValue = null;
    byte[] txState = ByteUtil.EMPTY_BYTE_ARRAY;
    if (table.isTransactional()) {
        txState = connection.getMutationState().encodeTransaction();
    }
    boolean hasIndexMetaData = indexMetaDataPtr.getLength() > 0;
    if (hasIndexMetaData) {
        if (useIndexMetadataCache(connection, mutations, indexMetaDataPtr.getLength() + txState.length)) {
            IndexMetaDataCacheClient client = new IndexMetaDataCacheClient(connection, table);
            cache = client.addIndexMetadataCache(mutations, indexMetaDataPtr, txState);
            uuidValue = cache.getId();
        } else {
            attribValue = ByteUtil.copyKeyBytesIfNecessary(indexMetaDataPtr);
            uuidValue = ServerCacheClient.generateId();
        }
    } else if (txState.length == 0) { return null; }
    // Either set the UUID to be able to access the index metadata from the cache
    // or set the index metadata directly on the Mutation
    for (Mutation mutation : mutations) {
        if (connection.getTenantId() != null) {
            mutation.setAttribute(PhoenixRuntime.TENANT_ID_ATTRIB, tenantIdBytes);
        }
        mutation.setAttribute(PhoenixIndexCodec.INDEX_UUID, uuidValue);
        if (attribValue != null) {
            mutation.setAttribute(PhoenixIndexCodec.INDEX_PROTO_MD, attribValue);
            mutation.setAttribute(BaseScannerRegionObserver.CLIENT_VERSION,
                    Bytes.toBytes(MetaDataProtocol.PHOENIX_VERSION));
            if (txState.length > 0) {
                mutation.setAttribute(BaseScannerRegionObserver.TX_STATE, txState);
            }
        } else if (!hasIndexMetaData && txState.length > 0) {
            mutation.setAttribute(BaseScannerRegionObserver.TX_STATE, txState);
        }
    }
    return cache;
}
 
Example 20
Source File: ServerCacheClient.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public boolean addServerCache(Table htable, byte[] key, final PTable cacheUsingTable, final byte[] cacheId,
        final ImmutableBytesWritable cachePtr, final ServerCacheFactory cacheFactory, final byte[] txState, final boolean usePersistentCache)
        throws Exception {
    byte[] keyInRegion = getKeyInRegion(key);
    final Map<byte[], AddServerCacheResponse> results;

    AddServerCacheRequest.Builder builder = AddServerCacheRequest.newBuilder();
    final byte[] tenantIdBytes;
    if (cacheUsingTable.isMultiTenant()) {
        try {
            tenantIdBytes = connection.getTenantId() == null ? null
                    : ScanUtil.getTenantIdBytes(cacheUsingTable.getRowKeySchema(),
                    cacheUsingTable.getBucketNum() != null, connection.getTenantId(),
                    cacheUsingTable.getViewIndexId() != null);
        } catch (SQLException e) {
            throw new IOException(e);
        }
    } else {
        tenantIdBytes = connection.getTenantId() == null ? null
                : connection.getTenantId().getBytes();
    }
    if (tenantIdBytes != null) {
        builder.setTenantId(ByteStringer.wrap(tenantIdBytes));
    }
    builder.setCacheId(ByteStringer.wrap(cacheId));
    builder.setUsePersistentCache(usePersistentCache);
    builder.setCachePtr(org.apache.phoenix.protobuf.ProtobufUtil.toProto(cachePtr));
    builder.setHasProtoBufIndexMaintainer(true);
    ServerCacheFactoryProtos.ServerCacheFactory.Builder svrCacheFactoryBuider = ServerCacheFactoryProtos.ServerCacheFactory
            .newBuilder();
    svrCacheFactoryBuider.setClassName(cacheFactory.getClass().getName());
    builder.setCacheFactory(svrCacheFactoryBuider.build());
    builder.setTxState(ByteStringer.wrap(txState));
    builder.setClientVersion(MetaDataProtocol.PHOENIX_VERSION);
    final AddServerCacheRequest request = builder.build();

    try {
        results = htable.coprocessorService(ServerCachingService.class, keyInRegion, keyInRegion,
                new Batch.Call<ServerCachingService, AddServerCacheResponse>() {
                    @Override
                    public AddServerCacheResponse call(ServerCachingService instance) throws IOException {
                        ServerRpcController controller = new ServerRpcController();
                        BlockingRpcCallback<AddServerCacheResponse> rpcCallback = new BlockingRpcCallback<AddServerCacheResponse>();
                        instance.addServerCache(controller, request, rpcCallback);
                        if (controller.getFailedOn() != null) { throw controller.getFailedOn(); }
                        return rpcCallback.get();
                    }
                });
    } catch (Throwable t) {
        throw new Exception(t);
    }
    if (results != null && results.size() == 1) { return results.values().iterator().next().getReturn(); }
    return false;
}