Search in sources :

Example 1 with PrimaryKeyReferenceHolder

use of org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder in project ballerina by ballerina-lang.

the class EventHolderPasser method parse.

public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventPool tableStreamEventPool, SiddhiAppContext siddhiAppContext) {
    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);
    }
    // indexes.
    Annotation indexAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations());
    if (indexAnnotation != null) {
        if (indexAnnotation.getElements().size() == 0) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation contains " + indexAnnotation.getElements().size() + " element");
        }
        for (Element element : indexAnnotation.getElements()) {
            Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
            if (previousValue != null) {
                throw new SiddhiAppCreationException("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;
                }
            }
        }
        return new IndexEventHolder(tableStreamEventPool, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
    } else {
        return new ListEventHolder(tableStreamEventPool, eventConverter);
    }
}
Also used : EventHolder(org.ballerinalang.siddhi.core.table.holder.EventHolder) Logger(org.slf4j.Logger) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) SiddhiConstants(org.ballerinalang.siddhi.core.util.SiddhiConstants) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) SiddhiAppContext(org.ballerinalang.siddhi.core.config.SiddhiAppContext) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) Element(org.ballerinalang.siddhi.query.api.annotation.Element) PrimaryKeyReferenceHolder(org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder) AnnotationHelper(org.ballerinalang.siddhi.query.api.util.AnnotationHelper) OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) Map(java.util.Map) IndexEventHolder(org.ballerinalang.siddhi.core.table.holder.IndexEventHolder) ListEventHolder(org.ballerinalang.siddhi.core.table.holder.ListEventHolder) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) IndexEventHolder(org.ballerinalang.siddhi.core.table.holder.IndexEventHolder) HashMap(java.util.HashMap) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) Element(org.ballerinalang.siddhi.query.api.annotation.Element) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) PrimaryKeyReferenceHolder(org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) ListEventHolder(org.ballerinalang.siddhi.core.table.holder.ListEventHolder)

Example 2 with PrimaryKeyReferenceHolder

use of org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) PrimaryKeyReferenceHolder(org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Map(java.util.Map)

Aggregations

Map (java.util.Map)2 PrimaryKeyReferenceHolder (org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder)2 HashMap (java.util.HashMap)1 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)1 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)1 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)1 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)1 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)1 OperationNotSupportedException (org.ballerinalang.siddhi.core.exception.OperationNotSupportedException)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)1 EventHolder (org.ballerinalang.siddhi.core.table.holder.EventHolder)1 IndexEventHolder (org.ballerinalang.siddhi.core.table.holder.IndexEventHolder)1 ListEventHolder (org.ballerinalang.siddhi.core.table.holder.ListEventHolder)1 SiddhiConstants (org.ballerinalang.siddhi.core.util.SiddhiConstants)1 Annotation (org.ballerinalang.siddhi.query.api.annotation.Annotation)1 Element (org.ballerinalang.siddhi.query.api.annotation.Element)1 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)1 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)1 SiddhiAppValidationException (org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)1