sashapolo commented on a change in pull request #203:
URL: https://github.com/apache/ignite-3/pull/203#discussion_r673183661



##########
File path: 
modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksStorageUtils.java
##########
@@ -180,27 +167,25 @@ static Value bytesToValue(byte[] valueBytes) {
      * @param value New long value.
      * @return Byte array with a new value.
      */
-    static byte @NotNull [] addLongToLongsByteArray(byte @Nullable [] bytes, 
long value) {
-        // Index to insert new value at
-        int idx = bytes != null ? bytes.length : 0;
+    static byte @NotNull [] appendLong(byte @Nullable [] bytes, long value) {
+        if (bytes == null)
+            return longToBytes(value);
 
         // Allocate a one long size bigger array
-        int size = idx + Long.BYTES;
-
-        var newBytes = new byte[size];
+        var result = new byte[bytes.length + Long.BYTES];
 
         if (bytes != null)

Review comment:
       this check is now redundant

##########
File path: 
modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RangeCursor.java
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.metastorage.server.persistence;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import org.apache.ignite.internal.metastorage.server.Entry;
+import org.apache.ignite.internal.util.Cursor;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Cursor by entries which correspond to the given keys range.
+ */
+class RangeCursor implements Cursor<Entry> {
+    /** Storage. */
+    private final RocksDBKeyValueStorage storage;
+
+    /** Lower iteration bound (included). */
+    private final byte[] keyFrom;
+
+    /** Upper iteration bound (excluded). */
+    private final byte @Nullable [] keyTo;
+
+    /** Revision upper bound (included). */
+    private final long rev;
+
+    /** Iterator. */
+    private final Iterator<Entry> it;
+
+    /** Next entry. */
+    @Nullable
+    private Entry nextRetEntry;
+
+    /** Key of the last returned entry. */
+    private byte[] lastRetKey;
+
+    /**
+     * {@code true} if the iteration is finished.
+     */
+    private boolean finished;
+
+    /**
+     * Constructor.
+     *
+     * @param storage Storage.
+     * @param keyFrom {@link #keyFrom}.
+     * @param keyTo {@link #keyTo}.
+     * @param rev {@link #rev}.
+     */
+    RangeCursor(RocksDBKeyValueStorage storage, byte[] keyFrom, byte @Nullable 
[] keyTo, long rev) {
+        this.storage = storage;
+        this.keyFrom = keyFrom;
+        this.keyTo = keyTo;
+        this.rev = rev;
+        this.it = createIterator();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean hasNext() {
+        return it.hasNext();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Entry next() {
+        return it.next();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void close() throws Exception {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @NotNull
+    @Override public Iterator<Entry> iterator() {
+        return it;
+    }
+
+    /**
+     * Creates an iterator for this cursor.
+     *
+     * @return Iterator.
+     */
+    @NotNull
+    private Iterator<Entry> createIterator() {
+        return new Iterator<>() {
+            /** {@inheritDoc} */
+            @Override public boolean hasNext() {
+                storage.lock().readLock().lock();
+
+                try {
+                    while (true) {
+                        if (finished)
+                            return false;
+
+                        if (nextRetEntry != null)
+                            return true;
+
+                        byte[] key = lastRetKey;
+
+                        while (nextRetEntry == null) {
+                            Map.Entry<byte[], long[]> e =
+                                key == null ? 
storage.revisionCeilingEntry(keyFrom) : storage.revisionHigherEntry(key);
+
+                            if (e == null) {
+                                finished = true;
+
+                                break;
+                            }
+
+                            key = e.getKey();
+
+                            if (keyTo != null && 
RocksDBKeyValueStorage.CMP.compare(key, keyTo) >= 0) {
+                                finished = true;
+
+                                break;
+                            }
+
+                            long[] revs = e.getValue();
+
+                            assert revs != null && !(revs.length == 0) :

Review comment:
       ```suggestion
                               assert revs != null && revs.length != 0 :
   ```

##########
File path: 
modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksStorageUtils.java
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.metastorage.server.persistence;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.ignite.internal.metastorage.server.Value;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.rocksdb.RocksDBException;
+import org.rocksdb.RocksIterator;
+
+import static org.apache.ignite.internal.metastorage.server.Value.TOMBSTONE;
+
+/**
+ * Utility class for {@link RocksDBKeyValueStorage}.
+ */
+class RocksStorageUtils {
+    /** Byte order for the {@link RocksDBKeyValueStorage} must be little 
endian for a correct lexicographic order comparation. */
+    private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
+
+    /** VarHandle that gives the access to the elements of a {@code byte[]} 
array viewed as if it were a {@code long[]} array. */
+    private static final VarHandle LONG_ARRAY_HANDLE = 
MethodHandles.byteArrayViewVarHandle(long[].class, BYTE_ORDER);
+
+    /**
+     * Convert a long value to a byte array.
+     *
+     * @param value Value.
+     * @return Byte array.
+     */
+    static byte[] longToBytes(long value) {
+        var buffer = new byte[Long.BYTES];
+
+        LONG_ARRAY_HANDLE.set(buffer, 0, value);
+
+        return buffer;
+    }
+
+    /**
+     * Convert a byte array to a long value.
+     *
+     * @param array Byte array.
+     * @return Long value.
+     */
+    static long bytesToLong(byte[] array) {
+        assert array.length == Long.BYTES;
+
+        return (long) LONG_ARRAY_HANDLE.get(array, 0);
+    }
+
+    /**
+     * Adds a revision to a key.
+     *
+     * @param revision Revision.
+     * @param key Key.
+     * @return Key with a revision.
+     */
+    static byte[] keyToRocksKey(long revision, byte[] key) {
+        var buffer = new byte[Long.BYTES + key.length];

Review comment:
       yeah, that's minor stuff anyway

##########
File path: 
modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDBKeyValueStorage.java
##########
@@ -895,11 +893,11 @@ Entry doGet(byte[] key, long rev, boolean exactRev) {
      * @return List of the revisions.
      * @throws RocksDBException If failed to perform {@link 
RocksDB#get(ColumnFamilyHandle, byte[])}.
      */
-    private List<Long> getRevisions(byte[] key) throws RocksDBException {
+    private long[] getRevisions(byte[] key) throws RocksDBException {
         byte[] revisions = index.get(key);
 
         if (revisions == null)
-            return Collections.emptyList();
+            return LONG_EMPTY_ARRAY;

Review comment:
       just a nitpick: `EMPTY_LONG_ARRAY` is grammatically more correct

##########
File path: 
modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDBKeyValueStorage.java
##########
@@ -208,14 +213,16 @@ public RocksDBKeyValueStorage(Path dbPath) {
      * @throws RocksDBException If failed.
      */
     private void destroyRocksDB() throws RocksDBException {
-        try (final Options opt = new Options()) {
+        try (Options opt = new Options()) {
             RocksDB.destroyDB(dbPath.toString(), opt);
         }
     }
 
     /** {@inheritDoc} */
     @Override public void close() throws Exception {
-        IgniteUtils.closeAll(options, data, index, db);
+        IgniteUtils.closeAll(data, index, db, options);
+
+        snapshotExecutor.shutdown();

Review comment:
       this is not a completely correct way to shutdown an executor, please use 
`IgniteUtils#shutdownAndAwaitTermination`. You should also place it before the 
`closeAll` call in order to let the background tasks to finish




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to