TableValueStyleCache { 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); } }  TableValueStyle getTableType(
            final ConnectionSupplier connectionSupplier,
            final TableReference tableRef,
            TableReference metadataTable); void clearCacheForTable(TableReference tableRef);  }
@Test public void testGetTableSizeOneTimeHasCacheMiss() throws Exception { assertThat(valueStyleCache.getTableType( connectionSupplier, TEST_TABLE, AtlasDbConstants.DEFAULT_METADATA_TABLE), is(TableValueStyle.OVERFLOW)); verify(connectionSupplier, times(1)).get(); }
@Test public void testGetTableSizeForSameTableHitsCache() throws Exception { assertThat(valueStyleCache.getTableType( connectionSupplier, TEST_TABLE, AtlasDbConstants.DEFAULT_METADATA_TABLE), is(TableValueStyle.OVERFLOW)); assertThat(valueStyleCache.getTableType( connectionSupplier, TEST_TABLE, AtlasDbConstants.DEFAULT_METADATA_TABLE), is(TableValueStyle.OVERFLOW)); verify(connectionSupplier, times(1)).get(); }
@Test public void testCacheHandlesMultipleTableRequests() throws Exception { assertThat(valueStyleCache.getTableType( connectionSupplier, TEST_TABLE, AtlasDbConstants.DEFAULT_METADATA_TABLE), is(TableValueStyle.OVERFLOW)); assertThat( valueStyleCache.getTableType( connectionSupplier, TEST_TABLE_2, AtlasDbConstants.DEFAULT_METADATA_TABLE), is(TableValueStyle.OVERFLOW)); verify(connectionSupplier, times(2)).get(); }