Skip to content

edge property hack: prereq for flatgraph -> odb converter #439

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

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all commits
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
30 changes: 24 additions & 6 deletions core/src/main/java/overflowdb/storage/NodeDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import overflowdb.util.StringInterner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

public class NodeDeserializer extends BookKeeper {
public static final String FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME = "EDGE_PROPERTY";
protected final Graph graph;
private final Map<String, NodeFactory> nodeFactoryByLabel;
private final OdbStorage storage;
Expand Down Expand Up @@ -68,7 +66,9 @@ private void deserializeEdges(MessageUnpacker unpacker, NodeDb node, Direction d
for (int edgeIdx = 0; edgeIdx < edgeCount; edgeIdx++) {
long adjacentNodeId = unpacker.unpackLong();
NodeRef adjacentNode = (NodeRef) graph.node(adjacentNodeId);
Object[] edgeProperties = unpackProperties(unpacker);
// hack for flatgraph->odb converter, see `maybeReviseKeyForFlatgraph` below
Set<String> allowedEdgePropertyKeys = node.layoutInformation().edgePropertyKeys(edgeLabel);
Object[] edgeProperties = unpackProperties(unpacker, allowedEdgePropertyKeys);
node.storeAdjacentNode(direction, edgeLabel, adjacentNode, edgeProperties);
}
}
Expand All @@ -88,6 +88,10 @@ public final NodeRef deserializeRef(byte[] bytes) throws IOException {
}

private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOException {
return unpackProperties(unpacker, new HashSet<>());
}

private final Object[] unpackProperties(MessageUnpacker unpacker, Set<String> allowedEdgePropertyKeys) throws IOException {
int propertyCount = unpacker.unpackMapHeader();
Object[] res = new Object[propertyCount * 2];
int resIdx = 0;
Expand All @@ -96,12 +100,26 @@ private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOExcep
final String key = storage.reverseLookupStringToIntMapping(keyId);
final ImmutableValue unpackedValue = unpacker.unpackValue();
final Object unpackedProperty = unpackValue(unpackedValue.asArrayValue());
res[resIdx++] = key;
final String keyRevised = maybeReviseKeyForFlatgraph(key, allowedEdgePropertyKeys);
res[resIdx++] = keyRevised;
res[resIdx++] = unpackedProperty;
}
return res;
}

/** In order to prepare for a potential rollback to overflowdb in the future, we're creating a
* flatgraph->overflowdb converter. Since flatgraph supports exactly one edge property only,
* there are no edge property names. So when we deserialise and find exactly one edge property with
* the name `EDGE_PROPERTY`, and the schema defines exactly one edge property, then we assume that's the one.
* Let's be clear: this is a dirty hack, but since we're phasing out overflowdb in favor of flatgraph, it's
* arguably ok.
*/
private String maybeReviseKeyForFlatgraph(String key, Set<String> allowedEdgePropertyKeys) {
if (FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME.equals(key) && allowedEdgePropertyKeys.size() == 1) {
return allowedEdgePropertyKeys.iterator().next();
} else return key;
}

private final Object unpackValue(final ArrayValue packedValueAndType) {
final Iterator<Value> iter = packedValueAndType.iterator();
final byte valueTypeId = iter.next().asIntegerValue().asByte();
Expand Down
Loading