Search in sources :

Example 11 with Operation

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

the class IndexEventHolder method delete.

@Override
public void delete(String attribute, Compare.Operator operator, Object value) {
    if (isOperationLogEnabled) {
        if (!isFullSnapshot()) {
            operationChangeLog.add(new Operation(DELETE_BY_OPERATOR, new Object[] { attribute, operator, value }));
        } else {
            operationChangeLog.clear();
            forceFullSnapshot = true;
        }
    }
    if (primaryKeyData != null && attribute.equals(primaryKeyAttributes)) {
        switch(operator) {
            case LESS_THAN:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case GREATER_THAN:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case LESS_THAN_EQUAL:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case GREATER_THAN_EQUAL:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case EQUAL:
                StreamEvent deletedEvent = primaryKeyData.remove(value);
                if (deletedEvent != null) {
                    deleteFromIndexes(deletedEvent);
                }
                return;
            case NOT_EQUAL:
                StreamEvent streamEvent = primaryKeyData.get(value);
                deleteAll();
                if (streamEvent != null) {
                    add(streamEvent);
                }
                return;
        }
    } else {
        switch(operator) {
            case LESS_THAN:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).headMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case GREATER_THAN:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).tailMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case LESS_THAN_EQUAL:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).headMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case GREATER_THAN_EQUAL:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).tailMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case EQUAL:
                Set<StreamEvent> deletedEventSet = indexData.get(attribute).remove(value);
                if (deletedEventSet != null && deletedEventSet.size() > 0) {
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                }
                return;
            case NOT_EQUAL:
                Set<StreamEvent> matchingEventSet = indexData.get(attribute).get(value);
                deleteAll();
                for (StreamEvent matchingEvent : matchingEventSet) {
                    add(matchingEvent);
                }
                return;
        }
    }
    throw new OperationNotSupportedException(operator + " not supported for '" + value + "' by " + getClass().getName());
}
Also used : OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) HashSet(java.util.HashSet) Set(java.util.Set) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Operation(io.siddhi.core.event.stream.Operation) TreeMap(java.util.TreeMap)

Example 12 with Operation

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

the class IndexEventHolder method add.

@Override
public void add(ComplexEventChunk<StreamEvent> addingEventChunk) {
    addingEventChunk.reset();
    while (addingEventChunk.hasNext()) {
        ComplexEvent complexEvent = addingEventChunk.next();
        StreamEvent streamEvent = tableStreamEventFactory.newInstance();
        eventConverter.convertComplexEvent(complexEvent, streamEvent);
        eventsCount++;
        if (isOperationLogEnabled) {
            if (!isFullSnapshot()) {
                StreamEvent streamEvent2 = tableStreamEventFactory.newInstance();
                eventConverter.convertComplexEvent(complexEvent, streamEvent2);
                operationChangeLog.add(new Operation(ADD, streamEvent2));
            } else {
                operationChangeLog.clear();
                forceFullSnapshot = true;
            }
        }
        add(streamEvent);
    }
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Operation(io.siddhi.core.event.stream.Operation)

Example 13 with Operation

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

the class IndexEventHolder method deleteAll.

@Override
public void deleteAll() {
    if (isOperationLogEnabled) {
        if (!isFullSnapshot()) {
            operationChangeLog.add(new Operation(CLEAR));
        } else {
            operationChangeLog.clear();
            forceFullSnapshot = true;
        }
    }
    if (primaryKeyData != null) {
        primaryKeyData.clear();
    }
    if (indexData != null) {
        for (TreeMap<Object, Set<StreamEvent>> aIndexedData : indexData.values()) {
            aIndexedData.clear();
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Operation(io.siddhi.core.event.stream.Operation)

Example 14 with Operation

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

the class IndexEventHolder method overwrite.

@Override
public void overwrite(StreamEvent streamEvent) {
    if (isOperationLogEnabled) {
        if (!isFullSnapshot()) {
            StreamEvent streamEvent2 = tableStreamEventFactory.newInstance();
            eventConverter.convertComplexEvent(streamEvent, streamEvent2);
            operationChangeLog.add(new Operation(OVERWRITE, streamEvent2));
        } else {
            operationChangeLog.clear();
            forceFullSnapshot = true;
        }
    }
    StreamEvent deletedEvent = null;
    if (primaryKeyData != null) {
        Object primaryKey = constructPrimaryKey(streamEvent, primaryKeyReferenceHolders);
        deletedEvent = primaryKeyData.put(primaryKey, streamEvent);
        if (deletedEvent != null) {
            handleCachePolicyAttributeUpdate(streamEvent);
        }
    }
    if (indexData != null) {
        for (Map.Entry<String, Integer> indexEntry : indexMetaData.entrySet()) {
            TreeMap<Object, Set<StreamEvent>> indexMap = indexData.get(indexEntry.getKey());
            Object key = streamEvent.getOutputData()[indexEntry.getValue()];
            if (deletedEvent != null) {
                Set<StreamEvent> values = indexMap.get(key);
                values.remove(deletedEvent);
                if (values.size() == 0) {
                    indexMap.remove(key);
                }
            }
            Set<StreamEvent> values = indexMap.get(key);
            if (values == null) {
                values = new HashSet<StreamEvent>();
                values.add(streamEvent);
                indexMap.put(streamEvent.getOutputData()[indexEntry.getValue()], values);
            } else {
                values.add(streamEvent);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Operation(io.siddhi.core.event.stream.Operation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 15 with Operation

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

the class SnapshotableEventQueueTestCase method incrementalPersistenceTest4.

@Test
public void incrementalPersistenceTest4() 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));
    snapshotableStreamEventQueue.poll();
    snapshotableStreamEventQueue.poll();
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    for (int i = 7; i < 10; i++) {
        snapshotableStreamEventQueue.poll();
    }
    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)

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