Search in sources :

Example 6 with IndexedEventHolder

use of org.wso2.siddhi.core.table.holder.IndexedEventHolder in project siddhi by wso2.

the class ExhaustiveCollectionExecutor method find.

public StreamEvent find(StateEvent matchingEvent, IndexedEventHolder indexedEventHolder, StreamEventCloner storeEventCloner) {
    ComplexEventChunk<StreamEvent> returnEventChunk = new ComplexEventChunk<StreamEvent>(false);
    Collection<StreamEvent> storeEvents = indexedEventHolder.getAllEvents();
    for (StreamEvent storeEvent : storeEvents) {
        matchingEvent.setEvent(storeEventIndex, storeEvent);
        if ((Boolean) expressionExecutor.execute(matchingEvent)) {
            if (storeEventCloner != null) {
                returnEventChunk.add(storeEventCloner.copyStreamEvent(storeEvent));
            } else {
                returnEventChunk.add(storeEvent);
            }
        }
        matchingEvent.setEvent(storeEventIndex, null);
    }
    return returnEventChunk.getFirst();
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 7 with IndexedEventHolder

use of org.wso2.siddhi.core.table.holder.IndexedEventHolder in project siddhi by wso2.

the class ExhaustiveCollectionExecutor method delete.

@Override
public void delete(StateEvent deletingEvent, IndexedEventHolder indexedEventHolder) {
    Collection<StreamEvent> storeEvents = indexedEventHolder.getAllEvents();
    Set<StreamEvent> toDeleteEvents = new HashSet<StreamEvent>();
    for (StreamEvent storeEvent : storeEvents) {
        deletingEvent.setEvent(storeEventIndex, storeEvent);
        if ((Boolean) expressionExecutor.execute(deletingEvent)) {
            toDeleteEvents.add(storeEvent);
        }
        deletingEvent.setEvent(storeEventIndex, null);
    }
    indexedEventHolder.deleteAll(toDeleteEvents);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) HashSet(java.util.HashSet)

Example 8 with IndexedEventHolder

use of org.wso2.siddhi.core.table.holder.IndexedEventHolder in project siddhi by wso2.

the class OverwriteTableIndexOperator method tryUpdate.

@Override
public ComplexEventChunk<StreamEvent> tryUpdate(ComplexEventChunk<StateEvent> updatingOrAddingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    updatingOrAddingEventChunk.reset();
    while (updatingOrAddingEventChunk.hasNext()) {
        StateEvent overwritingOrAddingEvent = updatingOrAddingEventChunk.next();
        ((IndexedEventHolder) storeEvents).overwrite(addingStreamEventExtractor.getAddingStreamEvent(overwritingOrAddingEvent));
    }
    return null;
}
Also used : IndexedEventHolder(org.wso2.siddhi.core.table.holder.IndexedEventHolder) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 9 with IndexedEventHolder

use of org.wso2.siddhi.core.table.holder.IndexedEventHolder in project siddhi by wso2.

the class IndexOperator method tryUpdate.

@Override
public ComplexEventChunk<StreamEvent> tryUpdate(ComplexEventChunk<StateEvent> updatingOrAddingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    ComplexEventChunk<StreamEvent> failedEventChunk = new ComplexEventChunk<StreamEvent>(updatingOrAddingEventChunk.isBatch());
    updatingOrAddingEventChunk.reset();
    while (updatingOrAddingEventChunk.hasNext()) {
        StateEvent overwritingOrAddingEvent = updatingOrAddingEventChunk.next();
        StreamEvent streamEvents = collectionExecutor.find(overwritingOrAddingEvent, (IndexedEventHolder) storeEvents, null);
        ComplexEventChunk<StreamEvent> foundEventChunk = new ComplexEventChunk<>(false);
        foundEventChunk.add(streamEvents);
        if (foundEventChunk.getFirst() != null) {
            // for cases when indexed attribute is also updated but that not changed
            // to reduce number of passes needed to update the events
            update((IndexedEventHolder) storeEvents, compiledUpdateSet, overwritingOrAddingEvent, foundEventChunk);
        } else {
            failedEventChunk.add(addingStreamEventExtractor.getAddingStreamEvent(overwritingOrAddingEvent));
        }
    }
    return failedEventChunk;
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 10 with IndexedEventHolder

use of org.wso2.siddhi.core.table.holder.IndexedEventHolder in project siddhi by wso2.

the class IndexOperator method update.

private void update(IndexedEventHolder storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, StateEvent overwritingOrAddingEvent, ComplexEventChunk<StreamEvent> foundEventChunk) {
    // for cases when indexed attribute is also updated but that not changed
    // to reduce number of passes needed to update the events
    boolean doDeleteUpdate = false;
    boolean fail = false;
    for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
        if (doDeleteUpdate || fail) {
            break;
        }
        if (storeEvents.isAttributeIndexed(entry.getKey())) {
            // Todo how much check we need to do before falling back to Delete and then Update
            foundEventChunk.reset();
            Set<Object> keys = null;
            PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = storeEvents.getPrimaryKeyReferenceHolders();
            if (primaryKeyReferenceHolders != null && primaryKeyReferenceHolders.length == 1 && entry.getKey() == primaryKeyReferenceHolders[0].getPrimaryKeyPosition()) {
                keys = new HashSet<>(storeEvents.getAllPrimaryKeyValues());
            }
            while (foundEventChunk.hasNext()) {
                StreamEvent streamEvent = foundEventChunk.next();
                Object updatingData = entry.getValue().execute(overwritingOrAddingEvent);
                Object storeEventData = streamEvent.getOutputData()[entry.getKey()];
                if (updatingData != null && storeEventData != null && !updatingData.equals(storeEventData)) {
                    doDeleteUpdate = true;
                    if (keys == null || keys.size() == 0) {
                        break;
                    } else {
                        keys.remove(storeEventData);
                        if (!keys.add(updatingData)) {
                            log.error("Update failed for event :" + overwritingOrAddingEvent + ", as there is " + "already an event stored with primary key '" + updatingData + "' at '" + queryName + "'");
                            fail = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    foundEventChunk.reset();
    if (!fail) {
        if (doDeleteUpdate) {
            collectionExecutor.delete(overwritingOrAddingEvent, storeEvents);
            ComplexEventChunk<StreamEvent> toUpdateEventChunk = new ComplexEventChunk<StreamEvent>(false);
            while (foundEventChunk.hasNext()) {
                StreamEvent streamEvent = foundEventChunk.next();
                foundEventChunk.remove();
                // to make the chained state back to normal
                streamEvent.setNext(null);
                for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                    streamEvent.setOutputData(entry.getValue().execute(overwritingOrAddingEvent), entry.getKey());
                }
                toUpdateEventChunk.add(streamEvent);
            }
            storeEvents.add(toUpdateEventChunk);
        } else {
            while (foundEventChunk.hasNext()) {
                StreamEvent streamEvent = foundEventChunk.next();
                // to make the chained state back to normal
                streamEvent.setNext(null);
                for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                    streamEvent.setOutputData(entry.getValue().execute(overwritingOrAddingEvent), entry.getKey());
                }
            }
        }
    }
}
Also used : ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) PrimaryKeyReferenceHolder(org.wso2.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Map(java.util.Map)

Aggregations

StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)10 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)9 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)4 HashSet (java.util.HashSet)3 Map (java.util.Map)2 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)2 IndexedEventHolder (org.wso2.siddhi.core.table.holder.IndexedEventHolder)2 AndMultiPrimaryKeyCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression)2 AttributeCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression)2 CollectionExpression (org.wso2.siddhi.core.util.collection.expression.CollectionExpression)2 CompareCollectionExpression (org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression)2 Collection (java.util.Collection)1 Set (java.util.Set)1 OperationNotSupportedException (org.wso2.siddhi.core.exception.OperationNotSupportedException)1 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)1 PrimaryKeyReferenceHolder (org.wso2.siddhi.core.table.holder.PrimaryKeyReferenceHolder)1 CollectionExecutor (org.wso2.siddhi.core.util.collection.executor.CollectionExecutor)1 AndCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression)1 BasicCollectionExpression (org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression)1 NotCollectionExpression (org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression)1