nastra commented on code in PR #6927: URL: https://github.com/apache/iceberg/pull/6927#discussion_r1118366131
########## core/src/test/java/org/apache/iceberg/inmemory/InMemoryCatalog.java: ########## @@ -0,0 +1,348 @@ +/* + * 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.iceberg.inmemory; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.stream.Collectors; +import org.apache.iceberg.BaseMetastoreCatalog; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SupportsNamespaces; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NamespaceNotEmptyException; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.InMemoryFileIO; +import org.apache.iceberg.relocated.com.google.common.base.Joiner; +import org.apache.iceberg.relocated.com.google.common.base.Objects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; + +/** + * Catalog implementation that uses in-memory data-structures to store the namespaces and tables. + * This class doesn't touch external resources and can be utilized to write unit tests without side + * effects. It uses {@link InMemoryFileIO}. + */ +public class InMemoryCatalog extends BaseMetastoreCatalog implements SupportsNamespaces, Closeable { + private static final Joiner SLASH = Joiner.on("/"); + private static final Joiner DOT = Joiner.on("."); + + private final ConcurrentMap<Namespace, Map<String, String>> namespaceDb; + private final ConcurrentMap<TableIdentifier, String> tableDb; + private FileIO io; + private String catalogName; + private String warehouseLocation; + + public InMemoryCatalog() { + namespaceDb = Maps.newConcurrentMap(); + tableDb = Maps.newConcurrentMap(); + } + + @Override + public String name() { + return catalogName; + } + + @Override + public void initialize(String name, Map<String, String> properties) { + this.catalogName = name != null ? name : InMemoryCatalog.class.getSimpleName(); + + String warehouse = properties.getOrDefault(CatalogProperties.WAREHOUSE_LOCATION, ""); + this.warehouseLocation = warehouse.replaceAll("/*$", ""); + this.io = new InMemoryFileIO(); + } + + @Override + protected TableOperations newTableOps(TableIdentifier tableIdentifier) { + return new InMemoryTableOperations(io, tableIdentifier); + } + + @Override + protected String defaultWarehouseLocation(TableIdentifier tableIdentifier) { + return SLASH.join( + defaultNamespaceLocation(tableIdentifier.namespace()), tableIdentifier.name()); + } + + private String defaultNamespaceLocation(Namespace namespace) { + if (namespace.isEmpty()) { + return warehouseLocation; + } else { + return SLASH.join(warehouseLocation, SLASH.join(namespace.levels())); + } + } + + @Override + public boolean dropTable(TableIdentifier tableIdentifier, boolean purge) { + TableOperations ops = newTableOps(tableIdentifier); + TableMetadata lastMetadata; + if (purge && ops.current() != null) { + lastMetadata = ops.current(); + } else { + lastMetadata = null; + } + + if (null == tableDb.remove(tableIdentifier)) { + return false; + } + + if (purge && lastMetadata != null) { + CatalogUtil.dropTableData(ops.io(), lastMetadata); + } + + return true; + } + + @Override + public List<TableIdentifier> listTables(Namespace namespace) { + if (!namespaceExists(namespace) && !namespace.isEmpty()) { + throw new NoSuchNamespaceException( + "Namespace does not exist: cannot list tables for namespace %s", namespace); + } + + return tableDb.keySet().stream() + .filter(t -> namespace.isEmpty() || t.namespace().equals(namespace)) + .sorted(Comparator.comparing(TableIdentifier::toString)) + .collect(Collectors.toList()); + } + + @Override + public void renameTable(TableIdentifier fromTableIdentifier, TableIdentifier toTableIdentifier) { + if (fromTableIdentifier.equals(toTableIdentifier)) { + return; + } + + if (!namespaceExists(toTableIdentifier.namespace())) { + throw new NoSuchNamespaceException( + "Cannot rename %s to %s because the namespace %s does not exist", + fromTableIdentifier, toTableIdentifier, toTableIdentifier.namespace()); + } + + if (!tableDb.containsKey(fromTableIdentifier)) { + throw new NoSuchTableException( + "Cannot rename %s to %s because the table %s does not exist", + fromTableIdentifier, toTableIdentifier, fromTableIdentifier); + } + + if (tableDb.containsKey(toTableIdentifier)) { + throw new AlreadyExistsException( + "Cannot rename %s to %s: Table already exists", fromTableIdentifier, toTableIdentifier); + } + + String fromLocation = tableDb.remove(fromTableIdentifier); + Preconditions.checkState( + null != fromLocation, + "Cannot rename from %s to %s because source table does not exist", + fromTableIdentifier, + toTableIdentifier); + + tableDb.put(toTableIdentifier, fromLocation); + } + + @Override + public void createNamespace(Namespace namespace) { + createNamespace(namespace, Collections.emptyMap()); + } + + @Override + public void createNamespace(Namespace namespace, Map<String, String> metadata) { + if (namespaceExists(namespace)) { + throw new AlreadyExistsException( + "Namespace already exists: %s. Cannot create namespace", namespace); + } + + namespaceDb.put(namespace, ImmutableMap.copyOf(metadata)); + } + + @Override + public boolean namespaceExists(Namespace namespace) { + return namespaceDb.containsKey(namespace); + } + + @Override + public boolean dropNamespace(Namespace namespace) throws NamespaceNotEmptyException { + if (!namespaceExists(namespace)) { + return false; + } + + List<TableIdentifier> tableIdentifiers = listTables(namespace); + if (!tableIdentifiers.isEmpty()) { + throw new NamespaceNotEmptyException( + "Namespace %s is not empty. Contains %d table(s).", namespace, tableIdentifiers.size()); + } + + return namespaceDb.remove(namespace) != null; + } + + @Override + public boolean setProperties(Namespace namespace, Map<String, String> properties) + throws NoSuchNamespaceException { + if (!namespaceExists(namespace)) { + throw new NoSuchNamespaceException("Namespace does not exist: %s", namespace); + } + + namespaceDb.computeIfPresent( + namespace, + (k, v) -> { + Map<String, String> newProperties = Maps.newHashMap(v); Review Comment: makes sense, I've changed this to use `ImmutableMap.<String, String>builder().putAll(v).putAll(properties).buildKeepingLast()` -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org