use of com.linkedin.databus2.ggParser.XmlStateMachine.TransactionState.PerSourceTransactionalUpdate in project databus by linkedin.
the class DBUpdatesMergeUtils method mergeTransactionData.
/**
* Merge 2 list of DBUpdates and return the merged list. The ordering of
* PerSourceTransactionalUpdates ( in the order of SourceIds) is guaranteed in the
* output list If two events have the same sourceId and same key, the event present in
* newDbUpdates is the winner and will be present in the output
*
* @param newTxnDbUpdates
* : New DbUpdates list to be merged
* @param oldDbUpdates
* : Old DbUpdates list to be merged.
* @return Merged list
*/
public static List<TransactionState.PerSourceTransactionalUpdate> mergeTransactionData(List<TransactionState.PerSourceTransactionalUpdate> newTxnDbUpdates, List<TransactionState.PerSourceTransactionalUpdate> oldDbUpdates) {
if ((null == oldDbUpdates) && (null == newTxnDbUpdates)) {
return null;
}
Map<Integer, TransactionState.PerSourceTransactionalUpdate> combinedTxnDbUpdatesMap = new HashMap<Integer, TransactionState.PerSourceTransactionalUpdate>();
if (null != oldDbUpdates) {
for (TransactionState.PerSourceTransactionalUpdate dbu : oldDbUpdates) {
combinedTxnDbUpdatesMap.put(dbu.getSourceId(), dbu);
}
}
if (null != newTxnDbUpdates) {
for (TransactionState.PerSourceTransactionalUpdate dbu : newTxnDbUpdates) {
TransactionState.PerSourceTransactionalUpdate oldDbu = combinedTxnDbUpdatesMap.get(dbu.getSourceId());
if (null == oldDbu) {
// No old Entry present. We can just copy the newDbUpdate's entry to
// combinedTxnDbbUpdatesMap
combinedTxnDbUpdatesMap.put(dbu.getSourceId(), dbu);
} else {
// Do the merge
Set<DbUpdateState.DBUpdateImage> mergeUpdateImages = mergeDbUpdates(dbu.getDbUpdatesSet(), oldDbu.getDbUpdatesSet());
TransactionState.PerSourceTransactionalUpdate mergedPerSourceTxnUpdate = new TransactionState.PerSourceTransactionalUpdate(dbu.getSourceId(), mergeUpdateImages);
combinedTxnDbUpdatesMap.put(dbu.getSourceId(), mergedPerSourceTxnUpdate);
}
}
}
List<PerSourceTransactionalUpdate> sourceTransactionalUpdates = new ArrayList<PerSourceTransactionalUpdate>(combinedTxnDbUpdatesMap.size());
for (Entry<Integer, PerSourceTransactionalUpdate> entry : combinedTxnDbUpdatesMap.entrySet()) {
sourceTransactionalUpdates.add(entry.getValue());
}
// Sort by source id to make sure the window has event batches sorted by sourceIds
Collections.sort(sourceTransactionalUpdates);
return sourceTransactionalUpdates;
}
use of com.linkedin.databus2.ggParser.XmlStateMachine.TransactionState.PerSourceTransactionalUpdate in project databus by linkedin.
the class TransactionState method onEndElement.
@Override
public void onEndElement(StateMachine stateMachine, XMLStreamReader xmlStreamReader) throws Exception {
_currentStateType = STATETYPE.ENDELEMENT;
if (LOG.isDebugEnabled())
LOG.debug("The current transaction has " + stateMachine.dbUpdateState.getSourceDbUpdatesMap().size() + " DbUpdates");
if (_transactionSuccessCallBack == null) {
throw new DatabusException("No callback specified for the transaction state! Cannot proceed without a callback");
}
long endTransactionLocation = xmlStreamReader.getLocation().getCharacterOffset();
_transactionSize = endTransactionLocation - _startTransLocation;
// collect stats
long trTime = System.nanoTime() - _startTransProcessingTimeNs;
long scn = stateMachine.dbUpdateState.getScn();
TransactionInfo trInfo = new TransactionInfo(_transactionSize, trTime, _currentTimeStamp, scn);
if (stateMachine.dbUpdateState.getSourceDbUpdatesMap().size() == 0) {
if (LOG.isDebugEnabled())
LOG.debug("The current transaction contains no dbUpdates, giving empty callback");
_transactionSuccessCallBack.onTransactionEnd(null, trInfo);
} else {
List<PerSourceTransactionalUpdate> dbUpdates = sortDbUpdates(stateMachine.dbUpdateState.getSourceDbUpdatesMap());
_transactionSuccessCallBack.onTransactionEnd(dbUpdates, trInfo);
}
stateMachine.dbUpdateState.cleanUpState(stateMachine, xmlStreamReader);
cleanUpState(stateMachine, xmlStreamReader);
xmlStreamReader.nextTag();
setNextStateProcessor(stateMachine, xmlStreamReader);
}
Aggregations