From 5686590a36529541a8bd78581bbf8cc1712a1d7b Mon Sep 17 00:00:00 2001 From: Winston Li Date: Sat, 8 Nov 2014 19:00:58 +0000 Subject: [PATCH] Began work on memory file store. --- .../application/WLGitBridgeServer.java | 2 +- .../writelatex/SnapshotFetcher.java | 4 +++ ...ttachment.java => SnapshotAttachment.java} | 4 +-- .../request/getforversion/SnapshotData.java | 18 ++++++------ .../{WLFile.java => SnapshotFile.java} | 4 +-- .../writelatex/db/WLDirectoryNode.java | 28 +++++++++++++++++++ .../wlgitbridge/writelatex/db/WLFileNode.java | 7 ----- .../writelatex/db/WLFileStore.java | 20 ++++++++++--- .../writelatex/model/Snapshot.java | 11 ++++---- .../writelatex/model/WLDataModel.java | 16 +++++++---- .../writelatex/model/WLProject.java | 15 +++++++++- 11 files changed, 91 insertions(+), 38 deletions(-) rename services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/{WLAttachment.java => SnapshotAttachment.java} (93%) rename services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/{WLFile.java => SnapshotFile.java} (92%) create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLDirectoryNode.java delete mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileNode.java diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java index ba2c06ac5a..bf92acfa11 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java @@ -65,7 +65,7 @@ public class WLGitBridgeServer { servletContextHandler.setContextPath("/"); servletContextHandler.addServlet( new ServletHolder( - new WLGitServlet(servletContextHandler, new WLDataModel(), rootGitDirectoryPath)), + new WLGitServlet(servletContextHandler, new WLDataModel(rootGitDirectoryPath), rootGitDirectoryPath)), "/*" ); jettyServer.setHandler(servletContextHandler); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java index e5bd10b700..9c5087ddcc 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotFetcher.java @@ -33,6 +33,10 @@ public class SnapshotFetcher { return newSnapshots; } + public Snapshot getLatestSnapshot() { + return snapshots.get(versions.last()); + } + private boolean getNew(List newSnapshots) throws FailedConnectionException, InvalidProjectException { SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName); SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLAttachment.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotAttachment.java similarity index 93% rename from services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLAttachment.java rename to services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotAttachment.java index 30e0dc3c96..cbc0189c94 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLAttachment.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotAttachment.java @@ -16,11 +16,11 @@ import java.util.concurrent.Future; /** * Created by Winston on 06/11/14. */ -public class WLAttachment extends WLFile { +public class SnapshotAttachment extends SnapshotFile { private Future future; - public WLAttachment(JsonElement json) throws FailedConnectionException { + public SnapshotAttachment(JsonElement json) throws FailedConnectionException { super(json); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java index f28df62598..59ee384f76 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java @@ -5,10 +5,8 @@ import com.google.gson.JsonElement; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; -import java.io.IOException; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. @@ -18,12 +16,12 @@ public class SnapshotData implements JSONSource { public static final String JSON_KEY_SRCS = "srcs"; public static final String JSON_KEY_ATTS = "atts"; - private List srcs; - private List atts; + private List srcs; + private List atts; public SnapshotData(JsonElement json) throws FailedConnectionException { - srcs = new LinkedList(); - atts = new LinkedList(); + srcs = new LinkedList(); + atts = new LinkedList(); fromJSON(json); } @@ -35,21 +33,21 @@ public class SnapshotData implements JSONSource { private void populateSrcs(JsonArray jsonArray) throws FailedConnectionException { for (JsonElement json : jsonArray) { - srcs.add(new WLFile(json)); + srcs.add(new SnapshotFile(json)); } } private void populateAtts(JsonArray jsonArray) throws FailedConnectionException { for (JsonElement json : jsonArray) { - atts.add(new WLAttachment(json)); + atts.add(new SnapshotAttachment(json)); } } - public List getSrcs() { + public List getSrcs() { return srcs; } - public List getAtts() { + public List getAtts() { return atts; } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLFile.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotFile.java similarity index 92% rename from services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLFile.java rename to services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotFile.java index 80bbcb5766..b76bc408b7 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLFile.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotFile.java @@ -14,12 +14,12 @@ import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. */ -public class WLFile implements JSONSource { +public class SnapshotFile implements JSONSource { protected byte[] contents; private String path; - public WLFile(JsonElement json) throws FailedConnectionException { + public SnapshotFile(JsonElement json) throws FailedConnectionException { fromJSON(json); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLDirectoryNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLDirectoryNode.java new file mode 100644 index 0000000000..3477cd74f0 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLDirectoryNode.java @@ -0,0 +1,28 @@ +package uk.ac.ic.wlgitbridge.writelatex.db; + +import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; +import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Winston on 08/11/14. + */ +public class WLDirectoryNode { + + private final Map srcs; + private final Map atts; + private final String rootGitDirectoryPath; + + public WLDirectoryNode(String rootGitDirectoryPath, String projectName) { + this.rootGitDirectoryPath = rootGitDirectoryPath; + srcs = new HashMap(); + atts = new HashMap(); + } + + public void updateFromProject(WLProject project) { + Snapshot snapshot = project.getLatestSnapshot(); + } + +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileNode.java deleted file mode 100644 index 1ffbbb2c46..0000000000 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileNode.java +++ /dev/null @@ -1,7 +0,0 @@ -package uk.ac.ic.wlgitbridge.writelatex.db; - -/** - * Created by Winston on 08/11/14. - */ -public class WLFileNode { -} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileStore.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileStore.java index 60a1887fcb..1fe52f6078 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileStore.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/db/WLFileStore.java @@ -1,5 +1,7 @@ package uk.ac.ic.wlgitbridge.writelatex.db; +import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; + import java.util.HashMap; import java.util.Map; @@ -8,12 +10,22 @@ import java.util.Map; */ public class WLFileStore { - private final Map fileStore; + private final Map fileStore; + private final String rootGitDirectoryPath; - public WLFileStore() { - fileStore = new HashMap(); + public WLFileStore(String rootGitDirectoryPath) { + fileStore = new HashMap(); + this.rootGitDirectoryPath = rootGitDirectoryPath; } - + public void updateForProject(WLProject project) { + String projectName = project.getName(); + WLDirectoryNode directoryNode = fileStore.get(projectName); + if (directoryNode == null) { + directoryNode = new WLDirectoryNode(rootGitDirectoryPath, projectName); + fileStore.put(projectName, directoryNode); + } + directoryNode.updateFromProject(project); + } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java index 00217e9c31..9cc43af1fc 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java @@ -2,13 +2,12 @@ package uk.ac.ic.wlgitbridge.writelatex.model; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; -import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.WLFile; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotFile; import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo; import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.WLUser; import java.io.IOException; import java.util.List; -import java.util.concurrent.ExecutionException; /** * Created by Winston on 03/11/14. @@ -20,8 +19,8 @@ public class Snapshot { private final String userName; private final String userEmail; - private final List srcs; - private final List atts; + private final List srcs; + private final List atts; public Snapshot(SnapshotInfo info, SnapshotData data) { versionID = info.getVersionId(); @@ -35,10 +34,10 @@ public class Snapshot { } public void writeToDisk(String basePath) throws IOException, FailedConnectionException { - for (WLFile file : srcs) { + for (SnapshotFile file : srcs) { file.writeToDisk(basePath); } - for (WLFile file : atts) { + for (SnapshotFile file : atts) { file.writeToDisk(basePath); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java index 7a6d499297..bf848fa56a 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java @@ -18,9 +18,9 @@ public class WLDataModel implements SnapshotDBAPI { private final Map projects; private final WLFileStore fileStore; - public WLDataModel() { + public WLDataModel(String rootGitDirectoryPath) { projects = new HashMap(); - fileStore = new WLFileStore(); + fileStore = new WLFileStore(rootGitDirectoryPath); } @Override @@ -41,10 +41,16 @@ public class WLDataModel implements SnapshotDBAPI { } private List updateProjectWithName(String name) throws FailedConnectionException, InvalidProjectException { - if (!projects.containsKey(name)) { - projects.put(name, new WLProject(name)); + WLProject project; + if (projects.containsKey(name)) { + project = projects.get(name); + } else { + project = new WLProject(name); + projects.put(name, project); } - return projects.get(name).fetchNewSnapshots(); + List newSnapshots = project.fetchNewSnapshots(); + fileStore.updateForProject(project); + return newSnapshots; } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java index 9649a3cd73..9a226984c2 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java @@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex.model; import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; +import uk.ac.ic.wlgitbridge.writelatex.db.WLFileStore; import java.util.HashMap; import java.util.List; @@ -17,6 +18,8 @@ public class WLProject { private final Map snapshots; private final SnapshotFetcher snapshotFetcher; + private Snapshot latestSnapshot; + public WLProject(String name) { this.name = name; snapshots = new HashMap(); @@ -24,7 +27,17 @@ public class WLProject { } public List fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException { - return snapshotFetcher.fetchNewSnapshots(); + List newSnapshots = snapshotFetcher.fetchNewSnapshots(); + latestSnapshot = snapshotFetcher.getLatestSnapshot(); + return newSnapshots; + } + + public String getName() { + return name; + } + + public Snapshot getLatestSnapshot() { + return latestSnapshot; } }