ibessonov commented on a change in pull request #169: URL: https://github.com/apache/ignite-3/pull/169#discussion_r655333991
########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/basic/ConcurrentHashMapStorage.java ########## @@ -0,0 +1,115 @@ +/* + * 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.ignite.internal.storage.basic; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Predicate; +import org.apache.ignite.internal.storage.DataRow; +import org.apache.ignite.internal.storage.InvokeClosure; +import org.apache.ignite.internal.storage.SearchRow; +import org.apache.ignite.internal.storage.Storage; +import org.apache.ignite.internal.storage.StorageException; +import org.apache.ignite.internal.util.Cursor; +import org.apache.ignite.lang.ByteArray; +import org.jetbrains.annotations.NotNull; + +/** + * Storage implementation based on {@link ConcurrentHashMap}. + */ +public class ConcurrentHashMapStorage implements Storage { + /** Storage content. */ + private final ConcurrentMap<ByteArray, byte[]> map = new ConcurrentHashMap<>(); + + /** {@inheritDoc} */ + @Override public DataRow read(SearchRow key) throws StorageException { + byte[] keyBytes = key.keyBytes(); + + byte[] valueBytes = map.get(new ByteArray(keyBytes)); + + return new SimpleDataRow(keyBytes, valueBytes); + } + + /** {@inheritDoc} */ + @Override public synchronized void write(DataRow row) throws StorageException { + map.put(new ByteArray(row.keyBytes()), row.valueBytes()); + } + + /** {@inheritDoc} */ + @Override public synchronized void remove(SearchRow key) throws StorageException { + map.remove(new ByteArray(key.keyBytes())); + } + + /** {@inheritDoc} */ + @Override public synchronized void invoke(SearchRow key, InvokeClosure clo) throws StorageException { + byte[] keyBytes = key.keyBytes(); + + ByteArray mapKey = new ByteArray(keyBytes); + byte[] existingDataBytes = map.get(mapKey); + + clo.call(new SimpleDataRow(keyBytes, existingDataBytes)); + + switch (clo.operationType()) { + case WRITE: + map.put(mapKey, clo.newRow().valueBytes()); + + break; + + case REMOVE: + map.remove(mapKey); + + break; + + case NOOP: + break; + } + } + + /** {@inheritDoc} */ + @Override public Cursor<DataRow> scan(Predicate<SearchRow> filter) throws StorageException { + Iterator<Map.Entry<ByteArray, byte[]>> iter = map.entrySet().stream() Review comment: Now I wonder why I didn't write it like this in the first place. ########## File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java ########## @@ -335,4 +339,46 @@ public static ClassLoader igniteClassLoader() { return cls; } + + /** + * Deletes file or directory with all sub-directories and files. + * + * @param path File or directory to delete. + * @return {@code true} if and only if the file or directory is successfully deleted, + * {@code false} otherwise + */ + public static boolean delete(Path path) { + if (Files.isDirectory(path)) { + try { + try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) { + for (Path innerPath : stream) { + boolean res = delete(innerPath); + + if (!res) + return false; + } + } + } catch (IOException e) { + return false; + } + } + + if (path.toFile().getName().endsWith("jar")) { + try { + // Why do we do this? Review comment: Have no clue ########## File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java ########## @@ -335,4 +339,46 @@ public static ClassLoader igniteClassLoader() { return cls; } + + /** + * Deletes file or directory with all sub-directories and files. + * + * @param path File or directory to delete. Review comment: I don't get it, in every other place we have this exact notation ########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/OperationType.java ########## @@ -0,0 +1,30 @@ +/* + * 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.ignite.internal.storage; + +/** Operation types for {@link InvokeClosure}. */ +public enum OperationType { + /** Noop, signifies read operation. */ + NOOP, Review comment: It's more general. We're not sure that implementations will actually want to "read" values, they can just validate it and decide to do nothing. ########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/StorageException.java ########## @@ -0,0 +1,45 @@ +/* + * 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.ignite.internal.storage; + +/** + * Exception thrown by storage. + */ +public class StorageException extends Exception { Review comment: Why not? ########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/SearchRow.java ########## @@ -0,0 +1,40 @@ +/* + * 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.ignite.internal.storage; + +import java.nio.ByteBuffer; + +/** + * Interface to be used as a key representation to search data in storage. + */ +public interface SearchRow { + /** + * @return Hash of the key. + */ + int hash(); Review comment: For the future, we don't need it now. I think I'll remove it ########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/basic/ConcurrentHashMapStorage.java ########## @@ -0,0 +1,115 @@ +/* + * 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.ignite.internal.storage.basic; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Predicate; +import org.apache.ignite.internal.storage.DataRow; +import org.apache.ignite.internal.storage.InvokeClosure; +import org.apache.ignite.internal.storage.SearchRow; +import org.apache.ignite.internal.storage.Storage; +import org.apache.ignite.internal.storage.StorageException; +import org.apache.ignite.internal.util.Cursor; +import org.apache.ignite.lang.ByteArray; +import org.jetbrains.annotations.NotNull; + +/** + * Storage implementation based on {@link ConcurrentHashMap}. + */ +public class ConcurrentHashMapStorage implements Storage { + /** Storage content. */ + private final ConcurrentMap<ByteArray, byte[]> map = new ConcurrentHashMap<>(); Review comment: I have even more "extreme" idea - use write lock in invoke, read lock in "write"/"remove" and no lock in "read", still having concurrent map. It this fine? "synchronized" methods are not great even as a temporary solution -- 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: [email protected]
