Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix slow CSV exports #845

Merged
merged 2 commits into from
Nov 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,33 @@ public List<String> generateExport() throws ExporterException {
* Write splits to CSV format
* @param splits Splits to be written
*/
private void writeSplitsToCsv(@NonNull List<Split> splits, @NonNull CsvWriter writer) throws IOException {
private void writeSplitsToCsv(@NonNull List<Split> splits, @NonNull CsvWriter writer,
Map<String, String> accountNames, Map<String, String> accountFullNames) throws IOException {
int index = 0;

Map<String, Account> uidAccountMap = new HashMap<>();

for (Split split : splits) {
if (index++ > 0){ // the first split is on the same line as the transactions. But after that, we
writer.write("" + mCsvSeparator + mCsvSeparator + mCsvSeparator + mCsvSeparator
+ mCsvSeparator + mCsvSeparator + mCsvSeparator + mCsvSeparator);
}
writer.writeToken(split.getMemo());

//cache accounts so that we do not have to go to the DB each time
String accountUID = split.getAccountUID();
Account account;
if (uidAccountMap.containsKey(accountUID)) {
account = uidAccountMap.get(accountUID);

// Cache account names
String fullName, name;
if (accountNames.containsKey(accountUID)) {
fullName = accountFullNames.get(accountUID);
name = accountNames.get(accountUID);
} else {
account = mAccountsDbAdapter.getRecord(accountUID);
uidAccountMap.put(accountUID, account);
fullName = mAccountsDbAdapter.getAccountFullName(accountUID);
name = mAccountsDbAdapter.getAccountName(accountUID);
accountFullNames.put(accountUID, fullName);
accountNames.put(accountUID, name);
}

writer.writeToken(account.getFullName());
writer.writeToken(account.getName());
writer.writeToken(fullName);
writer.writeToken(name);

String sign = split.getType() == TransactionType.CREDIT ? "-" : "";
writer.writeToken(sign + split.getQuantity().formattedString());
Expand All @@ -143,6 +146,8 @@ private void generateExport(final CsvWriter csvWriter) throws ExporterException
}
csvWriter.newLine();

Map<String, String> nameCache = new HashMap<>();
Map<String, String> fullNameCache = new HashMap<>();

Cursor cursor = mTransactionsDbAdapter.fetchTransactionsModifiedSince(mExportParams.getExportStartTime());
Log.d(LOG_TAG, String.format("Exporting %d transactions to CSV", cursor.getCount()));
Expand All @@ -159,7 +164,7 @@ private void generateExport(final CsvWriter csvWriter) throws ExporterException
csvWriter.writeToken("CURRENCY::" + transaction.getCurrencyCode());
csvWriter.writeToken(null); // Void Reason
csvWriter.writeToken(null); // Action
writeSplitsToCsv(transaction.getSplits(), csvWriter);
writeSplitsToCsv(transaction.getSplits(), csvWriter, nameCache, fullNameCache);
}

PreferencesHelper.setLastExportTime(TimestampHelper.getTimestampFromNow());
Expand Down