You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sk...@apache.org on 2022/02/17 13:57:36 UTC
[ignite-3] branch main updated: IGNITE-14269 Added multi-invoke feature for meta storage. Fixes #619
This is an automated email from the ASF dual-hosted git repository.
sk0x50 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push:
new a3b03c7 IGNITE-14269 Added multi-invoke feature for meta storage. Fixes #619
a3b03c7 is described below
commit a3b03c7e78ff4b96320d4cde1f4ebd6847e82b9f
Author: Kirill Gusakov <kg...@gmail.com>
AuthorDate: Thu Feb 17 16:57:12 2022 +0300
IGNITE-14269 Added multi-invoke feature for meta storage. Fixes #619
Signed-off-by: Slava Koptilin <sl...@gmail.com>
---
modules/metastorage-client/pom.xml | 6 +
.../client/ItMetaStorageServiceTest.java | 193 ++++++++++-
.../metastorage/client/CompoundCondition.java | 99 ++++++
.../internal/metastorage/client/Condition.java | 363 +--------------------
.../internal/metastorage/client/Conditions.java | 28 +-
.../ignite/internal/metastorage/client/If.java | 154 +++++++++
.../metastorage/client/MetaStorageService.java | 16 +-
.../metastorage/client/MetaStorageServiceImpl.java | 69 +++-
.../internal/metastorage/client/Operations.java | 67 +++-
.../{Condition.java => SimpleCondition.java} | 50 +--
.../internal/metastorage/client/Statement.java | 83 +++++
.../metastorage/client/StatementResult.java | 109 +++++++
.../ignite/internal/metastorage/client/Update.java | 60 ++++
.../internal/metastorage/common/StatementInfo.java | 86 +++++
.../metastorage/common/StatementResultInfo.java} | 32 +-
.../internal/metastorage/common/UpdateInfo.java | 63 ++++
.../common/command/CompoundConditionInfo.java | 73 +++++
.../common/command/CompoundConditionType.java} | 26 +-
.../metastorage/common/command/ConditionInfo.java | 72 +---
.../metastorage/common/command/IfInfo.java | 76 +++++
.../common/command/MultiInvokeCommand.java} | 32 +-
...ConditionInfo.java => SimpleConditionInfo.java} | 7 +-
modules/metastorage-server/pom.xml | 6 +
.../server/AbstractCompoundCondition.java | 97 ++++++
...Condition.java => AbstractSimpleCondition.java} | 33 +-
.../{TombstoneCondition.java => AndCondition.java} | 25 +-
.../internal/metastorage/server/Condition.java | 18 +-
.../metastorage/server/ExistenceCondition.java | 4 +-
.../ignite/internal/metastorage/server/If.java | 74 +++++
.../metastorage/server/KeyValueStorage.java | 11 +
.../{TombstoneCondition.java => OrCondition.java} | 25 +-
.../metastorage/server/RevisionCondition.java | 4 +-
.../internal/metastorage/server/Statement.java | 84 +++++
.../metastorage/server/StatementResult.java | 66 ++++
.../metastorage/server/TombstoneCondition.java | 2 +-
.../server/{Condition.java => Update.java} | 43 ++-
.../ignite/internal/metastorage/server/Value.java | 2 +-
.../metastorage/server/ValueCondition.java | 2 +-
.../server/persistence/RocksDbKeyValueStorage.java | 74 ++++-
.../server/raft/MetaStorageListener.java | 108 ++++--
.../server/AbstractKeyValueStorageTest.java | 236 ++++++++++++--
.../metastorage/server/AndConditionTest.java | 109 +++++++
.../metastorage/server/OrConditionTest.java | 109 +++++++
.../server/SimpleInMemoryKeyValueStorage.java | 52 ++-
.../internal/metastorage/MetaStorageManager.java | 19 ++
.../storage/DistributedConfigurationStorage.java | 8 +-
.../DistributedConfigurationStorageTest.java | 10 +-
47 files changed, 2298 insertions(+), 687 deletions(-)
diff --git a/modules/metastorage-client/pom.xml b/modules/metastorage-client/pom.xml
index f6dc897..12b46c7 100644
--- a/modules/metastorage-client/pom.xml
+++ b/modules/metastorage-client/pom.xml
@@ -63,6 +63,12 @@
</dependency>
<dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-raft</artifactId>
<scope>test</scope>
diff --git a/modules/metastorage-client/src/integrationTest/java/org/apache/ignite/internal/metastorage/client/ItMetaStorageServiceTest.java b/modules/metastorage-client/src/integrationTest/java/org/apache/ignite/internal/metastorage/client/ItMetaStorageServiceTest.java
index 7d9244e..c54f084 100644
--- a/modules/metastorage-client/src/integrationTest/java/org/apache/ignite/internal/metastorage/client/ItMetaStorageServiceTest.java
+++ b/modules/metastorage-client/src/integrationTest/java/org/apache/ignite/internal/metastorage/client/ItMetaStorageServiceTest.java
@@ -18,8 +18,18 @@
package org.apache.ignite.internal.metastorage.client;
import static java.util.stream.Collectors.toList;
+import static org.apache.ignite.internal.metastorage.client.CompoundCondition.or;
+import static org.apache.ignite.internal.metastorage.client.Conditions.revision;
+import static org.apache.ignite.internal.metastorage.client.Conditions.value;
+import static org.apache.ignite.internal.metastorage.client.If.iif;
+import static org.apache.ignite.internal.metastorage.client.ItMetaStorageServiceTest.ServerConditionMatcher.cond;
+import static org.apache.ignite.internal.metastorage.client.ItMetaStorageServiceTest.ServerUpdateMatcher.upd;
+import static org.apache.ignite.internal.metastorage.client.Operations.ops;
+import static org.apache.ignite.internal.metastorage.client.Operations.put;
+import static org.apache.ignite.internal.metastorage.client.Operations.remove;
import static org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology;
import static org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -36,6 +46,7 @@ import static org.mockito.Mockito.when;
import java.nio.file.Path;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -50,8 +61,16 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.ignite.internal.metastorage.common.OperationType;
+import org.apache.ignite.internal.metastorage.server.AbstractCompoundCondition;
+import org.apache.ignite.internal.metastorage.server.AbstractSimpleCondition;
import org.apache.ignite.internal.metastorage.server.EntryEvent;
import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
+import org.apache.ignite.internal.metastorage.server.OrCondition;
+import org.apache.ignite.internal.metastorage.server.RevisionCondition;
+import org.apache.ignite.internal.metastorage.server.StatementResult;
+import org.apache.ignite.internal.metastorage.server.Update;
+import org.apache.ignite.internal.metastorage.server.ValueCondition;
+import org.apache.ignite.internal.metastorage.server.ValueCondition.Type;
import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener;
import org.apache.ignite.internal.raft.Loza;
import org.apache.ignite.internal.raft.server.RaftServer;
@@ -73,6 +92,8 @@ import org.apache.ignite.raft.client.service.RaftGroupService;
import org.apache.ignite.raft.jraft.RaftMessagesFactory;
import org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl;
import org.apache.ignite.utils.ClusterServiceTestUtils;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -743,6 +764,67 @@ public class ItMetaStorageServiceTest {
}
@Test
+ public void testMultiInvoke() throws Exception {
+ ByteArray key1 = new ByteArray(new byte[]{1});
+ ByteArray key2 = new ByteArray(new byte[]{2});
+ ByteArray key3 = new ByteArray(new byte[]{3});
+
+ var val1 = new byte[]{4};
+ var val2 = new byte[]{5};
+
+ var rval1 = new byte[]{6};
+ var rval2 = new byte[]{7};
+
+ /*
+ if (key1.value == val1 || key2.value != val2)
+ if (key3.revision == 3):
+ put(key1, rval1)
+ return true
+ else
+ put(key1, rval1)
+ remove(key2, rval2)
+ return false
+ else
+ put(key2, rval2)
+ return false
+ */
+
+ var iif = If.iif(or(value(key1).eq(val1), value(key2).ne(val2)),
+ iif(revision(key3).eq(3),
+ ops(put(key1, rval1)).yield(true),
+ ops(put(key1, rval1), remove(key2)).yield(false)),
+ ops(put(key2, rval2)).yield(false));
+
+ var ifCaptor = ArgumentCaptor.forClass(org.apache.ignite.internal.metastorage.server.If.class);
+
+ when(mockStorage.invoke(any())).thenReturn(new StatementResult(true));
+
+ assertTrue(metaStorageSvc.invoke(iif).get().getAsBoolean());
+
+ verify(mockStorage).invoke(ifCaptor.capture());
+
+ var resultIf = ifCaptor.getValue();
+
+ assertThat(resultIf.cond(), cond(new OrCondition(new ValueCondition(Type.EQUAL, key1.bytes(), val1),
+ new ValueCondition(Type.NOT_EQUAL, key2.bytes(), val2))));
+
+ assertThat(resultIf.andThen().iif().cond(), cond(new RevisionCondition(RevisionCondition.Type.EQUAL, key3.bytes(), 3)));
+
+ assertThat(resultIf.andThen().iif().andThen().update(), upd(new Update(
+ Arrays.asList(new org.apache.ignite.internal.metastorage.server.Operation(OperationType.PUT, key1.bytes(), rval1)),
+ new StatementResult(true))));
+
+ assertThat(resultIf.andThen().iif().orElse().update(), upd(new Update(
+ Arrays.asList(new org.apache.ignite.internal.metastorage.server.Operation(OperationType.PUT, key1.bytes(), rval1),
+ new org.apache.ignite.internal.metastorage.server.Operation(OperationType.REMOVE, key2.bytes(), null)),
+ new StatementResult(false))));
+
+ assertThat(resultIf.orElse().update(), upd(new Update(
+ Arrays.asList(new org.apache.ignite.internal.metastorage.server.Operation(OperationType.PUT, key2.bytes(), rval2)),
+ new StatementResult(false))));
+ }
+
+ @Test
public void testInvoke() throws Exception {
ByteArray expKey = new ByteArray(new byte[]{1});
@@ -758,7 +840,7 @@ public class ItMetaStorageServiceTest {
assertTrue(metaStorageSvc.invoke(condition, success, failure).get());
- var conditionCaptor = ArgumentCaptor.forClass(org.apache.ignite.internal.metastorage.server.Condition.class);
+ var conditionCaptor = ArgumentCaptor.forClass(AbstractSimpleCondition.class);
ArgumentCaptor<Collection<org.apache.ignite.internal.metastorage.server.Operation>> successCaptor =
ArgumentCaptor.forClass(Collection.class);
@@ -853,6 +935,115 @@ public class ItMetaStorageServiceTest {
}
/**
+ * Matcher for {@link Update}.
+ */
+ protected static class ServerUpdateMatcher extends TypeSafeMatcher<Update> {
+
+ private final Update update;
+
+ public ServerUpdateMatcher(Update update) {
+ this.update = update;
+ }
+
+ public static ServerUpdateMatcher upd(Update update) {
+ return new ServerUpdateMatcher(update);
+ }
+
+ @Override
+ protected boolean matchesSafely(Update item) {
+ return item.operations().size() == update.operations().size()
+ && Arrays.equals(item.result().bytes(), update.result().bytes())
+ && opsEqual(item.operations().iterator(), update.operations().iterator());
+
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(toString(update));
+ }
+
+ @Override
+ protected void describeMismatchSafely(Update item, Description mismatchDescription) {
+ mismatchDescription.appendText(toString(item));
+ }
+
+ private String toString(Update upd) {
+ return "Update([" + upd.operations().stream()
+ .map(o -> o.type() + "(" + Arrays.toString(o.key()) + ", " + Arrays.toString(o.value()) + ")")
+ .collect(Collectors.joining(",")) + "])";
+ }
+
+ private boolean opsEqual(Iterator<org.apache.ignite.internal.metastorage.server.Operation> ops1,
+ Iterator<org.apache.ignite.internal.metastorage.server.Operation> ops2) {
+ if (!ops1.hasNext()) {
+ return true;
+ } else {
+ return opEqual(ops1.next(), ops2.next()) && opsEqual(ops1, ops2);
+ }
+ }
+
+ private boolean opEqual(org.apache.ignite.internal.metastorage.server.Operation op1,
+ org.apache.ignite.internal.metastorage.server.Operation op2) {
+ return Arrays.equals(op1.key(), op2.key()) && Arrays.equals(op1.value(), op2.value()) && op1.type() == op2.type();
+ }
+ }
+
+ /**
+ * Matcher for {@link org.apache.ignite.internal.metastorage.server.Condition}.
+ */
+ protected static class ServerConditionMatcher extends TypeSafeMatcher<org.apache.ignite.internal.metastorage.server.Condition> {
+
+ private org.apache.ignite.internal.metastorage.server.Condition condition;
+
+ public ServerConditionMatcher(org.apache.ignite.internal.metastorage.server.Condition condition) {
+ this.condition = condition;
+ }
+
+ public static ServerConditionMatcher cond(org.apache.ignite.internal.metastorage.server.Condition condition) {
+ return new ServerConditionMatcher(condition);
+ }
+
+ @Override
+ protected boolean matchesSafely(org.apache.ignite.internal.metastorage.server.Condition item) {
+ if (condition.getClass() == item.getClass() && Arrays.deepEquals(condition.keys(), item.keys())) {
+ if (condition.getClass().isInstance(AbstractCompoundCondition.class)) {
+ return new ServerConditionMatcher(((AbstractCompoundCondition) condition).leftCondition())
+ .matchesSafely(((AbstractCompoundCondition) item).leftCondition())
+ && new ServerConditionMatcher(((AbstractCompoundCondition) condition).rightCondition())
+ .matchesSafely(((AbstractCompoundCondition) item).rightCondition());
+ } else {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(toString(condition));
+ }
+
+ @Override
+ protected void describeMismatchSafely(org.apache.ignite.internal.metastorage.server.Condition item,
+ Description mismatchDescription) {
+ mismatchDescription.appendText(toString(item));
+ }
+
+ private String toString(org.apache.ignite.internal.metastorage.server.Condition cond) {
+ if (cond instanceof AbstractSimpleCondition) {
+ return cond.getClass().getSimpleName() + "(" + Arrays.deepToString(cond.keys()) + ")";
+ } else if (cond instanceof AbstractCompoundCondition) {
+ return cond.getClass() + "(" + toString(((AbstractCompoundCondition) cond).leftCondition()) + ", " + toString(
+ ((AbstractCompoundCondition) cond).rightCondition()) + ")";
+ } else {
+ throw new IllegalArgumentException("Unknown condition type " + cond.getClass().getSimpleName());
+ }
+
+ }
+ }
+
+ /**
* Prepares meta storage by instantiating corresponding raft server with {@link MetaStorageListener} and {@link
* MetaStorageServiceImpl}.
*
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/CompoundCondition.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/CompoundCondition.java
new file mode 100644
index 0000000..0335008
--- /dev/null
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/CompoundCondition.java
@@ -0,0 +1,99 @@
+/*
+ * 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.client;
+
+import org.apache.ignite.internal.metastorage.common.command.CompoundConditionType;
+
+/**
+ * Compound condition, which consists two other simple/compound conditions.
+ */
+public class CompoundCondition implements Condition {
+ /** Left condition. */
+ private final Condition leftCond;
+
+ /** Right condition. */
+ private final Condition rightCond;
+
+ /** Compound condition type. */
+ private final CompoundConditionType compoundCondType;
+
+ /**
+ * Constructs new compound condition.
+ *
+ * @param leftCond Left condition.
+ * @param rightCond Right condition.
+ * @param compoundCondType Compound condition type.
+ */
+ public CompoundCondition(Condition leftCond, Condition rightCond,
+ CompoundConditionType compoundCondType) {
+ this.leftCond = leftCond;
+ this.rightCond = rightCond;
+ this.compoundCondType = compoundCondType;
+ }
+
+ /**
+ * Returns left condition.
+ *
+ * @return left condition.
+ */
+ public Condition leftCondition() {
+ return leftCond;
+ }
+
+ /**
+ * Returns right condition.
+ *
+ * @return right condition.
+ */
+ public Condition rightCondition() {
+ return rightCond;
+ }
+
+ /**
+ * Returns compound condition type.
+ *
+ * @return compound condition type.
+ */
+ public CompoundConditionType compoundConditionType() {
+ return compoundCondType;
+ }
+
+ /**
+ * Shortcut for create logical AND condition,
+ * which consists two another simple/compound conditions as left and right operands.
+ *
+ * @param leftCond leftCond condition.
+ * @param rightCond right condition.
+ * @return new AND compound condition.
+ */
+ public static CompoundCondition and(Condition leftCond, Condition rightCond) {
+ return new CompoundCondition(leftCond, rightCond, CompoundConditionType.AND);
+ }
+
+ /**
+ * Shortcut for create logical OR condition,
+ * which consists two another simple/compound conditions as left and right operands.
+ *
+ * @param leftCond leftCond condition.
+ * @param rightCond right condition.
+ * @return new OR compound condition.
+ */
+ public static CompoundCondition or(Condition leftCond, Condition rightCond) {
+ return new CompoundCondition(leftCond, rightCond, CompoundConditionType.OR);
+ }
+}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java
index cbf60a6..a4935c5 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -17,362 +17,11 @@
package org.apache.ignite.internal.metastorage.client;
-import java.util.Collection;
-import org.apache.ignite.internal.metastorage.common.ConditionType;
-
/**
- * Represents a condition for meta storage conditional update.
+ * Interface for boolean conditions.
*
- * @see MetaStorageService#invoke(Condition, Operation, Operation)
- * @see MetaStorageService#invoke(Condition, Collection, Collection)
+ * @see If
+ * @see SimpleCondition
+ * @see CompoundCondition
*/
-public final class Condition {
- /** Actual condition implementation. */
- private final InnerCondition cond;
-
- /**
- * Constructs a condition which wraps the actual condition implementation.
- *
- * @param cond The actual condition implementation.
- */
- Condition(InnerCondition cond) {
- this.cond = cond;
- }
-
- public InnerCondition inner() {
- return cond;
- }
-
- public ConditionType type() {
- return cond.type();
- }
-
- /**
- * Represents condition on entry revision. Only one type of condition could be applied to the one instance of condition. Subsequent
- * invocations of any method which produces condition will throw {@link IllegalStateException}.
- */
- public static final class RevisionCondition extends AbstractCondition {
- /** The revision as the condition argument. */
- private long rev;
-
- /**
- * Constructs a condition by a revision for an entry identified by the given key.
- *
- * @param key Identifies an entry which condition will be applied to.
- */
- RevisionCondition(byte[] key) {
- super(key);
- }
-
- public long revision() {
- return rev;
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_EQUAL}. This condition tests the given revision on equality with target
- * entry revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition eq(long rev) {
- validate(type());
-
- type(ConditionType.REV_EQUAL);
-
- this.rev = rev;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_NOT_EQUAL}. This condition tests the given revision on inequality with
- * target entry revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_NOT_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition ne(long rev) {
- validate(type());
-
- type(ConditionType.REV_NOT_EQUAL);
-
- this.rev = rev;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_GREATER}. This condition tests that the target entry revision is greater
- * than given revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_GREATER}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition gt(long rev) {
- validate(type());
-
- type(ConditionType.REV_GREATER);
-
- this.rev = rev;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_GREATER_OR_EQUAL}. This condition tests that the target entry revision is
- * greater than or equal to given revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_GREATER_OR_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition ge(long rev) {
- validate(type());
-
- type(ConditionType.REV_GREATER_OR_EQUAL);
-
- this.rev = rev;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_LESS}. This condition tests that target entry revision is less than the
- * given revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_LESS}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition lt(long rev) {
- validate(type());
-
- type(ConditionType.REV_LESS);
- this.rev = rev;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#REV_LESS_OR_EQUAL}. This condition tests that target entry revision is less
- * than or equal to the given revision.
- *
- * @param rev The revision.
- * @return The condition of type {@link ConditionType#REV_LESS_OR_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition le(long rev) {
- validate(type());
-
- type(ConditionType.REV_LESS_OR_EQUAL);
-
- this.rev = rev;
-
- return new Condition(this);
- }
- }
-
- /**
- * Represents condition on entry value. Only one type of condition could be applied to the one instance of condition. Subsequent
- * invocations of any method which produces condition will throw {@link IllegalStateException}.
- */
- public static final class ValueCondition extends AbstractCondition {
- /** The value as the condition argument. */
- private byte[] val;
-
- /**
- * Constructs a condition by a value for an entry identified by the given key.
- *
- * @param key Identifies an entry which condition will be applied to.
- */
- ValueCondition(byte[] key) {
- super(key);
- }
-
- public byte[] value() {
- return val;
- }
-
- /**
- * Produces the condition of type {@link ConditionType#VAL_EQUAL}. This condition tests the given value on equality with target
- * entry value.
- *
- * @param val The value.
- * @return The condition of type {@link ConditionType#VAL_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition eq(byte[] val) {
- validate(type());
-
- type(ConditionType.VAL_EQUAL);
-
- this.val = val;
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#VAL_NOT_EQUAL}. This condition tests the given value on inequality with
- * target entry value.
- *
- * @param val The value.
- * @return The condition of type {@link ConditionType#VAL_NOT_EQUAL}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition ne(byte[] val) {
- validate(type());
-
- type(ConditionType.VAL_NOT_EQUAL);
-
- this.val = val;
-
- return new Condition(this);
- }
- }
-
- /**
- * Represents condition on an entry existence. Only one type of condition could be applied to the one instance of condition. Subsequent
- * invocations of any method which produces condition will throw {@link IllegalStateException}.
- */
- public static final class ExistenceCondition extends AbstractCondition {
- /**
- * Constructs a condition on existence an entry identified by the given key.
- *
- * @param key Identifies an entry which condition will be applied to.
- */
- ExistenceCondition(byte[] key) {
- super(key);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#KEY_EXISTS}. This condition tests the existence of an entry identified by the
- * given key.
- *
- * @return The condition of type {@link ConditionType#KEY_EXISTS}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition exists() {
- validate(type());
-
- type(ConditionType.KEY_EXISTS);
-
- return new Condition(this);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#KEY_NOT_EXISTS}. This condition tests the non-existence of an entry
- * identified by the given key.
- *
- * @return The condition of type {@link ConditionType#KEY_NOT_EXISTS}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition notExists() {
- validate(type());
-
- type(ConditionType.KEY_NOT_EXISTS);
-
- return new Condition(this);
- }
- }
-
- /**
- * Represents condition on an entry's value which checks whether value is tombstone or not. Only one type of condition could be applied
- * to the one instance of condition. Subsequent invocations of any method which produces condition will throw {@link
- * IllegalStateException}.
- */
- public static final class TombstoneCondition extends AbstractCondition {
- /**
- * Constructs a condition on an entry, identified by the given key, is tombstone.
- *
- * @param key Identifies an entry which condition will be applied to.
- */
- TombstoneCondition(byte[] key) {
- super(key);
- }
-
- /**
- * Produces the condition of type {@link ConditionType#TOMBSTONE}. This condition tests that an entry's value, identified by the
- * given key, is tombstone.
- *
- * @return The condition of type {@link ConditionType#TOMBSTONE}.
- * @throws IllegalStateException In case when the condition is already defined.
- */
- public Condition tombstone() {
- validate(type());
-
- type(ConditionType.TOMBSTONE);
-
- return new Condition(this);
- }
- }
-
- /**
- * Checks that condition is not defined yet. If the condition is already defined then exception will be thrown.
- *
- * @throws IllegalStateException In case when the condition is already defined.
- */
- private static void validate(Enum<?> type) {
- if (type != null) {
- throw new IllegalStateException("Condition type " + type.name() + " is already defined.");
- }
- }
-
- /**
- * Defines condition interface.
- */
- public interface InnerCondition {
- /**
- * Returns key which identifies an entry which condition will be applied to.
- *
- * @return Key which identifies an entry which condition will be applied to.
- */
- byte[] key();
-
- ConditionType type();
- }
-
- /**
- * Defines an abstract condition with the key which identifies an entry which condition will be applied to.
- */
- private abstract static class AbstractCondition implements InnerCondition {
- /** Entry key. */
- private final byte[] key;
-
- /**
- * Condition type.
- */
- private ConditionType type;
-
- /**
- * Constructs a condition with the given entry key.
- *
- * @param key Key which identifies an entry which condition will be applied to.
- */
- private AbstractCondition(byte[] key) {
- this.key = key;
- }
-
- /**
- * Returns the key which identifies an entry which condition will be applied to.
- *
- * @return Key which identifies an entry which condition will be applied to.
- */
- @Override
- public byte[] key() {
- return key;
- }
-
- @Override
- public ConditionType type() {
- return type;
- }
-
- protected void type(ConditionType type) {
- this.type = type;
- }
- }
-}
+public interface Condition {}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Conditions.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Conditions.java
index b765d7f..39affbf 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Conditions.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Conditions.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -24,7 +24,7 @@ import org.jetbrains.annotations.NotNull;
* This class contains fabric methods which produce conditions needed for conditional multi update functionality provided by meta storage
* service.
*
- * @see Condition
+ * @see SimpleCondition
*/
public final class Conditions {
/**
@@ -32,10 +32,10 @@ public final class Conditions {
*
* @param key Identifies an entry which condition will be applied to. Can't be {@code null}.
* @return Condition on entry revision.
- * @see Condition.RevisionCondition
+ * @see SimpleCondition.RevisionCondition
*/
- public static Condition.RevisionCondition revision(@NotNull ByteArray key) {
- return new Condition.RevisionCondition(key.bytes());
+ public static SimpleCondition.RevisionCondition revision(@NotNull ByteArray key) {
+ return new SimpleCondition.RevisionCondition(key.bytes());
}
/**
@@ -43,10 +43,10 @@ public final class Conditions {
*
* @param key Identifies an entry which condition will be applied to. Can't be {@code null}.
* @return Condition on entry value.
- * @see Condition.ValueCondition
+ * @see SimpleCondition.ValueCondition
*/
- public static Condition.ValueCondition value(@NotNull ByteArray key) {
- return new Condition.ValueCondition(key.bytes());
+ public static SimpleCondition.ValueCondition value(@NotNull ByteArray key) {
+ return new SimpleCondition.ValueCondition(key.bytes());
}
/**
@@ -55,8 +55,8 @@ public final class Conditions {
* @param key Identifies an entry which condition will be applied to. Can't be {@code null}.
* @return Condition on entry existence.
*/
- public static Condition exists(@NotNull ByteArray key) {
- return new Condition.ExistenceCondition(key.bytes()).exists();
+ public static SimpleCondition exists(@NotNull ByteArray key) {
+ return new SimpleCondition.ExistenceCondition(key.bytes()).exists();
}
/**
@@ -65,8 +65,8 @@ public final class Conditions {
* @param key Identifies an entry which condition will be applied to. Can't be {@code null}.
* @return Condition on entry not existence.
*/
- public static Condition notExists(@NotNull ByteArray key) {
- return new Condition.ExistenceCondition(key.bytes()).notExists();
+ public static SimpleCondition notExists(@NotNull ByteArray key) {
+ return new SimpleCondition.ExistenceCondition(key.bytes()).notExists();
}
/**
@@ -75,8 +75,8 @@ public final class Conditions {
* @param key Identifies an entry which condition will be applied to. Can't be {@code null}.
* @return Condition on entry's value is tombstone.
*/
- public static Condition tombstone(@NotNull ByteArray key) {
- return new Condition.TombstoneCondition(key.bytes()).tombstone();
+ public static SimpleCondition tombstone(@NotNull ByteArray key) {
+ return new SimpleCondition.TombstoneCondition(key.bytes()).tombstone();
}
/**
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/If.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/If.java
new file mode 100644
index 0000000..fb6dde7
--- /dev/null
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/If.java
@@ -0,0 +1,154 @@
+/*
+ * 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.client;
+
+/**
+ * Root building block for the compound metastorage invoke command.
+ * Contains of boolean condition and 2 branches of execution, like usual programming language's if.
+ * Every branch can be either new {@link If} statement (non-terminal) or result terminal statement {@link Update}.
+ *
+ * <p>The easiest way to construct the needed {@link If} conditional statement is the builtin shortcut methods.
+ * For example to create the statement, which implement the following pseudo code:
+ * <pre>
+ * {@code
+ * {@link CompoundCondition}
+ * |
+ * {@link SimpleCondition} | {@link SimpleCondition}
+ * | | |
+ * if (key1.value == val1 || key2.value != val2):
+ * if (key3.revision == 3):-------------------|
+ * put(key1, rval1) |
+ * return 1 |
+ * else: | {@link If}
+ * put(key1, rval1) -| |
+ * remove(key2, rval2) -| {@link Update} |
+ * return 2 -|-----------------|
+ * else:
+ * put(key2, rval2)
+ * return 3
+ * }
+ * </pre>
+ * you can use the following code with static shortcut methods from the according classes:
+ * <pre>
+ * {@code
+ * iif(or(value(key1).eq(val1), value(key2).ne(val2)),
+ * iif(revision(key3).eq(3),
+ * ops(put(key1, rval1)).yield(1),
+ * ops(put(key1, rval1), remove(key2)).yield(2)),
+ * ops(put(key2, rval2)).yield(3))
+ * }
+ * </pre>
+ */
+public class If {
+ /** Boolean condition. */
+ private final Condition condition;
+
+ /** Execution branch, if condition evaluates to true (aka left branch). */
+ private final Statement andThen;
+
+ /** Execution branch, if condition evaluates to false (aka right branch). */
+ private final Statement orElse;
+
+ /**
+ * Construct new If statement.
+ *
+ * @param condition Boolean condition.
+ * @param andThen Left execution branch.
+ * @param orElse Right execution branch.
+ */
+ public If(Condition condition, Statement andThen, Statement orElse) {
+ this.condition = condition;
+ this.andThen = andThen;
+ this.orElse = orElse;
+ }
+
+ /**
+ * Returns boolean condition.
+ *
+ * @return Boolean condition.
+ */
+ public Condition condition() {
+ return condition;
+ }
+
+ /**
+ * Execution branch, if condition evaluates to true (aka left branch).
+ *
+ * @return Left execution branch.
+ */
+ public Statement andThen() {
+ return andThen;
+ }
+
+ /**
+ * Execution branch, if condition evaluates to false (aka right branch).
+ *
+ * @return Right execution branch.
+ */
+ public Statement orElse() {
+ return orElse;
+ }
+
+ /**
+ * Simple helper for create the new {@link If} statement.
+ *
+ * @param condition Boolean condition.
+ * @param andThen Execution branch, if condition evaluates to true.
+ * @param orElse Execution branch, if condition evaluates to false.
+ * @return new {@link If} statement.
+ */
+ public static If iif(Condition condition, If andThen, If orElse) {
+ return new If(condition, new Statement(andThen), new Statement(orElse));
+ }
+
+ /**
+ * Simple helper for create the new {@link If} statement.
+ *
+ * @param condition Boolean condition.
+ * @param andThen Execution branch, if condition evaluates to true.
+ * @param orElse Execution branch, if condition evaluates to false.
+ * @return new {@link If} statement.
+ */
+ public static If iif(Condition condition, If andThen, Update orElse) {
+ return new If(condition, new Statement(andThen), new Statement(orElse));
+ }
+
+ /**
+ * Simple helper for create the new {@link If} statement.
+ *
+ * @param condition Boolean condition.
+ * @param andThen Execution branch, if condition evaluates to true.
+ * @param orElse Execution branch, if condition evaluates to false.
+ * @return new {@link If} statement.
+ */
+ public static If iif(Condition condition, Update andThen, If orElse) {
+ return new If(condition, new Statement(andThen), new Statement(orElse));
+ }
+
+ /**
+ * Simple helper for create the new {@link If} statement.
+ *
+ * @param condition Boolean condition.
+ * @param andThen Execution branch, if condition evaluates to true.
+ * @param orElse Execution branch, if condition evaluates to false.
+ * @return new {@link If} statement.
+ */
+ public static If iif(Condition condition, Update andThen, Update orElse) {
+ return new If(condition, new Statement(andThen), new Statement(orElse));
+ }
+}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageService.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageService.java
index 13b4dc8..7a7ee66 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageService.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageService.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -219,12 +219,24 @@ public interface MetaStorageService {
* @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 Condition condition,
@NotNull Collection<Operation> success, @NotNull Collection<Operation> failure);
/**
+ * Invoke, which supports nested conditional statements.
+ * For detailed docs about construction of new if statement, look at {@link If} javadocs.
+ *
+ * @param iif {@link If} statement to invoke
+ * @return execution result
+ *
+ * @see If
+ * @see StatementResult
+ */
+ @NotNull
+ CompletableFuture<StatementResult> invoke(@NotNull If iif);
+
+ /**
* Retrieves entries for the given key range in lexicographic order. Entries will be filtered out by upper bound of given revision
* number.
*
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageServiceImpl.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageServiceImpl.java
index f762ac9..7524243 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageServiceImpl.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/MetaStorageServiceImpl.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -29,6 +29,10 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionException;
import org.apache.ignite.internal.metastorage.common.OperationType;
+import org.apache.ignite.internal.metastorage.common.StatementInfo;
+import org.apache.ignite.internal.metastorage.common.StatementResultInfo;
+import org.apache.ignite.internal.metastorage.common.UpdateInfo;
+import org.apache.ignite.internal.metastorage.common.command.CompoundConditionInfo;
import org.apache.ignite.internal.metastorage.common.command.ConditionInfo;
import org.apache.ignite.internal.metastorage.common.command.GetAllCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand;
@@ -36,7 +40,9 @@ import org.apache.ignite.internal.metastorage.common.command.GetAndPutCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndRemoveAllCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndRemoveCommand;
import org.apache.ignite.internal.metastorage.common.command.GetCommand;
+import org.apache.ignite.internal.metastorage.common.command.IfInfo;
import org.apache.ignite.internal.metastorage.common.command.InvokeCommand;
+import org.apache.ignite.internal.metastorage.common.command.MultiInvokeCommand;
import org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse;
import org.apache.ignite.internal.metastorage.common.command.OperationInfo;
import org.apache.ignite.internal.metastorage.common.command.PutAllCommand;
@@ -44,6 +50,7 @@ import org.apache.ignite.internal.metastorage.common.command.PutCommand;
import org.apache.ignite.internal.metastorage.common.command.RangeCommand;
import org.apache.ignite.internal.metastorage.common.command.RemoveAllCommand;
import org.apache.ignite.internal.metastorage.common.command.RemoveCommand;
+import org.apache.ignite.internal.metastorage.common.command.SimpleConditionInfo;
import org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse;
import org.apache.ignite.internal.metastorage.common.command.WatchExactKeysCommand;
import org.apache.ignite.internal.metastorage.common.command.WatchRangeKeysCommand;
@@ -194,6 +201,13 @@ public class MetaStorageServiceImpl implements MetaStorageService {
/** {@inheritDoc} */
@Override
+ public @NotNull CompletableFuture<StatementResult> invoke(@NotNull If iif) {
+ return metaStorageRaftGrpSvc.run(new MultiInvokeCommand(toIfInfo(iif)))
+ .thenApply(bi -> new StatementResult(((StatementResultInfo) bi).result()));
+ }
+
+ /** {@inheritDoc} */
+ @Override
public @NotNull Cursor<Entry> range(@NotNull ByteArray keyFrom, @Nullable ByteArray keyTo, long revUpperBound) {
return new CursorImpl<>(
metaStorageRaftGrpSvc,
@@ -311,29 +325,54 @@ public class MetaStorageServiceImpl implements MetaStorageService {
return res;
}
+ private static UpdateInfo toUpdateInfo(Update update) {
+ return new UpdateInfo(toOperationInfos(update.operations()), new StatementResultInfo(update.result().bytes()));
+ }
+
+ private static StatementInfo toIfBranchInfo(Statement statement) {
+ if (statement.isTerminal()) {
+ return new StatementInfo(toUpdateInfo(statement.update()));
+ } else {
+ return new StatementInfo(toIfInfo(statement.iif()));
+ }
+ }
+
+ private static IfInfo toIfInfo(If iif) {
+ return new IfInfo(toConditionInfo(iif.condition()), toIfBranchInfo(iif.andThen()), toIfBranchInfo(iif.orElse()));
+ }
+
private static ConditionInfo toConditionInfo(@NotNull Condition condition) {
ConditionInfo cnd = null;
+ if (condition instanceof SimpleCondition) {
+ Object obj = ((SimpleCondition) condition).inner();
- Object obj = condition.inner();
+ if (obj instanceof SimpleCondition.ExistenceCondition) {
+ SimpleCondition.ExistenceCondition inner = (SimpleCondition.ExistenceCondition) obj;
- if (obj instanceof Condition.ExistenceCondition) {
- Condition.ExistenceCondition inner = (Condition.ExistenceCondition) obj;
+ cnd = new SimpleConditionInfo(inner.key(), inner.type(), null, 0);
+ } else if (obj instanceof SimpleCondition.TombstoneCondition) {
+ SimpleCondition.TombstoneCondition inner = (SimpleCondition.TombstoneCondition) obj;
- cnd = new ConditionInfo(inner.key(), inner.type(), null, 0);
- } else if (obj instanceof Condition.TombstoneCondition) {
- Condition.TombstoneCondition inner = (Condition.TombstoneCondition) obj;
+ cnd = new SimpleConditionInfo(inner.key(), inner.type(), null, 0);
+ } else if (obj instanceof SimpleCondition.RevisionCondition) {
+ SimpleCondition.RevisionCondition inner = (SimpleCondition.RevisionCondition) obj;
- cnd = new ConditionInfo(inner.key(), inner.type(), null, 0);
- } else if (obj instanceof Condition.RevisionCondition) {
- Condition.RevisionCondition inner = (Condition.RevisionCondition) obj;
+ cnd = new SimpleConditionInfo(inner.key(), inner.type(), null, inner.revision());
+ } else if (obj instanceof SimpleCondition.ValueCondition) {
+ SimpleCondition.ValueCondition inner = (SimpleCondition.ValueCondition) obj;
+
+ cnd = new SimpleConditionInfo(inner.key(), inner.type(), inner.value(), 0);
+ } else {
+ assert false : "Unknown condition type: " + obj.getClass().getSimpleName();
+ }
- cnd = new ConditionInfo(inner.key(), inner.type(), null, inner.revision());
- } else if (obj instanceof Condition.ValueCondition) {
- Condition.ValueCondition inner = (Condition.ValueCondition) obj;
+ } else if (condition instanceof CompoundCondition) {
+ CompoundCondition cond = (CompoundCondition) condition;
- cnd = new ConditionInfo(inner.key(), inner.type(), inner.value(), 0);
+ cnd = new CompoundConditionInfo(toConditionInfo(cond.leftCondition()), toConditionInfo(cond.rightCondition()),
+ cond.compoundConditionType());
} else {
- assert false : "Unknown condition type: " + obj.getClass().getSimpleName();
+ assert false : "Unknown condition type: " + condition.getClass().getSimpleName();
}
return cnd;
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Operations.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Operations.java
index 65ff927..c33a01c 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Operations.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Operations.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -17,18 +17,72 @@
package org.apache.ignite.internal.metastorage.client;
+import java.util.Arrays;
import org.apache.ignite.lang.ByteArray;
/**
- * This class contains fabric methods which produce operations needed for conditional multi update functionality (invoke) provided by meta
- * storage service.
+ * Class to accumulate some operations to one batch, which can form complete {@link Update} statement together with statement result.
+ * Also, this class contains fabric methods which produce operations
+ * needed for conditional multi update functionality (invoke) provided by meta storage service.
*
* @see Operation
+ * @see Update
*/
public final class Operations {
/** No-op operation singleton. */
private static final Operation.NoOp NO_OP = new Operation.NoOp();
+ /** Operations. */
+ private final Operation[] operations;
+
+ /**
+ * Constructs new operations batch.
+ *
+ * @param operations operations.
+ */
+ private Operations(Operation... operations) {
+ this.operations = operations;
+ }
+
+ /**
+ * Produce new {@link Update} statement with boolean result.
+ *
+ * @param result boolean result.
+ * @return update statement.
+ */
+ public Update yield(boolean result) {
+ return new Update(Arrays.asList(operations), new StatementResult(result));
+ }
+
+ /**
+ * Produce new {@link Update} statement with integer result.
+ *
+ * @param result integer result.
+ * @return update statement.
+ */
+ public Update yield(int result) {
+ return new Update(Arrays.asList(operations), new StatementResult(result));
+ }
+
+ /**
+ * Produce new {@link Update} statement with empty result.
+ *
+ * @return update statement.
+ */
+ public Update yield() {
+ return new Update(Arrays.asList(operations), new StatementResult(new byte[]{}));
+ }
+
+ /**
+ * Shortcut to create new batch of operations, for dsl-like API, see {@link If}.
+ *
+ * @param operations operations.
+ * @return batch of operations.
+ */
+ public static Operations ops(Operation... operations) {
+ return new Operations(operations);
+ }
+
/**
* Creates operation of type <i>remove</i>. This type of operation removes entry.
*
@@ -58,11 +112,4 @@ public final class Operations {
public static Operation noop() {
return new Operation(NO_OP);
}
-
- /**
- * Default no-op constructor.
- */
- private Operations() {
- // No-op.
- }
}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/SimpleCondition.java
similarity index 90%
copy from modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java
copy to modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/SimpleCondition.java
index cbf60a6..559dd34 100644
--- a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Condition.java
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/SimpleCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -26,7 +26,7 @@ import org.apache.ignite.internal.metastorage.common.ConditionType;
* @see MetaStorageService#invoke(Condition, Operation, Operation)
* @see MetaStorageService#invoke(Condition, Collection, Collection)
*/
-public final class Condition {
+public final class SimpleCondition implements Condition {
/** Actual condition implementation. */
private final InnerCondition cond;
@@ -35,7 +35,7 @@ public final class Condition {
*
* @param cond The actual condition implementation.
*/
- Condition(InnerCondition cond) {
+ SimpleCondition(InnerCondition cond) {
this.cond = cond;
}
@@ -76,14 +76,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition eq(long rev) {
+ public SimpleCondition eq(long rev) {
validate(type());
type(ConditionType.REV_EQUAL);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -94,14 +94,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_NOT_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition ne(long rev) {
+ public SimpleCondition ne(long rev) {
validate(type());
type(ConditionType.REV_NOT_EQUAL);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -112,14 +112,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_GREATER}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition gt(long rev) {
+ public SimpleCondition gt(long rev) {
validate(type());
type(ConditionType.REV_GREATER);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -130,14 +130,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_GREATER_OR_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition ge(long rev) {
+ public SimpleCondition ge(long rev) {
validate(type());
type(ConditionType.REV_GREATER_OR_EQUAL);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -148,13 +148,13 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_LESS}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition lt(long rev) {
+ public SimpleCondition lt(long rev) {
validate(type());
type(ConditionType.REV_LESS);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -165,14 +165,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#REV_LESS_OR_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition le(long rev) {
+ public SimpleCondition le(long rev) {
validate(type());
type(ConditionType.REV_LESS_OR_EQUAL);
this.rev = rev;
- return new Condition(this);
+ return new SimpleCondition(this);
}
}
@@ -205,14 +205,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#VAL_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition eq(byte[] val) {
+ public SimpleCondition eq(byte[] val) {
validate(type());
type(ConditionType.VAL_EQUAL);
this.val = val;
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -223,14 +223,14 @@ public final class Condition {
* @return The condition of type {@link ConditionType#VAL_NOT_EQUAL}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition ne(byte[] val) {
+ public SimpleCondition ne(byte[] val) {
validate(type());
type(ConditionType.VAL_NOT_EQUAL);
this.val = val;
- return new Condition(this);
+ return new SimpleCondition(this);
}
}
@@ -255,12 +255,12 @@ public final class Condition {
* @return The condition of type {@link ConditionType#KEY_EXISTS}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition exists() {
+ public SimpleCondition exists() {
validate(type());
type(ConditionType.KEY_EXISTS);
- return new Condition(this);
+ return new SimpleCondition(this);
}
/**
@@ -270,12 +270,12 @@ public final class Condition {
* @return The condition of type {@link ConditionType#KEY_NOT_EXISTS}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition notExists() {
+ public SimpleCondition notExists() {
validate(type());
type(ConditionType.KEY_NOT_EXISTS);
- return new Condition(this);
+ return new SimpleCondition(this);
}
}
@@ -301,12 +301,12 @@ public final class Condition {
* @return The condition of type {@link ConditionType#TOMBSTONE}.
* @throws IllegalStateException In case when the condition is already defined.
*/
- public Condition tombstone() {
+ public SimpleCondition tombstone() {
validate(type());
type(ConditionType.TOMBSTONE);
- return new Condition(this);
+ return new SimpleCondition(this);
}
}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Statement.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Statement.java
new file mode 100644
index 0000000..f9bdb08
--- /dev/null
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Statement.java
@@ -0,0 +1,83 @@
+/*
+ * 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.client;
+
+/**
+ * Simple Either-like wrapper to hold one of the statement type: {@link If} or {@link Update}.
+ * Needed to construct and simple deconstruction of nested {@link If},
+ * instead of empty interface and instanceof-based unwrap.
+ *
+ * @see If
+ */
+public class Statement {
+ /** If value holder. */
+ private final If iif;
+
+ /** Update value holder. */
+ private final Update update;
+
+ /**
+ * Constructs new {@link If} statement.
+ *
+ * @param iif If
+ */
+ public Statement(If iif) {
+ this.iif = iif;
+ this.update = null;
+ }
+
+ /**
+ * Constructs new {@link Update} terminal statement.
+ *
+ * @param update Update
+ */
+ public Statement(Update update) {
+ this.update = update;
+ this.iif = null;
+ }
+
+ /**
+ * Returns true, if statement has no nested statement (i.e. it is {@link Update} statement).
+ *
+ * @return true, if statement has no nested statement (i.e. it is {@link Update} statement).
+ */
+ public boolean isTerminal() {
+ return update != null;
+ }
+
+ /**
+ * Returns If or {@code null}, if no If value.
+ *
+ * <p>Note: check which field is filled by {@link #isTerminal()}</p>
+ *
+ * @return If or {@code null}, if no If value.
+ */
+ public If iif() {
+ return iif;
+ }
+
+ /**
+ * Returns Update or {@code null}, if no update value.
+ * Note: check which field is filled by {@link #isTerminal()}
+ *
+ * @return Update or {@code null}, if no update value.
+ */
+ public Update update() {
+ return update;
+ }
+}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/StatementResult.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/StatementResult.java
new file mode 100644
index 0000000..b56ecbc
--- /dev/null
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/StatementResult.java
@@ -0,0 +1,109 @@
+/*
+ * 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.client;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Simple result of statement execution, backed by byte[] array.
+ * Provides some shortcut methods to represent the values of some primitive types.
+ */
+public class StatementResult {
+ /** Result data. */
+ private final byte[] result;
+
+ /**
+ * Constructs result from the byte array.
+ *
+ * @param result byte array.
+ */
+ public StatementResult(byte[] result) {
+ this.result = result;
+ }
+
+ /**
+ * Constructs result from the boolean value.
+ *
+ * @param result boolean.
+ */
+ public StatementResult(boolean result) {
+ this.result = new byte[] {(byte) (result ? 1 : 0)};
+ }
+
+ /**
+ * Constructs result from the int value.
+ *
+ * @param result int.
+ */
+ public StatementResult(int result) {
+ this.result = ByteBuffer.allocate(4).putInt(result).array();
+ }
+
+ /**
+ * Returns result value as a boolean.
+ *
+ * @return boolean result.
+ * @throws ResultConversionException if boolean conversion is not possible, or can have ambiguous behaviour.
+ */
+ public boolean getAsBoolean() {
+ if (result.length != 1) {
+ throw new ResultConversionException("Result can't be interpreted as boolean");
+ }
+
+ return result[0] != 0;
+
+ }
+
+ /**
+ * Returns result as an int.
+ *
+ * @return int result.
+ * @throws ResultConversionException if int conversion is not possible, or can have ambiguous behaviour.
+ */
+ public Integer getAsInt() {
+ if (result.length != 4) {
+ throw new ResultConversionException("Result can't be interpreted as int");
+ }
+
+ return ByteBuffer.wrap(result).getInt();
+ }
+
+ /**
+ * Returns backed byte array.
+ *
+ * @return backed byte array.
+ */
+ public byte[] bytes() {
+ return result;
+ }
+
+ /**
+ * Exception to propagate result type conversion issues.
+ */
+ public static class ResultConversionException extends RuntimeException {
+
+ /**
+ * Constructs new conversion exception.
+ *
+ * @param msg exception message.
+ */
+ public ResultConversionException(String msg) {
+ super(msg);
+ }
+ }
+}
diff --git a/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Update.java b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Update.java
new file mode 100644
index 0000000..f0da1ca
--- /dev/null
+++ b/modules/metastorage-client/src/main/java/org/apache/ignite/internal/metastorage/client/Update.java
@@ -0,0 +1,60 @@
+/*
+ * 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.client;
+
+import java.util.Collection;
+
+/**
+ * Simple operations + result wrapper to describe the terminal branch of {@link If} execution.
+ */
+public class Update {
+ /** Operations. */
+ private final Collection<Operation> ops;
+
+ /** Result. */
+ private final StatementResult result;
+
+ /**
+ * Constructs new update object.
+ *
+ * @param ops operations
+ * @param result result
+ */
+ public Update(Collection<Operation> ops, StatementResult result) {
+ this.ops = ops;
+ this.result = result;
+ }
+
+ /**
+ * Returns operations.
+ *
+ * @return operations.
+ */
+ public Collection<Operation> operations() {
+ return ops;
+ }
+
+ /**
+ * Returns result.
+ *
+ * @return result.
+ */
+ public StatementResult result() {
+ return result;
+ }
+}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementInfo.java
new file mode 100644
index 0000000..a46e0c6
--- /dev/null
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementInfo.java
@@ -0,0 +1,86 @@
+/*
+ * 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.common;
+
+import java.io.Serializable;
+import org.apache.ignite.internal.metastorage.common.command.IfInfo;
+
+/**
+ * Definition of simple Either-like wrapper to hold one of the statement type: {@link IfInfo} or {@link UpdateInfo}.
+ * Needed to construct and simple deconstruction of nested {@link IfInfo},
+ * instead of empty interface and instanceof-based unwrap.
+ *
+ * @see IfInfo
+ * @see UpdateInfo
+ */
+public class StatementInfo implements Serializable {
+ /** If definition holder. */
+ private final IfInfo iif;
+
+ /** Update definition holder. */
+ private final UpdateInfo update;
+
+ /**
+ * Constructs new {@link IfInfo} statement definition.
+ *
+ * @param iif If statement definition
+ */
+ public StatementInfo(IfInfo iif) {
+ this.iif = iif;
+ this.update = null;
+ }
+
+ /**
+ * Constructs new {@link UpdateInfo} terminal statement definition.
+ *
+ * @param update Update statement definition
+ */
+ public StatementInfo(UpdateInfo update) {
+ this.update = update;
+ this.iif = null;
+ }
+
+ /**
+ * Returns true, if statement has no nested statement (i.e. it is {@link UpdateInfo} statement definition).
+ *
+ * @return true, if statement has no nested statement (i.e. it is {@link UpdateInfo} statement definition).
+ */
+ public boolean isTerminal() {
+ return update != null;
+ }
+
+ /**
+ * Returns {@link IfInfo} or {@code null}, if iif is not defined.
+ * Note: check which field is filled by {@link #isTerminal()}
+ *
+ * @return {@link IfInfo} or {@code null}, if iif is not defined.
+ */
+ public IfInfo iif() {
+ return iif;
+ }
+
+ /**
+ * Returns {@link UpdateInfo} or {@code null}, if update is not defined.
+ * Note: check which field is filled by {@link #isTerminal()}
+ *
+ * @return {@link UpdateInfo} or {@code null}, if update is not defined.
+ */
+ public UpdateInfo update() {
+ return update;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementResultInfo.java
similarity index 53%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
copy to modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementResultInfo.java
index 19a4b28..f41d26a 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/StatementResultInfo.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -15,26 +15,32 @@
* limitations under the License.
*/
-package org.apache.ignite.internal.metastorage.server;
+package org.apache.ignite.internal.metastorage.common;
-import org.jetbrains.annotations.NotNull;
+import java.io.Serializable;
/**
- * Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
+ * Simple result definition of statement execution, backed by byte[] array.
*/
-public class TombstoneCondition extends AbstractCondition {
+public class StatementResultInfo implements Serializable {
+ /** Result data. */
+ private final byte[] res;
+
/**
- * Constructs a condition with the given entry key.
+ * Constructs result definition from the byte array.
*
- * @param key Key identifies an entry which the condition will applied to.
+ * @param res byte array.
*/
- public TombstoneCondition(@NotNull byte[] key) {
- super(key);
+ public StatementResultInfo(byte[] res) {
+ this.res = res;
}
- /** {@inheritDoc} */
- @Override
- public boolean test(@NotNull Entry e) {
- return e.tombstone();
+ /**
+ * Returns result as row byte array.
+ *
+ * @return result as row byte array.
+ */
+ public byte[] result() {
+ return res;
}
}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/UpdateInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/UpdateInfo.java
new file mode 100644
index 0000000..d782e4b
--- /dev/null
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/UpdateInfo.java
@@ -0,0 +1,63 @@
+/*
+ * 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.common;
+
+import java.io.Serializable;
+import java.util.Collection;
+import org.apache.ignite.internal.metastorage.common.command.OperationInfo;
+
+/**
+ * Simple operations + result wrapper definition to describe the terminal branch
+ * of {@link org.apache.ignite.internal.metastorage.common.command.IfInfo} execution.
+ */
+public class UpdateInfo implements Serializable {
+ /** Operations. */
+ private final Collection<OperationInfo> ops;
+
+ /** Result. */
+ private final StatementResultInfo result;
+
+ /**
+ * Constructs new update definition.
+ *
+ * @param ops operations
+ * @param result result
+ */
+ public UpdateInfo(Collection<OperationInfo> ops, StatementResultInfo result) {
+ this.ops = ops;
+ this.result = result;
+ }
+
+ /**
+ * Retunrs operations.
+ *
+ * @return operations.
+ */
+ public Collection<OperationInfo> operations() {
+ return ops;
+ }
+
+ /**
+ * Returns result.
+ *
+ * @return result.
+ */
+ public StatementResultInfo result() {
+ return result;
+ }
+}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionInfo.java
new file mode 100644
index 0000000..3f1359c
--- /dev/null
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionInfo.java
@@ -0,0 +1,73 @@
+/*
+ * 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.common.command;
+
+/**
+ * Defines compound multi-key condition for {@link MultiInvokeCommand}.
+ */
+public class CompoundConditionInfo implements ConditionInfo {
+ /** Left condition. */
+ private final ConditionInfo leftCondInfo;
+
+ /** Right condition. */
+ private final ConditionInfo rightCondInfo;
+
+ /** Compound condition type. */
+ private final CompoundConditionType type;
+
+ /**
+ * Constructs new compound condition definition.
+ *
+ * @param leftCondInfo left condition definition.
+ * @param rightCondInfo right condition definition.
+ * @param type type of compound condition definition.
+ */
+ public CompoundConditionInfo(ConditionInfo leftCondInfo,
+ ConditionInfo rightCondInfo, CompoundConditionType type) {
+ this.leftCondInfo = leftCondInfo;
+ this.rightCondInfo = rightCondInfo;
+ this.type = type;
+ }
+
+ /**
+ * Returns left condition definition.
+ *
+ * @return left condition definition.
+ */
+ public ConditionInfo leftConditionInfo() {
+ return leftCondInfo;
+ }
+
+ /**
+ * Returns right condition definition.
+ *
+ * @return right condition definition.
+ */
+ public ConditionInfo rightConditionInfo() {
+ return rightCondInfo;
+ }
+
+ /**
+ * Returns definition for type of compound condition.
+ *
+ * @return definition for type of compound condition.
+ */
+ public CompoundConditionType type() {
+ return type;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionType.java
similarity index 51%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
copy to modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionType.java
index 19a4b28..c2f37d8 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/CompoundConditionType.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -15,26 +15,12 @@
* limitations under the License.
*/
-package org.apache.ignite.internal.metastorage.server;
-
-import org.jetbrains.annotations.NotNull;
+package org.apache.ignite.internal.metastorage.common.command;
/**
- * Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
+ * Type of compound condition.
*/
-public class TombstoneCondition extends AbstractCondition {
- /**
- * Constructs a condition with the given entry key.
- *
- * @param key Key identifies an entry which the condition will applied to.
- */
- public TombstoneCondition(@NotNull byte[] key) {
- super(key);
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean test(@NotNull Entry e) {
- return e.tombstone();
- }
+public enum CompoundConditionType {
+ AND,
+ OR
}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java
index 391cbd6..51fbfa3 100644
--- a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -18,72 +18,12 @@
package org.apache.ignite.internal.metastorage.common.command;
import java.io.Serializable;
-import org.apache.ignite.internal.metastorage.common.ConditionType;
/**
- * Defines condition for InvokeCommand.
+ * Interface for condition definition.
+ *
+ * @see SimpleConditionInfo
+ * @see CompoundConditionInfo
*/
-public class ConditionInfo implements Serializable {
- /** Key. */
- private final byte[] key;
-
- /** Type. */
- private final ConditionType type;
-
- /** Value. */
- private final byte[] val;
-
- /** Revision. */
- private final long rev;
-
- /**
- * Construct condition with given parameters.
- *
- * @param key Key.
- * @param type Condition type.
- * @param val Value.
- * @param rev Revision.
- */
- public ConditionInfo(byte[] key, ConditionType type, byte[] val, long rev) {
- this.key = key;
- this.type = type;
- this.val = val;
- this.rev = rev;
- }
-
- /**
- * Returns key.
- *
- * @return Key.
- */
- public byte[] key() {
- return key;
- }
-
- /**
- * Returns condition type.
- *
- * @return Condition type.
- */
- public ConditionType type() {
- return type;
- }
-
- /**
- * Returns value.
- *
- * @return Value.
- */
- public byte[] value() {
- return val;
- }
-
- /**
- * Returns revision.
- *
- * @return Revision.
- */
- public long revision() {
- return rev;
- }
+public interface ConditionInfo extends Serializable {
}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/IfInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/IfInfo.java
new file mode 100644
index 0000000..5055706
--- /dev/null
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/IfInfo.java
@@ -0,0 +1,76 @@
+/*
+ * 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.common.command;
+
+import java.io.Serializable;
+import org.apache.ignite.internal.metastorage.common.StatementInfo;
+
+/**
+ * Defines if-statement for {@link MultiInvokeCommand}.
+ */
+public class IfInfo implements Serializable {
+ /** Condition definition. */
+ private final ConditionInfo cond;
+
+ /** Definition of execution branch, if condition evaluates to true (aka left branch). */
+ private final StatementInfo andThen;
+
+ /** Definition execution branch, if condition evaluates to false (aka right branch). */
+ private final StatementInfo orElse;
+
+ /**
+ * Constructs new if statement definition.
+ *
+ * @param cond condition.
+ * @param andThen left execution branch.
+ * @param orElse right execution branch.
+ */
+ public IfInfo(ConditionInfo cond, StatementInfo andThen,
+ StatementInfo orElse) {
+ this.cond = cond;
+ this.andThen = andThen;
+ this.orElse = orElse;
+ }
+
+ /**
+ * Returns boolean condition definition.
+ *
+ * @return Boolean condition definition.
+ */
+ public ConditionInfo cond() {
+ return cond;
+ }
+
+ /**
+ * Returns definition of execution branch, if condition evaluates to true (aka left branch).
+ *
+ * @return Left execution branch definition.
+ */
+ public StatementInfo andThen() {
+ return andThen;
+ }
+
+ /**
+ * Returns definition of execution branch, if condition evaluates to false (aka right branch).
+ *
+ * @return Right execution branch definition.
+ */
+ public StatementInfo orElse() {
+ return orElse;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/MultiInvokeCommand.java
similarity index 53%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
copy to modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/MultiInvokeCommand.java
index 19a4b28..6137d27 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/MultiInvokeCommand.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -15,26 +15,32 @@
* limitations under the License.
*/
-package org.apache.ignite.internal.metastorage.server;
+package org.apache.ignite.internal.metastorage.common.command;
-import org.jetbrains.annotations.NotNull;
+import org.apache.ignite.raft.client.Command;
/**
- * Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
+ * Represents invoke command with nested conditions and execution branches.
*/
-public class TombstoneCondition extends AbstractCondition {
+public class MultiInvokeCommand implements Command {
+ /** If statement to invoke. */
+ private final IfInfo iif;
+
/**
- * Constructs a condition with the given entry key.
+ * Constructs new multi-invoke command.
*
- * @param key Key identifies an entry which the condition will applied to.
+ * @param iif if statement.
*/
- public TombstoneCondition(@NotNull byte[] key) {
- super(key);
+ public MultiInvokeCommand(IfInfo iif) {
+ this.iif = iif;
}
- /** {@inheritDoc} */
- @Override
- public boolean test(@NotNull Entry e) {
- return e.tombstone();
+ /**
+ * Returns if statement.
+ *
+ * @return if statement.
+ */
+ public IfInfo iif() {
+ return iif;
}
}
diff --git a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/SimpleConditionInfo.java
similarity index 89%
copy from modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java
copy to modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/SimpleConditionInfo.java
index 391cbd6..5ef0b07 100644
--- a/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/ConditionInfo.java
+++ b/modules/metastorage-common/src/main/java/org/apache/ignite/internal/metastorage/common/command/SimpleConditionInfo.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -17,13 +17,12 @@
package org.apache.ignite.internal.metastorage.common.command;
-import java.io.Serializable;
import org.apache.ignite.internal.metastorage.common.ConditionType;
/**
* Defines condition for InvokeCommand.
*/
-public class ConditionInfo implements Serializable {
+public class SimpleConditionInfo implements ConditionInfo {
/** Key. */
private final byte[] key;
@@ -44,7 +43,7 @@ public class ConditionInfo implements Serializable {
* @param val Value.
* @param rev Revision.
*/
- public ConditionInfo(byte[] key, ConditionType type, byte[] val, long rev) {
+ public SimpleConditionInfo(byte[] key, ConditionType type, byte[] val, long rev) {
this.key = key;
this.type = type;
this.val = val;
diff --git a/modules/metastorage-server/pom.xml b/modules/metastorage-server/pom.xml
index 7cc114b..6970df3 100644
--- a/modules/metastorage-server/pom.xml
+++ b/modules/metastorage-server/pom.xml
@@ -61,6 +61,12 @@
</dependency>
<dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCompoundCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCompoundCondition.java
new file mode 100644
index 0000000..c5d83ac
--- /dev/null
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCompoundCondition.java
@@ -0,0 +1,97 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import org.apache.ignite.internal.util.ArrayUtils;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Compound condition, which {@link #combine(boolean, boolean)} results of left and right conditions.
+ * Also, aggregate their keys in the left-right order.
+ */
+public abstract class AbstractCompoundCondition implements Condition {
+ /** Left condition. */
+ private final Condition leftCondition;
+
+ /** Right condition. */
+ private final Condition rightCondition;
+
+ /** Aggregated array of keys from left and right conditions. */
+ private final byte[][] keys;
+
+ /**
+ * Constructs new compound condition with aggregated keys.
+ *
+ * @param leftCondition left condition.
+ * @param rightCondition right condition.
+ */
+ public AbstractCompoundCondition(Condition leftCondition, Condition rightCondition) {
+ this.leftCondition = leftCondition;
+ this.rightCondition = rightCondition;
+ keys = ArrayUtils.concat(leftCondition.keys(), rightCondition.keys());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public @NotNull byte[][] keys() {
+ return keys;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean test(Entry... entries) {
+ return combine(
+ leftCondition.test(Arrays.copyOf(entries, leftCondition.keys().length)),
+ rightCondition.test(
+ Arrays.copyOfRange(entries,
+ leftCondition.keys().length,
+ leftCondition.keys().length + rightCondition.keys().length)));
+ }
+
+ /**
+ * Combine boolean condition from the results of left and right conditions.
+ *
+ * @param left condition.
+ * @param right condition.
+ * @return result of compound condition.
+ */
+ protected abstract boolean combine(boolean left, boolean right);
+
+ /**
+ * Returns left condition.
+ *
+ * @return left condition.
+ */
+ public Condition leftCondition() {
+ return leftCondition;
+ }
+
+ /**
+ * Returns right condition.
+ *
+ * @return right condition.
+ */
+ public Condition rightCondition() {
+ return rightCondition;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractSimpleCondition.java
similarity index 59%
rename from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCondition.java
rename to modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractSimpleCondition.java
index 732f6f9..79b25aa 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractSimpleCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -22,24 +22,43 @@ import org.jetbrains.annotations.NotNull;
/**
* An abstract condition which could be applied to an entry identified by the key.
*/
-public abstract class AbstractCondition implements Condition {
+public abstract class AbstractSimpleCondition implements Condition {
/** Entry key. */
@NotNull
- private final byte[] key;
+ private final byte[][] keys;
/**
* Constructs a condition with the given entry key.
*
* @param key Key identifies an entry which the condition will applied to.
*/
- public AbstractCondition(@NotNull byte[] key) {
- this.key = key;
+ public AbstractSimpleCondition(@NotNull byte[] key) {
+ keys = new byte[][]{ key };
}
/** {@inheritDoc} */
@NotNull
- @Override
public byte[] key() {
- return key;
+ return keys[0];
}
+
+ /** {@inheritDoc} */
+ @Override
+ public @NotNull byte[][] keys() {
+ return keys;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean test(@NotNull Entry... entries) {
+ return test(entries[0]);
+ }
+
+ /**
+ * Tests the given entry on condition.
+ *
+ * @param entry Entry which will be tested on the condition. Can't be {@code null}.
+ * @return {@code True} if the given entry satisfies to the condition, otherwise - {@code false}.
+ */
+ protected abstract boolean test(@NotNull Entry entry);
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AndCondition.java
similarity index 58%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
copy to modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AndCondition.java
index 19a4b28..9518ffa 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/AndCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -17,24 +17,25 @@
package org.apache.ignite.internal.metastorage.server;
-import org.jetbrains.annotations.NotNull;
-
/**
- * Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
+ * Compound condition, which implements logical AND of left and right subcondition.
*/
-public class TombstoneCondition extends AbstractCondition {
+public class AndCondition extends AbstractCompoundCondition {
/**
- * Constructs a condition with the given entry key.
+ * Constructs new and condition.
*
- * @param key Key identifies an entry which the condition will applied to.
+ * @param leftCondition left condition.
+ * @param rightCondition right condition.
*/
- public TombstoneCondition(@NotNull byte[] key) {
- super(key);
+ public AndCondition(Condition leftCondition, Condition rightCondition) {
+ super(leftCondition, rightCondition);
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
- public boolean test(@NotNull Entry e) {
- return e.tombstone();
+ protected boolean combine(boolean left, boolean right) {
+ return left && right;
}
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java
index 2d01a2a..a2d0dd4 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -21,23 +21,23 @@ import java.util.Collection;
import org.jetbrains.annotations.NotNull;
/**
- * Defines interface for condition which could be applied to an entry.
+ * Defines interface for boolean condition which could be applied to an array of entries.
*
* @see KeyValueStorage#invoke(Condition, Collection, Collection)
*/
public interface Condition {
/**
- * Returns the key which identifies an entry which condition will applied to.
+ * Returns the keys which identifies an entries which condition will be applied to.
*
- * @return The key which identifies an entry which condition will applied to.
+ * @return The keys which identifies an entries which condition will be applied to.
*/
- @NotNull byte[] key();
+ @NotNull byte[][] keys();
/**
- * Tests the given entry on condition.
+ * Tests the given entries on condition.
*
- * @param e Entry which will be tested on the condition. Can't be {@code null}.
- * @return {@code True} if the given entry satisfies to the condition, otherwise - {@code false}.
+ * @param entries Array of entries which will be tested on the condition. Can't be {@code null}.
+ * @return {@code True} if the given entries satisfies to the condition, otherwise - {@code false}.
*/
- boolean test(@NotNull Entry e);
+ boolean test(@NotNull Entry... entries);
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ExistenceCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ExistenceCondition.java
index 89a8215..45b4093 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ExistenceCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ExistenceCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Condition tests an entry on existence in meta storage. Entry exists if it is not empty and not tombstone.
*/
-public class ExistenceCondition extends AbstractCondition {
+public class ExistenceCondition extends AbstractSimpleCondition {
/** Condition type. */
@NotNull
private final Type type;
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/If.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/If.java
new file mode 100644
index 0000000..5477d59
--- /dev/null
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/If.java
@@ -0,0 +1,74 @@
+/*
+ * 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;
+
+/**
+ * Root building block for the compound meta storage invoke command.
+ * Contains of boolean condition and 2 branches of execution, like usual programming language's if.
+ * Every branch can be either new {@link If} statement (non-terminal) or result terminal statement {@link Update}.
+ */
+public class If {
+ /** Boolean condition. */
+ private final Condition cond;
+
+ /** Execution branch, if condition evaluates to true (aka left branch). */
+ private final Statement andThen;
+
+ /** Execution branch, if condition evaluates to false (aka right branch). */
+ private final Statement orElse;
+
+ /**
+ * Construct new {@link If} statement.
+ *
+ * @param cond Boolean condition.
+ * @param andThen Left execution branch.
+ * @param orElse Right execution branch.
+ */
+ public If(Condition cond, Statement andThen, Statement orElse) {
+ this.cond = cond;
+ this.andThen = andThen;
+ this.orElse = orElse;
+ }
+
+ /**
+ * Returns boolean condition.
+ *
+ * @return Boolean condition.
+ */
+ public Condition cond() {
+ return cond;
+ }
+
+ /**
+ * Execution branch, if condition evaluates to true (aka left branch).
+ *
+ * @return Left execution branch.
+ */
+ public Statement andThen() {
+ return andThen;
+ }
+
+ /**
+ * Execution branch, if condition evaluates to false (aka right branch).
+ *
+ * @return Right execution branch.
+ */
+ public Statement orElse() {
+ return orElse;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/KeyValueStorage.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/KeyValueStorage.java
index d707655..0e13422 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/KeyValueStorage.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/KeyValueStorage.java
@@ -157,6 +157,17 @@ public interface KeyValueStorage extends AutoCloseable {
boolean invoke(Condition condition, Collection<Operation> success, Collection<Operation> failure);
/**
+ * Invoke, which supports nested conditional statements with left and right branches of execution.
+ *
+ * @param iif {@link If} statement to invoke
+ * @return execution result
+ *
+ * @see If
+ * @see StatementResult
+ */
+ @NotNull StatementResult invoke(@NotNull If iif);
+
+ /**
* Returns cursor by entries which correspond to the given keys range.
*
* @param keyFrom Start key of range (inclusive).
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/OrCondition.java
similarity index 58%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
copy to modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/OrCondition.java
index 19a4b28..81e4eb2 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/OrCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -17,24 +17,25 @@
package org.apache.ignite.internal.metastorage.server;
-import org.jetbrains.annotations.NotNull;
-
/**
- * Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
+ * Compound condition, which implements logical OR of left and right subcondition.
*/
-public class TombstoneCondition extends AbstractCondition {
+public class OrCondition extends AbstractCompoundCondition {
/**
- * Constructs a condition with the given entry key.
+ * Constructs new or condition.
*
- * @param key Key identifies an entry which the condition will applied to.
+ * @param leftCondition left condition.
+ * @param rightCondition right condition.
*/
- public TombstoneCondition(@NotNull byte[] key) {
- super(key);
+ public OrCondition(Condition leftCondition, Condition rightCondition) {
+ super(leftCondition, rightCondition);
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
- public boolean test(@NotNull Entry e) {
- return e.tombstone();
+ protected boolean combine(boolean left, boolean right) {
+ return left || right;
}
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/RevisionCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/RevisionCondition.java
index 9c8c13e..5914430 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/RevisionCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/RevisionCondition.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Defines condition on entry revision.
*/
-public class RevisionCondition extends AbstractCondition {
+public class RevisionCondition extends AbstractSimpleCondition {
/** Condition type. */
@NotNull
private final Type type;
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Statement.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Statement.java
new file mode 100644
index 0000000..86b85f6
--- /dev/null
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Statement.java
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+/**
+ * Simple Either-like wrapper to hold one of the statement type: {@link If} or {@link Update}.
+ * Needed to construct and simple deconstruction of nested {@link If},
+ * instead of empty interface and instanceof-based unwrap.
+ *
+ * @see If
+ * @see Update
+ */
+public class Statement {
+ /** If value holder. */
+ private final If iif;
+
+ /** Update value holder. */
+ private final Update update;
+
+ /**
+ * Constructs new {@link If} statement.
+ *
+ * @param iif If
+ */
+ public Statement(If iif) {
+ this.iif = iif;
+ this.update = null;
+ }
+
+ /**
+ * Constructs new {@link Update} terminal statement.
+ *
+ * @param update Update
+ */
+ public Statement(Update update) {
+ this.update = update;
+ this.iif = null;
+ }
+
+ /**
+ * Returns true, if statement has no nested statement (i.e. it is {@link Update} statement).
+ *
+ * @return true, if statement has no nested statement (i.e. it is {@link Update} statement).
+ */
+ public boolean isTerminal() {
+ return update != null;
+ }
+
+ /**
+ * Returns If or {@code null}, if no If value.
+ * Note: check which field is filled by {@link #isTerminal()}
+ *
+ * @return If or {@code null}, if no If value.
+ */
+ public If iif() {
+ return iif;
+ }
+
+
+ /**
+ * Returns Update or {@code null}, if no update value.
+ * Note: check which field is filled by {@link #isTerminal()}
+ *
+ * @return Update or {@code null}, if no update value.
+ */
+ public Update update() {
+ return update;
+ }
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/StatementResult.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/StatementResult.java
new file mode 100644
index 0000000..1666be5
--- /dev/null
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/StatementResult.java
@@ -0,0 +1,66 @@
+/*
+ * 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;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Simple result of statement execution, backed by byte[] array.
+ * Provides some shortcut methods to represent the values of some primitive types.
+ */
+public class StatementResult {
+ /** Result data. */
+ private final byte[] res;
+
+ /**
+ * Constructs result from the byte array.
+ *
+ * @param res byte array.
+ */
+ public StatementResult(byte[] res) {
+ this.res = res;
+ }
+
+ /**
+ * Constructs result from the boolean value.
+ *
+ * @param res boolean.
+ */
+ public StatementResult(boolean res) {
+ this.res = new byte[] {(byte) (res ? 1 : 0)};
+ }
+
+ /**
+ * Constructs result from the int value.
+ *
+ * @param res int.
+ */
+ public StatementResult(int res) {
+ this.res = ByteBuffer.allocate(4).putInt(res).array();
+ }
+
+ /**
+ * Returns backed byte array.
+ *
+ * @return backed byte array.
+ */
+ public byte[] bytes() {
+ return res;
+ }
+
+}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
index 19a4b28..ca39ea6 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/TombstoneCondition.java
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Condition tests an entry's value is tombstone in meta storage. Entry is tombstone if it is not empty and doesn't exists.
*/
-public class TombstoneCondition extends AbstractCondition {
+public class TombstoneCondition extends AbstractSimpleCondition {
/**
* Constructs a condition with the given entry key.
*
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Update.java
similarity index 51%
copy from modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java
copy to modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Update.java
index 2d01a2a..4287629 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Condition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Update.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -18,26 +18,43 @@
package org.apache.ignite.internal.metastorage.server;
import java.util.Collection;
-import org.jetbrains.annotations.NotNull;
/**
- * Defines interface for condition which could be applied to an entry.
- *
- * @see KeyValueStorage#invoke(Condition, Collection, Collection)
+ * Simple operations + result wrapper to describe the terminal branch of {@link If} execution.
*/
-public interface Condition {
+public class Update {
+ /** Operations. */
+ private final Collection<Operation> ops;
+
+ /** Result. */
+ private final StatementResult result;
+
+ /**
+ * Constructs new update object.
+ *
+ * @param ops operations
+ * @param result result
+ */
+ public Update(Collection<Operation> ops, StatementResult result) {
+ this.ops = ops;
+ this.result = result;
+ }
+
/**
- * Returns the key which identifies an entry which condition will applied to.
+ * Returns operations.
*
- * @return The key which identifies an entry which condition will applied to.
+ * @return operations.
*/
- @NotNull byte[] key();
+ public Collection<Operation> operations() {
+ return ops;
+ }
/**
- * Tests the given entry on condition.
+ * Returns result.
*
- * @param e Entry which will be tested on the condition. Can't be {@code null}.
- * @return {@code True} if the given entry satisfies to the condition, otherwise - {@code false}.
+ * @return result.
*/
- boolean test(@NotNull Entry e);
+ public StatementResult result() {
+ return result;
+ }
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Value.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Value.java
index 9441ab3..7dad0d6 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Value.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/Value.java
@@ -36,7 +36,7 @@ public class Value {
* Constructs value.
*
* @param bytes Value bytes or {@code #TOMBSTONE}.
- * @param updCntr Update cpunter.
+ * @param updCntr Update counter.
*/
public Value(@NotNull byte[] bytes, long updCntr) {
this.bytes = bytes;
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ValueCondition.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ValueCondition.java
index 2dcc599..605d5be 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ValueCondition.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/ValueCondition.java
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Defines condition on entry value.
*/
-public class ValueCondition extends AbstractCondition {
+public class ValueCondition extends AbstractSimpleCondition {
/** Condition type. */
@NotNull
private final Type type;
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java
index 945bbae..ff172da 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java
@@ -1,6 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
@@ -53,8 +53,12 @@ import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ignite.internal.metastorage.server.Condition;
import org.apache.ignite.internal.metastorage.server.Entry;
+import org.apache.ignite.internal.metastorage.server.If;
import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
import org.apache.ignite.internal.metastorage.server.Operation;
+import org.apache.ignite.internal.metastorage.server.Statement;
+import org.apache.ignite.internal.metastorage.server.StatementResult;
+import org.apache.ignite.internal.metastorage.server.Update;
import org.apache.ignite.internal.metastorage.server.Value;
import org.apache.ignite.internal.metastorage.server.WatchEvent;
import org.apache.ignite.internal.rocksdb.ColumnFamily;
@@ -606,21 +610,69 @@ public class RocksDbKeyValueStorage implements KeyValueStorage {
public boolean invoke(Condition condition, Collection<Operation> success, Collection<Operation> failure) {
rwLock.writeLock().lock();
- try (WriteBatch batch = new WriteBatch()) {
- Entry e = get(condition.key());
+ try {
+ Entry[] entries = getAll(Arrays.asList(condition.keys())).toArray(new Entry[]{});
- boolean branch = condition.test(e);
+ boolean branch = condition.test(entries);
Collection<Operation> ops = branch ? success : failure;
- long curRev = rev + 1;
+ applyOperations(ops);
- boolean modified = false;
+ return branch;
+ } catch (RocksDBException e) {
+ throw new IgniteInternalException(e);
+ } finally {
+ rwLock.writeLock().unlock();
+ }
+ }
- long counter = updCntr;
+ @Override
+ public StatementResult invoke(If iif) {
+ rwLock.writeLock().lock();
+
+ try {
+ If currIf = iif;
+
+ byte maximumNumOfNestedBranch = 100;
+
+ while (true) {
+ if (maximumNumOfNestedBranch-- <= 0) {
+ throw new IgniteInternalException(
+ "Too many nested (" + maximumNumOfNestedBranch + ") statements in multi-invoke command.");
+ }
+
+ Entry[] entries = getAll(Arrays.asList(currIf.cond().keys())).toArray(new Entry[]{});
+
+ Statement branch = (currIf.cond().test(entries)) ? currIf.andThen() : currIf.orElse();
+
+ if (branch.isTerminal()) {
+ Update update = branch.update();
- List<byte[]> updatedKeys = new ArrayList<>();
+ applyOperations(update.operations());
+ return update.result();
+ } else {
+ currIf = branch.iif();
+ }
+ }
+ } catch (RocksDBException e) {
+ throw new IgniteInternalException(e);
+ } finally {
+ rwLock.writeLock().unlock();
+ }
+ }
+
+ private void applyOperations(Collection<Operation> ops) throws RocksDBException {
+ long curRev = rev + 1;
+
+ boolean modified = false;
+
+ long counter = updCntr;
+
+ List<byte[]> updatedKeys = new ArrayList<>();
+
+ try (WriteBatch batch = new WriteBatch()) {
for (Operation op : ops) {
byte[] key = op.key();
@@ -666,12 +718,6 @@ public class RocksDbKeyValueStorage implements KeyValueStorage {
fillAndWriteBatch(batch, curRev, counter);
}
-
- return branch;
- } catch (RocksDBException e) {
- throw new IgniteInternalException(e);
- } finally {
- rwLock.writeLock().unlock();
}
}
diff --git a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListener.java b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListener.java
index 003a2e3..e335480 100644
--- a/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListener.java
+++ b/modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListener.java
@@ -28,6 +28,11 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.apache.ignite.internal.metastorage.common.ConditionType;
+import org.apache.ignite.internal.metastorage.common.StatementInfo;
+import org.apache.ignite.internal.metastorage.common.StatementResultInfo;
+import org.apache.ignite.internal.metastorage.common.UpdateInfo;
+import org.apache.ignite.internal.metastorage.common.command.CompoundConditionInfo;
+import org.apache.ignite.internal.metastorage.common.command.CompoundConditionType;
import org.apache.ignite.internal.metastorage.common.command.ConditionInfo;
import org.apache.ignite.internal.metastorage.common.command.GetAllCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand;
@@ -35,7 +40,9 @@ import org.apache.ignite.internal.metastorage.common.command.GetAndPutCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndRemoveAllCommand;
import org.apache.ignite.internal.metastorage.common.command.GetAndRemoveCommand;
import org.apache.ignite.internal.metastorage.common.command.GetCommand;
+import org.apache.ignite.internal.metastorage.common.command.IfInfo;
import org.apache.ignite.internal.metastorage.common.command.InvokeCommand;
+import org.apache.ignite.internal.metastorage.common.command.MultiInvokeCommand;
import org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse;
import org.apache.ignite.internal.metastorage.common.command.OperationInfo;
import org.apache.ignite.internal.metastorage.common.command.PutAllCommand;
@@ -43,6 +50,7 @@ import org.apache.ignite.internal.metastorage.common.command.PutCommand;
import org.apache.ignite.internal.metastorage.common.command.RangeCommand;
import org.apache.ignite.internal.metastorage.common.command.RemoveAllCommand;
import org.apache.ignite.internal.metastorage.common.command.RemoveCommand;
+import org.apache.ignite.internal.metastorage.common.command.SimpleConditionInfo;
import org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse;
import org.apache.ignite.internal.metastorage.common.command.WatchExactKeysCommand;
import org.apache.ignite.internal.metastorage.common.command.WatchRangeKeysCommand;
@@ -50,14 +58,20 @@ import org.apache.ignite.internal.metastorage.common.command.cursor.CursorCloseC
import org.apache.ignite.internal.metastorage.common.command.cursor.CursorHasNextCommand;
import org.apache.ignite.internal.metastorage.common.command.cursor.CursorNextCommand;
import org.apache.ignite.internal.metastorage.common.command.cursor.CursorsCloseCommand;
+import org.apache.ignite.internal.metastorage.server.AndCondition;
import org.apache.ignite.internal.metastorage.server.Condition;
import org.apache.ignite.internal.metastorage.server.Entry;
import org.apache.ignite.internal.metastorage.server.EntryEvent;
import org.apache.ignite.internal.metastorage.server.ExistenceCondition;
+import org.apache.ignite.internal.metastorage.server.If;
import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
import org.apache.ignite.internal.metastorage.server.Operation;
+import org.apache.ignite.internal.metastorage.server.OrCondition;
import org.apache.ignite.internal.metastorage.server.RevisionCondition;
+import org.apache.ignite.internal.metastorage.server.Statement;
+import org.apache.ignite.internal.metastorage.server.StatementResult;
import org.apache.ignite.internal.metastorage.server.TombstoneCondition;
+import org.apache.ignite.internal.metastorage.server.Update;
import org.apache.ignite.internal.metastorage.server.ValueCondition;
import org.apache.ignite.internal.metastorage.server.WatchEvent;
import org.apache.ignite.internal.util.Cursor;
@@ -224,6 +238,14 @@ public class MetaStorageListener implements RaftGroupListener {
);
clo.result(res);
+ } else if (clo.command() instanceof MultiInvokeCommand) {
+ MultiInvokeCommand cmd = (MultiInvokeCommand) clo.command();
+
+ StatementResult res = storage.invoke(
+ toIf(cmd.iif())
+ );
+
+ clo.result(new StatementResultInfo(res.bytes()));
} else if (command instanceof RangeCommand) {
RangeCommand rangeCmd = (RangeCommand) command;
@@ -397,35 +419,67 @@ public class MetaStorageListener implements RaftGroupListener {
return storage;
}
+ private static If toIf(IfInfo iif) {
+ return new If(toCondition(iif.cond()), toConditionBranch(iif.andThen()), toConditionBranch(iif.orElse()));
+ }
+
+ private static Update toUpdate(UpdateInfo updateInfo) {
+ return new Update(toOperations(new ArrayList<>(updateInfo.operations())), new StatementResult(updateInfo.result().result()));
+ }
+
+ private static Statement toConditionBranch(StatementInfo statementInfo) {
+ if (statementInfo.isTerminal()) {
+ return new Statement(toUpdate(statementInfo.update()));
+ } else {
+ return new Statement(toIf(statementInfo.iif()));
+ }
+ }
+
private static Condition toCondition(ConditionInfo info) {
- byte[] key = info.key();
-
- ConditionType type = info.type();
-
- if (type == ConditionType.KEY_EXISTS) {
- return new ExistenceCondition(ExistenceCondition.Type.EXISTS, key);
- } else if (type == ConditionType.KEY_NOT_EXISTS) {
- return new ExistenceCondition(ExistenceCondition.Type.NOT_EXISTS, key);
- } else if (type == ConditionType.TOMBSTONE) {
- return new TombstoneCondition(key);
- } else if (type == ConditionType.VAL_EQUAL) {
- return new ValueCondition(ValueCondition.Type.EQUAL, key, info.value());
- } else if (type == ConditionType.VAL_NOT_EQUAL) {
- return new ValueCondition(ValueCondition.Type.NOT_EQUAL, key, info.value());
- } else if (type == ConditionType.REV_EQUAL) {
- return new RevisionCondition(RevisionCondition.Type.EQUAL, key, info.revision());
- } else if (type == ConditionType.REV_NOT_EQUAL) {
- return new RevisionCondition(RevisionCondition.Type.NOT_EQUAL, key, info.revision());
- } else if (type == ConditionType.REV_GREATER) {
- return new RevisionCondition(RevisionCondition.Type.GREATER, key, info.revision());
- } else if (type == ConditionType.REV_GREATER_OR_EQUAL) {
- return new RevisionCondition(RevisionCondition.Type.GREATER_OR_EQUAL, key, info.revision());
- } else if (type == ConditionType.REV_LESS) {
- return new RevisionCondition(RevisionCondition.Type.LESS, key, info.revision());
- } else if (type == ConditionType.REV_LESS_OR_EQUAL) {
- return new RevisionCondition(RevisionCondition.Type.LESS_OR_EQUAL, key, info.revision());
+ if (info instanceof SimpleConditionInfo) {
+ SimpleConditionInfo inf = (SimpleConditionInfo) info;
+ byte[] key = inf.key();
+
+ ConditionType type = inf.type();
+
+ if (type == ConditionType.KEY_EXISTS) {
+ return new ExistenceCondition(ExistenceCondition.Type.EXISTS, key);
+ } else if (type == ConditionType.KEY_NOT_EXISTS) {
+ return new ExistenceCondition(ExistenceCondition.Type.NOT_EXISTS, key);
+ } else if (type == ConditionType.TOMBSTONE) {
+ return new TombstoneCondition(key);
+ } else if (type == ConditionType.VAL_EQUAL) {
+ return new ValueCondition(ValueCondition.Type.EQUAL, key, inf.value());
+ } else if (type == ConditionType.VAL_NOT_EQUAL) {
+ return new ValueCondition(ValueCondition.Type.NOT_EQUAL, key, inf.value());
+ } else if (type == ConditionType.REV_EQUAL) {
+ return new RevisionCondition(RevisionCondition.Type.EQUAL, key, inf.revision());
+ } else if (type == ConditionType.REV_NOT_EQUAL) {
+ return new RevisionCondition(RevisionCondition.Type.NOT_EQUAL, key, inf.revision());
+ } else if (type == ConditionType.REV_GREATER) {
+ return new RevisionCondition(RevisionCondition.Type.GREATER, key, inf.revision());
+ } else if (type == ConditionType.REV_GREATER_OR_EQUAL) {
+ return new RevisionCondition(RevisionCondition.Type.GREATER_OR_EQUAL, key, inf.revision());
+ } else if (type == ConditionType.REV_LESS) {
+ return new RevisionCondition(RevisionCondition.Type.LESS, key, inf.revision());
+ } else if (type == ConditionType.REV_LESS_OR_EQUAL) {
+ return new RevisionCondition(RevisionCondition.Type.LESS_OR_EQUAL, key, inf.revision());
+ } else {
+ throw new IllegalArgumentException("Unknown condition type: " + type);
+ }
+ } else if (info instanceof CompoundConditionInfo) {
+ CompoundConditionInfo inf = (CompoundConditionInfo) info;
+
+ if (inf.type() == CompoundConditionType.AND) {
+ return new AndCondition(toCondition(inf.leftConditionInfo()), toCondition(inf.rightConditionInfo()));
+
+ } else if (inf.type() == CompoundConditionType.OR) {
+ return new OrCondition(toCondition(inf.leftConditionInfo()), toCondition(inf.rightConditionInfo()));
+ } else {
+ throw new IllegalArgumentException("Unknown compound condition " + inf.type());
+ }
} else {
- throw new IllegalArgumentException("Unknown condition type: " + type);
+ throw new IllegalArgumentException("Unknown condition info type " + info);
}
}
diff --git a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AbstractKeyValueStorageTest.java b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AbstractKeyValueStorageTest.java
index f2f77ee..7fd185e 100644
--- a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AbstractKeyValueStorageTest.java
+++ b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AbstractKeyValueStorageTest.java
@@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -34,6 +35,7 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import org.apache.ignite.internal.metastorage.common.OperationType;
+import org.apache.ignite.internal.metastorage.server.ValueCondition.Type;
import org.apache.ignite.internal.util.Cursor;
import org.apache.ignite.lang.ByteArray;
import org.junit.jupiter.api.AfterEach;
@@ -1618,69 +1620,233 @@ public abstract class AbstractKeyValueStorageTest {
assertArrayEquals(key3, e3.key());
}
+ /**
+ * <pre>
+ * if (key1.value == val1 || exist(key2))
+ * if (key3.revision == 3):
+ * put(key1, rval1) <------ TEST FOR THIS BRANCH
+ * return 1
+ * else
+ * put(key1, rval1)
+ * remove(key2)
+ * return 2
+ * else
+ * put(key3, rval3)
+ * return 3
+ * </pre>
+ */
@Test
- public void compact() {
+ public void multiInvokeOperationsBranch1() {
+ byte[] key1 = key(1);
+ byte[] val1 = keyValue(1, 1);
+ byte[] rval1 = keyValue(1, 4);
+
+ final byte[] key2 = key(2);
+ final byte[] val2 = keyValue(2, 2);
+
+ final byte[] key3 = key(3);
+ final byte[] val3 = keyValue(3, 3);
+ final byte[] rval3 = keyValue(2, 6);
+
assertEquals(0, storage.revision());
assertEquals(0, storage.updateCounter());
- // Compact empty.
- storage.compact();
+ storage.put(key1, val1);
+
+ assertEquals(1, storage.revision());
+ assertEquals(1, storage.updateCounter());
+
+ storage.put(key2, val2);
+
+ assertEquals(2, storage.revision());
+ assertEquals(2, storage.updateCounter());
+
+ storage.put(key3, val3);
+
+ assertEquals(3, storage.revision());
+ assertEquals(3, storage.updateCounter());
+
+ If iif = new If(
+ new OrCondition(new ValueCondition(Type.EQUAL, key1, val1), new ExistenceCondition(ExistenceCondition.Type.EXISTS, key2)),
+ new Statement(
+ new If(
+ new RevisionCondition(RevisionCondition.Type.EQUAL, key3, 3),
+ new Statement(
+ new Update(List.of(new Operation(OperationType.PUT, key1, rval1)), new StatementResult(1))),
+ new Statement(
+ new Update(
+ List.of(new Operation(OperationType.PUT, key1, rval1),
+ new Operation(OperationType.REMOVE, key2, null)),
+ new StatementResult(2))))),
+ new Statement(new Update(List.of(new Operation(OperationType.PUT, key3, rval3)), new StatementResult(3)))
+ );
+
+ StatementResult branch = storage.invoke(iif);
+
+ assertEquals(1, ByteBuffer.wrap(branch.bytes()).getInt());
+
+ assertEquals(4, storage.revision());
+ assertEquals(4, storage.updateCounter());
+
+ Entry e1 = storage.get(key1);
+ assertEquals(4, e1.revision());
+ assertArrayEquals(rval1, e1.value());
+
+ Entry e2 = storage.get(key2);
+ assertEquals(2, e2.revision());
+
+ Entry e3 = storage.get(key3);
+ assertEquals(3, e3.revision());
+ assertArrayEquals(val3, e3.value());
+ }
+
+ /**
+ * <pre>
+ * if (key1.value == val1 || exist(key2))
+ * if (key3.revision == 3):
+ * put(key1, rval1)
+ * return 1
+ * else
+ * put(key1, rval1) <------ TEST FOR THIS BRANCH
+ * remove(key2)
+ * return 2
+ * else
+ * put(key3, rval3)
+ * return 3
+ * </pre>
+ */
+ @Test
+ public void multiInvokeOperationsBranch2() {
+ byte[] key1 = key(1);
+ byte[] val1 = keyValue(1, 1);
+ byte[] rval1 = keyValue(1, 4);
+
+ final byte[] key2 = key(2);
+ final byte[] val2 = keyValue(2, 2);
+
+ final byte[] key3 = key(3);
+ final byte[] val3 = keyValue(3, 3);
+ final byte[] rval3 = keyValue(2, 6);
assertEquals(0, storage.revision());
assertEquals(0, storage.updateCounter());
- // Compact non-empty.
- fill(storage, 1, 1);
+ storage.put(key1, val1);
assertEquals(1, storage.revision());
assertEquals(1, storage.updateCounter());
- fill(storage, 2, 2);
+ storage.put(key2, val2);
+
+ assertEquals(2, storage.revision());
+ assertEquals(2, storage.updateCounter());
+
+ storage.put(key3, val3);
assertEquals(3, storage.revision());
assertEquals(3, storage.updateCounter());
- fill(storage, 3, 3);
+ storage.put(key3, val3);
- assertEquals(6, storage.revision());
+ assertEquals(4, storage.revision());
+ assertEquals(4, storage.updateCounter());
+
+ If iif = new If(
+ new OrCondition(new ValueCondition(Type.EQUAL, key1, val1), new ExistenceCondition(ExistenceCondition.Type.EXISTS, key2)),
+ new Statement(new If(new RevisionCondition(RevisionCondition.Type.EQUAL, key3, 3),
+ new Statement(new Update(List.of(new Operation(OperationType.PUT, key1, rval1)), new StatementResult(1))),
+ new Statement(new Update(
+ List.of(new Operation(OperationType.PUT, key1, rval1), new Operation(OperationType.REMOVE, key2, null)),
+ new StatementResult(2))))),
+ new Statement(new Update(List.of(new Operation(OperationType.PUT, key3, rval3)), new StatementResult(3))));
+
+ StatementResult branch = storage.invoke(iif);
+
+ assertEquals(2, ByteBuffer.wrap(branch.bytes()).getInt());
+
+ assertEquals(5, storage.revision());
assertEquals(6, storage.updateCounter());
- storage.getAndRemove(key(3));
+ Entry e1 = storage.get(key1);
+ assertEquals(5, e1.revision());
+ assertArrayEquals(rval1, e1.value());
- assertEquals(7, storage.revision());
- assertEquals(7, storage.updateCounter());
- assertTrue(storage.get(key(3)).tombstone());
+ Entry e2 = storage.get(key2);
+ assertEquals(5, e2.revision());
+ assertTrue(e2.tombstone());
- storage.compact();
+ Entry e3 = storage.get(key3);
+ assertEquals(4, e3.revision());
+ assertArrayEquals(val3, e3.value());
+ }
- assertEquals(7, storage.revision());
- assertEquals(7, storage.updateCounter());
+ /**
+ * <pre>
+ * if (key1.value == val1 || exist(key2))
+ * if (key3.revision == 3):
+ * put(key1, rval1)
+ * return 1
+ * else
+ * put(key1, rval1)
+ * remove(key2)
+ * return 2
+ * else
+ * put(key3, rval3) <------ TEST FOR THIS BRANCH
+ * return 3
+ * </pre>
+ */
+ @Test
+ public void multiInvokeOperationsBranch3() {
+ byte[] key1 = key(1);
+ byte[] val1 = keyValue(1, 1);
+ byte[] rval1 = keyValue(1, 4);
- Entry e1 = storage.get(key(1));
+ final byte[] key2 = key(2);
+ final byte[] val2 = keyValue(2, 2);
- assertFalse(e1.empty());
- assertFalse(e1.tombstone());
- assertArrayEquals(key(1), e1.key());
- assertArrayEquals(keyValue(1, 1), e1.value());
- assertEquals(1, e1.revision());
- assertEquals(1, e1.updateCounter());
+ final byte[] key3 = key(3);
+ final byte[] val3 = keyValue(3, 3);
+ final byte[] rval3 = keyValue(2, 6);
- Entry e2 = storage.get(key(2));
+ assertEquals(0, storage.revision());
+ assertEquals(0, storage.updateCounter());
- assertFalse(e2.empty());
- assertFalse(e2.tombstone());
- assertArrayEquals(key(2), e2.key());
- assertArrayEquals(keyValue(2, 2), e2.value());
- assertTrue(storage.get(key(2), 2).empty());
- assertEquals(3, e2.revision());
- assertEquals(3, e2.updateCounter());
+ storage.put(key1, val2);
- Entry e3 = storage.get(key(3));
+ assertEquals(1, storage.revision());
+ assertEquals(1, storage.updateCounter());
- assertTrue(e3.empty());
- assertTrue(storage.get(key(3), 5).empty());
- assertTrue(storage.get(key(3), 6).empty());
- assertTrue(storage.get(key(3), 7).empty());
+ storage.put(key3, val3);
+
+ assertEquals(2, storage.revision());
+ assertEquals(2, storage.updateCounter());
+
+ If iif = new If(
+ new OrCondition(new ValueCondition(Type.EQUAL, key1, val1), new ExistenceCondition(ExistenceCondition.Type.EXISTS, key2)),
+ new Statement(new If(new RevisionCondition(RevisionCondition.Type.EQUAL, key3, 3),
+ new Statement(new Update(List.of(new Operation(OperationType.PUT, key1, rval1)), new StatementResult(1))),
+ new Statement(new Update(
+ List.of(new Operation(OperationType.PUT, key1, rval1), new Operation(OperationType.REMOVE, key2, null)),
+ new StatementResult(2))))),
+ new Statement(new Update(List.of(new Operation(OperationType.PUT, key3, rval3)), new StatementResult(3))));
+
+ StatementResult branch = storage.invoke(iif);
+
+ assertEquals(3, ByteBuffer.wrap(branch.bytes()).getInt());
+
+ assertEquals(3, storage.revision());
+ assertEquals(3, storage.updateCounter());
+
+ Entry e1 = storage.get(key1);
+ assertEquals(1, e1.revision());
+ assertArrayEquals(val2, e1.value());
+
+ Entry e2 = storage.get(key2);
+ assertTrue(e2.empty());
+
+ Entry e3 = storage.get(key3);
+ assertEquals(3, e3.revision());
+ assertArrayEquals(rval3, e3.value());
}
@Test
diff --git a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AndConditionTest.java b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AndConditionTest.java
new file mode 100644
index 0000000..bb20479
--- /dev/null
+++ b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/AndConditionTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import org.apache.ignite.internal.util.ArrayUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for 'and' compound condition.
+ *
+ * @see AndCondition
+ */
+public class AndConditionTest {
+ private Condition cond1;
+ private Condition cond2;
+ private Condition cond3;
+ private Condition cond4;
+
+ private final Entry[] entries = new Entry[] {
+ new Entry(new byte[]{1}, new byte[]{10}, 1, 1),
+ new Entry(new byte[]{2}, new byte[]{20}, 2, 3),
+ new Entry(new byte[]{3}, new byte[]{30}, 3, 4),
+
+ };
+
+ @BeforeEach
+ public void setUp() {
+ cond1 = cond(new byte[][] {{1}, {2}}, true);
+ cond2 = cond(new byte[][] {{3}}, true);
+ cond3 = cond(new byte[][] {{5}, {6}}, false);
+ cond4 = cond(new byte[][] {{7}}, false);
+ }
+
+ @Test
+ public void testTrueTrue() {
+ var cond = new AndCondition(cond1, cond2);
+
+ assertArrayEquals(ArrayUtils.concat(cond1.keys(), cond2.keys()), cond.keys());
+ assertTrue(cond.test(entries));
+ verify(cond1, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond2, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ @Test
+ public void testTrueFalse() {
+ var cond = new AndCondition(cond2, cond3);
+
+ assertArrayEquals(ArrayUtils.concat(cond2.keys(), cond3.keys()), cond.keys());
+ assertFalse(cond.test(entries));
+ verify(cond2, times(1)).test(Arrays.copyOf(entries, 1));
+ verify(cond3, times(1)).test(Arrays.copyOfRange(entries, 1, 3));
+ }
+
+ @Test
+ public void testFalseTrue() {
+ var cond = new AndCondition(cond3, cond2);
+
+ assertArrayEquals(ArrayUtils.concat(cond3.keys(), cond2.keys()), cond.keys());
+ assertFalse(cond.test(entries));
+ verify(cond3, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond2, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ @Test
+ public void testFalseFalse() {
+ var cond = new AndCondition(cond3, cond4);
+
+ assertArrayEquals(ArrayUtils.concat(cond3.keys(), cond4.keys()), cond.keys());
+ assertFalse(cond.test(entries));
+ verify(cond3, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond4, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ private static Condition cond(byte[][] keys, boolean result) {
+ var m = mock(Condition.class);
+
+ when(m.keys()).thenReturn(keys);
+
+ when(m.test(any())).thenReturn(result);
+
+ return m;
+ }
+}
diff --git a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/OrConditionTest.java b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/OrConditionTest.java
new file mode 100644
index 0000000..b2befdd
--- /dev/null
+++ b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/OrConditionTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import org.apache.ignite.internal.util.ArrayUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for 'or' compound condition.
+ *
+ * @see OrCondition
+ */
+public class OrConditionTest {
+ private Condition cond1;
+ private Condition cond2;
+ private Condition cond3;
+ private Condition cond4;
+
+ private final Entry[] entries = new Entry[] {
+ new Entry(new byte[]{1}, new byte[]{10}, 1, 1),
+ new Entry(new byte[]{2}, new byte[]{20}, 2, 3),
+ new Entry(new byte[]{3}, new byte[]{30}, 3, 4),
+
+ };
+
+ @BeforeEach
+ public void setUp() {
+ cond1 = cond(new byte[][] {{1}, {2}}, true);
+ cond2 = cond(new byte[][] {{3}}, true);
+ cond3 = cond(new byte[][] {{5}, {6}}, false);
+ cond4 = cond(new byte[][] {{7}}, false);
+ }
+
+ @Test
+ public void testTrueTrue() {
+ var cond = new OrCondition(cond1, cond2);
+
+ assertArrayEquals(ArrayUtils.concat(cond1.keys(), cond2.keys()), cond.keys());
+ assertTrue(cond.test(entries));
+ verify(cond1, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond2, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ @Test
+ public void testTrueFalse() {
+ var cond = new OrCondition(cond2, cond3);
+
+ assertArrayEquals(ArrayUtils.concat(cond2.keys(), cond3.keys()), cond.keys());
+ assertTrue(cond.test(entries));
+ verify(cond2, times(1)).test(Arrays.copyOf(entries, 1));
+ verify(cond3, times(1)).test(Arrays.copyOfRange(entries, 1, 3));
+ }
+
+ @Test
+ public void testFalseTrue() {
+ var cond = new OrCondition(cond3, cond2);
+
+ assertArrayEquals(ArrayUtils.concat(cond3.keys(), cond2.keys()), cond.keys());
+ assertTrue(cond.test(entries));
+ verify(cond3, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond2, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ @Test
+ public void testFalseFalse() {
+ var cond = new AndCondition(cond3, cond4);
+
+ assertArrayEquals(ArrayUtils.concat(cond3.keys(), cond4.keys()), cond.keys());
+ assertFalse(cond.test(entries));
+ verify(cond3, times(1)).test(Arrays.copyOf(entries, 2));
+ verify(cond4, times(1)).test(Arrays.copyOfRange(entries, 2, 3));
+ }
+
+ private static Condition cond(byte[][] keys, boolean result) {
+ var m = mock(Condition.class);
+
+ when(m.keys()).thenReturn(keys);
+
+ when(m.test(any())).thenReturn(result);
+
+ return m;
+ }
+}
diff --git a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/SimpleInMemoryKeyValueStorage.java b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/SimpleInMemoryKeyValueStorage.java
index 75b581c..90f23ab 100644
--- a/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/SimpleInMemoryKeyValueStorage.java
+++ b/modules/metastorage-server/src/test/java/org/apache/ignite/internal/metastorage/server/SimpleInMemoryKeyValueStorage.java
@@ -263,9 +263,9 @@ public class SimpleInMemoryKeyValueStorage implements KeyValueStorage {
@Override
public boolean invoke(Condition condition, Collection<Operation> success, Collection<Operation> failure) {
synchronized (mux) {
- Entry e = get(condition.key());
+ Collection<Entry> e = getAll(Arrays.asList(condition.keys()));
- boolean branch = condition.test(e);
+ boolean branch = condition.test(e.toArray(new Entry[]{}));
Collection<Operation> ops = branch ? success : failure;
@@ -303,6 +303,54 @@ public class SimpleInMemoryKeyValueStorage implements KeyValueStorage {
}
}
+ @Override
+ public StatementResult invoke(If iif) {
+ synchronized (mux) {
+ If currIf = iif;
+ while (true) {
+ Collection<Entry> e = getAll(Arrays.asList(currIf.cond().keys()));
+
+ Statement branch = (currIf.cond().test(e.toArray(new Entry[]{}))) ? currIf.andThen() : currIf.orElse();
+
+ if (branch.isTerminal()) {
+ long curRev = rev + 1;
+
+ boolean modified = false;
+
+ for (Operation op : branch.update().operations()) {
+ switch (op.type()) {
+ case PUT:
+ doPut(op.key(), op.value(), curRev);
+
+ modified = true;
+
+ break;
+
+ case REMOVE:
+ modified |= doRemove(op.key(), curRev);
+
+ break;
+
+ case NO_OP:
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown operation type: " + op.type());
+ }
+ }
+
+ if (modified) {
+ rev = curRev;
+ }
+
+ return branch.update().result();
+ } else {
+ currIf = branch.iif();
+ }
+ }
+ }
+ }
+
/** {@inheritDoc} */
@Override
public Cursor<Entry> range(byte[] keyFrom, byte[] keyTo) {
diff --git a/modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java b/modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java
index 0889f1b..aad0360 100644
--- a/modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java
+++ b/modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java
@@ -39,10 +39,12 @@ import org.apache.ignite.internal.manager.IgniteComponent;
import org.apache.ignite.internal.metastorage.client.CompactedException;
import org.apache.ignite.internal.metastorage.client.Condition;
import org.apache.ignite.internal.metastorage.client.Entry;
+import org.apache.ignite.internal.metastorage.client.If;
import org.apache.ignite.internal.metastorage.client.MetaStorageService;
import org.apache.ignite.internal.metastorage.client.MetaStorageServiceImpl;
import org.apache.ignite.internal.metastorage.client.Operation;
import org.apache.ignite.internal.metastorage.client.OperationTimeoutException;
+import org.apache.ignite.internal.metastorage.client.StatementResult;
import org.apache.ignite.internal.metastorage.client.WatchListener;
import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener;
@@ -685,6 +687,23 @@ public class MetaStorageManager implements IgniteComponent {
}
/**
+ * Invoke, which supports nested conditional statements.
+ *
+ * @see MetaStorageService#invoke(org.apache.ignite.internal.metastorage.client.If)
+ */
+ public @NotNull CompletableFuture<StatementResult> invoke(@NotNull If iif) {
+ if (!busyLock.enterBusy()) {
+ return CompletableFuture.failedFuture(new NodeStoppingException());
+ }
+
+ try {
+ return metaStorageSvcFut.thenCompose(svc -> svc.invoke(iif));
+ } finally {
+ busyLock.leaveBusy();
+ }
+ }
+
+ /**
* Retrieves entries for the given key range in lexicographic order.
* Entries will be filtered out by upper bound of given revision number.
*
diff --git a/modules/runner/src/main/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorage.java b/modules/runner/src/main/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorage.java
index 683f597..668d2a3 100644
--- a/modules/runner/src/main/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorage.java
+++ b/modules/runner/src/main/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorage.java
@@ -27,12 +27,12 @@ import java.util.concurrent.atomic.AtomicLong;
import org.apache.ignite.configuration.annotation.ConfigurationType;
import org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil;
import org.apache.ignite.internal.metastorage.MetaStorageManager;
-import org.apache.ignite.internal.metastorage.client.Condition;
import org.apache.ignite.internal.metastorage.client.Conditions;
import org.apache.ignite.internal.metastorage.client.Entry;
import org.apache.ignite.internal.metastorage.client.EntryEvent;
import org.apache.ignite.internal.metastorage.client.Operation;
import org.apache.ignite.internal.metastorage.client.Operations;
+import org.apache.ignite.internal.metastorage.client.SimpleCondition;
import org.apache.ignite.internal.metastorage.client.WatchEvent;
import org.apache.ignite.internal.metastorage.client.WatchListener;
import org.apache.ignite.internal.util.ByteUtils;
@@ -90,8 +90,8 @@ public class DistributedConfigurationStorage implements ConfigurationStorage {
*
* <p>This is true for all cases except for node restart. Key-specific revision values are lost on local vault copy after restart, so
* stored {@link MetaStorageManager#APPLIED_REV} value is used instead. This fact has very important side effect: it's no longer
- * possible to use {@link Condition.RevisionCondition#eq} on {@link #MASTER_KEY} in {@link DistributedConfigurationStorage#write(Map,
- * long)}. {@link Condition.RevisionCondition#le(long)} must be used instead.
+ * possible to use {@link SimpleCondition.RevisionCondition#eq} on {@link #MASTER_KEY}
+ * in {@link DistributedConfigurationStorage#write(Map, long)}. {@link SimpleCondition.RevisionCondition#le(long)} must be used instead.
*
* @see #MASTER_KEY
* @see MetaStorageManager#APPLIED_REV
@@ -239,7 +239,7 @@ public class DistributedConfigurationStorage implements ConfigurationStorage {
// current APPLIED_REV. This allows to conclude that "MASTER_KEY revision <= curChangeId" is a valid
// condition for update.
// Combining all of the above, it's concluded that the following condition must be used:
- Condition condition = curChangeId == 0L
+ SimpleCondition condition = curChangeId == 0L
? Conditions.notExists(MASTER_KEY)
: Conditions.revision(MASTER_KEY).le(curChangeId);
diff --git a/modules/runner/src/test/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorageTest.java b/modules/runner/src/test/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorageTest.java
index 557652c..84e54ef 100644
--- a/modules/runner/src/test/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorageTest.java
+++ b/modules/runner/src/test/java/org/apache/ignite/internal/configuration/storage/DistributedConfigurationStorageTest.java
@@ -27,9 +27,9 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import org.apache.ignite.internal.metastorage.MetaStorageManager;
-import org.apache.ignite.internal.metastorage.client.Condition;
import org.apache.ignite.internal.metastorage.client.Entry;
import org.apache.ignite.internal.metastorage.client.Operation;
+import org.apache.ignite.internal.metastorage.client.SimpleCondition;
import org.apache.ignite.internal.metastorage.common.OperationType;
import org.apache.ignite.internal.metastorage.server.ExistenceCondition;
import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
@@ -88,7 +88,7 @@ public class DistributedConfigurationStorageTest extends ConfigurationStorageTes
var mock = mock(MetaStorageManager.class);
when(mock.invoke(any(), anyCollection(), anyCollection())).thenAnswer(invocation -> {
- Condition condition = invocation.getArgument(0);
+ SimpleCondition condition = invocation.getArgument(0);
Collection<Operation> success = invocation.getArgument(1);
Collection<Operation> failure = invocation.getArgument(2);
@@ -116,15 +116,15 @@ public class DistributedConfigurationStorageTest extends ConfigurationStorageTes
}
/**
- * Converts a {@link Condition} to a {@link org.apache.ignite.internal.metastorage.server.Condition}.
+ * Converts a {@link SimpleCondition} to a {@link org.apache.ignite.internal.metastorage.server.Condition}.
*/
- private static org.apache.ignite.internal.metastorage.server.Condition toServerCondition(Condition condition) {
+ private static org.apache.ignite.internal.metastorage.server.Condition toServerCondition(SimpleCondition condition) {
switch (condition.type()) {
case REV_LESS_OR_EQUAL:
return new RevisionCondition(
RevisionCondition.Type.LESS_OR_EQUAL,
condition.inner().key(),
- ((Condition.RevisionCondition) condition.inner()).revision()
+ ((SimpleCondition.RevisionCondition) condition.inner()).revision()
);
case KEY_NOT_EXISTS:
return new ExistenceCondition(