From 701780c7df2769619c305e9c63489bc6229dd122 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Thu, 28 Nov 2024 10:30:18 -0800 Subject: [PATCH] do not re-read from the same CSV or database during an operation if caching is enabled do not re-read from the same CSV or database during an operation if caching is enabled --- .../dataloader/dao/DAORowCache.java | 15 ++++++++---- .../dataloader/dao/csv/CSVFileReader.java | 8 ++++--- .../dao/database/DatabaseReader.java | 23 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/dao/DAORowCache.java b/src/main/java/com/salesforce/dataloader/dao/DAORowCache.java index 9ee30cea..9415438a 100644 --- a/src/main/java/com/salesforce/dataloader/dao/DAORowCache.java +++ b/src/main/java/com/salesforce/dataloader/dao/DAORowCache.java @@ -33,7 +33,7 @@ public class DAORowCache { private ArrayList rowList = new ArrayList(); private int currentRowIndex = 0; - private int totalRows = 0; + private int cachedRows = 0; public DAORowCache() { } @@ -44,7 +44,7 @@ public void resetCurrentRowIndex() { public TableRow getCurrentRow() { AppConfig appConfig = AppConfig.getCurrentConfig(); - if (currentRowIndex >= totalRows + if (currentRowIndex >= cachedRows || !appConfig.getBoolean(AppConfig.PROP_PROCESS_BULK_CACHE_DATA_FROM_DAO)) { return null; } @@ -52,8 +52,15 @@ public TableRow getCurrentRow() { } public void addRow(TableRow row) { - rowList.add(row); + // add a row to the cache only if it is not cached already + if (currentRowIndex >= cachedRows) { + rowList.add(row); + cachedRows++; + } currentRowIndex++; - totalRows++; + } + + public int getCachedRows() { + return cachedRows; } } \ No newline at end of file diff --git a/src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java b/src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java index ecb3105a..d3d04a67 100644 --- a/src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java +++ b/src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java @@ -127,11 +127,13 @@ public void open() throws DataAccessObjectInitializationException { if (isOpen) { close(); } + if (!appConfig.getBoolean(AppConfig.PROP_PROCESS_BULK_CACHE_DATA_FROM_DAO) + || rowCache.getCachedRows() == 0) { + initalizeInput(csvDelimiters); + readHeaderRow(); + } currentRowNumber = 0; rowCache.resetCurrentRowIndex(); - - initalizeInput(csvDelimiters); - readHeaderRow(); isOpen = true; } diff --git a/src/main/java/com/salesforce/dataloader/dao/database/DatabaseReader.java b/src/main/java/com/salesforce/dataloader/dao/database/DatabaseReader.java index fbacb6de..fe9cfb43 100644 --- a/src/main/java/com/salesforce/dataloader/dao/database/DatabaseReader.java +++ b/src/main/java/com/salesforce/dataloader/dao/database/DatabaseReader.java @@ -115,15 +115,22 @@ public void open() throws DataAccessObjectInitializationException { * @param params * @throws DataAccessObjectInitializationException */ - public void open(Map params) throws DataAccessObjectInitializationException { - currentRowNumber = 0; - try { - setupQuery(params); - } catch (DataAccessObjectInitializationException e) { - throw e; - } catch (Exception e) { - throw new DataAccessObjectInitializationException(e.getMessage(), e); + private void open(Map params) throws DataAccessObjectInitializationException { + if (dbContext.isOpen()) { + close(); } + if (!appConfig.getBoolean(AppConfig.PROP_PROCESS_BULK_CACHE_DATA_FROM_DAO) + || rowCache.getCachedRows() == 0) { + try { + setupQuery(params); + } catch (DataAccessObjectInitializationException e) { + throw e; + } catch (Exception e) { + throw new DataAccessObjectInitializationException(e.getMessage(), e); + } + } + currentRowNumber = 0; + rowCache.resetCurrentRowIndex(); dbContext.setOpen(true); }