Search in sources :

Example 1 with OrCondition

use of org.apache.ignite.internal.metastorage.server.OrCondition in project ignite-3 by apache.

the class ItMetaStorageServiceTest method testMultiInvoke.

@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 || key2.value > val1 || key1.value >= val2):
                put(key1, rval1)
                return true
            else
                if (key2.value < val1 && key1.value <= val2):
                    put(key1, rval1)
                    remove(key2, rval2)
                    return false
                else
                    return true
        else
            put(key2, rval2)
            return false
         */
    var iif = If.iif(or(value(key1).eq(val1), value(key2).ne(val2)), iif(or(revision(key3).eq(3), or(value(key2).gt(val1), value(key1).ge(val2))), ops(put(key1, rval1)).yield(true), iif(and(value(key2).lt(val1), value(key1).le(val2)), ops(put(key1, rval1), remove(key2)).yield(false), ops().yield(true))), 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 OrCondition(new RevisionCondition(RevisionCondition.Type.EQUAL, key3.bytes(), 3), new OrCondition(new ValueCondition(ValueCondition.Type.GREATER, key2.bytes(), val1), new ValueCondition(Type.GREATER_OR_EQUAL, key1.bytes(), val2)))));
    assertThat(resultIf.andThen().iif().orElse().iif().cond(), cond(new AndCondition(new ValueCondition(ValueCondition.Type.LESS, key2.bytes(), val1), new ValueCondition(Type.LESS_OR_EQUAL, key1.bytes(), val2))));
    assertThat(resultIf.andThen().iif().andThen().update(), upd(new Update(List.of(new org.apache.ignite.internal.metastorage.server.Operation(OperationType.PUT, key1.bytes(), rval1)), new StatementResult(true))));
    assertThat(resultIf.andThen().iif().orElse().iif().andThen().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.andThen().iif().orElse().iif().orElse().update(), upd(new Update(Collections.emptyList(), new StatementResult(true))));
    assertThat(resultIf.orElse().update(), upd(new Update(List.of(new org.apache.ignite.internal.metastorage.server.Operation(OperationType.PUT, key2.bytes(), rval2)), new StatementResult(false))));
}
Also used : StatementResult(org.apache.ignite.internal.metastorage.server.StatementResult) Update(org.apache.ignite.internal.metastorage.server.Update) AndCondition(org.apache.ignite.internal.metastorage.server.AndCondition) ByteArray(org.apache.ignite.lang.ByteArray) ValueCondition(org.apache.ignite.internal.metastorage.server.ValueCondition) OrCondition(org.apache.ignite.internal.metastorage.server.OrCondition) RevisionCondition(org.apache.ignite.internal.metastorage.server.RevisionCondition) Test(org.junit.jupiter.api.Test)

Example 2 with OrCondition

use of org.apache.ignite.internal.metastorage.server.OrCondition in project ignite-3 by apache.

the class MetaStorageListener method toCondition.

private static Condition toCondition(ConditionInfo info) {
    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.VAL_GREATER) {
            return new ValueCondition(ValueCondition.Type.GREATER, key, inf.value());
        } else if (type == ConditionType.VAL_GREATER_OR_EQUAL) {
            return new ValueCondition(ValueCondition.Type.GREATER_OR_EQUAL, key, inf.value());
        } else if (type == ConditionType.VAL_LESS) {
            return new ValueCondition(ValueCondition.Type.LESS, key, inf.value());
        } else if (type == ConditionType.VAL_LESS_OR_EQUAL) {
            return new ValueCondition(ValueCondition.Type.LESS_OR_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 info type " + info);
    }
}
Also used : SimpleConditionInfo(org.apache.ignite.internal.metastorage.common.command.SimpleConditionInfo) TombstoneCondition(org.apache.ignite.internal.metastorage.server.TombstoneCondition) CompoundConditionInfo(org.apache.ignite.internal.metastorage.common.command.CompoundConditionInfo) ValueCondition(org.apache.ignite.internal.metastorage.server.ValueCondition) ExistenceCondition(org.apache.ignite.internal.metastorage.server.ExistenceCondition) CompoundConditionType(org.apache.ignite.internal.metastorage.common.command.CompoundConditionType) ConditionType(org.apache.ignite.internal.metastorage.common.ConditionType) OrCondition(org.apache.ignite.internal.metastorage.server.OrCondition) RevisionCondition(org.apache.ignite.internal.metastorage.server.RevisionCondition) AndCondition(org.apache.ignite.internal.metastorage.server.AndCondition)

Aggregations

AndCondition (org.apache.ignite.internal.metastorage.server.AndCondition)2 OrCondition (org.apache.ignite.internal.metastorage.server.OrCondition)2 RevisionCondition (org.apache.ignite.internal.metastorage.server.RevisionCondition)2 ValueCondition (org.apache.ignite.internal.metastorage.server.ValueCondition)2 ConditionType (org.apache.ignite.internal.metastorage.common.ConditionType)1 CompoundConditionInfo (org.apache.ignite.internal.metastorage.common.command.CompoundConditionInfo)1 CompoundConditionType (org.apache.ignite.internal.metastorage.common.command.CompoundConditionType)1 SimpleConditionInfo (org.apache.ignite.internal.metastorage.common.command.SimpleConditionInfo)1 ExistenceCondition (org.apache.ignite.internal.metastorage.server.ExistenceCondition)1 StatementResult (org.apache.ignite.internal.metastorage.server.StatementResult)1 TombstoneCondition (org.apache.ignite.internal.metastorage.server.TombstoneCondition)1 Update (org.apache.ignite.internal.metastorage.server.Update)1 ByteArray (org.apache.ignite.lang.ByteArray)1 Test (org.junit.jupiter.api.Test)1