@@ -59,6 +59,7 @@ public class JdbcAdmin implements DistributedStorageAdmin {
5959
6060 private final RdbEngineStrategy rdbEngine ;
6161 private final BasicDataSource dataSource ;
62+ private final String metadataSchema ;
6263 private final TableMetadataService tableMetadataService ;
6364 private final NamespaceMetadataService namespaceMetadataService ;
6465
@@ -67,16 +68,18 @@ public JdbcAdmin(DatabaseConfig databaseConfig) {
6768 JdbcConfig config = new JdbcConfig (databaseConfig );
6869 rdbEngine = RdbEngineFactory .create (config );
6970 dataSource = JdbcUtils .initDataSourceForAdmin (config , rdbEngine );
70- tableMetadataService = new TableMetadataService (config .getMetadataSchema (), rdbEngine );
71- namespaceMetadataService = new NamespaceMetadataService (config .getMetadataSchema (), rdbEngine );
71+ metadataSchema = config .getMetadataSchema ();
72+ tableMetadataService = new TableMetadataService (metadataSchema , rdbEngine );
73+ namespaceMetadataService = new NamespaceMetadataService (metadataSchema , rdbEngine );
7274 }
7375
7476 @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
7577 public JdbcAdmin (BasicDataSource dataSource , JdbcConfig config ) {
7678 rdbEngine = RdbEngineFactory .create (config );
7779 this .dataSource = dataSource ;
78- tableMetadataService = new TableMetadataService (config .getMetadataSchema (), rdbEngine );
79- namespaceMetadataService = new NamespaceMetadataService (config .getMetadataSchema (), rdbEngine );
80+ metadataSchema = config .getMetadataSchema ();
81+ tableMetadataService = new TableMetadataService (metadataSchema , rdbEngine );
82+ namespaceMetadataService = new NamespaceMetadataService (metadataSchema , rdbEngine );
8083 }
8184
8285 private static boolean hasDescClusteringOrder (TableMetadata metadata ) {
@@ -105,6 +108,7 @@ public void createNamespace(String namespace, Map<String, String> options)
105108
106109 try (Connection connection = dataSource .getConnection ()) {
107110 execute (connection , rdbEngine .createSchemaSqls (namespace ));
111+ createMetadataSchemaIfNotExists (connection );
108112 createNamespacesTableIfNotExists (connection );
109113 namespaceMetadataService .insertIntoNamespacesTable (connection , namespace );
110114 } catch (SQLException e ) {
@@ -117,6 +121,7 @@ public void createTable(
117121 String namespace , String table , TableMetadata metadata , Map <String , String > options )
118122 throws ExecutionException {
119123 try (Connection connection = dataSource .getConnection ()) {
124+ createMetadataSchemaIfNotExists (connection );
120125 createTableInternal (connection , namespace , table , metadata , false );
121126 addTableMetadata (connection , namespace , table , metadata , true , false );
122127 } catch (SQLException e ) {
@@ -221,6 +226,7 @@ public void dropNamespace(String namespace) throws ExecutionException {
221226 execute (connection , rdbEngine .dropNamespaceSql (namespace ));
222227 namespaceMetadataService .deleteFromNamespacesTable (connection , namespace );
223228 namespaceMetadataService .deleteNamespacesTableIfEmpty (connection );
229+ deleteMetadataSchemaIfEmpty (connection );
224230 } catch (SQLException e ) {
225231 rdbEngine .dropNamespaceTranslateSQLException (e , namespace );
226232 }
@@ -282,7 +288,7 @@ TableMetadata getImportTableMetadata(
282288 String catalogName = rdbEngine .getCatalogName (namespace );
283289 String schemaName = rdbEngine .getSchemaName (namespace );
284290
285- if (!tableExistsInternal (connection , namespace , table )) {
291+ if (!internalTableExists (connection , namespace , table )) {
286292 throw new IllegalArgumentException (
287293 CoreError .TABLE_NOT_FOUND .buildMessage (getFullTableName (namespace , table )));
288294 }
@@ -337,6 +343,7 @@ public void importTable(
337343
338344 try (Connection connection = dataSource .getConnection ()) {
339345 TableMetadata tableMetadata = getImportTableMetadata (namespace , table , overrideColumnsType );
346+ createMetadataSchemaIfNotExists (connection );
340347 createNamespacesTableIfNotExists (connection );
341348 upsertIntoNamespacesTable (connection , namespace );
342349 addTableMetadata (connection , namespace , table , tableMetadata , true , false );
@@ -473,6 +480,7 @@ public void repairNamespace(String namespace, Map<String, String> options)
473480
474481 try (Connection connection = dataSource .getConnection ()) {
475482 createSchemaIfNotExists (connection , namespace );
483+ createMetadataSchemaIfNotExists (connection );
476484 createNamespacesTableIfNotExists (connection );
477485 upsertIntoNamespacesTable (connection , namespace );
478486 } catch (SQLException e ) {
@@ -485,6 +493,7 @@ public void repairTable(
485493 String namespace , String table , TableMetadata metadata , Map <String , String > options )
486494 throws ExecutionException {
487495 try (Connection connection = dataSource .getConnection ()) {
496+ createMetadataSchemaIfNotExists (connection );
488497 createTableInternal (connection , namespace , table , metadata , true );
489498 addTableMetadata (connection , namespace , table , metadata , true , true );
490499 } catch (SQLException e ) {
@@ -720,6 +729,7 @@ public void upgrade(Map<String, String> options) throws ExecutionException {
720729 return ;
721730 }
722731
732+ createMetadataSchemaIfNotExists (connection );
723733 createNamespacesTableIfNotExists (connection );
724734 for (String namespace : namespaceNamesOfExistingTables ) {
725735 upsertIntoNamespacesTable (connection , namespace );
@@ -762,6 +772,20 @@ void upsertIntoNamespacesTable(Connection connection, String namespace) throws S
762772 namespaceMetadataService .upsertIntoNamespacesTable (connection , namespace );
763773 }
764774
775+ private void createMetadataSchemaIfNotExists (Connection connection ) throws SQLException {
776+ createSchemaIfNotExists (connection , metadataSchema );
777+ }
778+
779+ private void deleteMetadataSchemaIfEmpty (Connection connection ) throws SQLException {
780+ Set <String > internalTableNames = getInternalTableNames (connection , metadataSchema );
781+ if (!internalTableNames .isEmpty ()) {
782+ return ;
783+ }
784+
785+ String sql = rdbEngine .deleteMetadataSchemaSql (metadataSchema );
786+ execute (connection , sql );
787+ }
788+
765789 private void createTable (Connection connection , String createTableStatement , boolean ifNotExists )
766790 throws SQLException {
767791 String stmt = createTableStatement ;
@@ -778,10 +802,10 @@ private void createTable(Connection connection, String createTableStatement, boo
778802 }
779803 }
780804
781- private boolean tableExistsInternal (Connection connection , String namespace , String table )
805+ private boolean internalTableExists (Connection connection , String namespace , String table )
782806 throws ExecutionException {
783807 String fullTableName = encloseFullTableName (namespace , table );
784- String sql = rdbEngine .tableExistsInternalTableCheckSql (fullTableName );
808+ String sql = rdbEngine .internalTableExistsCheckSql (fullTableName );
785809 try {
786810 execute (connection , sql );
787811 return true ;
0 commit comments