SammyVimes commented on a change in pull request #203: URL: https://github.com/apache/ignite-3/pull/203#discussion_r673114709
########## 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]; + System.arraycopy(valueBytes, idx, val, 0, val.length); + } + else + // There is no value, mark it as a tombstone. + val = TOMBSTONE; + + return new Value(val, updateCounter); + } + + /** + * Adds an update counter and a tombstone flag to a value. + * @param value Value byte array. + * @param updateCounter Update counter. + * @return Value with an update counter and a tombstone. + */ + static byte[] valueToBytes(byte[] value, long updateCounter) { + var bytes = new byte[Long.BYTES + Byte.BYTES + value.length]; + + LONG_ARRAY_HANDLE.set(bytes, 0, updateCounter); + + bytes[Long.BYTES] = (byte) (value == TOMBSTONE ? 0 : 1); + + System.arraycopy(value, 0, bytes, Long.BYTES + Byte.BYTES, value.length); + + return bytes; + } + + /** + * Gets a list of longs from the byte array of longs. + * + * @param bytes Byte array of longs. + * @return List of longs. + */ + @NotNull + static List<Long> getAsLongs(byte[] bytes) { + // Value must be divisible by a size of a long, because it's a list of longs + assert (bytes.length % Long.BYTES) == 0; + + int listSize = bytes.length / Long.BYTES; + + var longs = new ArrayList<Long>(listSize); + + for (int i = 0; i < listSize; i++) { + long l = (long) LONG_ARRAY_HANDLE.get(bytes, i * Long.BYTES); + + longs.add(l); + } + + return longs; + } + + /** + * Add a long value to an array of longs that is represented by an array of bytes. + * + * @param bytes Byte array that represents an array of longs. + * @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; + + // Allocate a one long size bigger array + int size = idx + Long.BYTES; + + var newBytes = new byte[size]; + + if (bytes != null) + // Copy the current value + System.arraycopy(bytes, 0, newBytes, 0, bytes.length); + + LONG_ARRAY_HANDLE.set(newBytes, idx, value); + + return newBytes; + } + + /** + * Iterates over the given iterator passing key-value pairs to the given consumer and checks the iterator's status + * afterwards. + * + * @param iterator Iterator. + * @param consumer Consumer of key-value pairs. + * @throws RocksDBException If failed. + */ + static void forEach(RocksIterator iterator, RocksBiConsumer consumer) throws RocksDBException { + for (; iterator.isValid(); iterator.next()) + consumer.accept(iterator.key(), iterator.value()); + + checkIterator(iterator); + } + + /** + * Iterates over the given iterator testing key-value pairs with the given predicate and checks the iterator's status + * afterwards. + * + * @param iterator Iterator. + * @param consumer Consumer of key-value pairs. + * @return {@code true} if a matching key-value pair has been found, {@code false} otherwise. + * @throws RocksDBException If failed. + */ + static boolean find(RocksIterator iterator, RocksBiPredicate consumer) throws RocksDBException { + for (; iterator.isValid(); iterator.next()) { + boolean result = consumer.test(iterator.key(), iterator.value()); + + if (result) { + checkIterator(iterator); Review comment: Not sure, but I think we can get the values from the invalid iterator -- 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]
