Search in sources :

Example 1 with CreateTableRequest

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.CreateTableRequest in project phoenix by apache.

the class ConnectionQueryServicesImpl method createTable.

@Override
public MetaDataMutationResult createTable(final List<Mutation> tableMetaData, final byte[] physicalTableName, PTableType tableType, Map<String, Object> tableProps, final List<Pair<byte[], Map<String, Object>>> families, byte[][] splits, boolean isNamespaceMapped, final boolean allocateIndexId) throws SQLException {
    byte[][] rowKeyMetadata = new byte[3][];
    Mutation m = MetaDataUtil.getPutOnlyTableHeaderRow(tableMetaData);
    byte[] key = m.getRow();
    SchemaUtil.getVarChars(key, rowKeyMetadata);
    byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
    byte[] schemaBytes = rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
    byte[] tableBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
    byte[] tableName = physicalTableName != null ? physicalTableName : SchemaUtil.getTableNameAsBytes(schemaBytes, tableBytes);
    boolean localIndexTable = false;
    for (Pair<byte[], Map<String, Object>> family : families) {
        if (Bytes.toString(family.getFirst()).startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
            localIndexTable = true;
            break;
        }
    }
    if ((tableType == PTableType.VIEW && physicalTableName != null) || (tableType != PTableType.VIEW && (physicalTableName == null || localIndexTable))) {
        // For views this will ensure that metadata already exists
        // For tables and indexes, this will create the metadata if it doesn't already exist
        ensureTableCreated(tableName, tableType, tableProps, families, splits, true, isNamespaceMapped);
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    if (tableType == PTableType.INDEX) {
        // TODO: if viewIndexId is Short.MIN_VALUE, then we don't need to attempt to create it
        if (physicalTableName != null) {
            if (!localIndexTable && !MetaDataUtil.isMultiTenant(m, kvBuilder, ptr)) {
                ensureViewIndexTableCreated(tenantIdBytes.length == 0 ? null : PNameFactory.newName(tenantIdBytes), physicalTableName, MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
            }
        }
    } else if (tableType == PTableType.TABLE && MetaDataUtil.isMultiTenant(m, kvBuilder, ptr)) {
        // Create view index table up front for multi tenant tables
        ptr.set(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES);
        MetaDataUtil.getMutationValue(m, PhoenixDatabaseMetaData.DEFAULT_COLUMN_FAMILY_NAME_BYTES, kvBuilder, ptr);
        List<Pair<byte[], Map<String, Object>>> familiesPlusDefault = null;
        for (Pair<byte[], Map<String, Object>> family : families) {
            byte[] cf = family.getFirst();
            if (Bytes.compareTo(cf, 0, cf.length, ptr.get(), ptr.getOffset(), ptr.getLength()) == 0) {
                familiesPlusDefault = families;
                break;
            }
        }
        // Don't override if default family already present
        if (familiesPlusDefault == null) {
            byte[] defaultCF = ByteUtil.copyKeyBytesIfNecessary(ptr);
            // Only use splits if table is salted, otherwise it may not be applicable
            // Always add default column family, as we don't know in advance if we'll need it
            familiesPlusDefault = Lists.newArrayList(families);
            familiesPlusDefault.add(new Pair<byte[], Map<String, Object>>(defaultCF, Collections.<String, Object>emptyMap()));
        }
        ensureViewIndexTableCreated(SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, tableType).getBytes(), tableProps, familiesPlusDefault, MetaDataUtil.isSalted(m, kvBuilder, ptr) ? splits : null, MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
    }
    byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);
    MetaDataMutationResult result = metaDataCoprocessorExec(tableKey, new Batch.Call<MetaDataService, MetaDataResponse>() {

        @Override
        public MetaDataResponse call(MetaDataService instance) throws IOException {
            ServerRpcController controller = new ServerRpcController();
            BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
            CreateTableRequest.Builder builder = CreateTableRequest.newBuilder();
            for (Mutation m : tableMetaData) {
                MutationProto mp = ProtobufUtil.toProto(m);
                builder.addTableMetadataMutations(mp.toByteString());
            }
            builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, PHOENIX_MINOR_VERSION, PHOENIX_PATCH_NUMBER));
            if (allocateIndexId) {
                builder.setAllocateIndexId(allocateIndexId);
            }
            CreateTableRequest build = builder.build();
            instance.createTable(controller, build, rpcCallback);
            if (controller.getFailedOn() != null) {
                throw controller.getFailedOn();
            }
            return rpcCallback.get();
        }
    });
    return result;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) KeyValueBuilder(org.apache.phoenix.hbase.index.util.KeyValueBuilder) NonTxIndexBuilder(org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) MutationProto(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto) MetaDataService(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService) Batch(org.apache.hadoop.hbase.client.coprocessor.Batch) BlockingRpcCallback(org.apache.hadoop.hbase.ipc.BlockingRpcCallback) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) Mutation(org.apache.hadoop.hbase.client.Mutation) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) CreateTableRequest(org.apache.phoenix.coprocessor.generated.MetaDataProtos.CreateTableRequest) Pair(org.apache.hadoop.hbase.util.Pair)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 Batch (org.apache.hadoop.hbase.client.coprocessor.Batch)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 BlockingRpcCallback (org.apache.hadoop.hbase.ipc.BlockingRpcCallback)1 ServerRpcController (org.apache.hadoop.hbase.ipc.ServerRpcController)1 MutationProto (org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto)1 Pair (org.apache.hadoop.hbase.util.Pair)1 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)1 CreateTableRequest (org.apache.phoenix.coprocessor.generated.MetaDataProtos.CreateTableRequest)1