Loading fetched versions from database.

This commit is contained in:
Winston Li 2014-11-19 19:30:59 +00:00
parent f103c7e3b5
commit dc47c17264
4 changed files with 35 additions and 14 deletions

View file

@ -9,13 +9,17 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForV
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 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 {
public class SnapshotFetcher implements PersistentStoreSource {
private PersistentStoreAPI persistentStore;
private final String projectName;
private final Map<Integer, Snapshot> snapshots;
@ -30,11 +34,17 @@ public class SnapshotFetcher {
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());
}
System.out.println("Snapshots fetched: " + newSnapshots);
return newSnapshots;
}
public Snapshot getLatestSnapshot() {
if (versions.isEmpty()) {
return null;
}
return snapshots.get(versions.last());
}
@ -42,6 +52,15 @@ public class SnapshotFetcher {
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);
}
}
private boolean getNew(SortedSet<Snapshot> newSnapshots) throws FailedConnectionException, InvalidProjectException {
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName);
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName);

View file

@ -34,7 +34,7 @@ public class WLProject implements PersistentStoreSource {
public SortedSet<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
SortedSet<Snapshot> newSnapshots = snapshotFetcher.fetchNewSnapshots();
latestSnapshotID = snapshotFetcher.getLatestSnapshot().getVersionID();
updateLatestSnapshot();
return newSnapshots;
}
@ -46,6 +46,15 @@ public class WLProject implements PersistentStoreSource {
return latestSnapshotID;
}
private void updateLatestSnapshot() {
Snapshot latest = snapshotFetcher.getLatestSnapshot();
if (latest == null) {
latestSnapshotID = -1;
} else {
latestSnapshotID = latest.getVersionID();
}
}
public void putLatestSnapshot(int versionID) {
snapshots.put(versionID, new Snapshot(versionID));
snapshotFetcher.putLatestVersion(versionID);
@ -54,6 +63,8 @@ public class WLProject implements PersistentStoreSource {
@Override
public void initFromPersistentStore(PersistentStoreAPI persistentStore) {
snapshotFetcher.initFromPersistentStore(persistentStore);
updateLatestSnapshot();
}
}

View file

@ -30,7 +30,7 @@ public class WLProjectStore implements PersistentStoreSource {
if (projects.containsKey(name)) {
project = projects.get(name);
} else {
project = new WLProject(name);
project = new WLProject(name, persistentStore);
projects.put(name, project);
persistentStore.addProject(name);
}

View file

@ -1,8 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
/**
* Created by Winston on 06/11/14.
@ -11,14 +9,7 @@ public class WLProjectTests {
@Test
public void nothingToTest() {
WLProject project = new WLProject("1826rqgsdb");
try {
project.fetchNewSnapshots();
} catch (FailedConnectionException e) {
e.printStackTrace();
} catch (InvalidProjectException e) {
e.printStackTrace();
}
}
}