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;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -60,7 +60,7 @@ public class Recon implements Serializable, HasFields, Jsonizable {
final public long id;
public Object[] features = new Object[Feature_max];
public List<ReconCandidate> candidates = new LinkedList<ReconCandidate>();
public List<ReconCandidate> candidates;
public Judgment judgment = Judgment.None;
public ReconCandidate match = null;
@ -79,6 +79,20 @@ public class Recon implements Serializable, HasFields, Jsonizable {
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) {
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) {
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)) {
return judgmentToString();
} else if ("matched".equals(name)) {
@ -142,9 +156,11 @@ public class Recon implements Serializable, HasFields, Jsonizable {
match.write(writer, options);
} else {
writer.key("c"); writer.array();
if (candidates != null) {
for (ReconCandidate c : candidates) {
c.write(writer, options);
}
}
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.Column;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.ReconCandidate;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.model.Recon.Judgment;
import com.metaweb.gridworks.model.changes.CellChange;
@ -71,18 +72,21 @@ public class ReconMatchBestCandidatesOperation extends EngineDependentMassCellOp
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
if (cellIndex < row.cells.size()) {
Cell cell = row.cells.get(cellIndex);
if (cell.recon != null && cell.recon.candidates.size() > 0) {
if (cell.recon != null) {
ReconCandidate candidate = cell.recon.getBestCandidate();
if (candidate != null) {
Cell newCell = new Cell(
cell.value,
cell.recon.dup()
);
newCell.recon.match = newCell.recon.candidates.get(0);
newCell.recon.match = candidate;
newCell.recon.judgment = Judgment.Matched;
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange);
}
}
}
return false;
}
}.init(column.getCellIndex(), cellChanges);

View File

@ -362,7 +362,8 @@ public class ReconOperation extends EngineDependentOperation {
Recon recon = new Recon();
try {
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);
if (!result.has("name")) {
continue;
@ -402,7 +403,8 @@ public class ReconOperation extends EngineDependentOperation {
}
}
recon.candidates.add(candidate);
recon.addCandidate(candidate);
count++;
}
} catch (JSONException e) {
e.printStackTrace();