You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ag...@apache.org on 2021/03/29 23:26:55 UTC

[ignite-3] 03/04: IGNITE-14198 Revised meta storage interface: introduced Key entity instead of byte array.

This is an automated email from the ASF dual-hosted git repository.

agura pushed a commit to branch ignite-14198
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit c373aa886457f71a5f474f261e814e8f91583e41
Author: Andrey Gura <ag...@apache.org>
AuthorDate: Fri Mar 26 00:22:04 2021 +0300

    IGNITE-14198 Revised meta storage interface: introduced Key entity instead of byte array.
---
 .../ignite/util/LexicographicComparator.java       | 51 +++++++++++++
 .../metastorage/client/MetaStorageService.java     | 88 +++++++++++++---------
 modules/metastorage-common/pom.xml                 |  6 ++
 .../apache/ignite/metastorage/common/Entry.java    |  4 +-
 .../org/apache/ignite/metastorage/common/Key.java  | 81 ++++++++++++++++++++
 pom.xml                                            |  4 +-
 6 files changed, 193 insertions(+), 41 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/util/LexicographicComparator.java b/modules/core/src/main/java/org/apache/ignite/util/LexicographicComparator.java
new file mode 100644
index 0000000..103fdc2
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/util/LexicographicComparator.java
@@ -0,0 +1,51 @@
+/*
+ * 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.util;
+
+import java.util.Comparator;
+
+/**
+ * Byte array lexicographic comparator.
+ */
+public class LexicographicComparator implements Comparator<byte[]> {
+    /** Comparator instance. */
+    private static final Comparator<byte[]> INSTANCE = new LexicographicComparator();
+
+    /**
+     * Returns instance of comparator.
+     *
+     * @return Comparator instance.
+     */
+    public static Comparator<byte[]> getInstance() {
+        return INSTANCE;
+    }
+
+    /** {@inheritDoc} */
+    @Override public int compare(byte[] o1, byte[] o2) {
+        int minLength = Math.min(o1.length, o2.length);
+
+        for (int i = 0; i < minLength; ++i) {
+            int res = Byte.compareUnsigned(o1[i], o2[i]);
+
+            if (res != 0)
+                return res;
+        }
+
+        return o1.length - o2.length;
+    }
+}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/metastorage/client/MetaStorageService.java b/modules/metastorage-client/src/main/java/org/apache/ignite/metastorage/client/MetaStorageService.java
index 2fb7b47..50e19dd 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/metastorage/client/MetaStorageService.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/metastorage/client/MetaStorageService.java
@@ -18,13 +18,14 @@
 package org.apache.ignite.metastorage.client;
 
 import java.util.Collection;
-import java.util.List;
-import java.util.UUID;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.lang.IgniteUuid;
 import org.apache.ignite.metastorage.common.CompactedException;
 import org.apache.ignite.metastorage.common.Condition;
 import org.apache.ignite.metastorage.common.Cursor;
 import org.apache.ignite.metastorage.common.Entry;
+import org.apache.ignite.metastorage.common.Key;
 import org.apache.ignite.metastorage.common.Operation;
 import org.apache.ignite.metastorage.common.OperationTimeoutException;
 import org.apache.ignite.metastorage.common.WatchListener;
