baiyangtx commented on code in PR #3394:
URL: https://github.com/apache/amoro/pull/3394#discussion_r1909707797


##########
amoro-ams/src/main/java/org/apache/amoro/server/catalog/DefaultCatalogManager.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.amoro.server.catalog;
+
+import org.apache.amoro.api.CatalogMeta;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.exception.AlreadyExistsException;
+import org.apache.amoro.exception.IllegalMetadataException;
+import org.apache.amoro.exception.ObjectNotExistsException;
+import org.apache.amoro.properties.CatalogMetaProperties;
+import org.apache.amoro.server.persistence.PersistentBase;
+import org.apache.amoro.server.persistence.mapper.CatalogMetaMapper;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class DefaultCatalogManager extends PersistentBase implements 
CatalogManager {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DefaultCatalogManager.class);
+  protected final Configurations serverConfiguration;
+
+  private final Map<String, ServerCatalog> serverCatalogMap = 
Maps.newConcurrentMap();
+
+  public DefaultCatalogManager(Configurations serverConfiguration) {
+    this.serverConfiguration = serverConfiguration;
+    listCatalogMetas()
+        .forEach(
+            c -> {
+              ServerCatalog serverCatalog =
+                  CatalogBuilder.buildServerCatalog(c, serverConfiguration);
+              serverCatalogMap.put(c.getCatalogName(), serverCatalog);
+            });
+    LOG.info("DefaultCatalogManager initialized, total catalogs: {}", 
serverCatalogMap.size());
+  }
+
+  @Override
+  public List<CatalogMeta> listCatalogMetas() {
+    return getAs(CatalogMetaMapper.class, CatalogMetaMapper::getCatalogs);
+  }
+
+  @Override
+  public CatalogMeta getCatalogMeta(String catalogName) {
+    return getCatalogMetaOptional(catalogName)
+        .orElseThrow(() -> new ObjectNotExistsException("Catalog " + 
catalogName));
+  }
+
+  private Optional<CatalogMeta> getCatalogMetaOptional(String catalogName) {
+    return Optional.ofNullable(
+        getAs(
+            CatalogMetaMapper.class,
+            catalogMetaMapper -> catalogMetaMapper.getCatalog(catalogName)));
+  }
+
+  @Override
+  public boolean catalogExist(String catalogName) {
+    return getCatalogMetaOptional(catalogName).isPresent();
+  }
+
+  @Override
+  public ServerCatalog getServerCatalog(String catalogName) {
+    Optional<CatalogMeta> catalogMeta = getCatalogMetaOptional(catalogName);

Review Comment:
   Cache will lead to consistency issues, and unit tests will also fail. 
   
   I have optimized this part, and now each call only accesses the database 
once.



##########
amoro-ams/src/main/java/org/apache/amoro/server/catalog/DefaultCatalogManager.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.amoro.server.catalog;
+
+import org.apache.amoro.api.CatalogMeta;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.exception.AlreadyExistsException;
+import org.apache.amoro.exception.IllegalMetadataException;
+import org.apache.amoro.exception.ObjectNotExistsException;
+import org.apache.amoro.properties.CatalogMetaProperties;
+import org.apache.amoro.server.persistence.PersistentBase;
+import org.apache.amoro.server.persistence.mapper.CatalogMetaMapper;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class DefaultCatalogManager extends PersistentBase implements 
CatalogManager {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DefaultCatalogManager.class);
+  protected final Configurations serverConfiguration;
+
+  private final Map<String, ServerCatalog> serverCatalogMap = 
Maps.newConcurrentMap();
+
+  public DefaultCatalogManager(Configurations serverConfiguration) {
+    this.serverConfiguration = serverConfiguration;
+    listCatalogMetas()
+        .forEach(
+            c -> {
+              ServerCatalog serverCatalog =
+                  CatalogBuilder.buildServerCatalog(c, serverConfiguration);
+              serverCatalogMap.put(c.getCatalogName(), serverCatalog);
+            });
+    LOG.info("DefaultCatalogManager initialized, total catalogs: {}", 
serverCatalogMap.size());
+  }
+
+  @Override
+  public List<CatalogMeta> listCatalogMetas() {
+    return getAs(CatalogMetaMapper.class, CatalogMetaMapper::getCatalogs);
+  }
+
+  @Override
+  public CatalogMeta getCatalogMeta(String catalogName) {
+    return getCatalogMetaOptional(catalogName)
+        .orElseThrow(() -> new ObjectNotExistsException("Catalog " + 
catalogName));
+  }
+
+  private Optional<CatalogMeta> getCatalogMetaOptional(String catalogName) {
+    return Optional.ofNullable(
+        getAs(
+            CatalogMetaMapper.class,
+            catalogMetaMapper -> catalogMetaMapper.getCatalog(catalogName)));
+  }
+
+  @Override
+  public boolean catalogExist(String catalogName) {
+    return getCatalogMetaOptional(catalogName).isPresent();
+  }
+
+  @Override
+  public ServerCatalog getServerCatalog(String catalogName) {
+    Optional<CatalogMeta> catalogMeta = getCatalogMetaOptional(catalogName);
+    if (!catalogMeta.isPresent()) {
+      // remove if catalog is deleted
+      disposeCatalog(catalogName);
+      throw new ObjectNotExistsException("Catalog " + catalogName);
+    }
+    ServerCatalog serverCatalog =
+        serverCatalogMap.computeIfAbsent(
+            catalogName,
+            n -> CatalogBuilder.buildServerCatalog(catalogMeta.get(), 
serverConfiguration));
+    serverCatalog.reload();
+    return serverCatalog;
+  }
+
+  @Override
+  public InternalCatalog getInternalCatalog(String catalogName) {
+    ServerCatalog serverCatalog = getServerCatalog(catalogName);
+    if (serverCatalog == null) {
+      throw new ObjectNotExistsException("Catalog " + catalogName);
+    }
+    if (serverCatalog.isInternal()) {
+      return (InternalCatalog) serverCatalog;
+    }
+    throw new ObjectNotExistsException("Catalog " + catalogName + " is not 
internal catalog");
+  }
+
+  @Override
+  public List<ExternalCatalog> getExternalCatalogs() {
+    return listCatalogMetas().stream()

Review Comment:
   fixed



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to