Search in sources :

Example 11 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class FormatToBytesWritableMapper method initColumnIndexes.

/*
    Map all unique pairs <family, name>  to index. Table name is part of TableRowkey, so we do
    not care about it
     */
private void initColumnIndexes() throws SQLException {
    columnIndexes = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    int columnIndex = 0;
    for (int index = 0; index < logicalNames.size(); index++) {
        PTable table = PhoenixRuntime.getTable(conn, logicalNames.get(index));
        if (!table.getImmutableStorageScheme().equals(ImmutableStorageScheme.ONE_CELL_PER_COLUMN)) {
            List<PColumnFamily> cfs = table.getColumnFamilies();
            for (int i = 0; i < cfs.size(); i++) {
                byte[] family = cfs.get(i).getName().getBytes();
                byte[] cfn = Bytes.add(family, QueryConstants.NAMESPACE_SEPARATOR_BYTES, QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES);
                columnIndexes.put(cfn, new Integer(columnIndex));
                columnIndex++;
            }
        } else {
            List<PColumn> cls = table.getColumns();
            for (int i = 0; i < cls.size(); i++) {
                PColumn c = cls.get(i);
                byte[] family = new byte[0];
                byte[] cq;
                if (!SchemaUtil.isPKColumn(c)) {
                    family = c.getFamilyName().getBytes();
                    cq = c.getColumnQualifierBytes();
                } else {
                    cq = c.getName().getBytes();
                }
                byte[] cfn = Bytes.add(family, QueryConstants.NAMESPACE_SEPARATOR_BYTES, cq);
                if (!columnIndexes.containsKey(cfn)) {
                    columnIndexes.put(cfn, new Integer(columnIndex));
                    columnIndex++;
                }
            }
            byte[] emptyColumnFamily = SchemaUtil.getEmptyColumnFamily(table);
            byte[] emptyKeyValue = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
            byte[] cfn = Bytes.add(emptyColumnFamily, QueryConstants.NAMESPACE_SEPARATOR_BYTES, emptyKeyValue);
            columnIndexes.put(cfn, new Integer(columnIndex));
            columnIndex++;
        }
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) PTable(org.apache.phoenix.schema.PTable)

Example 12 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class GuidePostsCache method invalidateAll.

public void invalidateAll(PTable table) {
    byte[] physicalName = table.getPhysicalName().getBytes();
    List<PColumnFamily> families = table.getColumnFamilies();
    if (families.isEmpty()) {
        invalidate(new GuidePostsKey(physicalName, SchemaUtil.getEmptyColumnFamily(table)));
    } else {
        for (PColumnFamily family : families) {
            invalidate(new GuidePostsKey(physicalName, family.getName().getBytes()));
        }
    }
}
Also used : GuidePostsKey(org.apache.phoenix.schema.stats.GuidePostsKey) PColumnFamily(org.apache.phoenix.schema.PColumnFamily)

Example 13 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class IndexTestUtil method generateIndexData.

public static List<Mutation> generateIndexData(PTable indexTable, PTable dataTable, Mutation dataMutation, ImmutableBytesWritable ptr, KeyValueBuilder builder) throws SQLException {
    byte[] dataRowKey = dataMutation.getRow();
    RowKeySchema dataRowKeySchema = dataTable.getRowKeySchema();
    List<PColumn> dataPKColumns = dataTable.getPKColumns();
    int i = 0;
    int indexOffset = 0;
    Boolean hasValue;
    // Skip salt column
    int maxOffset = dataRowKey.length;
    dataRowKeySchema.iterator(dataRowKey, ptr, dataTable.getBucketNum() == null ? i : ++i);
    List<PColumn> indexPKColumns = indexTable.getPKColumns();
    List<PColumn> indexColumns = indexTable.getColumns();
    int nIndexColumns = indexPKColumns.size();
    int maxIndexValues = indexColumns.size() - nIndexColumns - indexOffset;
    BitSet indexValuesSet = new BitSet(maxIndexValues);
    byte[][] indexValues = new byte[indexColumns.size() - indexOffset][];
    while ((hasValue = dataRowKeySchema.next(ptr, i, maxOffset)) != null) {
        if (hasValue) {
            PColumn dataColumn = dataPKColumns.get(i);
            PColumn indexColumn = indexTable.getColumnForColumnName(IndexUtil.getIndexColumnName(dataColumn));
            coerceDataValueToIndexValue(dataColumn, indexColumn, ptr);
            indexValues[indexColumn.getPosition() - indexOffset] = ptr.copyBytes();
        }
        i++;
    }
    PRow row;
    long ts = MetaDataUtil.getClientTimeStamp(dataMutation);
    if (dataMutation instanceof Delete && dataMutation.getFamilyCellMap().values().isEmpty()) {
        indexTable.newKey(ptr, indexValues);
        row = indexTable.newRow(builder, ts, ptr, false);
        row.delete();
    } else {
        // If no column families in table, then nothing to look for 
        if (!dataTable.getColumnFamilies().isEmpty()) {
            for (Map.Entry<byte[], List<Cell>> entry : dataMutation.getFamilyCellMap().entrySet()) {
                PColumnFamily family = dataTable.getColumnFamily(entry.getKey());
                for (Cell kv : entry.getValue()) {
                    @SuppressWarnings("deprecation") byte[] cq = kv.getQualifier();
                    byte[] emptyKVQualifier = EncodedColumnsUtil.getEmptyKeyValueInfo(dataTable).getFirst();
                    if (Bytes.compareTo(emptyKVQualifier, cq) != 0) {
                        try {
                            PColumn dataColumn = family.getPColumnForColumnQualifier(cq);
                            PColumn indexColumn = indexTable.getColumnForColumnName(IndexUtil.getIndexColumnName(family.getName().getString(), dataColumn.getName().getString()));
                            ptr.set(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
                            coerceDataValueToIndexValue(dataColumn, indexColumn, ptr);
                            indexValues[indexPKColumns.indexOf(indexColumn) - indexOffset] = ptr.copyBytes();
                            if (!SchemaUtil.isPKColumn(indexColumn)) {
                                indexValuesSet.set(indexColumn.getPosition() - nIndexColumns - indexOffset);
                            }
                        } catch (ColumnNotFoundException e) {
                        // Ignore as this means that the data column isn't in the index
                        }
                    }
                }
            }
        }
        indexTable.newKey(ptr, indexValues);
        row = indexTable.newRow(builder, ts, ptr, false);
        int pos = 0;
        while ((pos = indexValuesSet.nextSetBit(pos)) >= 0) {
            int index = nIndexColumns + indexOffset + pos++;
            PColumn indexColumn = indexColumns.get(index);
            row.setValue(indexColumn, indexValues[index]);
        }
    }
    return row.toRowMutations();
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) BitSet(java.util.BitSet) RowKeySchema(org.apache.phoenix.schema.RowKeySchema) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) PRow(org.apache.phoenix.schema.PRow) PColumn(org.apache.phoenix.schema.PColumn) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) List(java.util.List) Map(java.util.Map) Cell(org.apache.hadoop.hbase.Cell)

