Search in sources :

Example 1 with StatementResult

use of org.apache.ignite.internal.metastorage.server.StatementResult 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 StatementResult

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

the class MetaStorageListener method onWrite.

/**
 * {@inheritDoc}
 */
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iter) {
    while (iter.hasNext()) {
        CommandClosure<WriteCommand> clo = iter.next();
        WriteCommand command = clo.command();
        if (command instanceof PutCommand) {
            PutCommand putCmd = (PutCommand) command;
            storage.put(putCmd.key(), putCmd.value());
            clo.result(null);
        } else if (command instanceof GetAndPutCommand) {
            GetAndPutCommand getAndPutCmd = (GetAndPutCommand) command;
            Entry e = storage.getAndPut(getAndPutCmd.key(), getAndPutCmd.value());
            clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
        } else if (command instanceof PutAllCommand) {
            PutAllCommand putAllCmd = (PutAllCommand) command;
            storage.putAll(putAllCmd.keys(), putAllCmd.values());
            clo.result(null);
        } else if (command instanceof GetAndPutAllCommand) {
            GetAndPutAllCommand getAndPutAllCmd = (GetAndPutAllCommand) command;
            Collection<Entry> entries = storage.getAndPutAll(getAndPutAllCmd.keys(), getAndPutAllCmd.vals());
            List<SingleEntryResponse> resp = new ArrayList<>(entries.size());
            for (Entry e : entries) {
                resp.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
            }
            clo.result(new MultipleEntryResponse(resp));
        } else if (command instanceof RemoveCommand) {
            RemoveCommand rmvCmd = (RemoveCommand) command;
            storage.remove(rmvCmd.key());
            clo.result(null);
        } else if (command instanceof GetAndRemoveCommand) {
            GetAndRemoveCommand getAndRmvCmd = (GetAndRemoveCommand) command;
            Entry e = storage.getAndRemove(getAndRmvCmd.key());
            clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
        } else if (command instanceof RemoveAllCommand) {
            RemoveAllCommand rmvAllCmd = (RemoveAllCommand) command;
            storage.removeAll(rmvAllCmd.keys());
            clo.result(null);
        } else if (command instanceof GetAndRemoveAllCommand) {
            GetAndRemoveAllCommand getAndRmvAllCmd = (GetAndRemoveAllCommand) command;
            Collection<Entry> entries = storage.getAndRemoveAll(getAndRmvAllCmd.keys());
            List<SingleEntryResponse> resp = new ArrayList<>(entries.size());
            for (Entry e : entries) {
                resp.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
            }
            clo.result(new MultipleEntryResponse(resp));
        } else if (command instanceof InvokeCommand) {
            InvokeCommand cmd = (InvokeCommand) command;
            boolean res = storage.invoke(toCondition(cmd.condition()), toOperations(cmd.success()), toOperations(cmd.failure()));
            clo.result(res);
        } else if (command instanceof MultiInvokeCommand) {
            MultiInvokeCommand cmd = (MultiInvokeCommand) command;
            StatementResult res = storage.invoke(toIf(cmd.iif()));
            clo.result(new StatementResultInfo(res.bytes()));
        } else if (command instanceof RangeCommand) {
            RangeCommand rangeCmd = (RangeCommand) command;
            IgniteUuid cursorId = rangeCmd.getCursorId();
            Cursor<Entry> cursor = (rangeCmd.revUpperBound() != -1) ? storage.range(rangeCmd.keyFrom(), rangeCmd.keyTo(), rangeCmd.revUpperBound()) : storage.range(rangeCmd.keyFrom(), rangeCmd.keyTo());
            cursors.put(cursorId, new CursorMeta(cursor, CursorType.RANGE, rangeCmd.requesterNodeId()));
            clo.result(cursorId);
        } else if (command instanceof CursorNextCommand) {
            CursorNextCommand cursorNextCmd = (CursorNextCommand) command;
            CursorMeta cursorDesc = cursors.get(cursorNextCmd.cursorId());
            if (cursorDesc == null) {
                clo.result(new NoSuchElementException("Corresponding cursor on the server side is not found."));
                return;
            }
            try {
                if (cursorDesc.type() == CursorType.RANGE) {
                    Entry e = (Entry) cursorDesc.cursor().next();
                    clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
                } else if (cursorDesc.type() == CursorType.WATCH) {
                    WatchEvent evt = (WatchEvent) cursorDesc.cursor().next();
                    List<SingleEntryResponse> resp = new ArrayList<>(evt.entryEvents().size() * 2);
                    for (EntryEvent e : evt.entryEvents()) {
                        Entry o = e.oldEntry();
                        Entry n = e.entry();
                        resp.add(new SingleEntryResponse(o.key(), o.value(), o.revision(), o.updateCounter()));
                        resp.add(new SingleEntryResponse(n.key(), n.value(), n.revision(), n.updateCounter()));
                    }
                    clo.result(new MultipleEntryResponse(resp));
                }
            } catch (NoSuchElementException e) {
                clo.result(e);
            }
        } else if (command instanceof CursorCloseCommand) {
            CursorCloseCommand cursorCloseCmd = (CursorCloseCommand) command;
            CursorMeta cursorDesc = cursors.remove(cursorCloseCmd.cursorId());
            if (cursorDesc == null) {
                clo.result(null);
                return;
            }
            try {
                cursorDesc.cursor().close();
            } catch (Exception e) {
                throw new IgniteInternalException(e);
            }
            clo.result(null);
        } else if (command instanceof WatchRangeKeysCommand) {
            WatchRangeKeysCommand watchCmd = (WatchRangeKeysCommand) command;
            IgniteUuid cursorId = watchCmd.getCursorId();
            Cursor<WatchEvent> cursor = storage.watch(watchCmd.keyFrom(), watchCmd.keyTo(), watchCmd.revision());
            cursors.put(cursorId, new CursorMeta(cursor, CursorType.WATCH, watchCmd.requesterNodeId()));
            clo.result(cursorId);
        } else if (command instanceof WatchExactKeysCommand) {
            WatchExactKeysCommand watchCmd = (WatchExactKeysCommand) command;
            IgniteUuid cursorId = watchCmd.getCursorId();
            Cursor<WatchEvent> cursor = storage.watch(watchCmd.keys(), watchCmd.revision());
            cursors.put(cursorId, new CursorMeta(cursor, CursorType.WATCH, watchCmd.requesterNodeId()));
            clo.result(cursorId);
        } else if (command instanceof CursorsCloseCommand) {
            CursorsCloseCommand cursorsCloseCmd = (CursorsCloseCommand) command;
            Iterator<CursorMeta> cursorsIter = cursors.values().iterator();
            while (cursorsIter.hasNext()) {
                CursorMeta cursorDesc = cursorsIter.next();
                if (cursorDesc.requesterNodeId().equals(cursorsCloseCmd.nodeId())) {
                    try {
                        cursorDesc.cursor().close();
                    } catch (Exception e) {
                        throw new IgniteInternalException(e);
                    }
                    cursorsIter.remove();
                }
            }
            clo.result(null);
        } else {
            assert false : "Command was not found [cmd=" + command + ']';
        }
    }
}
Also used : WriteCommand(org.apache.ignite.raft.client.WriteCommand) GetAndRemoveCommand(org.apache.ignite.internal.metastorage.common.command.GetAndRemoveCommand) RemoveCommand(org.apache.ignite.internal.metastorage.common.command.RemoveCommand) StatementResult(org.apache.ignite.internal.metastorage.server.StatementResult) CursorNextCommand(org.apache.ignite.internal.metastorage.common.command.cursor.CursorNextCommand) GetAndPutCommand(org.apache.ignite.internal.metastorage.common.command.GetAndPutCommand) ArrayList(java.util.ArrayList) Cursor(org.apache.ignite.internal.util.Cursor) InvokeCommand(org.apache.ignite.internal.metastorage.common.command.InvokeCommand) MultiInvokeCommand(org.apache.ignite.internal.metastorage.common.command.MultiInvokeCommand) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteUuid(org.apache.ignite.lang.IgniteUuid) EntryEvent(org.apache.ignite.internal.metastorage.server.EntryEvent) CursorCloseCommand(org.apache.ignite.internal.metastorage.common.command.cursor.CursorCloseCommand) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) WatchEvent(org.apache.ignite.internal.metastorage.server.WatchEvent) PutAllCommand(org.apache.ignite.internal.metastorage.common.command.PutAllCommand) GetAndPutAllCommand(org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand) WatchExactKeysCommand(org.apache.ignite.internal.metastorage.common.command.WatchExactKeysCommand) GetAndRemoveCommand(org.apache.ignite.internal.metastorage.common.command.GetAndRemoveCommand) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) CursorsCloseCommand(org.apache.ignite.internal.metastorage.common.command.cursor.CursorsCloseCommand) GetAndRemoveAllCommand(org.apache.ignite.internal.metastorage.common.command.GetAndRemoveAllCommand) RemoveAllCommand(org.apache.ignite.internal.metastorage.common.command.RemoveAllCommand) MultipleEntryResponse(org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse) MultiInvokeCommand(org.apache.ignite.internal.metastorage.common.command.MultiInvokeCommand) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) NoSuchElementException(java.util.NoSuchElementException) WatchRangeKeysCommand(org.apache.ignite.internal.metastorage.common.command.WatchRangeKeysCommand) RangeCommand(org.apache.ignite.internal.metastorage.common.command.RangeCommand) StatementResultInfo(org.apache.ignite.internal.metastorage.common.StatementResultInfo) GetAndRemoveAllCommand(org.apache.ignite.internal.metastorage.common.command.GetAndRemoveAllCommand) GetAndPutAllCommand(org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand) Collection(java.util.Collection) GetAndPutCommand(org.apache.ignite.internal.metastorage.common.command.GetAndPutCommand) PutCommand(org.apache.ignite.internal.metastorage.common.command.PutCommand) SingleEntryResponse(org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

StatementResult (org.apache.ignite.internal.metastorage.server.StatementResult)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 StatementResultInfo (org.apache.ignite.internal.metastorage.common.StatementResultInfo)1 GetAndPutAllCommand (org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand)1 GetAndPutCommand (org.apache.ignite.internal.metastorage.common.command.GetAndPutCommand)1 GetAndRemoveAllCommand (org.apache.ignite.internal.metastorage.common.command.GetAndRemoveAllCommand)1 GetAndRemoveCommand (org.apache.ignite.internal.metastorage.common.command.GetAndRemoveCommand)1 InvokeCommand (org.apache.ignite.internal.metastorage.common.command.InvokeCommand)1 MultiInvokeCommand (org.apache.ignite.internal.metastorage.common.command.MultiInvokeCommand)1 MultipleEntryResponse (org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse)1 PutAllCommand (org.apache.ignite.internal.metastorage.common.command.PutAllCommand)1 PutCommand (org.apache.ignite.internal.metastorage.common.command.PutCommand)1 RangeCommand (org.apache.ignite.internal.metastorage.common.command.RangeCommand)1 RemoveAllCommand (org.apache.ignite.internal.metastorage.common.command.RemoveAllCommand)1 RemoveCommand (org.apache.ignite.internal.metastorage.common.command.RemoveCommand)1 SingleEntryResponse (org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse)1