Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

increase TableRow direct use by replacing readRowList() with readTabl… #1391

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
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 @@ -46,7 +46,7 @@ public AbstractDataReaderImpl(AppConfig appConfig) {

public List<TableRow> readTableRowList(int maxRows) throws DataAccessObjectException {
List<TableRow> rowList = null;
if (this.rowCache.size() >= this.currentRowNumber + maxRows) {
if (this.rowCache.size() > this.currentRowNumber + maxRows) {
rowList = this.rowCache.getRows(currentRowNumber, maxRows);
currentRowNumber = currentRowNumber + rowList.size();
return rowList;
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/salesforce/dataloader/dao/DataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public interface DataReader extends DataAccessObject {
*/
TableRow readTableRow() throws DataAccessObjectException;

/**
* Get a list of rows of data from a data source
*
* @param maxRows Maximum number of rows to read in one call
* @return a list of up to maxRows {@link Row} objects, each of them containing all the keys and values of a row
* @throws DataAccessObjectException
*/
List<Row> readRowList(int maxRows) throws DataAccessObjectException;

/**
* Get a list of rows of data from a data source
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/salesforce/dataloader/dao/DataWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.DataAccessObjectInitializationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;

/**
* Interface to be implemented for data writers -- data access objects that are used for writing rows of data.
Expand Down Expand Up @@ -63,4 +64,6 @@ public interface DataWriter extends DataAccessObject {
boolean writeRowList(List<Row> inputRowList) throws DataAccessObjectException;

public List<String> getColumnNamesFromRow(Row row) throws DataAccessObjectInitializationException;

public boolean writeTableRowList(List<TableRow> rows) throws DataAccessObjectException;
}
16 changes: 0 additions & 16 deletions src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,6 @@ protected boolean isOpenFlag() {
return this.isOpen;
}

@Override
public List<Row> readRowList(int maxRows) throws DataAccessObjectException {
List<Row> outputRows = new ArrayList<Row>();
for (int i = 0; i < maxRows; i++) {
Row outputRow = readRow();
if (outputRow != null) {
// if row has been returned, add it to the output
outputRows.add(outputRow);
} else {
// if encountered null, the reading is over
break;
}
}
return outputRows;
}

@Override
protected List<TableRow> readTableRowListFromDAO(int maxRows) throws DataAccessObjectException {
List<TableRow> outputRows = new ArrayList<TableRow>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.DataAccessObjectInitializationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import com.salesforce.dataloader.util.AppUtil;

/**
Expand Down Expand Up @@ -211,7 +212,21 @@ public boolean writeRowList(List<Row> rows) throws DataAccessObjectException {
}
return success;
}


/*
* (non-Javadoc)
* @see com.salesforce.dataloader.dao.csv.Writer#writeRowList(java.util.List)
*/
@Override
public boolean writeTableRowList(List<TableRow> rows) throws DataAccessObjectException {
boolean success = true;
// return the last result, should be same as others
for (TableRow trow : rows) {
success = writeRow(trow.convertToRow());
}
return success;
}

private void visitHeaderColumns(List<String> columnNames, CSVColumnVisitor visitor) throws IOException {
for (String colName : columnNames) {
String outColName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,10 @@ private void setupQuery(Map<String,Object> params) throws DataAccessObjectInitia
throw new DataAccessObjectInitializationException(errMsg, sqe);
}
}

/*
* (non-Javadoc)
* @see com.salesforce.dataloader.dao.DataReader#readRowList(int)
*/
@Override
public List<Row> readRowList(int maxRows) throws DataAccessObjectException {
List<Row> outputRows = new ArrayList<Row>();
for(int i=0; i < maxRows; i++) {
Row outputRow = readRow();
if(outputRow != null) {
// if row has been returned, add it to the output
outputRows.add(outputRow);
} else {
// if encountered null, the reading is over
break;
}
}
return outputRows;
}

/*
* (non-Javadoc)
* @see com.salesforce.dataloader.dao.DataReader#readRowList(int)
* @see com.salesforce.dataloader.dao.DataReader#readTableRowList(int)
*/
@Override
protected List<TableRow> readTableRowListFromDAO(int maxRows) throws DataAccessObjectException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.*;

import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.logging.log4j.Logger;
import com.salesforce.dataloader.util.DLLogManager;
Expand Down Expand Up @@ -119,7 +120,7 @@ private void setupUpdate() throws DataAccessObjectInitializationException {
dbContext.replaceSqlParams(sqlConfig.getSqlString());
dbContext.prepareStatement();
}

/*
* (non-Javadoc)
* @see com.salesforce.dataloader.dao.DataWriter#writeRowList(java.util.List)
Expand Down Expand Up @@ -285,4 +286,16 @@ public void setColumnNames(List<String> columnNames) {
public List<String> getColumnNamesFromRow(Row row) throws DataAccessObjectInitializationException {
return null;
}

@Override
public boolean writeTableRowList(List<TableRow> trowList) throws DataAccessObjectException {
if (trowList == null) {
return false;
}
ArrayList<Row> rowList = new ArrayList<Row>(trowList.size());
for (TableRow trow : trowList) {
rowList.add(trow.convertToRow());
}
return writeRowList(rowList);
}
}
17 changes: 16 additions & 1 deletion src/main/java/com/salesforce/dataloader/model/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
package com.salesforce.dataloader.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;

public class TableRow {
private TableHeader header;
Expand All @@ -36,6 +36,11 @@ public TableRow(TableHeader header) {
this.header = header;
cellValues = new Object[header.getColumns().size()];
}

public TableRow(TableRow rowToCopy) {
this.header = rowToCopy.getHeader();
cellValues = Arrays.copyOf(rowToCopy.cellValues, rowToCopy.cellValues.length);
}

public Object get(String key) {
Integer colPos = this.header.getColumnPosition(key);
Expand Down Expand Up @@ -77,4 +82,14 @@ public static TableRow singleEntryImmutableRow(String key, Object value) {
row.put(key, value);
return row;
}

public int getNonEmptyCellsCount() {
int numNonEmptyCells = 0;
for (int i = 0; i < this.cellValues.length; i++) {
if (cellValues[i] != null) {
numNonEmptyCells++;
}
}
return numNonEmptyCells;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import com.salesforce.dataloader.util.AccountRowComparator;
import org.apache.logging.log4j.Logger;
import com.salesforce.dataloader.util.DLLogManager;
Expand Down Expand Up @@ -140,7 +141,7 @@ private static void verifyDbInsertOrUpdate(Controller theController, boolean isI
reader = new DatabaseReader(theController.getAppConfig(), "queryAccountAll");
reader.open();
int readBatchSize = 1000;
List<Row> readRowList = reader.readRowList(readBatchSize);
List<TableRow> readRowList = reader.readTableRowList(readBatchSize);
int rowsProcessed = 0;
assertNotNull("Error reading " + readBatchSize + " rows", readRowList);
while(readRowList.size() > 0) {
Expand All @@ -149,18 +150,18 @@ private static void verifyDbInsertOrUpdate(Controller theController, boolean isI
Collections.sort(readRowList, new AccountRowComparator(!isInsert));
logger.info("Verifying database success for next " + (rowsProcessed + readRowList.size()) + " rows");
for (int i=0; i < readRowList.size(); i++) {
Row readRow = readRowList.get(i);
TableRow readRow = readRowList.get(i);
assertNotNull("Error reading data row #" + i + ": the row shouldn't be null", readRow);
assertTrue("Error reading data row #" + i + ": the row shouldn't be empty", readRow.size() > 0);
assertTrue("Error reading data row #" + i + ": the row shouldn't be empty", readRow.getNonEmptyCellsCount() > 0);
Row expectedRow = DatabaseTestUtil.getInsertOrUpdateAccountRow(isInsert, rowsProcessed, DatabaseTestUtil.DateType.VALIDATION);
// verify all expected data
for (String colName : VALIDATE_COLS) {
verifyCol(colName, readRow, expectedRow);
verifyCol(colName, readRow.convertToRow(), expectedRow);
}

rowsProcessed++;
}
readRowList = reader.readRowList(readBatchSize);
readRowList = reader.readTableRowList(readBatchSize);
assertNotNull("Error reading " + readBatchSize + " rows", readRowList);
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.ProcessInitializationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import com.sforce.soap.partner.GetUserInfoResult;
import com.sforce.soap.partner.SaveResult;
import com.sforce.soap.partner.sobject.SObject;
Expand Down Expand Up @@ -322,7 +323,7 @@ private void testBinaryDataInRTFQueryResult(String inclueRTFBinaryData) throws E
final CSVFileReader successRdr = new CSVFileReader(new File(fileName), getController().getAppConfig(), true, false);
String idFieldName = this.isBulkV2APIEnabled(insertArgMap)?"sf__Id":"ID";
try {
for (Row row : successRdr.readRowList(Integer.MAX_VALUE)) {
for (TableRow row : successRdr.readTableRowList(Integer.MAX_VALUE)) {
final String rowId = (String) row.get(idFieldName);
if (rowId != null) {
ids.add(rowId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.DataAccessObjectInitializationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -172,7 +174,7 @@ public InvalidIdTemplateListener(int numValidAccounts) {
}

@Override
public void updateRow(int idx, Row row) {
public void updateRow(int idx, TableRow row) {
if (idx == 0)
row.put("ID", "abcde0123456789XYZ");
else
Expand Down Expand Up @@ -237,7 +239,7 @@ public HeterogeneousIdTemplateListener(int numAccounts, int numContacts) {
}

@Override
public void updateRow(int idx, Row row) {
public void updateRow(int idx, TableRow row) {
if (idx < this.contactIds.length)
row.put("ID", this.contactIds[idx]);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.salesforce.dataloader.exception.ProcessInitializationException;
import com.salesforce.dataloader.exception.UnsupportedOperationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.SaveResult;
import com.sforce.soap.partner.sobject.SObject;
Expand Down Expand Up @@ -181,7 +182,7 @@ public AttachmentTemplateListener() {
}

@Override
public void updateRow(int idx, Row row) {
public void updateRow(int idx, TableRow row) {
// set parent account id
row.put("ParentId", getAccountIds()[0]);
// make body pathname absolute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.ProcessInitializationException;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;
import com.salesforce.dataloader.util.AppUtil;
import com.sforce.soap.partner.sobject.SObject;

Expand Down Expand Up @@ -367,7 +368,7 @@ private SObject[] retrieveAccounts(Controller resultController, String... accoun
final CSVFileReader successRdr = new CSVFileReader(new File(fileName), getController().getAppConfig(), true, false);
try {
// TODO: revise the use of Integer.MAX_VALUE
for (Row row : successRdr.readRowList(Integer.MAX_VALUE)) {
for (TableRow row : successRdr.readTableRowList(Integer.MAX_VALUE)) {
final String rowId = (String) row.get("ID");
if (rowId != null) {
ids.add(rowId);
Expand Down Expand Up @@ -446,7 +447,7 @@ public AttachmentTemplateListener() {
}

@Override
public void updateRow(int idx, Row row) {
public void updateRow(int idx, TableRow row) {
// set parent account id
row.put("ParentId", getAccountIds()[0]);
// make body pathname absolute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.salesforce.dataloader.dao.database.DatabaseReader;
import com.salesforce.dataloader.dao.database.DatabaseTestUtil;
import com.salesforce.dataloader.exception.*;
import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.TableRow;

/**
* Automated tests for dataloader database batch interface
Expand Down Expand Up @@ -204,14 +204,14 @@ private void verifyDbSuccess(Controller theController, String dbConfigName, int
reader = new DatabaseReader(theController.getAppConfig(), dbConfigName);
reader.open();
int readBatchSize = theController.getAppConfig().getInt(AppConfig.PROP_DAO_READ_BATCH_SIZE);
List<Row> successRows = reader.readRowList(readBatchSize);
List<TableRow> successRows = reader.readTableRowList(readBatchSize);
int rowsProcessed = 0;
assertNotNull("Error reading " + readBatchSize + " rows", successRows);
while(successRows.size() > 0) {
rowsProcessed += successRows.size();
logger.info("Verifying database success for next " + successRows.size() + " of total " + rowsProcessed + " rows");
assertTrue("No updated rows have been found in the database.", successRows.size() > 0);
successRows = reader.readRowList(readBatchSize);
successRows = reader.readTableRowList(readBatchSize);
}
assertEquals(expectedSuccesses, rowsProcessed);
} catch (DataAccessObjectInitializationException e) {
Expand Down
Loading
Loading