ibessonov commented on a change in pull request #169: URL: https://github.com/apache/ignite-3/pull/169#discussion_r655962656
########## File path: modules/storage-api/src/main/java/org/apache/ignite/internal/storage/Storage.java ########## @@ -0,0 +1,68 @@ +/* + * 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.util.function.Predicate; +import org.apache.ignite.internal.util.Cursor; + +/** + * Interface providing methods to read, remove and update keys in storage. + */ +public interface Storage { + /** + * Reads a DataRow for a given key. + * + * @param key Search row. + * @return Data row. + */ + public DataRow read(SearchRow key) throws StorageException; + + /** + * Writes DataRow to the storage. + * + * @param row Data row. Review comment: That's right, but I vividly remember that Ignite uses custom notation with capital letter and a period ########## File path: modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorage.java ########## @@ -0,0 +1,221 @@ +/* + * 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.rocksdb; + +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.Comparator; +import java.util.Iterator; +import java.util.NoSuchElementException; +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.storage.basic.SimpleDataRow; +import org.apache.ignite.internal.util.Cursor; +import org.jetbrains.annotations.NotNull; +import org.rocksdb.AbstractComparator; +import org.rocksdb.ComparatorOptions; +import org.rocksdb.Options; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksDBException; +import org.rocksdb.RocksIterator; + +/** + * Storage implementation based on a single RocksDB instance. + */ +public class RocksDbStorage implements Storage, AutoCloseable { + static { + RocksDB.loadLibrary(); + } + + /** RocksDB comparator options. */ + private final ComparatorOptions comparatorOptions; + + /** RocksDB comparator. */ + private final AbstractComparator comparator; + + /** RockDB options. */ + private final Options options; + + /** RocksDb instance. */ + private final RocksDB db; + + /** + * @param dbPath Path to the folder to store data. + * @param comparator Keys comparator. + * @throws StorageException If failed to create RocksDB instance. + */ + public RocksDbStorage(Path dbPath, Comparator<ByteBuffer> comparator) throws StorageException { + try { + comparatorOptions = new ComparatorOptions(); + + this.comparator = new AbstractComparator(comparatorOptions) { + /** {@inheritDoc} */ + @Override public String name() { + return "comparator"; + } + + /** {@inheritDoc} */ + @Override public int compare(ByteBuffer a, ByteBuffer b) { + return comparator.compare(a, b); + } + }; + + options = new Options(); + + options.setCreateIfMissing(true); + + options.setComparator(this.comparator); + + this.db = RocksDB.open(options, dbPath.toAbsolutePath().toString()); + } + catch (RocksDBException e) { + close(); + + throw new StorageException("Failed to start storage", e); + } + } + + /** {@inheritDoc} */ + @Override public DataRow read(SearchRow key) throws StorageException { + try { + byte[] keyBytes = key.keyBytes(); + + return new SimpleDataRow(keyBytes, db.get(keyBytes)); + } + catch (RocksDBException e) { + throw new StorageException("Failed to read data from the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void write(DataRow row) throws StorageException { + try { + db.put(row.keyBytes(), row.valueBytes()); + } + catch (RocksDBException e) { + throw new StorageException("Filed to write data to the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void remove(SearchRow key) throws StorageException { + try { + db.delete(key.keyBytes()); + } + catch (RocksDBException e) { + throw new StorageException("Failed to remove data from the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void invoke(SearchRow key, InvokeClosure clo) throws StorageException { + try { + byte[] keyBytes = key.keyBytes(); + byte[] existingDataBytes = db.get(keyBytes); + + clo.call(new SimpleDataRow(keyBytes, existingDataBytes)); + + switch (clo.operationType()) { + case WRITE: + db.put(keyBytes, clo.newRow().valueBytes()); + + break; + + case REMOVE: + db.delete(keyBytes); + + break; + + case NOOP: + break; + } + } + catch (RocksDBException e) { + throw new StorageException("Failed to access data in the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public Cursor<DataRow> scan(Predicate<SearchRow> filter) throws StorageException { + return new ScanCursor(db.newIterator(), filter); + } + + /** {@inheritDoc} */ + @Override public void close() { + if (db != null) + db.close(); Review comment: Can they? Is this documented somewhere? ########## File path: modules/storage-rocksdb/src/main/java/org/apache/ignite/internal/storage/rocksdb/RocksDbStorage.java ########## @@ -0,0 +1,221 @@ +/* + * 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.rocksdb; + +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.Comparator; +import java.util.Iterator; +import java.util.NoSuchElementException; +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.storage.basic.SimpleDataRow; +import org.apache.ignite.internal.util.Cursor; +import org.jetbrains.annotations.NotNull; +import org.rocksdb.AbstractComparator; +import org.rocksdb.ComparatorOptions; +import org.rocksdb.Options; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksDBException; +import org.rocksdb.RocksIterator; + +/** + * Storage implementation based on a single RocksDB instance. + */ +public class RocksDbStorage implements Storage, AutoCloseable { + static { + RocksDB.loadLibrary(); + } + + /** RocksDB comparator options. */ + private final ComparatorOptions comparatorOptions; + + /** RocksDB comparator. */ + private final AbstractComparator comparator; + + /** RockDB options. */ + private final Options options; + + /** RocksDb instance. */ + private final RocksDB db; + + /** + * @param dbPath Path to the folder to store data. + * @param comparator Keys comparator. + * @throws StorageException If failed to create RocksDB instance. + */ + public RocksDbStorage(Path dbPath, Comparator<ByteBuffer> comparator) throws StorageException { + try { + comparatorOptions = new ComparatorOptions(); + + this.comparator = new AbstractComparator(comparatorOptions) { + /** {@inheritDoc} */ + @Override public String name() { + return "comparator"; + } + + /** {@inheritDoc} */ + @Override public int compare(ByteBuffer a, ByteBuffer b) { + return comparator.compare(a, b); + } + }; + + options = new Options(); + + options.setCreateIfMissing(true); + + options.setComparator(this.comparator); + + this.db = RocksDB.open(options, dbPath.toAbsolutePath().toString()); + } + catch (RocksDBException e) { + close(); + + throw new StorageException("Failed to start storage", e); + } + } + + /** {@inheritDoc} */ + @Override public DataRow read(SearchRow key) throws StorageException { + try { + byte[] keyBytes = key.keyBytes(); + + return new SimpleDataRow(keyBytes, db.get(keyBytes)); + } + catch (RocksDBException e) { + throw new StorageException("Failed to read data from the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void write(DataRow row) throws StorageException { + try { + db.put(row.keyBytes(), row.valueBytes()); + } + catch (RocksDBException e) { + throw new StorageException("Filed to write data to the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void remove(SearchRow key) throws StorageException { + try { + db.delete(key.keyBytes()); + } + catch (RocksDBException e) { + throw new StorageException("Failed to remove data from the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public synchronized void invoke(SearchRow key, InvokeClosure clo) throws StorageException { + try { + byte[] keyBytes = key.keyBytes(); + byte[] existingDataBytes = db.get(keyBytes); + + clo.call(new SimpleDataRow(keyBytes, existingDataBytes)); + + switch (clo.operationType()) { + case WRITE: + db.put(keyBytes, clo.newRow().valueBytes()); + + break; + + case REMOVE: + db.delete(keyBytes); + + break; + + case NOOP: + break; + } + } + catch (RocksDBException e) { + throw new StorageException("Failed to access data in the storage", e); + } + } + + /** {@inheritDoc} */ + @Override public Cursor<DataRow> scan(Predicate<SearchRow> filter) throws StorageException { + return new ScanCursor(db.newIterator(), filter); + } + + /** {@inheritDoc} */ + @Override public void close() { + if (db != null) + db.close(); + + if (options != null) + options.close(); + + if (comparator != null) + comparator.close(); + + if (comparatorOptions != null) + comparatorOptions.close(); + } + + private static class ScanCursor implements Cursor<DataRow> { + private final RocksIterator iter; + + private final Predicate<SearchRow> filter; + + private ScanCursor(RocksIterator iter, Predicate<SearchRow> filter) { + this.iter = iter; + this.filter = filter; + + iter.seekToFirst(); + + hasNext(); Review comment: Artifact from the past :) We don't need it ########## File path: modules/storage-api/src/test/java/org/apache/ignite/internal/storage/AbstractStorageTest.java ########## @@ -0,0 +1,181 @@ +/* + * 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.charset.StandardCharsets; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; +import org.apache.ignite.internal.storage.basic.SimpleDataRow; +import org.apache.ignite.internal.storage.basic.SimpleReadInvokeClosure; +import org.apache.ignite.internal.storage.basic.SimpleRemoveInvokeClosure; +import org.apache.ignite.internal.storage.basic.SimpleWriteInvokeClosure; +import org.apache.ignite.internal.util.Cursor; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static java.util.Collections.emptyList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Abstract test that covers basic scenarios of the storage API. + */ +public abstract class AbstractStorageTest { + /** Storage instance. */ + protected Storage storage; + + /** + * Wraps string key into a search row. + * + * @param key String key. + * @return Search row. + */ + private SearchRow searchRow(String key) { + return new SimpleDataRow( + key.getBytes(StandardCharsets.UTF_8), + null + ); + } + + /** + * Wraps string key/value pair into a data row. + * + * @param key String key. + * @param value String value. + * @return Data row. + */ + private DataRow dataRow(String key, String value) { + return new SimpleDataRow( + key.getBytes(StandardCharsets.UTF_8), + value.getBytes(StandardCharsets.UTF_8) + ); + } + + @Test + public void readWriteRemove() throws Exception { + SearchRow searchRow = searchRow("key"); + + assertNull(storage.read(searchRow).value()); + + DataRow dataRow = dataRow("key", "value"); + + storage.write(dataRow); + + assertArrayEquals(dataRow.value().array(), storage.read(searchRow).value().array()); + + storage.remove(searchRow); + + assertNull(storage.read(searchRow).value()); + } + + @Test + public void invoke() throws Exception { + SearchRow searchRow = searchRow("key"); + + SimpleReadInvokeClosure readClosure = new SimpleReadInvokeClosure(); + + storage.invoke(searchRow, readClosure); + + assertNull(readClosure.row().value()); + + DataRow dataRow = dataRow("key", "value"); + + storage.invoke(searchRow, new SimpleWriteInvokeClosure(dataRow)); + + storage.invoke(searchRow, readClosure); + + Assertions.assertArrayEquals(dataRow.value().array(), readClosure.row().value().array()); + + storage.invoke(searchRow, new SimpleRemoveInvokeClosure()); + + storage.invoke(searchRow, readClosure); + + assertNull(readClosure.row().value()); + } + + @Test + public void scanSimple() throws Exception { + List<DataRow> list = toList(storage.scan(key -> true)); + + assertEquals(emptyList(), list); + + DataRow dataRow1 = dataRow("key1", "value1"); + + storage.write(dataRow1); + + list = toList(storage.scan(key -> true)); + + assertThat(list, hasSize(1)); + + assertArrayEquals(dataRow1.value().array(), list.get(0).value().array()); + + DataRow dataRow2 = dataRow("key2", "value2"); + + storage.write(dataRow2); + + list = toList(storage.scan(key -> true)); + + assertThat(list, hasSize(2)); + + // "key1" and "key2" have the same order both by hash and lexicographically. + assertArrayEquals(dataRow1.value().array(), list.get(0).value().array()); + assertArrayEquals(dataRow2.value().array(), list.get(1).value().array()); + } + + @Test + public void scanFiltered() throws Exception { + DataRow dataRow1 = dataRow("key1", "value1"); + DataRow dataRow2 = dataRow("key2", "value2"); + + storage.write(dataRow1); + storage.write(dataRow2); + + List<DataRow> list = toList(storage.scan(key -> key.keyBytes()[3] == '1')); + + assertThat(list, hasSize(1)); + + assertArrayEquals(dataRow1.value().array(), list.get(0).value().array()); + + list = toList(storage.scan(key -> key.keyBytes()[3] == '2')); + + assertThat(list, hasSize(1)); + + assertArrayEquals(dataRow2.value().array(), list.get(0).value().array()); + + list = toList(storage.scan(key -> false)); + + assertTrue(list.isEmpty()); + } + + /** + * Converts cursor to list. + * + * @param cursor Cursor. + * @param <T> Type of cursor content. + * @return List. + */ + @NotNull private <T> List<T> toList(Cursor<T> cursor) { + return StreamSupport.stream(cursor.spliterator(), false).collect(Collectors.toList()); Review comment: My bad -- 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]
