- int processedTransactionCount = 0;
-
- DetectedTransactionOrder transactionOrder = DetectedTransactionOrder.UNKNOWN;
- int orderAccumulator = 0;
- int lastTransactionId = 0;
-
- while (true) {
- throwIfCancelled();
- ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
- throwIfCancelled();
- if (parsedTransaction == null) break;
-
- LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
- if (transaction.getId() > lastTransactionId) orderAccumulator++;
- else orderAccumulator--;
- lastTransactionId = transaction.getId();
- if (transactionOrder == DetectedTransactionOrder.UNKNOWN) {
- if (orderAccumulator > 30) {
- transactionOrder = DetectedTransactionOrder.FILE;
- debug("rtt", String.format(Locale.ENGLISH,
- "Detected native file order after %d transactions (factor %d)",
- processedTransactionCount, orderAccumulator));
- progress.setTotal(Data.transactions.size());
- }
- else if (orderAccumulator < -30) {
- transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL;
- debug("rtt", String.format(Locale.ENGLISH,
- "Detected reverse chronological order after %d transactions (factor %d)",
- processedTransactionCount, orderAccumulator));
- }
- }
-
- if (transaction.existsInDb(db)) {
- profile.markTransactionAsPresent(db, transaction);
- matchedTransactionsCount++;
-
- if ((transactionOrder == DetectedTransactionOrder.REVERSE_CHRONOLOGICAL) &&
- (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT))
- {
- profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
- progress.setTotal(progress.getProgress());
- publishProgress(progress);
- db.setTransactionSuccessful();
- profile.setLastUpdateStamp();
- return true;
- }
- }
- else {
- profile.storeTransaction(db, transaction);
- matchedTransactionsCount = 0;
- progress.setTotal(maxTransactionId);
- }
-
-
- if ((transactionOrder != DetectedTransactionOrder.UNKNOWN) &&
- ((progress.getTotal() == Progress.INDETERMINATE) ||
- (progress.getTotal() < transaction.getId())))
- progress.setTotal(transaction.getId());
-
- progress.setProgress(++processedTransactionCount);
- publishProgress(progress);
- }