From 08f47ef635ac8756d0fa6366a69905252a557f9c Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Tue, 10 Dec 2024 15:33:55 -0800 Subject: [PATCH] Switch SforceDynaBean from Row to TableRow for reducing memory footprint Switch SforceDynaBean from Row to TableRow for reducing memory footprint --- .../dataloader/dyna/SforceDynaBean.java | 17 ++-- .../com/salesforce/dataloader/model/Row.java | 5 -- .../dataloader/model/RowInterface.java | 2 +- .../dataloader/model/TableHeader.java | 8 ++ .../salesforce/dataloader/model/TableRow.java | 86 ++++++++++++++++--- 5 files changed, 92 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/dyna/SforceDynaBean.java b/src/main/java/com/salesforce/dataloader/dyna/SforceDynaBean.java index 5f31d074..124e3d3e 100644 --- a/src/main/java/com/salesforce/dataloader/dyna/SforceDynaBean.java +++ b/src/main/java/com/salesforce/dataloader/dyna/SforceDynaBean.java @@ -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; @@ -225,7 +226,7 @@ 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; @@ -233,7 +234,7 @@ static public DynaBean convertToDynaBean(BasicDynaClass dynaClass, Row sforceDat 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 @@ -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 @@ -390,7 +386,10 @@ public static Map 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 parentLookupFieldList = new ArrayList(); + 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 parentRESTSObject = getCompositeRESTSObject(controller, idLookupFieldFormatter.getParent().getParentObjectName(), parentDynaBean); diff --git a/src/main/java/com/salesforce/dataloader/model/Row.java b/src/main/java/com/salesforce/dataloader/model/Row.java index c1ba8c16..79b9c884 100644 --- a/src/main/java/com/salesforce/dataloader/model/Row.java +++ b/src/main/java/com/salesforce/dataloader/model/Row.java @@ -140,11 +140,6 @@ public TableRow convertToTableRow(TableHeader header) { return trow; } - @Override - public Object get(String key) { - return get((Object)key); - } - @Override public List getColumnNames() { Set fieldNameSet = this.keySet(); diff --git a/src/main/java/com/salesforce/dataloader/model/RowInterface.java b/src/main/java/com/salesforce/dataloader/model/RowInterface.java index 6ba428ac..05f66d9d 100644 --- a/src/main/java/com/salesforce/dataloader/model/RowInterface.java +++ b/src/main/java/com/salesforce/dataloader/model/RowInterface.java @@ -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 getColumnNames(); } diff --git a/src/main/java/com/salesforce/dataloader/model/TableHeader.java b/src/main/java/com/salesforce/dataloader/model/TableHeader.java index bcb4ff21..a0e1fb7e 100644 --- a/src/main/java/com/salesforce/dataloader/model/TableHeader.java +++ b/src/main/java/com/salesforce/dataloader/model/TableHeader.java @@ -52,4 +52,12 @@ public Integer getColumnPosition(String columnName) { public List getColumns() { return new ArrayList(columns); } + + public void addColumn(String colName) { + this.columns.add(colName); + } + + public void removeColumn(String colName) { + this.columns.remove(colName); + } } \ No newline at end of file diff --git a/src/main/java/com/salesforce/dataloader/model/TableRow.java b/src/main/java/com/salesforce/dataloader/model/TableRow.java index 35e40723..8ab13165 100644 --- a/src/main/java/com/salesforce/dataloader/model/TableRow.java +++ b/src/main/java/com/salesforce/dataloader/model/TableRow.java @@ -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, RowInterface { private TableHeader header; private Object[] cellValues; @@ -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; } @@ -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; } @@ -98,4 +95,71 @@ public int getNonEmptyCellsCount() { public List getColumnNames() { return new ArrayList(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 m) { + // TODO Auto-generated method stub + } + + @Override + public void clear() { + ArrayList headerNames = new ArrayList(); + this.header = new TableHeader(headerNames); + } + + @Override + public Set keySet() { + return new HashSet(this.header.getColumns()); + } + + @Override + public Collection values() { + return Arrays.asList(this.cellValues); + } + + @Override + public Set> entrySet() { + HashSet> rowEntrySet = new HashSet>(); + for (String colName : this.header.getColumns()) { + Map.Entry colEntry = new AbstractMap.SimpleEntry(colName, get(colName)); + rowEntrySet.add(colEntry); + } + return rowEntrySet; + } } \ No newline at end of file