Search in sources :

Example 1 with PrimaryKeyReferenceHolder

use of io.siddhi.core.table.holder.PrimaryKeyReferenceHolder 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<>();
            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 {
            StreamEvent first = foundEventChunk.getFirst();
            while (first != null) {
                StreamEvent streamEvent = first;
                handleCachePolicyAttributeUpdate(streamEvent);
                for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                    streamEvent.setOutputData(entry.getValue().execute(overwritingOrAddingEvent), entry.getKey());
                }
                StreamEvent next = first.getNext();
                // to make the chained state back to normal
                first.setNext(null);
                first = next;
            }
        }
    }
}
Also used : ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) PrimaryKeyReferenceHolder(io.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Map(java.util.Map)

Example 2 with PrimaryKeyReferenceHolder

use of io.siddhi.core.table.holder.PrimaryKeyReferenceHolder in project siddhi by wso2.

the class EventHolderPasser method parse.

public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventFactory tableStreamEventFactory, SiddhiAppContext siddhiAppContext, boolean isCacheTable) {
    ZeroStreamEventConverter eventConverter = new ZeroStreamEventConverter();
    PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = null;
    Map<String, Integer> indexMetaData = new HashMap<String, Integer>();
    // primaryKey.
    Annotation primaryKeyAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY, tableDefinition.getAnnotations());
    if (primaryKeyAnnotation != null) {
        if (primaryKeyAnnotation.getElements().size() == 0) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_PRIMARY_KEY + " annotation " + "contains " + primaryKeyAnnotation.getElements().size() + " element, at '" + tableDefinition.getId() + "'");
        }
        primaryKeyReferenceHolders = primaryKeyAnnotation.getElements().stream().map(element -> element.getValue().trim()).map(key -> new PrimaryKeyReferenceHolder(key, tableDefinition.getAttributePosition(key))).toArray(PrimaryKeyReferenceHolder[]::new);
    }
    for (Annotation indexAnnotation : AnnotationHelper.getAnnotations(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations())) {
        if (indexAnnotation.getElements().size() == 0) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of " + "in-memory table should contain only one index element, but found " + indexAnnotation.getElements().size() + " element", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
        } else if (indexAnnotation.getElements().size() > 1) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of the " + "in-memory table should only contain one index element but found " + indexAnnotation.getElements().size() + " elements. To use multiple indexes, " + "define multiple '@index(<index key>)' annotations with one index element " + "per each index key", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
        }
        for (Element element : indexAnnotation.getElements()) {
            Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
            if (previousValue != null) {
                throw new SiddhiAppValidationException("Multiple " + SiddhiConstants.ANNOTATION_INDEX + " " + "annotations defined with same attribute '" + element.getValue().trim() + "', at '" + tableDefinition.getId() + "'", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
            }
        }
    }
    // not support indexBy.
    Annotation indexByAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX_BY, tableDefinition.getAnnotations());
    if (indexByAnnotation != null) {
        throw new OperationNotSupportedException(SiddhiConstants.ANNOTATION_INDEX_BY + " annotation is not " + "supported anymore, please use @PrimaryKey or @Index annotations instead," + " at '" + tableDefinition.getId() + "'");
    }
    if (primaryKeyReferenceHolders != null || indexMetaData.size() > 0) {
        boolean isNumeric = false;
        if (primaryKeyReferenceHolders != null) {
            if (primaryKeyReferenceHolders.length == 1) {
                Attribute.Type type = tableDefinition.getAttributeType(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute());
                if (type == Attribute.Type.DOUBLE || type == Attribute.Type.FLOAT || type == Attribute.Type.INT || type == Attribute.Type.LONG) {
                    isNumeric = true;
                }
            }
        }
        if (isCacheTable) {
            return new IndexEventHolderForCache(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
        } else {
            return new IndexEventHolder(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
        }
    } else {
        MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
        for (Attribute attribute : tableDefinition.getAttributeList()) {
            metaStreamEvent.addOutputData(attribute);
        }
        StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, tableStreamEventFactory);
        return new ListEventHolder(tableStreamEventFactory, eventConverter, new StreamEventClonerHolder(streamEventCloner));
    }
}
Also used : StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) AnnotationHelper(io.siddhi.query.api.util.AnnotationHelper) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) PrimaryKeyReferenceHolder(io.siddhi.core.table.holder.PrimaryKeyReferenceHolder) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) Attribute(io.siddhi.query.api.definition.Attribute) Annotation(io.siddhi.query.api.annotation.Annotation) HashMap(java.util.HashMap) IndexEventHolderForCache(io.siddhi.core.table.holder.IndexEventHolderForCache) Element(io.siddhi.query.api.annotation.Element) IndexEventHolder(io.siddhi.core.table.holder.IndexEventHolder) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) ListEventHolder(io.siddhi.core.table.holder.ListEventHolder) Logger(org.apache.logging.log4j.Logger) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) Map(java.util.Map) EventHolder(io.siddhi.core.table.holder.EventHolder) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) LogManager(org.apache.logging.log4j.LogManager) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) IndexEventHolder(io.siddhi.core.table.holder.IndexEventHolder) HashMap(java.util.HashMap) Attribute(io.siddhi.query.api.definition.Attribute) Element(io.siddhi.query.api.annotation.Element) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) PrimaryKeyReferenceHolder(io.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Annotation(io.siddhi.query.api.annotation.Annotation) StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) ListEventHolder(io.siddhi.core.table.holder.ListEventHolder) IndexEventHolderForCache(io.siddhi.core.table.holder.IndexEventHolderForCache) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

PrimaryKeyReferenceHolder (io.siddhi.core.table.holder.PrimaryKeyReferenceHolder)2 Map (java.util.Map)2 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)1 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)1 StreamEvent (io.siddhi.core.event.stream.StreamEvent)1 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)1 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)1 ZeroStreamEventConverter (io.siddhi.core.event.stream.converter.ZeroStreamEventConverter)1 StreamEventClonerHolder (io.siddhi.core.event.stream.holder.StreamEventClonerHolder)1 OperationNotSupportedException (io.siddhi.core.exception.OperationNotSupportedException)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 EventHolder (io.siddhi.core.table.holder.EventHolder)1 IndexEventHolder (io.siddhi.core.table.holder.IndexEventHolder)1 IndexEventHolderForCache (io.siddhi.core.table.holder.IndexEventHolderForCache)1 ListEventHolder (io.siddhi.core.table.holder.ListEventHolder)1 SiddhiConstants (io.siddhi.core.util.SiddhiConstants)1 Annotation (io.siddhi.query.api.annotation.Annotation)1 Element (io.siddhi.query.api.annotation.Element)1 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)1