Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent duplication of nodeRef on deserialization #355

Merged
merged 3 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/overflowdb/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private NodeRef createNode(final long idValue, final String label, final Object.
throw new IllegalArgumentException("No NodeFactory for label=" + label + " available.");
}
final NodeFactory factory = nodeFactoryByLabel.get(label);
final NodeDb node = factory.createNode(this, idValue);
final NodeDb node = factory.createNode(this, idValue, null);
PropertyHelper.attachProperties(node, keyValues);
registerNodeRef(node.ref);

Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/overflowdb/NodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ public abstract class NodeFactory<V extends NodeDb> {

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

public V createNode(Graph graph, long id) {
final NodeRef<V> ref = createNodeRef(graph, id);
public V createNode(Graph graph, long id) {
return createNode(graph, id, null);
bbrehm marked this conversation as resolved.
Show resolved Hide resolved
}

public V createNode(Graph graph, long id, NodeRef<V> ref) {
if(ref == null) ref = createNodeRef(graph, id);
final V node = createNode(ref);
node.markAsDirty(); //freshly created, i.e. not yet serialized
ref.setNode(node);
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/overflowdb/NodeRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ private final synchronized N getSynchronized() throws IOException {
if (ref != null) {
return ref;
} else {
final N node = readFromDisk(id);
final N node = readFromDisk();
if (node == null) throw new IllegalStateException("unable to read node from disk; id=" + id);
this.node = node;
assert(this.node == node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java's asserts are not being checked at runtime by default 😢
I tend to throw new AssertionError in this case, but I guess there's other options.

Also: the error text in this case wouldn't be very helpful.

graph.registerNodeRef(this);
return node;
}
Expand All @@ -115,9 +115,9 @@ public void setNode(N node) {
this.node = node;
}

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

public long id() {
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/overflowdb/storage/NodeDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public NodeDeserializer(Graph graph, Map<String, NodeFactory> nodeFactoryByLabel
this.storage = storage;
}

public final NodeDb deserialize(byte[] bytes) throws IOException {
public final NodeDb deserialize(byte[] bytes) throws IOException {
return deserialize(bytes, null);
}
public final NodeDb deserialize(byte[] bytes, NodeRef<?> ref) throws IOException {
long startTimeNanos = getStartTimeNanos();
if (null == bytes)
return null;
Expand All @@ -44,7 +47,7 @@ public final NodeDb deserialize(byte[] bytes) throws IOException {
final Object[] properties = unpackProperties(unpacker);

final String label = storage.reverseLookupStringToIntMapping(labelStringId);
NodeDb node = getNodeFactory(label).createNode(graph, id);
NodeDb node = getNodeFactory(label).createNode(graph, id, ref);
PropertyHelper.attachProperties(node, properties);

deserializeEdges(unpacker, node, Direction.OUT);
Expand Down