if (allContexts != null) {
planContexts = new ArrayList<VariableContext>(allContexts.size());
}
for (int commandIndex = 0; commandIndex < numCommands; commandIndex++) {
// Potentially the first command of a batch
Command updateCommand = (Command)updateCommands.get(commandIndex);
boolean commandWasBatched = false;
// If this command can be placed in a batch
if (isEligibleForBatching(updateCommand, metadata)) {
// Get the model ID. Subsequent and contiguous commands that update a group in this model are candidates for this batch
Object batchModelID = metadata.getModelID(getUpdatedGroup(updateCommand).getMetadataID());
String modelName = metadata.getFullName(batchModelID);
SourceCapabilities caps = capFinder.findCapabilities(modelName);
// Only attempt batching if the source supports batching
if (caps.supportsCapability(Capability.BATCHED_UPDATES)) {
// Start a new batch
List<Command> batch = new ArrayList<Command>();
List<VariableContext> contexts = new ArrayList<VariableContext>();
List<Boolean> shouldEvaluate = new ArrayList<Boolean>();
// This is the first command in a potential batch, so add it to the batch
batch.add(updateCommand);
if (allContexts != null) {
contexts.add(allContexts.get(commandIndex));
shouldEvaluate.add(Boolean.TRUE);
} else {
shouldEvaluate.add(EvaluatableVisitor.needsProcessingEvaluation(updateCommand));
}
// Find out if there are other commands called on the same physical model
// immediately and contiguously after this one
batchLoop: for (int batchIndex = commandIndex+1; batchIndex < numCommands; batchIndex++) {
Command batchingCandidate = (Command)updateCommands.get(batchIndex);
// If this command updates the same model, and is eligible for batching, add it to the batch
if (canBeAddedToBatch(batchingCandidate, batchModelID, metadata, capFinder)) {
batch.add(batchingCandidate);
if (allContexts != null) {
contexts.add(allContexts.get(batchIndex));
shouldEvaluate.add(Boolean.TRUE);
} else {
shouldEvaluate.add(EvaluatableVisitor.needsProcessingEvaluation(batchingCandidate));
}
} else { // Otherwise, stop batching at this point. The next command may well be the start of a new batch
break batchLoop;
}
}
// If two or more contiguous commands made on the same model were found, then batch them
if (batch.size() > 1) {
ProjectNode projectNode = new ProjectNode(((IntegerID)idGenerator.create()).getValue());
// Create a BatchedUpdateNode that creates a batched request for the connector
BatchedUpdateNode batchNode = new BatchedUpdateNode(((IntegerID)idGenerator.create()).getValue(),
batch, contexts, shouldEvaluate,
modelName);
List symbols = batchedUpdateCommand.getProjectedSymbols();
projectNode.setSelectSymbols(symbols);
projectNode.setElements(symbols);
batchNode.setElements(symbols);
projectNode.addChild(batchNode);
// Add a new RelationalPlan that represents the plan for this batch.
childPlans.add(new RelationalPlan(projectNode));
if (planContexts != null) {
planContexts.add(new VariableContext());
}
// Skip those commands that were added to this batch
commandIndex += batch.size() - 1;
commandWasBatched = true;
}
}
}
if (!commandWasBatched) { // If the command wasn't batched, just add the plan for this command to the list of plans
Command cmd = (Command)batchedUpdateCommand.getUpdateCommands().get(commandIndex);
ProcessorPlan plan = cmd.getProcessorPlan();
if (plan == null) {
plan = QueryOptimizer.optimizePlan(cmd, metadata, idGenerator, capFinder, analysisRecord, context);
}
childPlans.add(plan);
if (allContexts != null) {