Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitay Joffe committed Nov 4, 2013
2 parents 22f34ca + d869413 commit 3a477a5
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 147 deletions.
19 changes: 15 additions & 4 deletions src/org/ggp/base/util/game/CloudGameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,19 @@ public void run() {
if (bundledMetadata != null) {
Set<String> unchangedKeys = new HashSet<String>();
for (String theKey : theGameKeys) {
try {
try {
Game myGameVersion = loadGameFromCache(theKey);
if (myGameVersion == null)
continue;

String remoteGameURL = remoteRepository.getGameURL(theKey);
int remoteVersion = bundledMetadata.getJSONObject(theKey).getInt("version");
String remoteVersionedGameURL = RemoteGameRepository.addVersionToGameURL(remoteGameURL, remoteVersion);

if (myGameVersion.getRepositoryURL().equals(remoteVersionedGameURL)) {

// Skip updating the game cache entry if the version is the same
// and the cache entry was written less than a week ago.
if (myGameVersion.getRepositoryURL().equals(remoteVersionedGameURL) &&
getCacheEntryAge(theKey) < 604800000) {
unchangedKeys.add(theKey);
}
} catch (Exception e) {
Expand Down Expand Up @@ -261,6 +264,14 @@ private synchronized Game loadGameFromCache(String theKey) {
if (theLine == null) return null;
return Game.loadFromJSON(theLine);
}

private synchronized long getCacheEntryAge(String theKey) {
File theGameFile = new File(theCacheDirectory, theKey + ".zip");
if (theGameFile.exists()) {
return System.currentTimeMillis() - theGameFile.lastModified();
}
return System.currentTimeMillis();
}

// ================================================================

Expand Down
12 changes: 9 additions & 3 deletions src/org/ggp/base/util/game/LocalGameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public void handle(HttpExchange t) throws IOException {
static class BaseRepository {
public static final String repositoryRootDirectory = theLocalRepoURL;

public static boolean shouldIgnoreFile(String fileName) {
if (fileName.startsWith(".")) return true;
if (fileName.contains(" ")) return true;
return false;
}

public static byte[] getResponseBytesForURI(String reqURI) throws IOException {
// Files not under /games/games/ aren't versioned,
// and can just be accessed directly.
Expand All @@ -102,7 +108,7 @@ public static byte[] getResponseBytesForURI(String reqURI) throws IOException {
if (reqURI.equals("/games/metadata")) {
JSONObject theGameMetaMap = new JSONObject();
for (String gameName : new File("games", "games").list()) {
if (gameName.equals(".svn")) continue;
if (shouldIgnoreFile(gameName)) continue;
try {
theGameMetaMap.put(gameName, new JSONObject(new String(getResponseBytesForURI("/games/" + gameName + "/"))));
} catch (JSONException e) {
Expand Down Expand Up @@ -188,7 +194,7 @@ private static int getMaxVersionForDirectory(File theDir) {
int maxVersion = 0;
String[] children = theDir.list();
for (String s : children) {
if (s.equals(".svn")) continue;
if (shouldIgnoreFile(s)) continue;
if (s.startsWith("v")) {
int nVersion = Integer.parseInt(s.substring(1));
if (nVersion > maxVersion) {
Expand Down Expand Up @@ -252,7 +258,7 @@ private static String readDirectory(File theDirectory) throws IOException {

String[] children = theDirectory.list();
for (int i=0; i<children.length; i++) {
if (children[i].equals(".svn")) continue;
if (shouldIgnoreFile(children[i])) continue;
// Get filename of file or directory
response.append("\"");
response.append(children[i]);
Expand Down
36 changes: 31 additions & 5 deletions src/org/ggp/base/util/gdl/model/DependencyGraphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,66 @@
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;

/**
* When dealing with GDL, dependency graphs are often useful. DependencyGraphs
* offers a variety of functionality for dealing with dependency graphs expressed
* in the form of SetMultimaps.
*
* These multimaps are paired with sets of all nodes, to account for the
* possibility of nodes not included in the multimap representation.
*
* All methods assume that keys in multimaps depend on their associated values,
* or in other words are downstream of or are children of those values.
*/
public class DependencyGraphs {
private DependencyGraphs() {}

/**
* Returns all elements of the dependency graph that match the
* given predicate, and any elements downstream of those matching
* given predicate, and any elements upstream of those matching
* elements.
*
* The graph may contain cycles.
*
* Each key in the dependency graph depends on/is downstream of
* its associated values.
*/
public static <T> ImmutableSet<T> getMatchingAndDownstream(
public static <T> ImmutableSet<T> getMatchingAndUpstream(
Set<T> allNodes,
SetMultimap<T, T> dependencyGraph,
Predicate<T> matcher) {
Set<T> results = Sets.newHashSet();

SetMultimap<T, T> reversedGraph = reverseGraph(dependencyGraph);

Deque<T> toTry = Queues.newArrayDeque();
toTry.addAll(Collections2.filter(allNodes, matcher));

while (!toTry.isEmpty()) {
T curElem = toTry.remove();
if (!results.contains(curElem)) {
results.add(curElem);
toTry.addAll(reversedGraph.get(curElem));
toTry.addAll(dependencyGraph.get(curElem));
}
}
return ImmutableSet.copyOf(results);
}

/**
* Returns all elements of the dependency graph that match the
* given predicate, and any elements downstream of those matching
* elements.
*
* The graph may contain cycles.
*
* Each key in the dependency graph depends on/is downstream of
* its associated values.
*/
public static <T> ImmutableSet<T> getMatchingAndDownstream(
Set<T> allNodes,
SetMultimap<T, T> dependencyGraph,
Predicate<T> matcher) {
return getMatchingAndUpstream(allNodes, reverseGraph(dependencyGraph), matcher);
}

public static <T> SetMultimap<T, T> reverseGraph(SetMultimap<T, T> graph) {
return Multimaps.invertFrom(graph, HashMultimap.<T, T>create());
}
Expand Down
Loading

0 comments on commit 3a477a5

Please sign in to comment.