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



##########
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];
+
+        LONG_ARRAY_HANDLE.set(buffer, 0, revision);
+
+        System.arraycopy(key, 0, buffer, Long.BYTES, key.length);
+
+        return buffer;
+    }
+
+    /**
+     * Gets a key from a key with revision.
+     *
+     * @param rocksKey Key with a revision.
+     * @return Key without a revision.
+     */
+    static byte[] rocksKeyToBytes(byte[] rocksKey) {
+        // Copy bytes of the rocks key ignoring the revision (first 8 bytes)
+        return Arrays.copyOfRange(rocksKey, Long.BYTES, rocksKey.length);
+    }
+
+    /**
+     * Builds a value from a byte array.
+     *
+     * @param valueBytes Value byte array.
+     * @return Value.
+     */
+    static Value bytesToValue(byte[] valueBytes) {
+        int idx = 0;
+
+        // At least an 8-byte update counter and a 1-byte boolean
+        assert valueBytes.length > Long.BYTES;
+
+        // Read an update counter (8-byte long) from the entry.
+        long updateCounter = (long) LONG_ARRAY_HANDLE.get(valueBytes, 0);
+
+        idx += Long.BYTES;
+
+        // Read a has-value flag (1 byte) from the entry.
+        boolean hasValue = valueBytes[Long.BYTES] != 0;
+
+        idx++;
+
+        byte[] val;
+        if (hasValue) {
+            // Read the value.
+            val = new byte[valueBytes.length - idx];

Review comment:
       I thought that it's better to keep this explicit




-- 
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