14
14
import overflowdb .util .StringInterner ;
15
15
16
16
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 .*;
21
18
22
19
public class NodeDeserializer extends BookKeeper {
23
20
protected final Graph graph ;
24
21
private final Map <String , NodeFactory > nodeFactoryByLabel ;
25
22
private final OdbStorage storage ;
26
23
private final StringInterner stringInterner ;
24
+ private static final String FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME = "EDGE_PROPERTY" ;
27
25
28
26
public NodeDeserializer (Graph graph , Map <String , NodeFactory > nodeFactoryByLabel , boolean statsEnabled , OdbStorage storage ) {
29
27
super (statsEnabled );
@@ -68,7 +66,9 @@ private void deserializeEdges(MessageUnpacker unpacker, NodeDb node, Direction d
68
66
for (int edgeIdx = 0 ; edgeIdx < edgeCount ; edgeIdx ++) {
69
67
long adjacentNodeId = unpacker .unpackLong ();
70
68
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 );
72
72
node .storeAdjacentNode (direction , edgeLabel , adjacentNode , edgeProperties );
73
73
}
74
74
}
@@ -87,7 +87,7 @@ public final NodeRef deserializeRef(byte[] bytes) throws IOException {
87
87
}
88
88
}
89
89
90
- private final Object [] unpackProperties (MessageUnpacker unpacker ) throws IOException {
90
+ private final Object [] unpackProperties (MessageUnpacker unpacker , Set < String > allowedEdgePropertyKeys ) throws IOException {
91
91
int propertyCount = unpacker .unpackMapHeader ();
92
92
Object [] res = new Object [propertyCount * 2 ];
93
93
int resIdx = 0 ;
@@ -96,12 +96,26 @@ private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOExcep
96
96
final String key = storage .reverseLookupStringToIntMapping (keyId );
97
97
final ImmutableValue unpackedValue = unpacker .unpackValue ();
98
98
final Object unpackedProperty = unpackValue (unpackedValue .asArrayValue ());
99
- res [resIdx ++] = key ;
99
+ final String keyRevised = maybeReviseKeyForFlatgraph (key , allowedEdgePropertyKeys );
100
+ res [resIdx ++] = keyRevised ;
100
101
res [resIdx ++] = unpackedProperty ;
101
102
}
102
103
return res ;
103
104
}
104
105
106
+ /** In order to prepare for a potential rollback to overflowdb in the future, we're creating a
107
+ * flatgraph->overflowdb converter. Since flatgraph supports exactly one edge property only,
108
+ * there are no edge property names. So when we deserialise and find exactly one edge property with
109
+ * the name `EDGE_PROPERTY`, and the schema defines exactly one edge property, then we assume that's the one.
110
+ * Let's be clear: this is a dirty hack, but since we're phasing out overflowdb in favor of flatgraph, it's
111
+ * arguably ok.
112
+ */
113
+ private String maybeReviseKeyForFlatgraph (String key , Set <String > allowedEdgePropertyKeys ) {
114
+ if (FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME .equals (key ) && allowedEdgePropertyKeys .size () == 1 ) {
115
+ return allowedEdgePropertyKeys .iterator ().next ();
116
+ } else return key ;
117
+ }
118
+
105
119
private final Object unpackValue (final ArrayValue packedValueAndType ) {
106
120
final Iterator <Value > iter = packedValueAndType .iterator ();
107
121
final byte valueTypeId = iter .next ().asIntegerValue ().asByte ();
0 commit comments