@@ -41,10 +42,11 @@ public interface MetaStorageService {
      * @param key Key. Couldn't be {@code null}.
      * @return An entry for the given key. Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Entry> get(@NotNull byte[] key);
+    CompletableFuture<Entry> get(@NotNull Key key);
 
     /**
      * Retrieves an entry for the given key and the revision upper bound.
@@ -56,23 +58,24 @@ public interface MetaStorageService {
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
      * Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Entry> get(@NotNull byte[] key, long revUpperBound);
+    CompletableFuture<Entry> get(@NotNull Key key, long revUpperBound);
 
     /**
      * Retrieves entries for given keys.
      *
      * @param keys The collection of keys. Couldn't be {@code null} or empty.
      *             Collection elements couldn't be {@code null}.
-     * @return A list of entries for given keys. The order of entries in the result list corresponds to
-     * the traversal order of {@code keys} collection. Couldn't be {@code null}.
+     * @return A map of entries for given keys. Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<List<Entry>> getAll(Collection<byte[]> keys);
+    CompletableFuture<Map<Key, Entry>> getAll(Collection<Key> keys);
 
     /**
      * Retrieves entries for given keys and the revision upper bound.
@@ -80,16 +83,16 @@ public interface MetaStorageService {
      * @param keys The collection of keys. Couldn't be {@code null} or empty.
      *             Collection elements couldn't be {@code null}.
      * @param revUpperBound  The upper bound for entry revisions. Must be positive.
-     * @return A list of entries for given keys and maximum revision limited by {@code revUpperBound}.
-     * The order of entries in the result list corresponds to the traversal order of {@code keys} collection.
+     * @return A map of entries for given keys and maximum revision limited by {@code revUpperBound}.
      * Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
      * Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<List<Entry>> getAll(Collection<byte[]> keys, long revUpperBound);
+    CompletableFuture<Map<Key, Entry>> getAll(Collection<Key> keys, long revUpperBound);
 
     /**
      * Inserts or updates an entry with the given key and the given value.
@@ -98,10 +101,11 @@ public interface MetaStorageService {
      * @param value The value.Couldn't be {@code null}.
      * @return Completed future.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Void> put(@NotNull byte[] key, @NotNull byte[] value);
+    CompletableFuture<Void> put(@NotNull Key key, @NotNull byte[] value);
 
     /**
      * Inserts or updates an entry with the given key and the given value and
@@ -111,37 +115,36 @@ public interface MetaStorageService {
      * @param value The value.Couldn't be {@code null}.
      * @return A previous entry for the given key. Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Entry> getAndPut(@NotNull byte[] key, @NotNull byte[] value);
+    CompletableFuture<Entry> getAndPut(@NotNull Key key, @NotNull byte[] value);
 
     /**
      * Inserts or updates entries with given keys and given values.
-     * Size of {@code keys} and {@code values} must be the same.
      *
-     * @param keys The list of keys. Couldn't be {@code null} or empty.
-     * @param values The list of values corresponding to the list of keys. Couldn't be {@code null} or empty.
+     * @param vals The map of keys and corresponding values. Couldn't be {@code null} or empty.
      * @return Completed future.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Void> putAll(@NotNull List<byte[]> keys, @NotNull List<byte[]> values);
+    CompletableFuture<Void> putAll(@NotNull Map<Key, byte[]> vals);
 
     /**
      * Inserts or updates entries with given keys and given values and
      * retrieves a previous entries for given keys.
-     * Size of {@code keys} and {@code values} must be the same.
      *
-     * @param keys The list of keys. Couldn't be {@code null} or empty.
-     * @param values The list of values corresponding to the list of keys. Couldn't be {@code null} or empty.
-     * @return A list of entries for given keys. Couldn't be {@code null}.
+     * @param vals The map of keys and corresponding values. Couldn't be {@code null} or empty.
+     * @return A map of entries for given keys. Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<List<Entry>> getAndPutAll(@NotNull List<byte[]> keys, @NotNull List<byte[]> values);
+    CompletableFuture<Map<Key, Entry>> getAndPutAll(@NotNull Map<Key, byte[]> vals);
 
     /**
      * Removes an entry for the given key.
@@ -149,10 +152,11 @@ public interface MetaStorageService {
      * @param key The key. Couldn't be {@code null}.
      * @return Completed future.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Void> remove(@NotNull byte[] key);
+    CompletableFuture<Void> remove(@NotNull Key key);
 
     /**
      * Removes an entry for the given key.
@@ -160,34 +164,37 @@ public interface MetaStorageService {
      * @param key The key. Couldn't be {@code null}.
      * @return A previous entry for the given key. Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Void> getAndRemove(@NotNull byte[] key);
+    CompletableFuture<Entry> getAndRemove(@NotNull Key key);
 
     /**
      * Removes entries for given keys.
      *
-     * @param key The key. Couldn't be {@code null}.
+     * @param keys The keys collection. Couldn't be {@code null}.
      * @return Completed future.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<Void> removeAll(@NotNull Collection<byte[]> key);
+    CompletableFuture<Void> removeAll(@NotNull Collection<Key> keys);
 
     /**
      * Removes entries for given keys and retrieves previous entries.
      *
-     * @param key The key. Couldn't be {@code null}.
-     * @return A list of previous entries for given keys..
+     * @param keys The keys collection. Couldn't be {@code null}.
+     * @return A map of previous entries for given keys..
      * The order of entries in the result list corresponds to the traversal order of {@code keys} collection.
      * Couldn't be {@code null}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    CompletableFuture<List<Entry>> getAndRemoveAll(@NotNull Collection<byte[]> key);
+    CompletableFuture<Map<Key, Entry>> getAndRemoveAll(@NotNull Collection<Key> keys);
 
 
     /**
@@ -201,13 +208,14 @@ public interface MetaStorageService {
      * @param failure The update which will be applied in case of condition evaluation yields {@code false}.
      * @return Future result {@code true} if {@code success} update was applied, otherwise {@code false}.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      * @see Condition
      * @see Operation
      */
     // TODO: https://issues.apache.org/jira/browse/IGNITE-14269: will be replaced by conditional multi update.
     @NotNull
-    CompletableFuture<Boolean> invoke(@NotNull byte[] key, @NotNull Condition condition,
+    CompletableFuture<Boolean> invoke(@NotNull Key key, @NotNull Condition condition,
                                       @NotNull Operation success, @NotNull Operation failure);
 
     /**
@@ -221,13 +229,14 @@ public interface MetaStorageService {
      * @param failure The update which will be applied in case of condition evaluation yields {@code false}.
      * @return A previous entry for the given key.
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      * @see Condition
      * @see Operation
      */
     //TODO: https://issues.apache.org/jira/browse/IGNITE-14269: will be replaced by conditional multi update.
     @NotNull
-    CompletableFuture<Entry> getAndInvoke(@NotNull byte[] key, @NotNull Condition condition,
+    CompletableFuture<Entry> getAndInvoke(@NotNull Key key, @NotNull Condition condition,
                                           @NotNull Operation success, @NotNull Operation failure);
 
     /**
@@ -240,24 +249,26 @@ public interface MetaStorageService {
      * @return Cursor built upon entries corresponding to the given range and revision.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    Cursor<Entry> range(@NotNull byte[] keyFrom, @Nullable byte[] keyTo, long revUpperBound);
+    Cursor<Entry> range(@NotNull Key keyFrom, @Nullable Key keyTo, long revUpperBound);
 
     /**
      * Retrieves entries for the given key range in lexicographic order. Short cut for
-     * {@link #range(byte[], byte[], long)} where {@code revUpperBound == -1}.
+     * {@link #range(Key, Key, long)} where {@code revUpperBound == -1}.
      *
      * @param keyFrom Start key of range (inclusive). Couldn't be {@code null}.
      * @param keyTo End key of range (exclusive). Could be {@code null}.
      * @return Cursor built upon entries corresponding to the given range and revision.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
+     * @see Key
      * @see Entry
      */
     @NotNull
-    Cursor<Entry> range(@NotNull byte[] keyFrom, @Nullable byte[] keyTo);
+    Cursor<Entry> range(@NotNull Key keyFrom, @Nullable Key keyTo);
 
     /**
      * Subscribes on meta storage updates matching the parameters.
@@ -271,11 +282,12 @@ public interface MetaStorageService {
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
      * Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
     //TODO: UUID Should be replaced by IgniteUUID when it will be introduced.
-    CompletableFuture<UUID> watch(@Nullable byte[] keyFrom, @Nullable byte[] keyTo,
+    CompletableFuture<IgniteUuid> watch(@Nullable Key keyFrom, @Nullable Key keyTo,
                                   long revision, @NotNull WatchListener lsnr);
 
     /**
@@ -289,11 +301,12 @@ public interface MetaStorageService {
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
      * Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
     //TODO: UUID Should be replaced by IgniteUUID when it will be introduced.
-    CompletableFuture<UUID> watch(@NotNull byte[] key, long revision, @NotNull WatchListener lsnr);
+    CompletableFuture<IgniteUuid> watch(@NotNull Key key, long revision, @NotNull WatchListener lsnr);
 
     /**
      * Subscribes on meta storage updates for given keys.
@@ -306,11 +319,12 @@ public interface MetaStorageService {
      * @throws OperationTimeoutException If the operation is timed out. Will be thrown on getting future result.
      * @throws CompactedException If the desired revisions are removed from the storage due to a compaction.
      * Will be thrown on getting future result.
+     * @see Key
      * @see Entry
      */
     @NotNull
     //TODO: UUID Should be replaced by IgniteUUID when it will be introduced.
-    CompletableFuture<UUID> watch(@NotNull Collection<byte[]> keys, long revision, @NotNull WatchListener lsnr);
+    CompletableFuture<IgniteUuid> watch(@NotNull Collection<Key> keys, long revision, @NotNull WatchListener lsnr);
 
     /**
      * Cancels subscription for the given identifier.
@@ -321,7 +335,7 @@ public interface MetaStorageService {
      */
     @NotNull
     //TODO: UUID Should be replaced by IgniteUUID when it will be introduced.
-    CompletableFuture<Void> stopWatch(@NotNull UUID id);
+    CompletableFuture<Void> stopWatch(@NotNull IgniteUuid id);
 
     /**
      * Compacts meta storage (removes all tombstone entries and old entries except of entries with latest revision).
diff --git a/modules/metastorage-common/pom.xml b/modules/metastorage-common/pom.xml
index cd9e1c6..4d48d03 100644
--- a/modules/metastorage-common/pom.xml
+++ b/modules/metastorage-common/pom.xml
@@ -34,6 +34,12 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.apache.ignite</groupId>
+            <artifactId>ignite-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
             <groupId>org.jetbrains</groupId>
             <artifactId>annotations</artifactId>
         </dependency>
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Entry.java b/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Entry.java
index b01128e..03263b7 100644
--- a/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Entry.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Entry.java
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
 /**
  * Represents a storage unit as entry with key, value and revision, where
  * <ul>
- *     <li>key - an unique entry's key represented by an array of bytes. Keys are comparable in lexicographic manner.</li>
+ *     <li>key - an unique entry's key. Keys are comparable in lexicographic manner.</li>
  *     <ul>value - a data which is associated with a key and represented as an array of bytes.</ul>
  *     <ul>revision - a number which denotes a version of whole meta storage. Each change increments the revision.</ul>
  * </ul>
@@ -34,7 +34,7 @@ public interface Entry {
      *
      * @return The key.
      */
-    @NotNull byte[] key();
+    @NotNull Key key();
 
     /**
      * Returns a value. Could be {@code null} for empty entry.
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Key.java b/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Key.java
new file mode 100644
index 0000000..a160307
--- /dev/null
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/metastorage/common/Key.java
@@ -0,0 +1,81 @@
+/*
+ * 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.metastorage.common;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import org.apache.ignite.util.LexicographicComparator;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A wrapper for meta storage key represented by byte array.
+ */
+public final class Key implements Comparable<Key> {
+    /** Byte-wise representation of the key. */
+    @NotNull
+    private final byte[] arr;
+
+    /**
+     * Constructs key instance from the given string.
+     *
+     * @param s The string key representation. Can't be {@code null}.
+     */
+    public Key(@NotNull String s) {
+        this(s.getBytes(StandardCharsets.UTF_8));
+    }
+
+    /**
+     * Constructs key instance from the given byte array. <em>Note:</em> copy of the given byte array will not be
+     * created in order to avoid redundant memory consumption.
+     *
+     * @param arr Byte array. Can't be {@code null}.
+     */
+    public Key(@NotNull byte[] arr) {
+        this.arr = arr;
+    }
+
+    /**
+     * Returns the key as byte array.
+     *
+     * @return Bytes of the key.
+     */
+    public byte[] bytes() {
+        return arr;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object o) {
+        if (this == o) return true;
+
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Key key = (Key)o;
+
+        return Arrays.equals(arr, key.arr);
+    }
+
+    /** {@inheritDoc} */
+    @Override public int hashCode() {
+        return Arrays.hashCode(arr);
+    }
+
+    /** {@inheritDoc} */
+    @Override public int compareTo(@NotNull Key other) {
+        return LexicographicComparator.getInstance().compare(this.arr, other.arr);
+    }
+}
diff --git a/pom.xml b/pom.xml
index 05ce46d..a81b672 100644
--- a/pom.xml
+++ b/pom.xml
@@ -41,9 +41,9 @@
         <module>modules/configuration</module>
         <module>modules/configuration-annotation-processor</module>
         <module>modules/core</module>
-        <module>modules/network</module>
-        <module>modules/metastorage-common</module>
         <module>modules/metastorage-client</module>
+        <module>modules/metastorage-common</module>
+        <module>modules/network</module>
         <module>modules/raft-client</module>
         <module>modules/rest</module>
         <module>modules/runner</module>