public TableValueStyle getTableType( final ConnectionSupplier connectionSupplier, final TableReference tableRef, TableReference metadataTable) { try { return valueStyleByTableRef.get(tableRef, () -> { SqlConnection conn = connectionSupplier.get(); AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( String.format( "SELECT table_size FROM %s WHERE table_name = ?", metadataTable.getQualifiedName()), tableRef.getQualifiedName()); Preconditions.checkArgument( !results.rows().isEmpty(), "table %s not found", tableRef.getQualifiedName()); return TableValueStyle.byId(Iterables.getOnlyElement(results.rows()).getInteger("table_size")); }); } catch (ExecutionException e) { log.error("TableValueStyle for the table {} could not be retrieved.", tableRef.getQualifiedName()); throw Throwables.propagate(e); } }
@Override public Set<TableReference> getAllTableNames() { return run(conn -> { AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT table_name FROM " + config.metadataTable().getQualifiedName()); Set<TableReference> ret = Sets.newHashSetWithExpectedSize(results.size()); for (AgnosticResultRow row : results.rows()) { ret.add(TableReference.createUnsafe(row.getString("table_name"))); } return ret; }); }
@Before public void setup() { SqlConnection mockConnection = mock(SqlConnection.class); when(connectionSupplier.get()).thenReturn(mockConnection); AgnosticResultSet resultSet = mock(AgnosticResultSet.class); when(mockConnection.selectResultSetUnregisteredQuery(startsWith("SELECT table_size FROM"), anyObject())) .thenReturn(resultSet); AgnosticResultRow row = mock(AgnosticResultRow.class); when(row.getInteger(eq("table_size"))).thenReturn(TableValueStyle.OVERFLOW.getId()); doReturn(ImmutableList.of(row)).when(resultSet).rows(); when(mockConnection.getUnderlyingConnection()).thenReturn(mock(Connection.class)); }
@Test public void shouldReturnIfTableMappingExists() throws TableMappingNotFoundException { when(resultSet.size()).thenReturn(1); AgnosticResultRow row = mock(AgnosticResultRow.class); when(row.getString(eq("short_table_name"))).thenReturn(SHORT_TABLE_NAME); doReturn(ImmutableList.of(row)).when(resultSet).rows(); String shortName = oracleTableNameUnmapper .getShortTableNameFromMappingTable(connectionSupplier, TEST_PREFIX, TABLE_REF); assertThat(shortName, is(SHORT_TABLE_NAME)); }
@Override @SuppressWarnings("deprecation") public byte[] getMetadata() { AgnosticResultSet results = conns.get().selectResultSetUnregisteredQuery( "SELECT value FROM " + config.metadataTable().getQualifiedName() + " WHERE table_name = ?", tableRef.getQualifiedName()); if (results.size() < 1) { return PtBytes.EMPTY_BYTE_ARRAY; } else { return MoreObjects.firstNonNull( Iterables.getOnlyElement(results.rows()).getBytes("value"), PtBytes.EMPTY_BYTE_ARRAY); } }
@Override public Map<TableReference, byte[]> getMetadataForTables() { return run(conn -> { AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT table_name, value FROM " + config.metadataTable().getQualifiedName()); Map<TableReference, byte[]> ret = Maps.newHashMapWithExpectedSize(results.size()); for (AgnosticResultRow row : results.rows()) { ret.put(TableReference.createUnsafe(row.getString("table_name")), row.getBytes("value")); } return ret; }); }
@SuppressWarnings("checkstyle:NestedTryDepth") public String getShortTableNameFromMappingTable( ConnectionSupplier connectionSupplier, String tablePrefix, TableReference tableRef) throws TableMappingNotFoundException { String fullTableName = tablePrefix + DbKvs.internalTableName(tableRef); try { return unmappingCache.get(fullTableName, () -> { SqlConnection conn = connectionSupplier.get(); AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT short_table_name " + "FROM " + AtlasDbConstants.ORACLE_NAME_MAPPING_TABLE + " WHERE table_name = ?", fullTableName); if (results.size() == 0) { throw new TableMappingNotFoundException( "The table " + fullTableName + " does not have a mapping." + "This might be because the table does not exist."); } return Iterables.getOnlyElement(results.rows()).getString("short_table_name"); }); } catch (ExecutionException e) { throw new TableMappingNotFoundException(e.getCause()); } }
/** * Returns the number of milliseconds since the last compaction, or Long.MAX_VALUE if * compaction has never run. */ private long getMillisSinceLastCompact() { AgnosticResultSet rs = conns.get().selectResultSetUnregisteredQuery( "SELECT FLOOR(EXTRACT(EPOCH FROM GREATEST( " + " last_vacuum, last_autovacuum, last_analyze, last_autoanalyze" + "))*1000) AS last, " + "FLOOR(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)*1000) AS current " + "FROM pg_stat_user_tables WHERE relname = ?", prefixedTableName()); AgnosticResultRow row = Iterables.getOnlyElement(rs.rows()); // last could be null if vacuum has never run long last = row.getLong("last", -1); if (last == -1) { return Long.MAX_VALUE; } long current = row.getLong("current"); return current - last; }
@Test public void cacheIsActuallyUsed() throws TableMappingNotFoundException { // do a normal read when(resultSet.size()).thenReturn(1); AgnosticResultRow row = mock(AgnosticResultRow.class); when(row.getString(eq("short_table_name"))).thenReturn(SHORT_TABLE_NAME); doReturn(ImmutableList.of(row)).when(resultSet).rows(); String shortName = oracleTableNameUnmapper .getShortTableNameFromMappingTable(connectionSupplier, TEST_PREFIX, TABLE_REF_2); assertThat(shortName, is(SHORT_TABLE_NAME)); // verify that underlying datastore was called once instead of hitting the cache verify(row, times(1)).getString("short_table_name"); // read again looking for the same table oracleTableNameUnmapper.getShortTableNameFromMappingTable(connectionSupplier, TEST_PREFIX, TABLE_REF_2); // verify that cache was hit and underlying datastore was _still_ only called once verify(row, times(1)).getString("short_table_name"); } }
private static SortedSetMultimap<Integer, byte[]> getRowsForBatches( Supplier<SqlConnection> connectionSupplier, String query, Object[] args) { SqlConnection connection = connectionSupplier.get(); try { AgnosticResultSet results = connection.selectResultSetUnregisteredQuery(query, args); SortedSetMultimap<Integer, byte[]> ret = TreeMultimap.create( Ordering.natural(), UnsignedBytes.lexicographicalComparator()); for (AgnosticResultRow row : results.rows()) { @SuppressWarnings("deprecation") byte[] rowName = row.getBytes("row_name"); int batchNum = row.getInteger("batch_num"); if (rowName != null) { ret.put(batchNum, rowName); } } return ret; } finally { closeSql(connection); } }
public TableValueStyle getTableType( final ConnectionSupplier connectionSupplier, final TableReference tableRef, TableReference metadataTable) { try { return valueStyleByTableRef.get(tableRef, () -> { SqlConnection conn = connectionSupplier.get(); AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( String.format( "SELECT table_size FROM %s WHERE table_name = ?", metadataTable.getQualifiedName()), tableRef.getQualifiedName()); Preconditions.checkArgument( !results.rows().isEmpty(), "table %s not found", tableRef.getQualifiedName()); return TableValueStyle.byId(Iterables.getOnlyElement(results.rows()).getInteger("table_size")); }); } catch (ExecutionException e) { log.error("TableValueStyle for the table {} could not be retrieved.", tableRef.getQualifiedName()); throw Throwables.propagate(e); } }
@Override public Set<TableReference> getAllTableNames() { return run(conn -> { AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT table_name FROM " + config.metadataTable().getQualifiedName()); Set<TableReference> ret = Sets.newHashSetWithExpectedSize(results.size()); for (AgnosticResultRow row : results.rows()) { ret.add(TableReference.createUnsafe(row.getString("table_name"))); } return ret; }); }
@Override @SuppressWarnings("deprecation") public byte[] getMetadata() { AgnosticResultSet results = conns.get().selectResultSetUnregisteredQuery( "SELECT value FROM " + config.metadataTable().getQualifiedName() + " WHERE table_name = ?", tableRef.getQualifiedName()); if (results.size() < 1) { return PtBytes.EMPTY_BYTE_ARRAY; } else { return MoreObjects.firstNonNull( Iterables.getOnlyElement(results.rows()).getBytes("value"), PtBytes.EMPTY_BYTE_ARRAY); } }
@Override public Map<TableReference, byte[]> getMetadataForTables() { return run(conn -> { AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT table_name, value FROM " + config.metadataTable().getQualifiedName()); Map<TableReference, byte[]> ret = Maps.newHashMapWithExpectedSize(results.size()); for (AgnosticResultRow row : results.rows()) { ret.put(TableReference.createUnsafe(row.getString("table_name")), row.getBytes("value")); } return ret; }); }
@SuppressWarnings("checkstyle:NestedTryDepth") public String getShortTableNameFromMappingTable( ConnectionSupplier connectionSupplier, String tablePrefix, TableReference tableRef) throws TableMappingNotFoundException { String fullTableName = tablePrefix + DbKvs.internalTableName(tableRef); try { return unmappingCache.get(fullTableName, () -> { SqlConnection conn = connectionSupplier.get(); AgnosticResultSet results = conn.selectResultSetUnregisteredQuery( "SELECT short_table_name " + "FROM " + AtlasDbConstants.ORACLE_NAME_MAPPING_TABLE + " WHERE table_name = ?", fullTableName); if (results.size() == 0) { throw new TableMappingNotFoundException( "The table " + fullTableName + " does not have a mapping." + "This might be because the table does not exist."); } return Iterables.getOnlyElement(results.rows()).getString("short_table_name"); }); } catch (ExecutionException e) { throw new TableMappingNotFoundException(e.getCause()); } }
/** * Returns the number of milliseconds since the last compaction, or Long.MAX_VALUE if * compaction has never run. */ private long getMillisSinceLastCompact() { AgnosticResultSet rs = conns.get().selectResultSetUnregisteredQuery( "SELECT FLOOR(EXTRACT(EPOCH FROM GREATEST( " + " last_vacuum, last_autovacuum, last_analyze, last_autoanalyze" + "))*1000) AS last, " + "FLOOR(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)*1000) AS current " + "FROM pg_stat_user_tables WHERE relname = ?", prefixedTableName()); AgnosticResultRow row = Iterables.getOnlyElement(rs.rows()); // last could be null if vacuum has never run long last = row.getLong("last", -1); if (last == -1) { return Long.MAX_VALUE; } long current = row.getLong("current"); return current - last; }
private static SortedSetMultimap<Integer, byte[]> getRowsForBatches( Supplier<SqlConnection> connectionSupplier, String query, Object[] args) { SqlConnection connection = connectionSupplier.get(); try { AgnosticResultSet results = connection.selectResultSetUnregisteredQuery(query, args); SortedSetMultimap<Integer, byte[]> ret = TreeMultimap.create( Ordering.natural(), UnsignedBytes.lexicographicalComparator()); for (AgnosticResultRow row : results.rows()) { @SuppressWarnings("deprecation") byte[] rowName = row.getBytes("row_name"); int batchNum = row.getInteger("batch_num"); if (rowName != null) { ret.put(batchNum, rowName); } } return ret; } finally { closeSql(connection); } }