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 {
20
+ public static final String FLATGRAPH_DEFAULT_EDGE_PROPERTY_NAME = "EDGE_PROPERTY" ;
23
21
protected final Graph graph ;
24
22
private final Map <String , NodeFactory > nodeFactoryByLabel ;
25
23
private final OdbStorage storage ;
@@ -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
}
@@ -88,6 +88,10 @@ public final NodeRef deserializeRef(byte[] bytes) throws IOException {
88
88
}
89
89
90
90
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 {
91
95
int propertyCount = unpacker .unpackMapHeader ();
92
96
Object [] res = new Object [propertyCount * 2 ];
93
97
int resIdx = 0 ;
@@ -96,12 +100,26 @@ private final Object[] unpackProperties(MessageUnpacker unpacker) throws IOExcep
96
100
final String key = storage .reverseLookupStringToIntMapping (keyId );
97
101
final ImmutableValue unpackedValue = unpacker .unpackValue ();
98
102
final Object unpackedProperty = unpackValue (unpackedValue .asArrayValue ());
99
- res [resIdx ++] = key ;
103
+ final String keyRevised = maybeReviseKeyForFlatgraph (key , allowedEdgePropertyKeys );
104
+ res [resIdx ++] = keyRevised ;
100
105
res [resIdx ++] = unpackedProperty ;
101
106
}
102
107
return res ;
103
108
}
104
109
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
+
105
123
private final Object unpackValue (final ArrayValue packedValueAndType ) {
106
124
final Iterator <Value > iter = packedValueAndType .iterator ();
107
125
final byte valueTypeId = iter .next ().asIntegerValue ().asByte ();
0 commit comments