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); } }
@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)); }
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); } }
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); } }