Example 14 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class PhoenixRuntime method getPColumn.

@Deprecated
private static PColumn getPColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table == null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName == null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) SQLException(java.sql.SQLException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily)

Example 15 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class ConnectionQueryServicesImpl method updateDescriptorForTx.

private void updateDescriptorForTx(PTable table, Map<String, Object> tableProps, HTableDescriptor tableDescriptor, String txValue, Set<HTableDescriptor> descriptorsToUpdate, Set<HTableDescriptor> origDescriptors) throws SQLException {
    byte[] physicalTableName = table.getPhysicalName().getBytes();
    try (HBaseAdmin admin = getAdmin()) {
        setTransactional(tableDescriptor, table.getType(), txValue, tableProps);
        Map<String, Object> indexTableProps;
        if (txValue == null) {
            indexTableProps = Collections.<String, Object>emptyMap();
        } else {
            indexTableProps = Maps.newHashMapWithExpectedSize(1);
            indexTableProps.put(TxConstants.READ_NON_TX_DATA, Boolean.valueOf(txValue));
        }
        for (PTable index : table.getIndexes()) {
            HTableDescriptor indexDescriptor = admin.getTableDescriptor(index.getPhysicalName().getBytes());
            origDescriptors.add(indexDescriptor);
            indexDescriptor = new HTableDescriptor(indexDescriptor);
            descriptorsToUpdate.add(indexDescriptor);
            if (index.getColumnFamilies().isEmpty()) {
                byte[] dataFamilyName = SchemaUtil.getEmptyColumnFamily(table);
                byte[] indexFamilyName = SchemaUtil.getEmptyColumnFamily(index);
                HColumnDescriptor indexColDescriptor = indexDescriptor.getFamily(indexFamilyName);
                HColumnDescriptor tableColDescriptor = tableDescriptor.getFamily(dataFamilyName);
                indexColDescriptor.setMaxVersions(tableColDescriptor.getMaxVersions());
                indexColDescriptor.setValue(TxConstants.PROPERTY_TTL, tableColDescriptor.getValue(TxConstants.PROPERTY_TTL));
            } else {
                for (PColumnFamily family : index.getColumnFamilies()) {
                    byte[] familyName = family.getName().getBytes();
                    indexDescriptor.getFamily(familyName).setMaxVersions(tableDescriptor.getFamily(familyName).getMaxVersions());
                    HColumnDescriptor indexColDescriptor = indexDescriptor.getFamily(familyName);
                    HColumnDescriptor tableColDescriptor = tableDescriptor.getFamily(familyName);
                    indexColDescriptor.setMaxVersions(tableColDescriptor.getMaxVersions());
                    indexColDescriptor.setValue(TxConstants.PROPERTY_TTL, tableColDescriptor.getValue(TxConstants.PROPERTY_TTL));
                }
            }
            setTransactional(indexDescriptor, index.getType(), txValue, indexTableProps);
        }
        try {
            HTableDescriptor indexDescriptor = admin.getTableDescriptor(MetaDataUtil.getViewIndexPhysicalName(physicalTableName));
            origDescriptors.add(indexDescriptor);
            indexDescriptor = new HTableDescriptor(indexDescriptor);
            descriptorsToUpdate.add(indexDescriptor);
            setSharedIndexMaxVersion(table, tableDescriptor, indexDescriptor);
            setTransactional(indexDescriptor, PTableType.INDEX, txValue, indexTableProps);
        } catch (org.apache.hadoop.hbase.TableNotFoundException ignore) {
        // Ignore, as we may never have created a view index table
        }
        try {
            HTableDescriptor indexDescriptor = admin.getTableDescriptor(MetaDataUtil.getLocalIndexPhysicalName(physicalTableName));
            origDescriptors.add(indexDescriptor);
            indexDescriptor = new HTableDescriptor(indexDescriptor);
            descriptorsToUpdate.add(indexDescriptor);
            setSharedIndexMaxVersion(table, tableDescriptor, indexDescriptor);
            setTransactional(indexDescriptor, PTableType.INDEX, txValue, indexTableProps);
        } catch (org.apache.hadoop.hbase.TableNotFoundException ignore) {
        // Ignore, as we may never have created a view index table
        }
    } catch (IOException e) {
        throw ServerUtil.parseServerException(e);
    }
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) PTable(org.apache.phoenix.schema.PTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin)

Aggregations

PColumnFamily (org.apache.phoenix.schema.PColumnFamily)22 PColumn (org.apache.phoenix.schema.PColumn)13 PTable (org.apache.phoenix.schema.PTable)10 SQLException (java.sql.SQLException)6 ColumnFamilyNotFoundException (org.apache.phoenix.schema.ColumnFamilyNotFoundException)6 ColumnNotFoundException (org.apache.phoenix.schema.ColumnNotFoundException)6 List (java.util.List)4 Map (java.util.Map)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 TreeMap (java.util.TreeMap)3 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)3 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)3 Pair (org.apache.hadoop.hbase.util.Pair)3 BaseTerminalExpression (org.apache.phoenix.expression.BaseTerminalExpression)3 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)3 Expression (org.apache.phoenix.expression.Expression)3 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)3 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)3