sashapolo commented on a change in pull request #169: URL: https://github.com/apache/ignite-3/pull/169#discussion_r656026514
########## 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: AFAIK, it is not documented anywhere that they **never** throw exceptions, therefore you should always assume that they do -- 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]
