use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class SnapshotableEventQueueTestCase method incrementalPersistenceTest1.
@Test
public void incrementalPersistenceTest1() 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 < 20; 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 = 20; i < 40; i++) {
streamEvent.getOutputData()[2] = i;
snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
}
Snapshot snapshot2 = snapshotableStreamEventQueue.getSnapshot();
ArrayList<Operation> operationLog = (ArrayList<Operation>) snapshot2.getState();
Assert.assertTrue(operationLog != null);
snapshots.put(4L, toString(snapshot2));
for (int i = 40; i < 80; 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);
for (int i = 80; i < 130; i++) {
streamEvent.getOutputData()[2] = i;
snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
}
Snapshot snapshot4 = snapshotableStreamEventQueue.getSnapshot();
streamEvents = (StreamEvent) snapshot4.getState();
Assert.assertTrue(streamEvents != null);
snapshots = new HashMap<>();
snapshots.put(6L, toString(snapshot4));
SnapshotableStreamEventQueue snapshotableStreamEventQueue3 = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
snapshotStateList = new SnapshotStateList();
for (Map.Entry<Long, String> entry : snapshots.entrySet()) {
snapshotStateList.putSnapshotState(entry.getKey(), (Snapshot) fromString(entry.getValue()));
}
snapshotableStreamEventQueue3.restore(snapshotStateList);
snapshotableStreamEventQueue.reset();
Assert.assertEquals(snapshotableStreamEventQueue, snapshotableStreamEventQueue3);
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class OutputParser method constructOutputCallback.
public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, boolean convertToStreamEvent, SiddhiQueryContext siddhiQueryContext) {
String id = outStream.getId();
Table table = null;
Window window = null;
if (id != null) {
table = tableMap.get(id);
window = eventWindowMap.get(id);
}
StreamEventFactory streamEventFactory = null;
StreamEventConverter streamEventConverter = null;
MetaStreamEvent tableMetaStreamEvent = null;
if (table != null) {
tableMetaStreamEvent = new MetaStreamEvent();
tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
TableDefinition matchingTableDefinition = TableDefinition.id("");
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
tableMetaStreamEvent.addOutputData(attribute);
matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
}
matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
streamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
streamEventConverter = new ZeroStreamEventConverter();
}
// Construct CallBack
if (outStream instanceof InsertIntoStream || outStream instanceof ReturnStream) {
if (window != null) {
if (!siddhiQueryContext.isPartitioned()) {
return new InsertIntoWindowCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
} else {
return new InsertIntoWindowEndPartitionCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
}
} else if (table != null) {
DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
} else {
if (!siddhiQueryContext.isPartitioned() || outputStreamDefinition.getId().startsWith("#")) {
return new InsertIntoStreamCallback(outputStreamDefinition, siddhiQueryContext.getName());
} else {
return new InsertIntoStreamEndPartitionCallback(outputStreamDefinition, siddhiQueryContext.getName());
}
}
} else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
if (table != null) {
if (outStream instanceof UpdateStream) {
if (((UpdateStream) outStream).getUpdateSet() == null) {
TableDefinition tableDefinition = table.getTableDefinition();
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
if (!tableDefinition.getAttributeList().contains(attribute)) {
throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
}
}
if (outStream instanceof UpdateOrInsertStream) {
TableDefinition tableDefinition = table.getTableDefinition();
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
if (!tableDefinition.getAttributeList().contains(attribute)) {
throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
}
if (outStream instanceof DeleteStream) {
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getName(), siddhiQueryContext.getSiddhiAppContext().getSiddhiAppString());
}
} else if (outStream instanceof UpdateStream) {
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
if (updateSet == null) {
updateSet = new UpdateSet();
for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
}
}
CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
}
} else {
DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
if (updateSet == null) {
updateSet = new UpdateSet();
for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
}
}
CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
}
}
} else {
throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
} else {
throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class DefinitionParserHelper method getAttributeMappings.
private static AttributesHolder getAttributeMappings(Annotation mapAnnotation, String mapType, StreamDefinition streamDefinition) {
List<Annotation> attributeAnnotations = mapAnnotation.getAnnotations(SiddhiConstants.ANNOTATION_ATTRIBUTES);
DefinitionParserHelper.AttributesHolder attributesHolder = new DefinitionParserHelper.AttributesHolder();
if (attributeAnnotations.size() > 0) {
Map<String, String> elementMap = new HashMap<>();
List<String> elementList = new ArrayList<>();
Boolean attributesNameDefined = null;
for (Element element : attributeAnnotations.get(0).getElements()) {
if (element.getKey() == null) {
if (attributesNameDefined != null && attributesNameDefined) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream'" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
}
attributesNameDefined = false;
elementList.add(element.getValue());
} else {
if (attributesNameDefined != null && !attributesNameDefined) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
}
attributesNameDefined = true;
elementMap.put(element.getKey(), element.getValue());
}
}
if (elementMap.size() > 0) {
List<Attribute> attributeList = streamDefinition.getAttributeList();
for (int i = 0, attributeListSize = attributeList.size(); i < attributeListSize; i++) {
Attribute attribute = attributeList.get(i);
String value = elementMap.get(attribute.getName());
if (value == null) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', attribute '" + attribute.getName() + "' is not mapped.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
}
assignMapping(attributesHolder, elementMap, i, attribute);
}
} else {
List<Attribute> attributeList = streamDefinition.getAttributeList();
if (elementList.size() != attributeList.size()) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', '" + elementList.size() + "' mapping attributes are " + "provided but expected attributes are '" + attributeList.size() + "'.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
} else {
for (int i = 0; i < attributeList.size(); i++) {
Attribute attribute = attributeList.get(i);
String value = elementList.get(i);
elementMap.put(attribute.getName(), value);
}
}
for (int i = 0; i < attributeList.size(); i++) {
Attribute attribute = attributeList.get(i);
assignMapping(attributesHolder, elementMap, i, attribute);
}
}
}
return attributesHolder;
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class DefinitionParserHelper method addTable.
public static void addTable(TableDefinition tableDefinition, ConcurrentMap<String, Table> tableMap, SiddhiAppContext siddhiAppContext) {
if (!tableMap.containsKey(tableDefinition.getId())) {
MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
tableMetaStreamEvent.addInputDefinition(tableDefinition);
for (Attribute attribute : tableDefinition.getAttributeList()) {
tableMetaStreamEvent.addOutputData(attribute);
}
StreamEventFactory tableStreamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
StreamEventCloner tableStreamEventCloner = new StreamEventCloner(tableMetaStreamEvent, tableStreamEventFactory);
Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STORE, tableDefinition.getAnnotations());
Table table;
ConfigReader configReader = null;
RecordTableHandlerManager recordTableHandlerManager = null;
RecordTableHandler recordTableHandler = null;
if (annotation != null) {
annotation = updateAnnotationRef(annotation, SiddhiConstants.NAMESPACE_STORE, siddhiAppContext);
String tableType = annotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
if (tableType == null) {
throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + annotation + "'", annotation, siddhiAppContext);
}
Extension extension = new Extension() {
@Override
public String getNamespace() {
return SiddhiConstants.NAMESPACE_STORE;
}
@Override
public String getName() {
return tableType;
}
};
recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
if (recordTableHandlerManager != null) {
recordTableHandler = recordTableHandlerManager.generateRecordTableHandler();
}
table = (Table) SiddhiClassLoader.loadExtensionImplementation(extension, TableExtensionHolder.getInstance(siddhiAppContext));
configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
} else {
table = new InMemoryTable();
}
table.initTable(tableDefinition, tableStreamEventFactory, tableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
if (recordTableHandler != null) {
recordTableHandlerManager.registerRecordTableHandler(recordTableHandler.getId(), recordTableHandler);
}
tableMap.putIfAbsent(tableDefinition.getId(), table);
}
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class QueryAPITestCaseForTestStore method test12.
@Test
public void test12() {
log.info("Test12 - Test output attributes and its types for table");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
String storeQuery = "" + "from StockTable " + "select * ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
siddhiAppRuntime.start();
Attribute[] actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery(storeQuery));
Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
Attribute volumeAttribute = new Attribute("volume", Attribute.Type.LONG);
Attribute[] expectedAttributeArray = new Attribute[] { symbolAttribute, priceAttribute, volumeAttribute };
AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
storeQuery = "" + "from StockTable " + "select symbol, sum(volume) as totalVolume ;";
actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery(storeQuery));
Attribute totalVolumeAttribute = new Attribute("totalVolume", Attribute.Type.LONG);
expectedAttributeArray = new Attribute[] { symbolAttribute, totalVolumeAttribute };
siddhiAppRuntime.shutdown();
AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
Aggregations