Began work on memory file store.

This commit is contained in:
Winston Li 2014-11-08 19:00:58 +00:00
parent b5513618d0
commit 5686590a36
11 changed files with 91 additions and 38 deletions

View file

@ -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);

View file

@ -33,6 +33,10 @@ public class SnapshotFetcher {
return newSnapshots;
}
public Snapshot getLatestSnapshot() {
return snapshots.get(versions.last());
}
private boolean getNew(List<Snapshot> newSnapshots) throws FailedConnectionException, InvalidProjectException {
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName);
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);

View file

@ -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<byte[]> future;
public WLAttachment(JsonElement json) throws FailedConnectionException {
public SnapshotAttachment(JsonElement json) throws FailedConnectionException {
super(json);
}

View file

@ -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<WLFile> srcs;
private List<WLFile> atts;
private List<SnapshotFile> srcs;
private List<SnapshotFile> atts;
public SnapshotData(JsonElement json) throws FailedConnectionException {
srcs = new LinkedList<WLFile>();
atts = new LinkedList<WLFile>();
srcs = new LinkedList<SnapshotFile>();
atts = new LinkedList<SnapshotFile>();
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<WLFile> getSrcs() {
public List<SnapshotFile> getSrcs() {
return srcs;
}
public List<WLFile> getAtts() {
public List<SnapshotFile> getAtts() {
return atts;
}
}

View file

@ -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);
}

View file

@ -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<String, String> srcs;
private final Map<String, String> atts;
private final String rootGitDirectoryPath;
public WLDirectoryNode(String rootGitDirectoryPath, String projectName) {
this.rootGitDirectoryPath = rootGitDirectoryPath;
srcs = new HashMap<String, String>();
atts = new HashMap<String, String>();
}
public void updateFromProject(WLProject project) {
Snapshot snapshot = project.getLatestSnapshot();
}
}

View file

@ -1,7 +0,0 @@
package uk.ac.ic.wlgitbridge.writelatex.db;
/**
* Created by Winston on 08/11/14.
*/
public class WLFileNode {
}

View file

@ -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<String, WLFileNode> fileStore;
private final Map<String, WLDirectoryNode> fileStore;
private final String rootGitDirectoryPath;
public WLFileStore() {
fileStore = new HashMap<String, WLFileNode>();
public WLFileStore(String rootGitDirectoryPath) {
fileStore = new HashMap<String, WLDirectoryNode>();
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);
}
}

View file

@ -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<WLFile> srcs;
private final List<WLFile> atts;
private final List<SnapshotFile> srcs;
private final List<SnapshotFile> 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);
}
}

View file

@ -18,9 +18,9 @@ public class WLDataModel implements SnapshotDBAPI {
private final Map<String, WLProject> projects;
private final WLFileStore fileStore;
public WLDataModel() {
public WLDataModel(String rootGitDirectoryPath) {
projects = new HashMap<String, WLProject>();
fileStore = new WLFileStore();
fileStore = new WLFileStore(rootGitDirectoryPath);
}
@Override
@ -41,10 +41,16 @@ public class WLDataModel implements SnapshotDBAPI {
}
private List<Snapshot> 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<Snapshot> newSnapshots = project.fetchNewSnapshots();
fileStore.updateForProject(project);
return newSnapshots;
}
}

View file

@ -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<Integer, Snapshot> snapshots;
private final SnapshotFetcher snapshotFetcher;
private Snapshot latestSnapshot;
public WLProject(String name) {
this.name = name;
snapshots = new HashMap<Integer, Snapshot>();
@ -24,7 +27,17 @@ public class WLProject {
}
public List<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
return snapshotFetcher.fetchNewSnapshots();
List<Snapshot> newSnapshots = snapshotFetcher.fetchNewSnapshots();
latestSnapshot = snapshotFetcher.getLatestSnapshot();
return newSnapshots;
}
public String getName() {
return name;
}
public Snapshot getLatestSnapshot() {
return latestSnapshot;
}
}