public PTableRef getTableRef(PTableKey key) throws TableNotFoundException { return metaData.getTableRef(key); }
@Override public PTableRef getTableRef(PTableKey key) throws TableNotFoundException { readWriteLock.readLock().lock(); try { return delegate.getTableRef(key); } finally { readWriteLock.readLock().unlock(); } }
public PTable getTable(PTableKey key) throws TableNotFoundException { return metaData.getTableRef(key).getTable(); }
private HashSet<String> existingColumnFamiliesForBaseTable(PName baseTableName) throws TableNotFoundException { throwConnectionClosedIfNullMetaData(); PTable table = latestMetaData.getTableRef(new PTableKey(null, baseTableName.getString())).getTable(); return existingColumnFamilies(table); }
@Override public MetaDataMutationResult getTable(PName tenantId, byte[] schemaBytes, byte[] tableBytes, long tableTimestamp, long clientTimestamp, boolean skipAddingIndexes, boolean skipCombiningColumns, PTable ancestorTable) 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); } }
private static boolean hasInactiveIndex(PMetaData metaCache, PTableKey key) throws TableNotFoundException { PTable table = metaCache.getTableRef(key).getTable(); for (PTable index : table.getIndexes()) { if (index.getIndexState() == PIndexState.INACTIVE) { return true; } } return false; }
@Test public void testNullAlterTableDefaultRemoved() throws Exception { String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY)"; String ddl2 = "ALTER TABLE table_with_default " + "ADD v CHAR(3) DEFAULT null"; Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute(ddl); conn.createStatement().execute(ddl2); PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache() .getTableRef(new PTableKey(null,"TABLE_WITH_DEFAULT")).getTable(); assertNull(table.getColumnForColumnName("V").getExpressionStr()); }
private static boolean hasIndexWithState(PMetaData metaCache, PTableKey key, PIndexState expectedState) throws TableNotFoundException { PTable table = metaCache.getTableRef(key).getTable(); for (PTable index : table.getIndexes()) { if (index.getIndexState() == expectedState) { return true; } } return false; }
private static PTable getFromTable(PMetaData metaData, String name, TestTimeKeeper timeKeeper) throws TableNotFoundException { PTable table = metaData.getTableRef(new PTableKey(null,name)).getTable(); timeKeeper.incrementTime(); return table; }
@Override public void addTable(PTable table, long resolvedTime) throws SQLException { synchronized (latestMetaDataLock) { try { throwConnectionClosedIfNullMetaData(); // If existing table isn't older than new table, don't replace // If a client opens a connection at an earlier timestamp, this can happen PTable existingTable = latestMetaData.getTableRef(new PTableKey(table.getTenantId(), table.getName().getString())).getTable(); if (existingTable.getTimeStamp() >= table.getTimeStamp()) { return; } } catch (TableNotFoundException e) {} latestMetaData.addTable(table, resolvedTime); latestMetaDataLock.notifyAll(); } } @Override
@Test public void testNullDefaultRemoved() throws Exception { String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY, " + "v VARCHAR DEFAULT null)"; Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute(ddl); PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache() .getTableRef(new PTableKey(null,"TABLE_WITH_DEFAULT")).getTable(); assertNull(table.getColumnForColumnName("V").getExpressionStr()); }
@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.builderWithColumns(index, getColumnsToClone(index)) .setState(newState == PIndexState.USABLE ? PIndexState.ACTIVE : newState == PIndexState.UNUSABLE ? PIndexState.INACTIVE : newState) .build(); return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, index); }
private PTable getTable(PName tenantId, String fullTableName, long timestamp) throws SQLException { PTable table; try { PMetaData metadata = latestMetaData; throwConnectionClosedIfNullMetaData(); table = metadata.getTableRef(new PTableKey(tenantId, fullTableName)).getTable(); if (table.getTimeStamp() >= timestamp) { // Table in cache is newer than client timestamp which shouldn't be // the case throw new TableNotFoundException(table.getSchemaName().getString(), table.getTableName().getString()); } } catch (TableNotFoundException e) { byte[] schemaName = Bytes.toBytes(SchemaUtil.getSchemaNameFromFullName(fullTableName)); byte[] tableName = Bytes.toBytes(SchemaUtil.getTableNameFromFullName(fullTableName)); MetaDataMutationResult result = this.getTable(tenantId, schemaName, tableName, HConstants.LATEST_TIMESTAMP, timestamp, false, false, null); table = result.getTable(); if (table == null) { throw e; } } return table; }
private static void assertUseStatsForQueryFlag(String tableName, PhoenixConnection conn, Boolean expected) throws TableNotFoundException, SQLException { assertEquals(expected, conn.unwrap(PhoenixConnection.class).getMetaDataCache() .getTableRef(new PTableKey(null, tableName)).getTable() .useStatsForParallelization()); String query = "SELECT USE_STATS_FOR_PARALLELIZATION FROM SYSTEM.CATALOG WHERE TABLE_NAME = ? AND COLUMN_NAME IS NULL AND COLUMN_FAMILY IS NULL AND TENANT_ID IS NULL"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, tableName); ResultSet rs = stmt.executeQuery(); rs.next(); boolean b = rs.getBoolean(1); if (expected == null) { assertTrue(rs.wasNull()); } else { assertEquals(expected, b); } }
PTableKey key = new PTableKey(null,fullTableName); PMetaData metaCache = conn1.unwrap(PhoenixConnection.class).getMetaDataCache(); PTable table = metaCache.getTableRef(key).getTable(); for (PTable index : table.getIndexes()) { assertEquals(PIndexState.DISABLE, index.getIndexState());
public KeyRangeClipTest(String tableDef, List<Object> startValues, List<Object> endValues, int clipTo, KeyRange expectedOutput) throws SQLException { PhoenixConnection pconn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class); pconn.createStatement().execute("CREATE TABLE T(" + tableDef+ ")"); PTable table = pconn.getMetaDataCache().getTableRef(new PTableKey(null,"T")).getTable(); this.schema = table.getRowKeySchema(); byte[] lowerRange = getRange(pconn, startValues); byte[] upperRange = getRange(pconn, endValues); this.input = KeyRange.getKeyRange(lowerRange, upperRange); this.expectedOutput = expectedOutput; this.clipTo = clipTo; }
try (PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class)) { pconn.createStatement().execute("CREATE TABLE " + tableName + " (k DECIMAL PRIMARY KEY)"); final PTable table = pconn.getMetaDataCache().getTableRef(new PTableKey(null, tableName)).getTable(); KeyPart baseKeyPart = new KeyPart() {
private static void invalidateStats(Connection conn, String tableName) throws SQLException { PTable ptable = conn.unwrap(PhoenixConnection.class) .getMetaDataCache().getTableRef(new PTableKey(null, tableName)) .getTable(); byte[] name = ptable.getPhysicalName().getBytes(); conn.unwrap(PhoenixConnection.class).getQueryServices().invalidateStats(new GuidePostsKey(name, SchemaUtil.getEmptyColumnFamily(ptable))); }
@Test public void testRegionsOnlineCheck() throws Throwable { String schemaName = generateUniqueName(); String tableName = generateUniqueName(); final String fullTableName = SchemaUtil.getTableName(schemaName, tableName); PTableKey key = new PTableKey(null,fullTableName); try (Connection conn = DriverManager.getConnection(getUrl())) { PMetaData metaCache = conn.unwrap(PhoenixConnection.class).getMetaDataCache(); conn.createStatement().execute("CREATE TABLE " + fullTableName + "(k VARCHAR PRIMARY KEY)"); conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a')"); conn.commit(); Configuration conf = conn.unwrap(PhoenixConnection.class).getQueryServices().getConfiguration(); PTable table = metaCache.getTableRef(key).getTable(); assertTrue(MetaDataUtil.tableRegionsOnline(conf, table)); try (Admin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) { admin.disableTable(TableName.valueOf(fullTableName)); assertFalse(MetaDataUtil.tableRegionsOnline(conf, table)); admin.enableTable(TableName.valueOf(fullTableName)); } assertTrue(MetaDataUtil.tableRegionsOnline(conf, table)); } }
@Test public void testAge() throws Exception { TestTimeKeeper timeKeeper = new TestTimeKeeper(); Map<String, String> props = Maps.newHashMapWithExpectedSize(2); props.put(QueryServices.MAX_CLIENT_METADATA_CACHE_SIZE_ATTRIB, "10"); props.put(QueryServices.CLIENT_CACHE_ENCODING, "object"); PMetaData metaData = new PMetaDataImpl(5, timeKeeper, new ReadOnlyProps(props)); String tableName = "a"; addToTable(metaData, tableName, 1, timeKeeper); PTableRef aTableRef = metaData.getTableRef(new PTableKey(null,tableName)); assertNotNull(aTableRef); assertEquals(1, metaData.getAge(aTableRef)); tableName = "b"; addToTable(metaData, tableName, 1, timeKeeper); PTableRef bTableRef = metaData.getTableRef(new PTableKey(null,tableName)); assertNotNull(bTableRef); assertEquals(1, metaData.getAge(bTableRef)); assertEquals(2, metaData.getAge(aTableRef)); }