@Test
 public void testGetTableName() {
   String tableDisplayName = SchemaUtil.getTableName("schemaName", "tableName");
   assertEquals(tableDisplayName, "schemaName.tableName");
   tableDisplayName = SchemaUtil.getTableName(null, "tableName");
   assertEquals(tableDisplayName, "tableName");
 }
 @Override
 public MetaDataMutationResult updateIndexState(
     List<Mutation> tableMetadata, String parentTableName) throws SQLException {
   byte[][] rowKeyMetadata = new byte[3][];
   SchemaUtil.getVarChars(tableMetadata.get(0).getRow(), rowKeyMetadata);
   Mutation m = MetaDataUtil.getTableHeaderRow(tableMetadata);
   ImmutableBytesWritable ptr = new ImmutableBytesWritable();
   if (!MetaDataUtil.getMutationValue(m, INDEX_STATE_BYTES, kvBuilder, ptr)) {
     throw new IllegalStateException();
   }
   PIndexState newState = PIndexState.fromSerializedValue(ptr.get()[ptr.getOffset()]);
   byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
   String schemaName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX]);
   String indexName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX]);
   String indexTableName = SchemaUtil.getTableName(schemaName, indexName);
   PName tenantId = tenantIdBytes.length == 0 ? null : PNameFactory.newName(tenantIdBytes);
   PTable index = metaData.getTableRef(new PTableKey(tenantId, indexTableName)).getTable();
   index =
       PTableImpl.makePTable(
           index,
           newState == PIndexState.USABLE
               ? PIndexState.ACTIVE
               : newState == PIndexState.UNUSABLE ? PIndexState.INACTIVE : newState);
   return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, index);
 }
示例#3
0
 protected TableRef createTableRef(NamedTableNode tableNode, boolean updateCacheImmediately)
     throws SQLException {
   String tableName = tableNode.getName().getTableName();
   String schemaName = tableNode.getName().getSchemaName();
   long timeStamp = QueryConstants.UNSET_TIMESTAMP;
   String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
   PName tenantId = connection.getTenantId();
   PTable theTable = null;
   if (updateCacheImmediately || connection.getAutoCommit()) {
     MetaDataMutationResult result = client.updateCache(schemaName, tableName);
     timeStamp = result.getMutationTime();
     theTable = result.getTable();
     if (theTable == null) {
       throw new TableNotFoundException(schemaName, tableName, timeStamp);
     }
   } else {
     try {
       theTable = connection.getMetaDataCache().getTable(new PTableKey(tenantId, fullTableName));
     } catch (TableNotFoundException e1) {
       if (tenantId != null) { // Check with null tenantId next
         try {
           theTable = connection.getMetaDataCache().getTable(new PTableKey(null, fullTableName));
         } catch (TableNotFoundException e2) {
         }
       }
     }
     // We always attempt to update the cache in the event of a TableNotFoundException
     if (theTable == null) {
       MetaDataMutationResult result = client.updateCache(schemaName, tableName);
       if (result.wasUpdated()) {
         timeStamp = result.getMutationTime();
         theTable = result.getTable();
       }
     }
     if (theTable == null) {
       throw new TableNotFoundException(schemaName, tableName, timeStamp);
     }
   }
   // Add any dynamic columns to the table declaration
   List<ColumnDef> dynamicColumns = tableNode.getDynamicColumns();
   theTable = addDynamicColumns(dynamicColumns, theTable);
   TableRef tableRef =
       new TableRef(tableNode.getAlias(), theTable, timeStamp, !dynamicColumns.isEmpty());
   if (logger.isDebugEnabled() && timeStamp != QueryConstants.UNSET_TIMESTAMP) {
     logger.debug(
         "Re-resolved stale table "
             + fullTableName
             + " with seqNum "
             + tableRef.getTable().getSequenceNumber()
             + " at timestamp "
             + tableRef.getTable().getTimeStamp()
             + " with "
             + tableRef.getTable().getColumns().size()
             + " columns: "
             + tableRef.getTable().getColumns());
   }
   return tableRef;
 }
