Skip to content

Commit 379a2d3

Browse files
authored
prevent duplication of nodeRef on deserialization (#355)
1 parent 76d4393 commit 379a2d3

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

core/src/main/java/overflowdb/Graph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private NodeRef createNode(final long idValue, final String label, final Object.
173173
throw new IllegalArgumentException("No NodeFactory for label=" + label + " available.");
174174
}
175175
final NodeFactory factory = nodeFactoryByLabel.get(label);
176-
final NodeDb node = factory.createNode(this, idValue);
176+
final NodeDb node = factory.createNode(this, idValue, null);
177177
PropertyHelper.attachProperties(node, keyValues);
178178
registerNodeRef(node.ref);
179179

core/src/main/java/overflowdb/NodeFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ public abstract class NodeFactory<V extends NodeDb> {
77

88
public abstract NodeRef<V> createNodeRef(Graph graph, long id);
99

10-
public V createNode(Graph graph, long id) {
11-
final NodeRef<V> ref = createNodeRef(graph, id);
10+
public V createNode(Graph graph, long id) {
11+
return createNode(graph, id, null);
12+
}
13+
14+
public V createNode(Graph graph, long id, NodeRef<V> ref) {
15+
if(ref == null) ref = createNodeRef(graph, id);
1216
final V node = createNode(ref);
1317
node.markAsDirty(); //freshly created, i.e. not yet serialized
1418
ref.setNode(node);

core/src/main/java/overflowdb/NodeRef.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ private final synchronized N getSynchronized() throws IOException {
9999
if (ref != null) {
100100
return ref;
101101
} else {
102-
final N node = readFromDisk(id);
102+
final N node = readFromDisk();
103103
if (node == null) throw new IllegalStateException("unable to read node from disk; id=" + id);
104-
this.node = node;
104+
if (this.node != node) throw new AssertionError("invalid state after reading node from dist; id=" + id);
105105
graph.registerNodeRef(this);
106106
return node;
107107
}
@@ -115,9 +115,9 @@ public void setNode(N node) {
115115
this.node = node;
116116
}
117117

118-
private final N readFromDisk(long nodeId) throws IOException {
119-
byte[] bytes = graph.storage.getSerializedNode(nodeId);
120-
return (N) graph.nodeDeserializer.deserialize(bytes);
118+
private final N readFromDisk() throws IOException {
119+
byte[] bytes = graph.storage.getSerializedNode(this.id);
120+
return (N) graph.nodeDeserializer.deserialize(bytes, this);
121121
}
122122

123123
public long id() {

core/src/main/java/overflowdb/storage/NodeDeserializer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public NodeDeserializer(Graph graph, Map<String, NodeFactory> nodeFactoryByLabel
3333
this.storage = storage;
3434
}
3535

36-
public final NodeDb deserialize(byte[] bytes) throws IOException {
36+
public final NodeDb deserialize(byte[] bytes) throws IOException {
37+
return deserialize(bytes, null);
38+
}
39+
public final NodeDb deserialize(byte[] bytes, NodeRef<?> ref) throws IOException {
3740
long startTimeNanos = getStartTimeNanos();
3841
if (null == bytes)
3942
return null;
@@ -44,7 +47,7 @@ public final NodeDb deserialize(byte[] bytes) throws IOException {
4447
final Object[] properties = unpackProperties(unpacker);
4548

4649
final String label = storage.reverseLookupStringToIntMapping(labelStringId);
47-
NodeDb node = getNodeFactory(label).createNode(graph, id);
50+
NodeDb node = getNodeFactory(label).createNode(graph, id, ref);
4851
PropertyHelper.attachProperties(node, properties);
4952

5053
deserializeEdges(unpacker, node, Direction.OUT);

0 commit comments

Comments
 (0)