Delay constructing the candidates array in recon objects to save memory.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@124 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-22 23:27:16 +00:00
parent c45e0edc10
commit 0f505c72c5
3 changed files with 40 additions and 18 deletions

View File

@ -1,8 +1,8 @@
package com.metaweb.gridworks.model; package com.metaweb.gridworks.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -12,7 +12,7 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.Jsonizable;
import com.metaweb.gridworks.expr.HasFields; import com.metaweb.gridworks.expr.HasFields;
public class Recon implements Serializable, HasFields, Jsonizable { public class Recon implements Serializable, HasFields, Jsonizable {
private static final long serialVersionUID = 8906257833709315762L; private static final long serialVersionUID = 8906257833709315762L;
@ -60,7 +60,7 @@ public class Recon implements Serializable, HasFields, Jsonizable {
final public long id; final public long id;
public Object[] features = new Object[Feature_max]; public Object[] features = new Object[Feature_max];
public List<ReconCandidate> candidates = new LinkedList<ReconCandidate>(); public List<ReconCandidate> candidates;
public Judgment judgment = Judgment.None; public Judgment judgment = Judgment.None;
public ReconCandidate match = null; public ReconCandidate match = null;
@ -79,6 +79,20 @@ public class Recon implements Serializable, HasFields, Jsonizable {
return r; return r;
} }
public void addCandidate(ReconCandidate candidate) {
if (candidates == null) {
candidates = new ArrayList<ReconCandidate>(3);
}
candidates.add(candidate);
}
public ReconCandidate getBestCandidate() {
if (candidates != null && candidates.size() > 0) {
return candidates.get(0);
}
return null;
}
public Object getFeature(int feature) { public Object getFeature(int feature) {
return feature < features.length ? features[feature] : null; return feature < features.length ? features[feature] : null;
} }
@ -104,7 +118,7 @@ public class Recon implements Serializable, HasFields, Jsonizable {
public Object getField(String name, Properties bindings) { public Object getField(String name, Properties bindings) {
if ("best".equals(name)) { if ("best".equals(name)) {
return candidates.size() > 0 ? candidates.get(0) : null; return candidates != null && candidates.size() > 0 ? candidates.get(0) : null;
} else if ("judgment".equals(name) || "judgement".equals(name)) { } else if ("judgment".equals(name) || "judgement".equals(name)) {
return judgmentToString(); return judgmentToString();
} else if ("matched".equals(name)) { } else if ("matched".equals(name)) {
@ -142,8 +156,10 @@ public class Recon implements Serializable, HasFields, Jsonizable {
match.write(writer, options); match.write(writer, options);
} else { } else {
writer.key("c"); writer.array(); writer.key("c"); writer.array();
for (ReconCandidate c : candidates) { if (candidates != null) {
c.write(writer, options); for (ReconCandidate c : candidates) {
c.write(writer, options);
}
} }
writer.endArray(); writer.endArray();
} }

View File

@ -12,6 +12,7 @@ import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Cell; import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column; import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.model.Recon.Judgment; import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.CellChange;
@ -71,16 +72,19 @@ public class ReconMatchBestCandidatesOperation extends EngineDependentMassCellOp
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
if (cellIndex < row.cells.size()) { if (cellIndex < row.cells.size()) {
Cell cell = row.cells.get(cellIndex); Cell cell = row.cells.get(cellIndex);
if (cell.recon != null && cell.recon.candidates.size() > 0) { if (cell.recon != null) {
Cell newCell = new Cell( ReconCandidate candidate = cell.recon.getBestCandidate();
cell.value, if (candidate != null) {
cell.recon.dup() Cell newCell = new Cell(
); cell.value,
newCell.recon.match = newCell.recon.candidates.get(0); cell.recon.dup()
newCell.recon.judgment = Judgment.Matched; );
newCell.recon.match = candidate;
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); newCell.recon.judgment = Judgment.Matched;
cellChanges.add(cellChange);
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange);
}
} }
} }
return false; return false;

View File

@ -362,7 +362,8 @@ public class ReconOperation extends EngineDependentOperation {
Recon recon = new Recon(); Recon recon = new Recon();
try { try {
int length = results.length(); int length = results.length();
for (int i = 0; i < length && recon.candidates.size() < 3; i++) { int count = 0;
for (int i = 0; i < length && count < 3; i++) {
JSONObject result = results.getJSONObject(i); JSONObject result = results.getJSONObject(i);
if (!result.has("name")) { if (!result.has("name")) {
continue; continue;
@ -402,7 +403,8 @@ public class ReconOperation extends EngineDependentOperation {
} }
} }
recon.candidates.add(candidate); recon.addCandidate(candidate);
count++;
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();