|
|
@@ -1,163 +1,76 @@
|
|
|
package uk.ac.ic.wlgitbridge.writelatex;
|
|
|
|
|
|
-import uk.ac.ic.wlgitbridge.util.Util;
|
|
|
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
|
|
|
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.getdoc.exception.InvalidProjectException;
|
|
|
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.api.request.push.exception.SnapshotPostException;
|
|
|
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
|
|
|
-import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreAPI;
|
|
|
-import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreSource;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by Winston on 07/11/14.
|
|
|
*/
|
|
|
-public class SnapshotFetcher implements PersistentStoreSource {
|
|
|
+public class SnapshotFetcher {
|
|
|
|
|
|
- private PersistentStoreAPI persistentStore;
|
|
|
-
|
|
|
- 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 SortedSet<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
|
|
|
- SortedSet<Snapshot> newSnapshots = new TreeSet<Snapshot>();
|
|
|
- while (getNew(newSnapshots));
|
|
|
- for (Snapshot snapshot : newSnapshots) {
|
|
|
-// persistentStore.addSnapshot(projectName, snapshot.getVersionID());
|
|
|
- }
|
|
|
- Util.sout("Fetched snapshots: " + newSnapshots);
|
|
|
- return newSnapshots;
|
|
|
- }
|
|
|
-
|
|
|
- public Snapshot getLatestSnapshot() {
|
|
|
- if (versions.isEmpty()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return snapshots.get(versions.last());
|
|
|
- }
|
|
|
-
|
|
|
- public void putLatestVersion(int versionID) {
|
|
|
- versions.add(versionID);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void initFromPersistentStore(PersistentStoreAPI persistentStore) {
|
|
|
- this.persistentStore = persistentStore;
|
|
|
-// for (Integer savedVersionID : persistentStore.getVersionIDsForProjectName(projectName)) {
|
|
|
-// snapshots.put(savedVersionID, new Snapshot(savedVersionID));
|
|
|
-// versions.add(savedVersionID);
|
|
|
-// }
|
|
|
+ public List<Snapshot> getSnapshotsForProjectAfterVersion(String projectName, int version) throws FailedConnectionException, SnapshotPostException {
|
|
|
+ List<SnapshotInfo> snapshotInfos = getSnapshotInfosAfterVersion(projectName, version);
|
|
|
+ List<SnapshotData> snapshotDatas = getMatchingSnapshotData(projectName, snapshotInfos);
|
|
|
+ List<Snapshot> snapshots = combine(snapshotInfos, snapshotDatas);
|
|
|
+ return snapshots;
|
|
|
}
|
|
|
|
|
|
- private boolean getNew(SortedSet<Snapshot> newSnapshots) throws FailedConnectionException, InvalidProjectException {
|
|
|
+ private List<SnapshotInfo> getSnapshotInfosAfterVersion(String projectName, int version) throws FailedConnectionException, SnapshotPostException {
|
|
|
+ SortedSet<SnapshotInfo> versions = new TreeSet<SnapshotInfo>();
|
|
|
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 FailedConnectionException, InvalidProjectException {
|
|
|
- SnapshotGetDocResult result = getDoc.getResult();
|
|
|
- int latestVersionID = 0;
|
|
|
- try {
|
|
|
- latestVersionID = result.getVersionID();
|
|
|
- } catch (SnapshotPostException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- putFetchedResult(new SnapshotInfo(latestVersionID, result.getCreatedAt(), result.getName(), result.getEmail()), fetchedIDs, fetchedSnapshotInfos);
|
|
|
- return latestVersionID;
|
|
|
- }
|
|
|
-
|
|
|
- private void putSavedVers(SnapshotGetSavedVersRequest getSavedVers, Set<Integer> fetchedIDs, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws FailedConnectionException {
|
|
|
- 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);
|
|
|
+ SnapshotGetDocResult latestDoc = getDoc.getResult();
|
|
|
+ int latest = latestDoc.getVersionID();
|
|
|
+ if (latest > version) {
|
|
|
+ for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
|
|
|
+ if (snapshotInfo.getVersionId() > version) {
|
|
|
+ versions.add(snapshotInfo);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- return idsToUpdate;
|
|
|
- }
|
|
|
+ versions.add(new SnapshotInfo(latest, latestDoc.getCreatedAt(), latestDoc.getName(), latestDoc.getEmail()));
|
|
|
|
|
|
- private boolean updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, SortedSet<Snapshot> newSnapshots) throws FailedConnectionException {
|
|
|
- if (idsToUpdate.isEmpty()) {
|
|
|
- return false;
|
|
|
}
|
|
|
- fetchVersions(idsToUpdate, fetchedSnapshotInfos, newSnapshots);
|
|
|
- return true;
|
|
|
+ return new LinkedList<SnapshotInfo>(versions);
|
|
|
}
|
|
|
|
|
|
- private void fetchVersions(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos, SortedSet<Snapshot> newSnapshots) throws FailedConnectionException {
|
|
|
- List<SnapshotGetForVersionRequest> requests = createFiredRequests(idsToUpdate);
|
|
|
- processResults(fetchedSnapshotInfos, newSnapshots, requests);
|
|
|
+ private List<SnapshotData> getMatchingSnapshotData(String projectName, List<SnapshotInfo> snapshotInfos) throws FailedConnectionException {
|
|
|
+ List<SnapshotGetForVersionRequest> firedRequests = fireDataRequests(projectName, snapshotInfos);
|
|
|
+ List<SnapshotData> snapshotDataList = new LinkedList<SnapshotData>();
|
|
|
+ for (SnapshotGetForVersionRequest fired : firedRequests) {
|
|
|
+ snapshotDataList.add(fired.getResult().getSnapshotData());
|
|
|
+ }
|
|
|
+ return snapshotDataList;
|
|
|
}
|
|
|
|
|
|
- private List<SnapshotGetForVersionRequest> createFiredRequests(List<Integer> idsToUpdate) {
|
|
|
+ private List<SnapshotGetForVersionRequest> fireDataRequests(String projectName, List<SnapshotInfo> snapshotInfos) {
|
|
|
List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
|
|
|
- for (int id : idsToUpdate) {
|
|
|
- SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(projectName, id);
|
|
|
+ for (SnapshotInfo snapshotInfo : snapshotInfos) {
|
|
|
+ SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(projectName, snapshotInfo.getVersionId());
|
|
|
requests.add(request);
|
|
|
request.request();
|
|
|
}
|
|
|
return requests;
|
|
|
}
|
|
|
|
|
|
- private void processResults(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, SortedSet<Snapshot> newSnapshots, List<SnapshotGetForVersionRequest> requests) throws FailedConnectionException {
|
|
|
- for (SnapshotGetForVersionRequest request : requests) {
|
|
|
- processResult(fetchedSnapshotInfos, newSnapshots, request);
|
|
|
+ private List<Snapshot> combine(List<SnapshotInfo> snapshotInfos, List<SnapshotData> snapshotDatas) {
|
|
|
+ List<Snapshot> snapshots = new LinkedList<Snapshot>();
|
|
|
+ Iterator<SnapshotInfo> infos = snapshotInfos.iterator();
|
|
|
+ Iterator<SnapshotData> datas = snapshotDatas.iterator();
|
|
|
+ while (infos.hasNext()) {
|
|
|
+ snapshots.add(new Snapshot(infos.next(), datas.next()));
|
|
|
}
|
|
|
+ return snapshots;
|
|
|
}
|
|
|
|
|
|
- private void processResult(Map<Integer, SnapshotInfo> fetchedSnapshotInfos, SortedSet<Snapshot> newSnapshots, SnapshotGetForVersionRequest request) throws FailedConnectionException {
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- public SortedSet<Integer> getVersions() {
|
|
|
- return versions;
|
|
|
- }
|
|
|
}
|