You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/02/07 17:54:08 UTC
[GitHub] [iceberg] mayursrivastava opened a new pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
mayursrivastava opened a new pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054
The purpose of this pr is to create pure in-memory implementations of FileIO and Catalog+SupportsNamespace that can be utilized to write unit tests that don't use external resources such local disk, databases, etc.
The FileIO implementation is backed by in-memory data-structures, specifically, InMemoryFileStore which uses ConcurrentHashMap.
The Catalog implementation uses in-memory data-structures to store the namespaces and tables, specifically InMemoryCatalogDb which uses ConcurrentHashMaps. The InMemoryCatalog uses InMemoryFileIO.
There was an old corrupted (closed) pr ([commit](https://github.com/apache/iceberg/pull/3294/commits/31cab1d7f06cf3439c3ed1fc8a40ae9e1ef36f96)) with this same change.
@rdblue apologies for messing up the old pr. I've created this new pr with the same changes in the commit mentioned above. Could you please take a look again?
@nastra this is the new inmemory catalog pr.
Thanks,
Mayur
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] nastra commented on a change in pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
nastra commented on a change in pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#discussion_r802741780
##########
File path: core/src/test/java/org/apache/iceberg/hadoop/CommonCatalogTests.java
##########
@@ -0,0 +1,443 @@
+/*
+ * 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.hadoop;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.SortOrder;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.io.inmemory.InMemoryCatalog;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.transforms.Transform;
+import org.apache.iceberg.transforms.Transforms;
+import org.apache.iceberg.types.Types;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.NullOrder.NULLS_FIRST;
+import static org.apache.iceberg.SortDirection.ASC;
+
+/**
+ * Common tests for the Iceberg catalog.
+ * The tests expect the catalog to support namespaces.
+ * <p>
+ * Currently, the following catalogs are tested:
+ * <ol>
+ * <li>{@link HadoopCatalog}</li>
+ * <li>{@link InMemoryCatalog}</li>
+ * </ol>
+ * Catalog specific tests should be tested in a separate test,
+ * e.g. {@link TestHadoopCatalog}.
+ */
+@RunWith(Parameterized.class)
+public class CommonCatalogTests extends HadoopTableTestBase {
+
+ private enum CatalogFactory {
Review comment:
+1 on that idea of having a way to "certify" catalog implementations
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] mayursrivastava commented on pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
mayursrivastava commented on pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#issuecomment-1031871157
@kbendick I'll break it into the two new suggested prs and get back to you. I'll also apply the code review comments from this pr.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] danielcweeks commented on a change in pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
danielcweeks commented on a change in pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#discussion_r802110885
##########
File path: core/src/main/java/org/apache/iceberg/io/inmemory/InMemoryCatalog.java
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.io.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.ConcurrentHashMap;
+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.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+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.relocated.com.google.common.base.Joiner;
+import org.apache.iceberg.relocated.com.google.common.base.Objects;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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}.
+ * <p>
+ * This catalog supports namespaces.
+ * This catalog automatically creates namespaces if needed during table creation.
+ */
+public class InMemoryCatalog extends BaseMetastoreCatalog implements SupportsNamespaces, Closeable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InMemoryCatalog.class);
+ 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 = new ConcurrentHashMap<>();
+ tableDb = new ConcurrentHashMap<>();
+ }
+
+ @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 Table createTable(
+ TableIdentifier tableIdentifier,
+ Schema schema,
+ PartitionSpec spec,
+ String location,
+ Map<String, String> properties) {
+
+ // Create namespace automatically if it does not exist.
+ if (!namespaceExists(tableIdentifier.namespace())) {
+ createNamespace(tableIdentifier.namespace());
+ }
+
+ return super.createTable(tableIdentifier, schema, spec, location, properties);
+ }
+
+ @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 (tableDb.remove(tableIdentifier) == null) {
+ LOG.info("Table does not exist: cannot drop table {}", 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 because the table %s already exists",
+ fromTableIdentifier, toTableIdentifier, toTableIdentifier);
+ }
+
+ String fromLocation = tableDb.remove(fromTableIdentifier);
Review comment:
If we intend this to support concurrent access, we should be synchronizing on the `tabldDB` for operations like this.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] kbendick commented on a change in pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#discussion_r800964205
##########
File path: core/src/main/java/org/apache/iceberg/io/inmemory/InMemoryFileStore.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.io.inmemory;
+
+import java.nio.ByteBuffer;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * An in-memory collection based storage for file contents
+ * keyed by a string location.
+ */
+class InMemoryFileStore {
+
+ private final ConcurrentMap<String, byte[]> store;
+
+ InMemoryFileStore() {
+ this.store = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * Put the file contents at the given location, overwrite if it already exists.
+ */
+ public void put(String location, ByteBuffer data) {
+ // Copy the contents and store it.
+ byte[] bytes = new byte[data.remaining()];
+ data.get(bytes);
+ store.put(location, bytes);
+ }
+
+ /**
+ * Get the file contents for the given location.
+ */
+ public Optional<ByteBuffer> get(String location) {
+ return Optional.ofNullable(store.get(location)).map(bytes -> {
+ // Copy the contents and return it.
+ byte[] copy = new byte[bytes.length];
+ System.arraycopy(bytes, 0, copy, 0, bytes.length);
+ return ByteBuffer.wrap(copy);
+ });
Review comment:
Nit: This might be a good candidate for a `ByteBuffers#fromByteArray` function in the `ByteBuffers` utility class.
##########
File path: core/src/test/java/org/apache/iceberg/io/inmemory/InMemoryCatalogTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.io.inmemory;
+
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.hadoop.CommonCatalogTests;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.IntegerType;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests specific to {@link InMemoryCatalog}, e.g. rename table, alter namespace properties, etc.
+ * Common catalog tests can be found in {@link CommonCatalogTests}.
+ */
+public class InMemoryCatalogTest {
+
+ private InMemoryCatalog catalog;
+
+ @Before
+ public void before() {
+ catalog = new InMemoryCatalog();
+ catalog.initialize("in-memory-catalog", ImmutableMap.of());
+ }
+
+ @Test
+ public void testSetProperties() {
+ Namespace namespace = Namespace.of("a");
+
+ // Create a namespace with properties: {'k1': 'v1', 'k2': 'v2'}
+ catalog.createNamespace(namespace, ImmutableMap.of("k1", "v1", "k2", "v2"));
+ // Verify the properties
+ assertEquals(ImmutableMap.of("k1", "v1", "k2", "v2"), catalog.loadNamespaceMetadata(namespace));
+
+ // Set properties such that 'k1' is overwritten and a new key 'k3' is added.
+ catalog.setProperties(namespace, ImmutableMap.of("k1", "v1'", "k3", "v3"));
+ // Verify the result of set properties.
+ assertEquals(ImmutableMap.of("k1", "v1'", "k2", "v2", "k3", "v3"), catalog.loadNamespaceMetadata(namespace));
+ }
+
+ @Test
+ public void testRemoveProperties() {
+ Namespace namespace = Namespace.of("a");
+
+ // Create a namespace with properties: {'k1': 'v1', 'k2': 'v2'}
+ catalog.createNamespace(namespace, ImmutableMap.of("k1", "v1", "k2", "v2"));
+ // Verify the properties
+ assertEquals(ImmutableMap.of("k1", "v1", "k2", "v2"), catalog.loadNamespaceMetadata(namespace));
+
+ // Remove properties 'k1'
+ catalog.removeProperties(namespace, ImmutableSet.of("k1", "k3"));
+ // Verify the result of set properties.
Review comment:
Nit: These comments don't add much value in my opinion. The code is readable as is, especially given the method names. I would remove most of them.
##########
File path: core/src/main/java/org/apache/iceberg/io/inmemory/InMemoryCatalog.java
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.io.inmemory;
Review comment:
Nit: Maybe the catalog itself deserves its own package? Everything in `org.apache.iceberg.io` (at least from `core`) seems to be just IO related.
I'd personally put the actual FileIO stuff directly into `org.apache.iceberg.io`, as most of it begins with `InMemory` and there are a number of files in that package already. You could also do `org.apache.iceberg.io.memory` if you wanted, but I don't feel the extra level is needed based on the current contents.
Then for the in-memory catalog implementation, I'd add it directly to `org.apache.iceberg.catalog`, but put it in `core` not `api`.
But the catalog itself should probably not be in package `io` like this, as everything there is concerned with readers / writers and not actual catalog specific stuff.
Just my 2 cents 😄
##########
File path: core/src/main/java/org/apache/iceberg/io/inmemory/InMemoryFileStore.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.io.inmemory;
+
+import java.nio.ByteBuffer;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * An in-memory collection based storage for file contents
+ * keyed by a string location.
+ */
+class InMemoryFileStore {
+
+ private final ConcurrentMap<String, byte[]> store;
+
+ InMemoryFileStore() {
+ this.store = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * Put the file contents at the given location, overwrite if it already exists.
+ */
+ public void put(String location, ByteBuffer data) {
+ // Copy the contents and store it.
+ byte[] bytes = new byte[data.remaining()];
+ data.get(bytes);
+ store.put(location, bytes);
+ }
+
+ /**
+ * Get the file contents for the given location.
+ */
+ public Optional<ByteBuffer> get(String location) {
+ return Optional.ofNullable(store.get(location)).map(bytes -> {
+ // Copy the contents and return it.
+ byte[] copy = new byte[bytes.length];
+ System.arraycopy(bytes, 0, copy, 0, bytes.length);
+ return ByteBuffer.wrap(copy);
+ });
+ }
+
+ /**
+ * Remove the given location and its contents.
+ */
Review comment:
Nit: Consider adding what's returned to the Javadoc? Maybe `Returns true if the item was found and removed from the in-memory store`
##########
File path: core/src/test/java/org/apache/iceberg/io/inmemory/InMemoryFileIOTest.java
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.io.inmemory;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.stream.IntStream;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.io.PositionOutputStream;
+import org.apache.iceberg.io.SeekableInputStream;
+import org.junit.Before;
+import org.junit.Test;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class InMemoryFileIOTest {
+
+ private InMemoryFileIO fileIO;
+ private InMemoryFileStore store;
+
+ @Before
+ public void before() {
+ fileIO = new InMemoryFileIO();
+ store = fileIO.getStore();
+ }
+
+ @Test
+ public void testGetStore() {
+ assertNotNull(store);
+ }
+
+ @Test
+ public void testDeleteFile() {
+ assertFalse(store.exists("file1"));
+
+ // Create the file
+ store.put("file1", ByteBuffer.wrap(new byte[0]));
+ assertTrue(store.exists("file1"));
+
+ // Delete the file
+ fileIO.deleteFile("file1");
+ // Verify that the file has been deleted
+ assertFalse(store.exists("file1"));
+ }
+
+ @Test
+ public void testNewInputFile() throws IOException {
+ String fileName = "file1";
+
+ store.put(fileName, ByteBuffer.wrap("data1".getBytes(UTF_8)));
+
+ InputFile inputFile = fileIO.newInputFile(fileName);
+ assertNotNull(inputFile);
+
+ assertTrue(inputFile.exists());
+ assertEquals(fileName, inputFile.location());
+ assertEquals(5, inputFile.getLength());
+
+ SeekableInputStream inputStream = inputFile.newStream();
+ assertNotNull(inputStream);
+
+ // Test read()
+ assertEquals(0, inputStream.getPos());
+ assertEquals('d', inputStream.read());
+
+ // Test read(byte[], index, len)
+ byte[] dataRead = new byte[3];
+ assertEquals(2, inputStream.read(dataRead, 0, 2));
+ assertEquals("at", new String(dataRead, 0, 2, UTF_8));
+
+ inputStream.close();
+ }
+
+ @Test
+ public void testSeek() throws IOException {
+ String fileName = "file1";
+
+ // Number of rows
+ int numRowsInChunk = 10;
+
+ // Emulate parquet file structure
+ ByteBuffer buffer = ByteBuffer.allocate(1024);
+ // Add magic number
+ buffer.put("par1".getBytes(UTF_8));
+ // Add column chunk 1 for int32
+ IntStream.range(0, numRowsInChunk).forEach(buffer::putInt);
+ // Add column chunk 2 for float64
+ IntStream.range(0, numRowsInChunk).forEach(buffer::putDouble);
+ // Add metadata footer (index to the start of chunks)
+ buffer.putInt(4);
+ buffer.putInt(4 + numRowsInChunk * 4);
+ // Add footer size
+ buffer.putInt(8);
+ // Add magic number
+ buffer.put("par1".getBytes(UTF_8));
+
+ // Put the data in the store
+ buffer.flip();
+ store.put(fileName, buffer);
+
+ // magic number, chunk1, chunk2, footer, footer size, magic number
+ int expectedFileSize = 4 + numRowsInChunk * 4 + numRowsInChunk * 8 + 8 + 4 + 4;
Review comment:
Nit: for the `4 * numRowsInChunk` and `numRowsInChunk * 8` (and generally for all of the commented magic numbners), could you make named constants for them?
Something like `int expectedFileSize = MAGIC_NUMBER_SIZE + numRowsInChunk * BYTES_IN_INT32 + numRowsInChunk * BYTES_IN_FLOAT64 + FOOTER + SIZE_OF_FOOTER + ....` (or however it is exactly that these numbers work out)?
It's a bit hard to grok on first appearance and I think that would help quite a lot.
##########
File path: core/src/main/java/org/apache/iceberg/io/inmemory/InMemoryFileStore.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.io.inmemory;
+
+import java.nio.ByteBuffer;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * An in-memory collection based storage for file contents
+ * keyed by a string location.
+ */
+class InMemoryFileStore {
+
+ private final ConcurrentMap<String, byte[]> store;
+
+ InMemoryFileStore() {
+ this.store = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * Put the file contents at the given location, overwrite if it already exists.
+ */
+ public void put(String location, ByteBuffer data) {
+ // Copy the contents and store it.
+ byte[] bytes = new byte[data.remaining()];
+ data.get(bytes);
Review comment:
Nit: You might consider using the `ByteBuffers` utility class instead. It's `org.apache.iceberg.util.ByteBuffers`. There's a `toByteArray` method there that handles some corner cases and thing.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] danielcweeks commented on pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
danielcweeks commented on pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#issuecomment-1033136607
Hey @mayursrivastava, took a quick look and overall, I think this approach will work for testing purposes. A couple observations:
1. I'm not sure if we want to include this in the core library (I think @kbendick made the same observation). One option might actually be to put this implementation into the test src so that there isn't confusion around the fact that this is intended for for test use only (i.e. It's not a supported Catalog/FileIO for production use). Projects that want to use this implementation just need to depend on the test artifact to get access (a good example is the `iceberg-data` project).
2. Even though we have concurrent collections, there are a fair number of places where we're doing checks/changes without synchronizing on the collection, so there are race conditions (I pointed out the `renameTable`, but there are many others and I believe even the table ops commit is not atomic.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] pvary commented on a change in pull request #4054: Core: Add InMemory FileIO/Catalog and extract common catalog tests from HadoopCatalog into a common test
Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #4054:
URL: https://github.com/apache/iceberg/pull/4054#discussion_r802358858
##########
File path: core/src/test/java/org/apache/iceberg/hadoop/CommonCatalogTests.java
##########
@@ -0,0 +1,443 @@
+/*
+ * 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.hadoop;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.SortOrder;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.io.inmemory.InMemoryCatalog;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.transforms.Transform;
+import org.apache.iceberg.transforms.Transforms;
+import org.apache.iceberg.types.Types;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.NullOrder.NULLS_FIRST;
+import static org.apache.iceberg.SortDirection.ASC;
+
+/**
+ * Common tests for the Iceberg catalog.
+ * The tests expect the catalog to support namespaces.
+ * <p>
+ * Currently, the following catalogs are tested:
+ * <ol>
+ * <li>{@link HadoopCatalog}</li>
+ * <li>{@link InMemoryCatalog}</li>
+ * </ol>
+ * Catalog specific tests should be tested in a separate test,
+ * e.g. {@link TestHadoopCatalog}.
+ */
+@RunWith(Parameterized.class)
+public class CommonCatalogTests extends HadoopTableTestBase {
+
+ private enum CatalogFactory {
Review comment:
Is there a plan to use this test class to test other catalogs as well? Maybe these tests could be used to validate the Catalog implementations, so if the tests are passing then we can "certify" that the implementation adheres to the Iceberg Catalog standard.
I think it would be good to test HiveCatalog, GlueCatalog, JDBCCatalog etc, but I do not see a way to extend this ATM.
Do I miss something?
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org