return;
}
// there is a single callback and a single context, if they match we are done
if (agentInstances.size() == 1 && callbacks.size() == 1) {
AgentInstance agentInstance = agentInstances.get(0);
if (agentInstance.getAgentInstanceContext().getStatementId().equals(callbacks.getFirst().getStatementId())) {
process(agentInstance, servicesContext, callbacks, theEvent);
}
return;
}
// use the right sorted/unsorted Map keyed by AgentInstance to sort
boolean isPrioritized = servicesContext.getConfigSnapshot().getEngineDefaults().getExecution().isPrioritized();
Map<AgentInstance, Object> stmtCallbacks;
if (!isPrioritized) {
stmtCallbacks = new HashMap<AgentInstance, Object>();
}
else {
stmtCallbacks = new TreeMap<AgentInstance, Object>(AgentInstanceComparator.INSTANCE);
}
// process all callbacks
for (FilterHandle filterHandle : callbacks)
{
// determine if this filter entry applies to any of the affected agent instances
String statementId = filterHandle.getStatementId();
AgentInstance agentInstanceFound = null;
for (AgentInstance agentInstance : agentInstances) {
if (agentInstance.getAgentInstanceContext().getStatementId().equals(statementId)) {
agentInstanceFound = agentInstance;
break;
}
}
if (agentInstanceFound == null) { // when the callback is for some other stmt
continue;
}
EPStatementHandleCallback handleCallback = (EPStatementHandleCallback) filterHandle;
EPStatementAgentInstanceHandle handle = handleCallback.getAgentInstanceHandle();
// Self-joins require that the internal dispatch happens after all streams are evaluated.
// Priority or preemptive settings also require special ordering.
if (handle.isCanSelfJoin() || isPrioritized)
{
Object stmtCallback = stmtCallbacks.get(agentInstanceFound);
if (stmtCallback == null) {
stmtCallbacks.put(agentInstanceFound, handleCallback);
}
else if (stmtCallback instanceof ArrayDeque) {
ArrayDeque<EPStatementHandleCallback> q = (ArrayDeque<EPStatementHandleCallback>) stmtCallback;
q.add(handleCallback);
}
else {
ArrayDeque<EPStatementHandleCallback> q = new ArrayDeque<EPStatementHandleCallback>(4);
q.add((EPStatementHandleCallback) stmtCallback);
q.add(handleCallback);
stmtCallbacks.put(agentInstanceFound, q);
}
continue;
}
// no need to be sorted, process
process(agentInstanceFound, servicesContext, Collections.<FilterHandle>singletonList(handleCallback), theEvent);
}
if (stmtCallbacks.isEmpty()) {
return;
}
// Process self-join or sorted prioritized callbacks
for (Map.Entry<AgentInstance, Object> entry : stmtCallbacks.entrySet())
{
AgentInstance agentInstance = entry.getKey();
Object callbackList = entry.getValue();
if (callbackList instanceof ArrayDeque) {
process(agentInstance, servicesContext, (Collection<FilterHandle>) callbackList, theEvent);
}
else {
process(agentInstance, servicesContext, Collections.<FilterHandle>singletonList((FilterHandle) callbackList), theEvent);
}
if (agentInstance.getAgentInstanceContext().getEpStatementAgentInstanceHandle().isPreemptive()) {
return;
}
}
}