Skip to content

Commit

Permalink
fixing a cache issue here
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Apr 3, 2014
1 parent f35ffcc commit b940d46
Showing 1 changed file with 88 additions and 48 deletions.
136 changes: 88 additions & 48 deletions core/src/main/java/xdi2/core/impl/json/JSONGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class JSONGraph extends AbstractGraph implements Graph {
private final Map<String, JsonObject> jsonObjectsCached;
private final Set<String> jsonObjectsCachedWithPrefix;

private boolean useCache;

private StringBuffer logBuffer;
private boolean logEnabled;

Expand All @@ -48,9 +50,12 @@ public class JSONGraph extends AbstractGraph implements Graph {
this.jsonStore = jsonStore;

this.jsonRootContextNode = new JSONContextNode(this, null, null, XDIConstants.XRI_S_ROOT);

this.jsonObjectsCached = new HashMap<String, JsonObject> ();
this.jsonObjectsCachedWithPrefix = new HashSet<String> ();

this.useCache = false;

this.logBuffer = new StringBuffer();
this.logEnabled = false;
}
Expand Down Expand Up @@ -92,6 +97,8 @@ public void beginTransaction() {
this.jsonObjectsCached.clear();
this.jsonObjectsCachedWithPrefix.clear();

this.useCache = true;

this.jsonStore.beginTransaction();
}

Expand All @@ -101,6 +108,8 @@ public void commitTransaction() {
this.jsonObjectsCached.clear();
this.jsonObjectsCachedWithPrefix.clear();

this.useCache = false;

this.jsonStore.commitTransaction();
}

Expand All @@ -110,6 +119,8 @@ public void rollbackTransaction() {
this.jsonObjectsCached.clear();
this.jsonObjectsCachedWithPrefix.clear();

this.useCache = false;

this.jsonStore.rollbackTransaction();
}

Expand Down Expand Up @@ -153,20 +164,26 @@ JsonObject jsonLoad(String id) {

try {

jsonObjectCached = this.jsonObjectsCached.get(id);
if (this.useCache) {

if (jsonObjectCached != null) {
jsonObjectCached = this.jsonObjectsCached.get(id);

jsonObject = jsonObjectCached;
return jsonObject;
if (jsonObjectCached != null) {

jsonObject = jsonObjectCached;
return jsonObject;
}
}

try {

jsonObject = this.jsonStore.load(id);
if (jsonObject == null) jsonObject = new JsonObject();

this.jsonObjectsCached.put(id, jsonObject);
if (this.useCache) {

this.jsonObjectsCached.put(id, jsonObject);
}

return jsonObject;
} catch (IOException ex) {
Expand All @@ -188,22 +205,28 @@ Map<String, JsonObject> jsonLoadWithPrefix(String id) {

try {

boolean jsonObjectCachedWithPrefix = this.jsonObjectsCachedWithPrefix.contains(id);
if (this.useCache) {

if (jsonObjectCachedWithPrefix) {
boolean jsonObjectCachedWithPrefix = this.jsonObjectsCachedWithPrefix.contains(id);

jsonObjectCached = this.jsonObjectsCached.get(id);
if (jsonObjectCachedWithPrefix) {

jsonObjects = Collections.singletonMap(id, jsonObjectCached);
return jsonObjects;
jsonObjectCached = this.jsonObjectsCached.get(id);

jsonObjects = Collections.singletonMap(id, jsonObjectCached);
return jsonObjects;
}
}

try {

jsonObjects = this.jsonStore.loadWithPrefix(id);

this.jsonObjectsCached.putAll(jsonObjects);
this.jsonObjectsCachedWithPrefix.addAll(jsonObjects.keySet());
if (this.useCache) {

this.jsonObjectsCached.putAll(jsonObjects);
this.jsonObjectsCachedWithPrefix.addAll(jsonObjects.keySet());
}

return jsonObjects;
} catch (IOException ex) {
Expand All @@ -228,7 +251,10 @@ void jsonSave(String id, JsonObject jsonObject) {

this.jsonStore.save(id, jsonObject);

this.jsonObjectsCached.put(id, jsonObject);
if (this.useCache) {

this.jsonObjectsCached.put(id, jsonObject);
}
} catch (IOException ex) {

throw new Xdi2RuntimeException("Cannot save JSON at " + id + ": " + ex.getMessage(), ex);
Expand All @@ -245,30 +271,33 @@ void jsonSaveToArray(String id, String key, JsonPrimitive jsonPrimitive) {

this.jsonStore.saveToArray(id, key, jsonPrimitive);

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (this.useCache) {

if (jsonObject == null) {
JsonObject jsonObject = this.jsonObjectsCached.get(id);

jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
jsonArray.add(jsonPrimitive);
jsonObject.add(key, jsonArray);
} else {
if (jsonObject == null) {

JsonArray jsonArray = jsonObject.getAsJsonArray(key);

if (jsonArray == null) {

jsonArray = new JsonArray();
jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
jsonArray.add(jsonPrimitive);
jsonObject.add(key, jsonArray);
} else {

if (! new IteratorContains<JsonElement> (jsonArray.iterator(), jsonPrimitive).contains()) jsonArray.add(jsonPrimitive);
JsonArray jsonArray = jsonObject.getAsJsonArray(key);

if (jsonArray == null) {

jsonArray = new JsonArray();
jsonArray.add(jsonPrimitive);
jsonObject.add(key, jsonArray);
} else {

if (! new IteratorContains<JsonElement> (jsonArray.iterator(), jsonPrimitive).contains()) jsonArray.add(jsonPrimitive);
}
}
}

this.jsonObjectsCached.put(id, jsonObject);
this.jsonObjectsCached.put(id, jsonObject);
}
} catch (IOException ex) {

throw new Xdi2RuntimeException("Cannot save JSON to array " + id + ": " + ex.getMessage(), ex);
Expand All @@ -285,18 +314,21 @@ void jsonSaveToObject(String id, String key, JsonElement jsonElement) {

this.jsonStore.saveToObject(id, key, jsonElement);

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (this.useCache) {

if (jsonObject == null) {
JsonObject jsonObject = this.jsonObjectsCached.get(id);

jsonObject = new JsonObject();
jsonObject.add(key, jsonElement);
} else {
if (jsonObject == null) {

jsonObject.add(key, jsonElement);
}
jsonObject = new JsonObject();
jsonObject.add(key, jsonElement);
} else {

jsonObject.add(key, jsonElement);
}

this.jsonObjectsCached.put(id, jsonObject);
this.jsonObjectsCached.put(id, jsonObject);
}
} catch (IOException ex) {

throw new Xdi2RuntimeException("Cannot save JSON to object " + id + ": " + ex.getMessage(), ex);
Expand All @@ -313,9 +345,11 @@ void jsonDelete(String id) {

this.jsonStore.delete(id);

for (Iterator<Entry<String, JsonObject>> iterator = this.jsonObjectsCached.entrySet().iterator(); iterator.hasNext(); ) {
if (this.useCache) {
for (Iterator<Entry<String, JsonObject>> iterator = this.jsonObjectsCached.entrySet().iterator(); iterator.hasNext(); ) {

if (iterator.next().getKey().startsWith(id)) iterator.remove();
if (iterator.next().getKey().startsWith(id)) iterator.remove();
}
}
} catch (IOException ex) {

Expand All @@ -333,15 +367,18 @@ void jsonDeleteFromArray(String id, String key, JsonPrimitive jsonPrimitive) {

this.jsonStore.deleteFromArray(id, key, jsonPrimitive);

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (jsonObject == null) return;
if (this.useCache) {

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (jsonObject == null) return;

JsonArray jsonArray = jsonObject.getAsJsonArray(key);
if (jsonArray == null) return;
JsonArray jsonArray = jsonObject.getAsJsonArray(key);
if (jsonArray == null) return;

new IteratorRemover<JsonElement> (jsonArray.iterator(), jsonPrimitive).remove();
new IteratorRemover<JsonElement> (jsonArray.iterator(), jsonPrimitive).remove();

this.jsonObjectsCached.put(id, jsonObject);
this.jsonObjectsCached.put(id, jsonObject);
}
} catch (IOException ex) {

throw new Xdi2RuntimeException("Cannot remove JSON from array " + id + ": " + ex.getMessage(), ex);
Expand All @@ -358,12 +395,15 @@ void jsonDeleteFromObject(String id, String key) {

this.jsonStore.deleteFromObject(id, key);

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (jsonObject == null) return;
if (this.useCache) {

JsonObject jsonObject = this.jsonObjectsCached.get(id);
if (jsonObject == null) return;

jsonObject.remove(key);
jsonObject.remove(key);

this.jsonObjectsCached.put(id, jsonObject);
this.jsonObjectsCached.put(id, jsonObject);
}
} catch (IOException ex) {

throw new Xdi2RuntimeException("Cannot remove JSON from object " + id + ": " + ex.getMessage(), ex);
Expand Down

0 comments on commit b940d46

Please sign in to comment.