mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Refactoring.
This commit is contained in:
parent
0d7f5e4430
commit
ea34136fe7
5 changed files with 132 additions and 83 deletions
|
@ -0,0 +1,122 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by Winston on 07/11/14.
|
||||
*/
|
||||
public class SnapshotFetcher {
|
||||
|
||||
private final String projectName;
|
||||
private final Map<Integer, Snapshot> snapshots;
|
||||
private final SortedSet<Integer> versions;
|
||||
|
||||
public SnapshotFetcher(String projectName, Map<Integer, Snapshot> snapshots) {
|
||||
this.projectName = projectName;
|
||||
this.snapshots = snapshots;
|
||||
versions = new TreeSet<Integer>();
|
||||
}
|
||||
|
||||
public List<Snapshot> fetchNewSnapshots() throws Throwable {
|
||||
List<Snapshot> newSnapshots = new LinkedList<Snapshot>();
|
||||
while (getNew(newSnapshots));
|
||||
return newSnapshots;
|
||||
}
|
||||
|
||||
private boolean getNew(List<Snapshot> newSnapshots) throws Throwable {
|
||||
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName);
|
||||
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);
|
||||
|
||||
getDoc.request();
|
||||
getSavedVers.request();
|
||||
|
||||
Set<Integer> fetchedIDs = new HashSet<Integer>();
|
||||
Map<Integer, SnapshotInfo> fetchedSnapshotInfos = new HashMap<Integer, SnapshotInfo>();
|
||||
|
||||
int latestVersionID = putLatestDoc(getDoc, fetchedIDs, fetchedSnapshotInfos);
|
||||
|
||||
putSavedVers(getSavedVers, fetchedIDs, fetchedSnapshotInfos);
|
||||
|
||||
List<Integer> idsToUpdate = getIDsToUpdate(fetchedIDs);
|
||||
|
||||
versions.addAll(fetchedIDs);
|
||||
versions.add(latestVersionID);
|
||||
|
||||
return updateIDs(idsToUpdate, fetchedSnapshotInfos, newSnapshots);
|
||||
}
|
||||
|
||||
private void putFetchedResult(SnapshotInfo snapshotInfo, Set<Integer> ids, Map<Integer, SnapshotInfo> snapshotInfos) {
|
||||
int versionID = snapshotInfo.getVersionId();
|
||||
snapshotInfos.put(versionID, snapshotInfo);
|
||||
ids.add(versionID);
|
||||
}
|
||||
|
||||
private int putLatestDoc(SnapshotGetDocRequest getDoc, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable {
|
||||
int latestVersionID = getDoc.getResult().getVersionID();
|
||||
putFetchedResult(new SnapshotInfo(latestVersionID), fetchedIDs, fetchedSnapshotInfos);
|
||||
return latestVersionID;
|
||||
}
|
||||
|
||||
private void putSavedVers(SnapshotGetSavedVersRequest getSavedVers, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable {
|
||||
for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
|
||||
putFetchedResult(snapshotInfo, fetchedIDs, fetchedSnapshotInfos);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> getIDsToUpdate(Set<Integer> fetchedIDs) {
|
||||
List<Integer> idsToUpdate = new LinkedList<Integer>();
|
||||
for (Integer id : fetchedIDs) {
|
||||
if (!versions.contains(id)) {
|
||||
idsToUpdate.add(id);
|
||||
}
|
||||
}
|
||||
return idsToUpdate;
|
||||
}
|
||||
|
||||
private boolean updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws Throwable {
|
||||
if (idsToUpdate.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
System.out.println("Fetching versions: " + idsToUpdate);
|
||||
fetchVersions(idsToUpdate, fetchedSnapshotInfos, newSnapshots);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void fetchVersions(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots) throws Throwable {
|
||||
List<SnapshotGetForVersionRequest> requests = createFiredRequests(idsToUpdate);
|
||||
processResults(fetchedSnapshotInfos, newSnapshots, requests);
|
||||
}
|
||||
|
||||
private List<SnapshotGetForVersionRequest> createFiredRequests(List<Integer> idsToUpdate) {
|
||||
List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
|
||||
for (int id : idsToUpdate) {
|
||||
SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(projectName, id);
|
||||
requests.add(request);
|
||||
request.request();
|
||||
}
|
||||
return requests;
|
||||
}
|
||||
|
||||
private void processResults(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, List<SnapshotGetForVersionRequest> requests) throws Throwable {
|
||||
for (SnapshotGetForVersionRequest request : requests) {
|
||||
processResult(fetchedSnapshotInfos, newSnapshots, request);
|
||||
}
|
||||
}
|
||||
|
||||
private void processResult(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, List<Snapshot> newSnapshots, SnapshotGetForVersionRequest request) throws Throwable {
|
||||
SnapshotGetForVersionResult result = request.getResult();
|
||||
SnapshotData data = result.getSnapshotData();
|
||||
Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data);
|
||||
snapshots.put(request.getVersionID(), snapshot);
|
||||
newSnapshots.add(snapshot);
|
||||
}
|
||||
|
||||
}
|
|
@ -14,6 +14,7 @@ public class SnapshotInfo {
|
|||
private String createdAt;
|
||||
|
||||
public SnapshotInfo(int versionID) {
|
||||
this.versionId = versionID;
|
||||
comment = "Update on WriteLatex.com.";
|
||||
user = new WLUser();
|
||||
}
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.model;
|
||||
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
|
@ -24,11 +21,11 @@ public class WLDataModel implements SnapshotDBAPI {
|
|||
projects = new HashMap<String, WLProject>();
|
||||
}
|
||||
|
||||
private void updateProjectWithName(String name) throws Throwable {
|
||||
private List<Snapshot> updateProjectWithName(String name) throws Throwable {
|
||||
if (!projects.containsKey(name)) {
|
||||
projects.put(name, new WLProject(name));
|
||||
}
|
||||
projects.get(name).update();
|
||||
return projects.get(name).fetchNewSnapshots();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -41,8 +38,7 @@ public class WLDataModel implements SnapshotDBAPI {
|
|||
|
||||
@Override
|
||||
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable {
|
||||
updateProjectWithName(name);
|
||||
return projects.get(name).getSnapshotsToAdd();
|
||||
return updateProjectWithName(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
package uk.ac.ic.wlgitbridge.writelatex.model;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
|
||||
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* Created by Winston on 06/11/14.
|
||||
|
@ -21,82 +17,16 @@ public class WLProject {
|
|||
|
||||
private final String name;
|
||||
private final Map<Integer, Snapshot> snapshots;
|
||||
private final SortedSet<Integer> versions;
|
||||
private List<Snapshot> snapshotsToAdd;
|
||||
private final SnapshotFetcher snapshotFetcher;
|
||||
|
||||
public WLProject(String name) {
|
||||
this.name = name;
|
||||
snapshots = new HashMap<Integer, Snapshot>();
|
||||
versions = new TreeSet<Integer>();
|
||||
snapshotFetcher = new SnapshotFetcher(name, snapshots);
|
||||
}
|
||||
|
||||
public void update() throws Throwable {
|
||||
getNew();
|
||||
}
|
||||
|
||||
private boolean getNew() throws Throwable {
|
||||
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(name);
|
||||
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(name);
|
||||
|
||||
getDoc.request();
|
||||
getSavedVers.request();
|
||||
|
||||
Set<Integer> fetchedIDs = new HashSet<Integer>();
|
||||
Map<Integer, SnapshotInfo> fetchedSnapshotInfos = new HashMap<Integer, SnapshotInfo>();
|
||||
|
||||
int latestVersionID = getDoc.getResult().getVersionID();
|
||||
fetchedSnapshotInfos.put(latestVersionID, new SnapshotInfo(latestVersionID));
|
||||
fetchedIDs.add(latestVersionID);
|
||||
|
||||
for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
|
||||
int versionId = snapshotInfo.getVersionId();
|
||||
fetchedSnapshotInfos.put(versionId, snapshotInfo);
|
||||
fetchedIDs.add(versionId);
|
||||
}
|
||||
|
||||
boolean result = false;
|
||||
|
||||
List<Integer> idsToUpdate = new LinkedList<Integer>();
|
||||
|
||||
boolean hasNew = false;
|
||||
for (Integer id : fetchedIDs) {
|
||||
boolean contains = versions.contains(id);
|
||||
result = result || contains;
|
||||
if (!contains) {
|
||||
idsToUpdate.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
versions.addAll(fetchedIDs);
|
||||
versions.add(latestVersionID);
|
||||
|
||||
updateIDs(idsToUpdate, fetchedSnapshotInfos);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable {
|
||||
System.out.println(idsToUpdate);
|
||||
List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
|
||||
for (int id : idsToUpdate) {
|
||||
SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(name, id);
|
||||
requests.add(request);
|
||||
request.request();
|
||||
}
|
||||
for (SnapshotGetForVersionRequest request : requests) {
|
||||
SnapshotGetForVersionResult result = request.getResult();
|
||||
SnapshotData data = result.getSnapshotData();
|
||||
Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data);
|
||||
snapshots.put(request.getVersionID(), snapshot);
|
||||
}
|
||||
snapshotsToAdd = new LinkedList<Snapshot>();
|
||||
for (int id : idsToUpdate) {
|
||||
snapshotsToAdd.add(snapshots.get(id));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Snapshot> getSnapshotsToAdd() {
|
||||
return snapshotsToAdd;
|
||||
public List<Snapshot> fetchNewSnapshots() throws Throwable {
|
||||
return snapshotFetcher.fetchNewSnapshots();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ public class WLProjectTests {
|
|||
public void nothingToTest() {
|
||||
WLProject project = new WLProject("1826rqgsdb");
|
||||
try {
|
||||
project.update();
|
||||
project.fetchNewSnapshots();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
|
|
Loading…
Reference in a new issue