The file system coupled method getProjectDirectory is now removed from ProjectManager.

Methods of HistoryEntry which directly work with the File System have been moved to FileHistoryEntry in the io directory, and HistoryEntry made abstract.

As the abstract HistoryEntry cannot be instantiated directly, the ProjectManager is now responsible for creating new HistoryEntry.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@973 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-06-15 22:11:35 +00:00
parent b07075bed5
commit 17f1dc2e6f
25 changed files with 658 additions and 591 deletions

View File

@ -1,6 +1,6 @@
package com.metaweb.gridworks;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -8,6 +8,9 @@ import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
@ -44,9 +47,6 @@ public abstract class ProjectManager {
return _interProjectModel;
}
//FIXME this is File System specific, need to remove from this abstract class
public abstract File getProjectDir(long id);
public void registerProject(Project project, ProjectMetadata projectMetadata) {
synchronized (this) {
_projects.put(project.id, project);
@ -119,4 +119,6 @@ public abstract class ProjectManager {
public abstract void deleteProject(long projectID) ;
public abstract HistoryEntry createHistoryEntry(long id, long projectID, String description, AbstractOperation operation, Date time);
public abstract HistoryEntry createHistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change);
}

View File

@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
@ -121,7 +122,7 @@ public class EditOneCellCommand extends Command {
Change change = new CellChange(rowIndex, cellIndex, cell, newCell);
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID, _project, description, null, change);
}
}

View File