示例#4
0
 @Override
 public TableRef resolveTable(String schemaName, String tableName) throws SQLException {
   String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
   List<TableRef> tableRefs = tableMap.get(fullTableName);
   if (tableRefs.size() == 0) {
     throw new TableNotFoundException(fullTableName);
   } else if (tableRefs.size() > 1) {
     throw new AmbiguousTableException(tableName);
   } else {
     return tableRefs.get(0);
   }
 }
 @Override
 public MetaDataMutationResult getTable(
     PName tenantId,
     byte[] schemaBytes,
     byte[] tableBytes,
     long tableTimestamp,
     long clientTimestamp)
     throws SQLException {
   // Return result that will cause client to use it's own metadata instead of needing
   // to get anything from the server (since we don't have a connection)
   try {
     String fullTableName = SchemaUtil.getTableName(schemaBytes, tableBytes);
     PTable table = metaData.getTableRef(new PTableKey(tenantId, fullTableName)).getTable();
     return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, table, true);
   } catch (TableNotFoundException e) {
     return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, 0, null);
   }
 }
示例#6
0
  private void assertIteration(String dataColumns, String pk, Object[] values, String dataProps)
      throws Exception {
    String schemaName = "";
    String tableName = "T";
    Connection conn = DriverManager.getConnection(getUrl());
    String fullTableName =
        SchemaUtil.getTableName(
            SchemaUtil.normalizeIdentifier(schemaName), SchemaUtil.normalizeIdentifier(tableName));
    conn.createStatement()
        .execute(
            "CREATE TABLE "
                + fullTableName
                + "("
                + dataColumns
                + " CONSTRAINT pk PRIMARY KEY ("
                + pk
                + "))  "
                + (dataProps.isEmpty() ? "" : dataProps));
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName));
    conn.close();
    StringBuilder buf = new StringBuilder("UPSERT INTO " + fullTableName + " VALUES(");
    for (int i = 0; i < values.length; i++) {
      buf.append("?,");
    }
    buf.setCharAt(buf.length() - 1, ')');
    PreparedStatement stmt = conn.prepareStatement(buf.toString());
    for (int i = 0; i < values.length; i++) {
      stmt.setObject(i + 1, values[i]);
    }
    stmt.execute();
    Iterator<Pair<byte[], List<KeyValue>>> iterator =
        PhoenixRuntime.getUncommittedDataIterator(conn);
    List<KeyValue> dataKeyValues = iterator.next().getSecond();
    KeyValue keyValue = dataKeyValues.get(0);

    List<SortOrder> sortOrders = Lists.newArrayListWithExpectedSize(table.getPKColumns().size());
    for (PColumn col : table.getPKColumns()) {
      sortOrders.add(col.getSortOrder());
    }
    RowKeySchema schema = table.getRowKeySchema();
    int minOffset = keyValue.getRowOffset();
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    int nExpectedValues = values.length;
    for (int i = values.length - 1; i >= 0; i--) {
      if (values[i] == null) {
        nExpectedValues--;
      } else {
        break;
      }
    }
    int i = 0;
    int maxOffset =
        schema.iterator(keyValue.getRowArray(), minOffset, keyValue.getRowLength(), ptr);
    for (i = 0; i < schema.getFieldCount(); i++) {
      Boolean hasValue = schema.next(ptr, i, maxOffset);
      if (hasValue == null) {
        break;
      }
      assertTrue(hasValue);
      PDataType type = PDataType.fromLiteral(values[i]);
      SortOrder sortOrder = sortOrders.get(i);
      Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder);
      assertEquals(values[i], value);
    }
    assertEquals(nExpectedValues, i);
    assertNull(schema.next(ptr, i, maxOffset));

    for (i--; i >= 0; i--) {
      Boolean hasValue = schema.previous(ptr, i, minOffset);
      if (hasValue == null) {
        break;
      }
      assertTrue(hasValue);
      PDataType type = PDataType.fromLiteral(values[i]);
      SortOrder sortOrder = sortOrders.get(i);
      Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder);
      assertEquals(values[i], value);
    }
    assertEquals(-1, i);
    assertNull(schema.previous(ptr, i, minOffset));
  }