private int getTableNumberFromTableNames(String truncatedTableName, AgnosticResultSet results) { for (int i = 0; i < results.size(); i++) { String shortName = results.get(i).getString("short_table_name"); try { return Integer.parseInt(shortName.substring(truncatedTableName.length() + 1)) + 1; } catch (NumberFormatException e) { //Table in different format - Do nothing; } } return 0; } }
@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; }); }
@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()); } }
@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"); } }
@Test public void shouldThrowIfTable9999Exists() { when(resultSet.size()).thenReturn(1); AgnosticResultRow row = mock(AgnosticResultRow.class); when(row.getString(eq("short_table_name"))).thenReturn(getTableNameWithNumber(9999)); when(resultSet.get(eq(0))).thenReturn(row); TableReference tableRef = TableReference.create(TEST_NAMESPACE, LONG_TABLE_NAME); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Cannot create any more tables with name starting with a_te__ThisIsAVeryLongT"); oracleTableNameMapper.getShortPrefixedTableName(connectionSupplier, TEST_PREFIX, tableRef); }
@Test public void shouldThrowIfTableMappingDoesNotExist() throws TableMappingNotFoundException { when(resultSet.size()).thenReturn(0); expectedException.expect(TableMappingNotFoundException.class); expectedException.expectMessage("The table a_test_namespace__ThisIsAVeryLongTableNameThatWillExceed"); oracleTableNameUnmapper.getShortTableNameFromMappingTable(connectionSupplier, TEST_PREFIX, TABLE_REF); }
@Test public void shouldModifyTableNameForShortTableName() { when(resultSet.size()).thenReturn(0); TableReference tableRef = TableReference.create(Namespace.create("ns1"), "short"); String shortPrefixedTableName = oracleTableNameMapper .getShortPrefixedTableName(connectionSupplier, TEST_PREFIX, tableRef); assertThat(shortPrefixedTableName, is("a_ns__short_0000")); }
private int getTableNumberFromTableNames(String truncatedTableName, AgnosticResultSet results) { for (int i = 0; i < results.size(); i++) { String shortName = results.get(i).getString("short_table_name"); try { return Integer.parseInt(shortName.substring(truncatedTableName.length() + 1)) + 1; } catch (NumberFormatException e) { //Table in different format - Do nothing; } } return 0; } }
@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()); } }