Skip to content

Commit

Permalink
Switch SforceDynaBean from Row to TableRow for reducing memory footprint
Browse files Browse the repository at this point in the history
Switch SforceDynaBean from Row to TableRow for reducing memory footprint
  • Loading branch information
ashitsalesforce committed Dec 10, 2024
1 parent 5ed439e commit 08f47ef
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 26 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/salesforce/dataloader/dyna/SforceDynaBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import com.salesforce.dataloader.model.Row;
import com.salesforce.dataloader.model.RowInterface;
import com.salesforce.dataloader.model.TableHeader;
import com.salesforce.dataloader.model.TableRow;
import com.salesforce.dataloader.util.DateOnlyCalendar;

Expand Down Expand Up @@ -225,15 +226,15 @@ static public BasicDynaClass getDynaBeanInstance(DynaProperty[] dynaProps) {
* @throws ConversionException
* @throws LoadException
*/
static public DynaBean convertToDynaBean(BasicDynaClass dynaClass, Row sforceDataRow)
static public DynaBean convertToDynaBean(BasicDynaClass dynaClass, RowInterface sforceDataRow)
throws ConversionException, LoadException {
//now convert the data types, through our strongly typed bean
DynaBean sforceObj = null;
try {
sforceObj = dynaClass.newInstance();
//This does an automatic conversion of types.
BeanUtils.copyProperties(sforceObj, sforceDataRow);
for (String sforceField : sforceDataRow.keySet()) {
for (String sforceField : sforceDataRow.getColumnNames()) {
Object val = sforceDataRow.get(sforceField);
if (val != null
&& val instanceof String
Expand All @@ -257,11 +258,6 @@ static public DynaBean convertToDynaBean(BasicDynaClass dynaClass, Row sforceDat
throw new LoadException(e);
}
}

static public DynaBean convertToDynaBean(BasicDynaClass dynaClass, TableRow sforceDataRow)
throws ConversionException, LoadException {
return convertToDynaBean(dynaClass, sforceDataRow.convertToRow());
}

/**
* Set all the fields specified in the dynaBean to null on the sObj
Expand Down Expand Up @@ -390,7 +386,10 @@ public static Map<String, Object> getCompositeRESTSObject(Controller controller,
DescribeSObjectResult parentSObjectDescribe = controller.getPartnerClient().describeSObject(idLookupFieldFormatter.getParent().getParentObjectName());
DynaProperty[] parentDynaProps = createDynaProps(parentSObjectDescribe, controller);
BasicDynaClass parentDynaClass = getDynaBeanInstance(parentDynaProps);
Row parentDataRow = new Row();
ArrayList<String> parentLookupFieldList = new ArrayList<String>();
parentLookupFieldList.add(idLookupFieldFormatter.getParentFieldName());
TableHeader header = new TableHeader(parentLookupFieldList);
TableRow parentDataRow = new TableRow(header);
parentDataRow.put(idLookupFieldFormatter.getParentFieldName(), value);
DynaBean parentDynaBean = convertToDynaBean(parentDynaClass, parentDataRow);
Map<String, Object> parentRESTSObject = getCompositeRESTSObject(controller, idLookupFieldFormatter.getParent().getParentObjectName(), parentDynaBean);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/salesforce/dataloader/model/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ public TableRow convertToTableRow(TableHeader header) {
return trow;
}

@Override
public Object get(String key) {
return get((Object)key);
}

@Override
public List<String> getColumnNames() {
Set<String> fieldNameSet = this.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

public interface RowInterface {
public Object put(String key, Object value);
public Object get(String key);
public Object get(Object key);
public List<String> getColumnNames();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ public Integer getColumnPosition(String columnName) {
public List<String> getColumns() {
return new ArrayList<String>(columns);
}

public void addColumn(String colName) {
this.columns.add(colName);
}

public void removeColumn(String colName) {
this.columns.remove(colName);
}
}
86 changes: 75 additions & 11 deletions src/main/java/com/salesforce/dataloader/model/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
*/
package com.salesforce.dataloader.model;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TableRow implements RowInterface {
public class TableRow implements Map<String, Object>, RowInterface {
private TableHeader header;
private Object[] cellValues;

Expand All @@ -43,8 +48,8 @@ public TableRow(TableRow rowToCopy) {
cellValues = Arrays.copyOf(rowToCopy.cellValues, rowToCopy.cellValues.length);
}

public Object get(String key) {
Integer colPos = this.header.getColumnPosition(key);
public Object get(Object key) {
Integer colPos = this.header.getColumnPosition((String)key);
if (colPos == null) {
return null;
}
Expand All @@ -59,14 +64,6 @@ public Object put(String key, Object value) {
return this.cellValues[colPos] = value;
}

public Row convertToRow() {
Row row = new Row();
for (String colName : this.header.getColumns()) {
row.put(colName, cellValues[this.header.getColumnPosition(colName)]);
}
return row;
}

public TableHeader getHeader() {
return this.header;
}
Expand Down Expand Up @@ -98,4 +95,71 @@ public int getNonEmptyCellsCount() {
public List<String> getColumnNames() {
return new ArrayList<String>(this.header.getColumns());
}

@Override
public int size() {
return this.header.getColumns().size();
}

@Override
public boolean isEmpty() {
return size() == 0;
}

@Override
public boolean containsKey(Object key) {
return this.header.getColumns().contains(key);
}

@Override
public boolean containsValue(Object value) {
for (Object val : this.cellValues) {
if (val != null && val.equals(value)) {
return true;
}
}
return false;
}

@Override
public Object remove(Object key) {
Integer colPos = this.header.getColumnPosition((String)key);
if (colPos == null) {
return null;
}
Object value = this.get(key);
this.header.removeColumn((String)key);
return value;
}

@Override
public void putAll(Map<? extends String, ? extends Object> m) {
// TODO Auto-generated method stub
}

@Override
public void clear() {
ArrayList<String> headerNames = new ArrayList<String>();
this.header = new TableHeader(headerNames);
}

@Override
public Set<String> keySet() {
return new HashSet<String>(this.header.getColumns());
}

@Override
public Collection<Object> values() {
return Arrays.asList(this.cellValues);
}

@Override
public Set<Map.Entry<String, Object>> entrySet() {
HashSet<Map.Entry<String, Object>> rowEntrySet = new HashSet<Entry<String, Object>>();
for (String colName : this.header.getColumns()) {
Map.Entry<String, Object> colEntry = new AbstractMap.SimpleEntry<String, Object>(colName, get(colName));
rowEntrySet.add(colEntry);
}
return rowEntrySet;
}
}

0 comments on commit 08f47ef

Please sign in to comment.