xuefuz commented on a change in pull request #8007: [FLINK-11474][table] Add 
ReadableCatalog, ReadableWritableCatalog, and other …
URL: https://github.com/apache/flink/pull/8007#discussion_r274576469
 
 

 ##########
 File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/catalog/GenericInMemoryCatalog.java
 ##########
 @@ -0,0 +1,271 @@
+/*
+ * 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.table.catalog.exceptions.DatabaseAlreadyExistException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException;
+import org.apache.flink.table.catalog.exceptions.TableAlreadyExistException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/**
+ * A generic catalog implementation that holds all meta objects in memory.
+ */
+public class GenericInMemoryCatalog implements ReadableWritableCatalog {
+
+       public static final String DEFAULT_DB = "default";
+
+       private String currentDatabase = DEFAULT_DB;
+
+       private final String catalogName;
+       private final Map<String, CatalogDatabase> databases;
+       private final Map<ObjectPath, CommonTable> tables;
+
+       public GenericInMemoryCatalog(String name) {
+               checkArgument(!StringUtils.isNullOrWhitespaceOnly(name), "name 
cannot be null or empty");
+
+               this.catalogName = name;
+               this.databases = new LinkedHashMap<>();
+               this.databases.put(DEFAULT_DB, new GenericCatalogDatabase(new 
HashMap<>()));
+               this.tables = new LinkedHashMap<>();
+       }
+
+       @Override
+       public void open() {
+
+       }
+
+       @Override
+       public void close() {
+
+       }
+
+       // ------ databases ------
+
+       @Override
+       public String getCurrentDatabase() {
+               return currentDatabase;
+       }
+
+       @Override
+       public void setCurrentDatabase(String databaseName) throws 
DatabaseNotExistException {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+               if (!databaseExists(databaseName)) {
+                       throw new DatabaseNotExistException(catalogName, 
databaseName);
+               }
+
+               currentDatabase = databaseName;
+       }
+
+       @Override
+       public void createDatabase(String databaseName, CatalogDatabase db, 
boolean ignoreIfExists)
+               throws DatabaseAlreadyExistException {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+               checkArgument(db != null);
+
+               if (databaseExists(databaseName)) {
+                       if (!ignoreIfExists) {
+                               throw new 
DatabaseAlreadyExistException(catalogName, databaseName);
+                       }
+               } else {
+                       databases.put(databaseName, db.copy());
+               }
+       }
+
+       @Override
+       public void dropDatabase(String databaseName, boolean 
ignoreIfNotExists) throws DatabaseNotExistException {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+               if (databases.containsKey(databaseName)) {
+
+                       // Make sure the database is empty
+                       if (isDatabaseEmpty(databaseName)) {
+                               databases.remove(databaseName);
+                       } else {
+                               throw new 
DatabaseNotEmptyException(catalogName, databaseName);
+                       }
+               } else if (!ignoreIfNotExists) {
+                       throw new DatabaseNotExistException(catalogName, 
databaseName);
+               }
+       }
+
+       private boolean isDatabaseEmpty(String databaseName) {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+               return tables.keySet().stream().noneMatch(op -> 
op.getDatabaseName().equals(databaseName));
+               // TODO: also check function when function is added.
+       }
+
+       @Override
+       public void alterDatabase(String databaseName, CatalogDatabase 
newDatabase, boolean ignoreIfNotExists)
+               throws DatabaseNotExistException {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+               checkArgument(newDatabase != null);
+
+               if (databaseExists(databaseName)) {
+                       databases.put(databaseName, newDatabase.copy());
+               } else if (!ignoreIfNotExists) {
+                       throw new DatabaseNotExistException(catalogName, 
databaseName);
+               }
+       }
+
+       @Override
+       public List<String> listDatabases() {
+               return new ArrayList<>(databases.keySet());
+       }
+
+       @Override
+       public CatalogDatabase getDatabase(String databaseName) throws 
DatabaseNotExistException {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+               if (!databaseExists(databaseName)) {
+                       throw new DatabaseNotExistException(catalogName, 
databaseName);
+               } else {
+                       return databases.get(databaseName).copy();
+               }
+       }
+
+       @Override
+       public boolean databaseExists(String databaseName) {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+               return databases.containsKey(databaseName);
+       }
+
+       // ------ tables ------
+
+       @Override
+       public void createTable(ObjectPath tablePath, CommonTable table, 
boolean ignoreIfExists)
+               throws TableAlreadyExistException, DatabaseNotExistException {
+               checkArgument(tablePath != null);
+               checkArgument(table != null);
+
+               if (!databaseExists(tablePath.getDatabaseName())) {
+                       throw new DatabaseNotExistException(catalogName, 
tablePath.getDatabaseName());
+               }
+
+               if (tableExists(tablePath)) {
+                       if (!ignoreIfExists) {
+                               throw new 
TableAlreadyExistException(catalogName, tablePath);
+                       }
+               } else {
+                       tables.put(tablePath, table.copy());
+               }
+       }
+
+       @Override
+       public void alterTable(ObjectPath tablePath, CommonTable newTable, 
boolean ignoreIfNotExists)
+               throws TableNotExistException {
+               checkArgument(tablePath != null);
+               checkArgument(newTable != null);
+
+               if (tableExists(tablePath)) {
+                       tables.put(tablePath, newTable.copy());
+               } else if (!ignoreIfNotExists) {
+                       throw new TableNotExistException(catalogName, 
tablePath);
+               }
+       }
+
+       // ------ tables and views ------
+
+       @Override
+       public void dropTable(ObjectPath tablePath, boolean ignoreIfNotExists) 
throws TableNotExistException {
+               checkArgument(tablePath != null);
+
+               if (tableExists(tablePath)) {
+                       tables.remove(tablePath);
+               } else if (!ignoreIfNotExists) {
+                       throw new TableNotExistException(catalogName, 
tablePath);
+               }
+       }
+
+       @Override
+       public void renameTable(ObjectPath tablePath, String newTableName, 
boolean ignoreIfNotExists)
 
 Review comment:
   Yes, we can. However, I'd think pure renaming is probably more common than 
moving, which is the reason why it's here. For moving table between databases, 
we may provide a different API purely for that purpose rather than overloading 
it here, if that requirement ever comes. Any further thoughts?

----------------------------------------------------------------
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