Skip to content

Commit 513200b

Browse files
author
peacekeeper
committed
some improvements related to concurrency
1 parent 71e4c6f commit 513200b

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

core/src/main/java/xdi2/core/impl/json/JSONContextNode.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ public Relation map(JsonElement jsonElement) {
295295
if (contextNode == null) {
296296

297297
log.warn("In context node " + JSONContextNode.this.getXDIAddress() + " found incoming relation " + XDIaddress + " from non-existent context node " + contextNodeXDIAddress);
298-
299298
return null;
300299
}
301300

core/src/main/java/xdi2/core/impl/json/JSONGraph.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import java.io.IOException;
44
import java.util.Collections;
5-
import java.util.HashMap;
65
import java.util.HashSet;
76
import java.util.Iterator;
87
import java.util.Map;
98
import java.util.Map.Entry;
109
import java.util.Set;
10+
import java.util.concurrent.ConcurrentHashMap;
1111

1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
@@ -51,7 +51,7 @@ public class JSONGraph extends AbstractGraph implements Graph {
5151

5252
this.jsonRootContextNode = new JSONContextNode(this, null, null, XDIConstants.XDI_ADD_ROOT);
5353

54-
this.jsonObjectsCached = new HashMap<String, JsonObject> ();
54+
this.jsonObjectsCached = new ConcurrentHashMap<String, JsonObject> ();
5555
this.jsonObjectsCachedWithPrefix = new HashSet<String> ();
5656

5757
this.useCache = false;

core/src/main/java/xdi2/core/impl/json/JSONLiteralNode.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package xdi2.core.impl.json;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.google.gson.JsonElement;
37
import com.google.gson.JsonObject;
48

59
import xdi2.core.ContextNode;
@@ -11,6 +15,8 @@ public class JSONLiteralNode extends AbstractLiteralNode implements LiteralNode
1115

1216
private static final long serialVersionUID = 5656043671598618588L;
1317

18+
private static final Logger log = LoggerFactory.getLogger(JSONLiteralNode.class);
19+
1420
public JSONLiteralNode(ContextNode contextNode) {
1521

1622
super(contextNode);
@@ -23,7 +29,15 @@ public Object getLiteralData() {
2329

2430
JsonObject jsonObject = ((JSONGraph) this.getGraph()).jsonLoad(jsonContextNode.getXDIAddress().toString());
2531

26-
return AbstractLiteralNode.jsonElementToLiteralData(jsonObject.get(XDIConstants.XDI_ARC_LITERAL.toString()));
32+
JsonElement jsonElement = jsonObject.get(XDIConstants.XDI_ARC_LITERAL.toString());
33+
34+
if (jsonElement == null) {
35+
36+
log.warn("In literal node " + this.getContextNode() + " found non-existent value.");
37+
return null;
38+
}
39+
40+
return AbstractLiteralNode.jsonElementToLiteralData(jsonElement);
2741
}
2842

2943
@Override

core/src/main/java/xdi2/core/impl/json/memory/MemoryJSONStore.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package xdi2.core.impl.json.memory;
22

33
import java.io.IOException;
4-
import java.util.HashMap;
54
import java.util.Iterator;
65
import java.util.Map;
76
import java.util.Map.Entry;
7+
import java.util.concurrent.ConcurrentHashMap;
88

9+
import com.google.gson.JsonArray;
10+
import com.google.gson.JsonElement;
911
import com.google.gson.JsonObject;
1012

1113
import xdi2.core.impl.json.AbstractJSONStore;
@@ -17,7 +19,7 @@ public class MemoryJSONStore extends AbstractJSONStore implements JSONStore {
1719

1820
public MemoryJSONStore() {
1921

20-
this.jsonObjects = new HashMap<String, JsonObject> ();
22+
this.jsonObjects = new ConcurrentHashMap<String, JsonObject> ();
2123
}
2224

2325
@Override
@@ -32,10 +34,44 @@ public void close() {
3234
this.jsonObjects = null;
3335
}
3436

37+
private static JsonElement deepCopy(JsonElement jsonElement) {
38+
39+
if (jsonElement instanceof JsonObject) return deepCopy((JsonObject) jsonElement);
40+
if (jsonElement instanceof JsonArray) return deepCopy((JsonArray) jsonElement);
41+
42+
return jsonElement;
43+
}
44+
45+
private static JsonObject deepCopy(JsonObject jsonObject) {
46+
47+
JsonObject result = new JsonObject();
48+
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
49+
50+
result.add(entry.getKey(), deepCopy(entry.getValue()));
51+
}
52+
53+
return result;
54+
}
55+
56+
private static JsonArray deepCopy(JsonArray jsonArray) {
57+
58+
JsonArray result = new JsonArray();
59+
60+
for (JsonElement element : jsonArray) {
61+
62+
result.add(deepCopy(element));
63+
64+
}
65+
return result;
66+
}
67+
3568
@Override
3669
public JsonObject load(String id) throws IOException {
3770

38-
return this.jsonObjects.get(id);
71+
JsonObject jsonObject = this.jsonObjects.get(id);
72+
if (jsonObject == null) return null;
73+
74+
return deepCopy(jsonObject);
3975
}
4076

4177
@Override

0 commit comments

Comments
 (0)