bowenli86 commented on a change in pull request #8404: [FLINK-11476][table] 
Create CatalogManager to manage multiple catalogs
URL: https://github.com/apache/flink/pull/8404#discussion_r283062652
 
 

 ##########
 File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/catalog/CatalogManager.java
 ##########
 @@ -0,0 +1,315 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.catalog;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.CatalogAlreadyExistsException;
+import org.apache.flink.table.api.CatalogNotExistException;
+import org.apache.flink.table.api.ExternalCatalogAlreadyExistException;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.operations.CatalogTableOperation;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A CatalogManager that encapsulates all available catalogs. It also 
implements the logic of
+ * table path resolution. Supports both new API ({@link ReadableCatalog} as 
well as {@link ExternalCatalog}.
+ */
+@Internal
+public class CatalogManager {
+       private static final Logger LOG = 
LoggerFactory.getLogger(CatalogManager.class);
+
+       // A map between names and catalogs.
+       private Map<String, Catalog> catalogs;
+
+       // TO BE REMOVED along with ExternalCatalog API
+       private Map<String, ExternalCatalog>  externalCatalogs;
+
+       // The name of the default catalog and schema
+       private String currentCatalogName;
+
+       private String currentDatabaseName;
+
+       public CatalogManager(String defaultCatalogName, Catalog 
defaultCatalog) {
+               catalogs = new LinkedHashMap<>();
+               externalCatalogs = new LinkedHashMap<>();
+               catalogs.put(defaultCatalogName, defaultCatalog);
+               this.currentCatalogName = defaultCatalogName;
+               this.currentDatabaseName = defaultCatalog.getCurrentDatabase();
+       }
+
+       /**
+        * Registers a catalog under the given name. The catalog name must be 
unique across both
+        * {@link Catalog}s and {@link ExternalCatalog}s.
+        *
+        * @param catalogName name under which to register the given catalog
+        * @param catalog catalog to register
+        * @throws CatalogAlreadyExistsException thrown if the name is already 
taken
+        */
+       public void registerCatalog(String catalogName, Catalog catalog) throws 
CatalogAlreadyExistsException {
+               checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), 
"Catalog name cannot be null or empty.");
+               checkNotNull(catalog, "Catalog cannot be null");
+
+               if (catalogs.containsKey(catalogName) || 
externalCatalogs.containsKey(catalogName)) {
+                       throw new CatalogAlreadyExistsException(catalogName);
+               }
+
+               catalogs.put(catalogName, catalog);
+               catalog.open();
+       }
+
+       /**
+        * Gets a catalog by name.
+        *
+        * @param catalogName name of the catalog to retrieve
+        * @return the requested catalog
+        * @throws CatalogNotExistException thrown if the catalog doesn't exist
+        * @see CatalogManager#getExternalCatalog(String)
+        */
+       public Catalog getCatalog(String catalogName) throws 
CatalogNotExistException {
+               if (!catalogs.keySet().contains(catalogName)) {
+                       throw new CatalogNotExistException(catalogName);
+               }
+
+               return catalogs.get(catalogName);
+       }
+
+       /**
+        * Registers an external catalog under the given name. The catalog name 
must be unique across both
+        * {@link Catalog}s and {@link ExternalCatalog}s.
+        *
+        * @param catalogName name under which to register the given catalog
+        * @param catalog catalog to register
+        * @throws ExternalCatalogAlreadyExistException thrown if the name is 
already taken
+        * @deprecated {@link ExternalCatalog} APIs will be dropped
+        */
+       @Deprecated
 
 Review comment:
   I feel it's a bit weird to add a new API and immediately marked as 
“Deprecated”

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to