Skip to content

Commit befafef

Browse files
committed
edge property hack: prereq for flatgraph -> odb converter
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.
1 parent 09bc7ec commit befafef

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
import overflowdb.util.StringInterner;
1515

1616
import java.io.IOException;
17-
import java.util.ArrayList;
18-
import java.util.Iterator;
19-
import java.util.List;
20-
import java.util.Map;
17+
import java.util.*;
2118

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

9090
private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOException {
91+
return unpackProperties(unpacker, new HashSet<>());
92+
}
93+
94+
private final Object[] unpackProperties(MessageUnpacker unpacker, Set<String> allowedEdgePropertyKeys) throws IOException {
9195
int propertyCount = unpacker.unpackMapHeader();
9296
Object[] res = new Object[propertyCount * 2];
9397
int resIdx = 0;
@@ -96,12 +100,26 @@ private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOExcep
96100
final String key = storage.reverseLookupStringToIntMapping(keyId);
97101
final ImmutableValue unpackedValue = unpacker.unpackValue();
98102
final Object unpackedProperty = unpackValue(unpackedValue.asArrayValue());
99-
res[resIdx++] = key;
103+
final String keyRevised = maybeReviseKeyForFlatgraph(key, allowedEdgePropertyKeys);
104+
res[resIdx++] = keyRevised;
100105
res[resIdx++] = unpackedProperty;
101106
}
102107
return res;
103108
}
104109

110+
/** In order to prepare for a potential rollback to overflowdb in the future, we're creating a
111+
* flatgraph->overflowdb converter. Since flatgraph supports exactly one edge property only,
112+
* there are no edge property names. So when we deserialise and find exactly one edge property with
113+
* the name `EDGE_PROPERTY`, and the schema defines exactly one edge property, then we assume that's the one.
114+
* Let's be clear: this is a dirty hack, but since we're phasing out overflowdb in favor of flatgraph, it's
115+
* arguably ok.
116+
*/
117+
private String maybeReviseKeyForFlatgraph(String key, Set<String> allowedEdgePropertyKeys) {
118+
if (FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME.equals(key) && allowedEdgePropertyKeys.size() == 1) {
119+
return allowedEdgePropertyKeys.iterator().next();
120+
} else return key;
121+
}
122+
105123
private final Object unpackValue(final ArrayValue packedValueAndType) {
106124
final Iterator<Value> iter = packedValueAndType.iterator();
107125
final byte valueTypeId = iter.next().asIntegerValue().asByte();

0 commit comments

Comments
 (0)