Search in sources :

Example 6 with Operation

use of io.siddhi.core.event.stream.Operation in project siddhi by wso2.

the class SnapshotableEventQueueTestCase method incrementalPersistenceTest2.

@Test
public void incrementalPersistenceTest2() throws InterruptedException, IOException, ClassNotFoundException {
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addOutputData(new Attribute("symbol", Attribute.Type.STRING));
    metaStreamEvent.addOutputData(new Attribute("price", Attribute.Type.FLOAT));
    metaStreamEvent.addOutputData(new Attribute("volume", Attribute.Type.LONG));
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, new StreamEventFactory(metaStreamEvent));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setOutputData(new Object[] { "IBM", 500.6f, 1 });
    for (int i = 0; i < 10; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    HashMap<Long, String> snapshots = new HashMap<>();
    Snapshot snapshot1 = snapshotableStreamEventQueue.getSnapshot();
    StreamEvent streamEvents = (StreamEvent) snapshot1.getState();
    Assert.assertTrue(streamEvents != null);
    snapshots.put(3L, toString(snapshot1));
    for (int i = 7; i < 10; i++) {
        snapshotableStreamEventQueue.next();
        snapshotableStreamEventQueue.remove();
    }
    Snapshot snapshot2 = snapshotableStreamEventQueue.getSnapshot();
    ArrayList<Operation> operationLog = (ArrayList<Operation>) snapshot2.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(4L, toString(snapshot2));
    for (int i = 10; i < 15; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    Snapshot snapshot3 = snapshotableStreamEventQueue.getSnapshot();
    operationLog = (ArrayList<Operation>) snapshot3.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(5L, toString(snapshot3));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue2 = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    SnapshotStateList snapshotStateList = new SnapshotStateList();
    for (Map.Entry<Long, String> entry : snapshots.entrySet()) {
        snapshotStateList.putSnapshotState(entry.getKey(), (Snapshot) fromString(entry.getValue()));
    }
    snapshotableStreamEventQueue2.restore(snapshotStateList);
    Assert.assertEquals(snapshotableStreamEventQueue, snapshotableStreamEventQueue2);
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) HashMap(java.util.HashMap) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SnapshotableStreamEventQueue(io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue) Test(org.testng.annotations.Test)

Example 7 with Operation

use of io.siddhi.core.event.stream.Operation in project siddhi by wso2.

the class IndexEventHolder method deleteAll.

@Override
public void deleteAll(Collection<StreamEvent> storeEventSet) {
    for (StreamEvent streamEvent : storeEventSet) {
        if (isOperationLogEnabled) {
            if (!isFullSnapshot()) {
                StreamEvent streamEvent2 = tableStreamEventFactory.newInstance();
                eventConverter.convertComplexEvent(streamEvent, streamEvent2);
                operationChangeLog.add(new Operation(REMOVE, streamEvent));
            } else {
                operationChangeLog.clear();
                forceFullSnapshot = true;
            }
        }
        deleteAll(streamEvent);
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) Operation(io.siddhi.core.event.stream.Operation)

Example 8 with Operation

use of io.siddhi.core.event.stream.Operation in project siddhi by wso2.

the class IndexEventHolder method restore.

public void restore(SnapshotStateList snapshotStatelist) {
    TreeMap<Long, Snapshot> revisions = snapshotStatelist.getSnapshotStates();
    Iterator<Map.Entry<Long, Snapshot>> itr = revisions.entrySet().iterator();
    this.isOperationLogEnabled = false;
    while (itr.hasNext()) {
        Map.Entry<Long, Snapshot> snapshotEntry = itr.next();
        if (!snapshotEntry.getValue().isIncrementalSnapshot()) {
            this.deleteAll();
            IndexEventHolder snapshotEventHolder = (IndexEventHolder) snapshotEntry.getValue().getState();
            if (primaryKeyData != null) {
                primaryKeyData.clear();
                primaryKeyData.putAll(snapshotEventHolder.primaryKeyData);
            }
            if (indexData != null) {
                indexData.clear();
                indexData.putAll(snapshotEventHolder.indexData);
            }
            forceFullSnapshot = false;
        } else {
            ArrayList<Operation> operations = (ArrayList<Operation>) snapshotEntry.getValue().getState();
            for (Operation op : operations) {
                switch(op.operation) {
                    case ADD:
                        add((StreamEvent) op.parameters);
                        break;
                    case REMOVE:
                        deleteAll((StreamEvent) op.parameters);
                        break;
                    case CLEAR:
                        deleteAll();
                        break;
                    case OVERWRITE:
                        overwrite((StreamEvent) op.parameters);
                        break;
                    case DELETE_BY_OPERATOR:
                        Object[] args = (Object[]) op.parameters;
                        delete((String) args[0], (Compare.Operator) args[1], args[2]);
                        break;
                    default:
                        continue;
                }
            }
        }
    }
    this.isOperationLogEnabled = true;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) Compare(io.siddhi.query.api.expression.condition.Compare) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 9 with Operation

use of io.siddhi.core.event.stream.Operation in project siddhi by wso2.

the class SnapshotableStreamEventQueue method remove.

/**
 * Removes from the underlying collection the last element returned by the
 * iterator (optional operation).  This method can be called only once per
 * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
 * the underlying collection is modified while the iteration is in
 * progress in any way other than by calling this method.
 *
 * @throws UnsupportedOperationException if the <tt>remove</tt>
 *                                       operation is not supported by this Iterator.
 * @throws IllegalStateException         if the <tt>next</tt> method has not
 *                                       yet been called, or the <tt>remove</tt> method has already
 *                                       been called after the last call to the <tt>next</tt>
 *                                       method.
 */
public void remove() {
    if (lastReturned == null) {
        throw new IllegalStateException();
    }
    if (previousToLastReturned != null) {
        previousToLastReturned.setNext(lastReturned.getNext());
    } else {
        first = lastReturned.getNext();
        if (first == null) {
            last = null;
        }
    }
    lastReturned.setNext(null);
    lastReturned = null;
    if (!isFullSnapshot()) {
        if (isOperationLogEnabled) {
            operationChangeLog.add(new Operation(Operator.DELETE_BY_INDEX, eventIndex));
        }
        operationChangeLogSize++;
    } else {
        operationChangeLog.clear();
        operationChangeLogSize = 0;
        forceFullSnapshot = true;
    }
    eventIndex--;
    size--;
}
Also used : Operation(io.siddhi.core.event.stream.Operation)

Example 10 with Operation

use of io.siddhi.core.event.stream.Operation in project siddhi by wso2.

the class SnapshotableStreamEventQueue method restore.

public void restore(SnapshotStateList snapshotStatelist) {
    TreeMap<Long, Snapshot> revisions = snapshotStatelist.getSnapshotStates();
    Iterator<Map.Entry<Long, Snapshot>> itr = revisions.entrySet().iterator();
    this.isOperationLogEnabled = false;
    while (itr.hasNext()) {
        Map.Entry<Long, Snapshot> snapshotEntry = itr.next();
        if (!snapshotEntry.getValue().isIncrementalSnapshot()) {
            this.clear();
            this.add((StreamEvent) snapshotEntry.getValue().getState());
            forceFullSnapshot = false;
        } else {
            ArrayList<Operation> operations = (ArrayList<Operation>) snapshotEntry.getValue().getState();
            for (Operation op : operations) {
                switch(op.operation) {
                    case ADD:
                        add((StreamEvent) op.parameters);
                        break;
                    case REMOVE:
                        poll();
                        break;
                    case CLEAR:
                        clear();
                        break;
                    case OVERWRITE:
                        int overwriteIndex = (int) ((Object[]) op.parameters)[0];
                        StreamEvent streamEvent = (StreamEvent) ((Object[]) op.parameters)[1];
                        while (hasNext()) {
                            next();
                            if (overwriteIndex == eventIndex) {
                                overwrite(streamEvent);
                                break;
                            }
                        }
                        break;
                    case DELETE_BY_OPERATOR:
                        break;
                    case DELETE_BY_INDEX:
                        int deleteIndex = (int) op.parameters;
                        while (hasNext()) {
                            next();
                            if (deleteIndex == eventIndex) {
                                remove();
                                break;
                            }
                        }
                        break;
                    default:
                        continue;
                }
            }
        }
    }
    this.isOperationLogEnabled = true;
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Operation (io.siddhi.core.event.stream.Operation)16 StreamEvent (io.siddhi.core.event.stream.StreamEvent)12 Map (java.util.Map)8 Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)5 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)5 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)5 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)5 StreamEventClonerHolder (io.siddhi.core.event.stream.holder.StreamEventClonerHolder)5 SnapshotStateList (io.siddhi.core.util.snapshot.state.SnapshotStateList)5 Attribute (io.siddhi.query.api.definition.Attribute)5 Test (org.testng.annotations.Test)5 TreeMap (java.util.TreeMap)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 LinkedHashMap (java.util.LinkedHashMap)2 ComplexEvent (io.siddhi.core.event.ComplexEvent)1 OperationNotSupportedException (io.siddhi.core.exception.OperationNotSupportedException)1