use of org.jumpmind.symmetric.route.IBatchAlgorithm in project symmetric-ds by JumpMind.
the class RouterService method insertDataEvents.
protected int insertDataEvents(ProcessInfo processInfo, ChannelRouterContext context, DataMetaData dataMetaData, Collection<String> nodeIds) {
int numberOfDataEventsInserted = 0;
if (nodeIds == null || nodeIds.size() == 0) {
nodeIds = new HashSet<String>(1);
nodeIds.add(Constants.UNROUTED_NODE_ID);
}
long ts = System.currentTimeMillis();
long batchIdToReuse = -1;
boolean dataEventAdded = false;
for (String nodeId : nodeIds) {
if (nodeId != null) {
Map<String, OutgoingBatch> batches = context.getBatchesByNodes();
OutgoingBatch batch = batches.get(nodeId);
if (batch == null) {
batch = new OutgoingBatch(nodeId, dataMetaData.getNodeChannel().getChannelId(), Status.RT);
batch.setBatchId(batchIdToReuse);
batch.setCommonFlag(context.isProduceCommonBatches());
log.debug("About to insert a new batch for node {} on the '{}' channel. Batches in progress are: {}.", new Object[] { nodeId, batch.getChannelId(), context.getBatchesByNodes().values() });
engine.getOutgoingBatchService().insertOutgoingBatch(batch);
processInfo.incrementBatchCount();
context.getBatchesByNodes().put(nodeId, batch);
// if in reuse mode, then share the batch id
if (context.isProduceCommonBatches()) {
batchIdToReuse = batch.getBatchId();
}
}
if (dataMetaData.getData().getDataEventType() == DataEventType.RELOAD) {
long loadId = context.getLastLoadId();
if (loadId < 0) {
loadId = engine.getSequenceService().nextVal(context.getSqlTransaction(), Constants.SEQUENCE_OUTGOING_BATCH_LOAD_ID);
context.setLastLoadId(loadId);
}
batch.setLoadId(loadId);
} else {
context.setLastLoadId(-1);
}
batch.incrementEventCount(dataMetaData.getData().getDataEventType());
batch.incrementDataEventCount();
batch.incrementTableCount(dataMetaData.getTable().getNameLowerCase());
if (!context.isProduceCommonBatches() || (context.isProduceCommonBatches() && !dataEventAdded)) {
Router router = dataMetaData.getRouter();
context.addDataEvent(dataMetaData.getData().getDataId(), batch.getBatchId(), router != null ? router.getRouterId() : Constants.UNKNOWN_ROUTER_ID);
numberOfDataEventsInserted++;
dataEventAdded = true;
}
Map<String, IBatchAlgorithm> batchAlgorithms = extensionService.getExtensionPointMap(IBatchAlgorithm.class);
if (batchAlgorithms.get(context.getChannel().getBatchAlgorithm()).isBatchComplete(batch, dataMetaData, context)) {
context.setNeedsCommitted(true);
}
}
}
context.incrementStat(System.currentTimeMillis() - ts, ChannelRouterContext.STAT_INSERT_DATA_EVENTS_MS);
return numberOfDataEventsInserted;
}
Aggregations