@ -15,6 +15,7 @@ import org.apache.tools.tar.TarOutputStream;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.io.FileProjectManager;
import com.metaweb.gridworks.model.Project;
public class ExportProjectCommand extends Command {
@ -31,8 +32,8 @@ public class ExportProjectCommand extends Command {
OutputStream os = response.getOutputStream();
try {
gzipTarToOutputStream(
ProjectManager.singleton.getProjectDir(project.id),
gzipTarToOutputStream(//FIXME relies on FileProjectManager
((FileProjectManager)ProjectManager.singleton).getProjectDir(project.id),
os
);
} finally {

View File

@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.io.FileProjectManager;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.ParsingUtilities;
@ -137,7 +138,7 @@ public class ImportProjectCommand extends Command {
}
protected void internalImportInputStream(long projectID, InputStream inputStream, boolean gziped) throws IOException {
File destDir = ProjectManager.singleton.getProjectDir(projectID);
File destDir = ((FileProjectManager)ProjectManager.singleton).getProjectDir(projectID);//FIXME relies on FileProjectManager
destDir.mkdirs();
if (gziped) {

View File

@ -9,6 +9,7 @@ import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.history.Change;
@ -198,7 +199,7 @@ public class ReconJudgeOneCellCommand extends Command {
stats
);
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID, _project, description, null, change);
}
}

View File

@ -6,6 +6,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Project;
@ -82,7 +83,7 @@ public class AnnotateOneRowCommand extends Command {
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
_project,
(starred ? "Star row " : "Unstar row ") + (rowIndex + 1),
@ -108,7 +109,7 @@ public class AnnotateOneRowCommand extends Command {
}
protected HistoryEntry createHistoryEntry(long historyEntryID) throws Exception {
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
_project,
(flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1),

View File

@ -1,14 +1,8 @@
package com.metaweb.gridworks.history;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Date;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
@ -20,13 +14,12 @@ import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.OperationRegistry;
import com.metaweb.gridworks.util.ParsingUtilities;
import com.metaweb.gridworks.util.Pool;
/**
* This is the metadata of a Change. It's small, so we can load it in order to
* obtain information about a change without actually loading the change.
*/
public class HistoryEntry implements Jsonizable {
public abstract class HistoryEntry implements Jsonizable {
final public long id;
final public long projectID;
final public String description;
@ -105,21 +98,13 @@ public class HistoryEntry implements Jsonizable {
_change.revert(project);
}
public void delete() {
File file = getChangeFile();
if (file.exists()) {
file.delete();
}
}
public abstract void loadChange();
public void save(Writer writer, Properties options) {
JSONWriter jsonWriter = new JSONWriter(writer);
try {
write(jsonWriter, options);
} catch (JSONException e) {
e.printStackTrace();
}
}
protected abstract void saveChange() throws Exception;
public abstract void save(Writer writer, Properties options);
protected abstract void delete();
static public HistoryEntry load(Project project, String s) throws Exception {
JSONObject obj = ParsingUtilities.evaluateJsonStringToObject(s);
@ -129,7 +114,7 @@ public class HistoryEntry implements Jsonizable {
operation = OperationRegistry.reconstruct(project, obj.getJSONObject(OPERATION));
}
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
obj.getLong("id"),
project.id,
obj.getString("description"),
@ -139,71 +124,5 @@ public class HistoryEntry implements Jsonizable {
}
protected void loadChange() {
File changeFile = getChangeFile();
try {
loadChange(changeFile);
} catch (Exception e) {
throw new RuntimeException("Failed to load change file " + changeFile.getAbsolutePath(), e);
}
}
protected void loadChange(File file) throws Exception {
ZipFile zipFile = new ZipFile(file);
try {
Pool pool = new Pool();
ZipEntry poolEntry = zipFile.getEntry("pool.txt");
if (poolEntry != null) {
pool.load(new InputStreamReader(
zipFile.getInputStream(poolEntry)));
} // else, it's a legacy project file
_change = History.readOneChange(
zipFile.getInputStream(zipFile.getEntry("change.txt")), pool);
} finally {
zipFile.close();
}
}
protected void saveChange() throws Exception {
File changeFile = getChangeFile();
if (!(changeFile.exists())) {
saveChange(changeFile);
}
}
protected void saveChange(File file) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
try {
Pool pool = new Pool();
out.putNextEntry(new ZipEntry("change.txt"));
try {
History.writeOneChange(out, _change, pool);
} finally {
out.closeEntry();
}
out.putNextEntry(new ZipEntry("pool.txt"));
try {
pool.save(out);
} finally {
out.closeEntry();
}
} finally {
out.close();
}
}
protected File getChangeFile() {
return new File(getHistoryDir(), id + ".change.zip");
}
protected File getHistoryDir() {
File dir = new File(ProjectManager.singleton.getProjectDir(projectID), "history");
dir.mkdirs();
return dir;
}
}

View File

@ -0,0 +1,116 @@
package com.metaweb.gridworks.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import java.io.Writer;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.History;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.Pool;
public class FileHistoryEntry extends HistoryEntry{
protected FileHistoryEntry(long id, long projectID, String description, AbstractOperation operation, Date time) {
super(id, projectID, description, operation, time);
}
protected FileHistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change){
super(id, project, description, operation, change);
}
protected void delete() {
File file = getChangeFile();
if (file.exists()) {
file.delete();
}
}
public void save(Writer writer, Properties options) {
JSONWriter jsonWriter = new JSONWriter(writer);
try {
write(jsonWriter, options);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void loadChange() {
File changeFile = getChangeFile();
try {
loadChange(changeFile);
} catch (Exception e) {
throw new RuntimeException("Failed to load change file " + changeFile.getAbsolutePath(), e);
}
}
protected void loadChange(File file) throws Exception {
ZipFile zipFile = new ZipFile(file);
try {
Pool pool = new Pool();
ZipEntry poolEntry = zipFile.getEntry("pool.txt");
if (poolEntry != null) {
pool.load(new InputStreamReader(
zipFile.getInputStream(poolEntry)));
} // else, it's a legacy project file
_change = History.readOneChange(
zipFile.getInputStream(zipFile.getEntry("change.txt")), pool);
} finally {
zipFile.close();
}
}
protected void saveChange() throws Exception {
File changeFile = getChangeFile();
if (!(changeFile.exists())) {
saveChange(changeFile);
}
}
protected void saveChange(File file) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
try {
Pool pool = new Pool();
out.putNextEntry(new ZipEntry("change.txt"));
try {
History.writeOneChange(out, _change, pool);
} finally {
out.closeEntry();
}
out.putNextEntry(new ZipEntry("pool.txt"));
try {
pool.save(out);
} finally {
out.closeEntry();
}
} finally {
out.close();
}
}
protected File getChangeFile() {
return new File(getHistoryDir(), id + ".change.zip");
}
protected File getHistoryDir() {//FIXME relies on FileProjectManager
File dir = new File(((FileProjectManager)ProjectManager.singleton).getProjectDir(projectID), "history");
dir.mkdirs();
return dir;
}
}

View File

@ -22,6 +22,9 @@ import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities;
@ -351,4 +354,11 @@ public class FileProjectManager extends ProjectManager{
return found;
}
public HistoryEntry createHistoryEntry(long id, long projectID, String description, AbstractOperation operation, Date time){
return new FileHistoryEntry(id, projectID, description, operation, time);
}
public HistoryEntry createHistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change){
return new FileHistoryEntry(id, project, description, operation, change);
}
}

View File

@ -23,7 +23,7 @@ public class ProjectUtilities {
synchronized public static void save(Project project) {
synchronized (project) {
long id = project.id;
File dir = ProjectManager.singleton.getProjectDir(id);
File dir = ((FileProjectManager)ProjectManager.singleton).getProjectDir(id);
File tempFile = new File(dir, "data.temp.zip");
try {

View File

@ -5,6 +5,7 @@ import java.util.List;
import org.json.JSONObject;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -45,7 +46,7 @@ abstract public class EngineDependentMassCellOperation extends EngineDependentOp
String description = createDescription(column, cellChanges);
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID, project, description, this, createChange(project, column, cellChanges));
}

View File

@ -9,6 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
@ -50,7 +51,7 @@ public class SaveProtographOperation extends AbstractOperation {
Change change = new ProtographChange(_protograph);
return new HistoryEntry(historyEntryID, project, description, SaveProtographOperation.this, change);
return ProjectManager.singleton.createHistoryEntry(historyEntryID, project, description, SaveProtographOperation.this, change);
}
static public class ProtographChange implements Change {

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
@ -118,7 +119,7 @@ public class MultiValuedCellJoinOperation extends AbstractOperation {
r = r2 - 1; // r will be incremented by the for loop anyway
}
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
getBriefDescription(null),

View File

@ -9,6 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Cell;
@ -136,7 +137,7 @@ public class MultiValuedCellSplitOperation extends AbstractOperation {
r = r2 - 1; // r will be incremented by the for loop anyway
}
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
getBriefDescription(null),

View File

@ -9,6 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -115,7 +116,7 @@ public class ColumnAdditionOperation extends EngineDependentOperation {
Change change = new ColumnAdditionChange(_newColumnName, _columnInsertIndex, cellsAtRows);
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID, project, description, this, change);
}

View File

@ -6,6 +6,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
@ -54,6 +55,6 @@ public class ColumnRemovalOperation extends AbstractOperation {
Change change = new ColumnRemovalChange(project.columnModel.columns.indexOf(column));
return new HistoryEntry(historyEntryID, project, description, ColumnRemovalOperation.this, change);
return ProjectManager.singleton.createHistoryEntry(historyEntryID, project, description, ColumnRemovalOperation.this, change);
}
}

View File

@ -6,6 +6,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
@ -58,6 +59,6 @@ public class ColumnRenameOperation extends AbstractOperation {
Change change = new ColumnRenameChange(_oldColumnName, _newColumnName);
return new HistoryEntry(historyEntryID, project, getBriefDescription(null), ColumnRenameOperation.this, change);
return ProjectManager.singleton.createHistoryEntry(historyEntryID, project, getBriefDescription(null), ColumnRenameOperation.this, change);
}
}

View File

@ -11,6 +11,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -211,7 +212,7 @@ public class ColumnSplitOperation extends EngineDependentOperation {
_removeOriginalColumn
);
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID, project, description, this, change);
}

View File

@ -13,6 +13,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -252,7 +253,7 @@ public class ExtendDataOperation extends EngineDependentOperation {
columnTypes.add(info.expectedType);
}
HistoryEntry historyEntry = new HistoryEntry(
HistoryEntry historyEntry = ProjectManager.singleton.createHistoryEntry(
_historyEntryID,
_project,
_description,

View File

@ -10,6 +10,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -281,7 +282,7 @@ public class ReconOperation extends EngineDependentOperation {
null
);
HistoryEntry historyEntry = new HistoryEntry(
HistoryEntry historyEntry = ProjectManager.singleton.createHistoryEntry(
_historyEntryID,
_project,
_description,

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Cell;
@ -71,7 +72,7 @@ public class DenormalizeOperation extends AbstractOperation {
newRows.add(newRow != null ? newRow : oldRow);
}
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
getBriefDescription(project),

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -62,7 +63,7 @@ public class RowFlagOperation extends EngineDependentOperation {
FilteredRows filteredRows = engine.getAllFilteredRows();
filteredRows.accept(project, createRowVisitor(project, changes));
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
(_flagged ? "Flag" : "Unflag") + " " + changes.size() + " rows",

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -54,7 +55,7 @@ public class RowRemovalOperation extends EngineDependentOperation {
FilteredRows filteredRows = engine.getAllFilteredRows();
filteredRows.accept(project, createRowVisitor(project, rowIndices));
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
"Remove " + rowIndices.size() + " rows",

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.RecordVisitor;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -85,7 +86,7 @@ public class RowReorderOperation extends AbstractOperation {
engine.getAllRecords().accept(project, visitor);
}
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
"Reorder rows",

View File

@ -8,6 +8,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -62,7 +63,7 @@ public class RowStarOperation extends EngineDependentOperation {
FilteredRows filteredRows = engine.getAllFilteredRows();
filteredRows.accept(project, createRowVisitor(project, changes));
return new HistoryEntry(
return ProjectManager.singleton.createHistoryEntry(
historyEntryID,
project,
(_starred ? "Star" : "Unstar") + " " + changes.size() + " rows",