-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Neo4jPack.java
433 lines (411 loc) · 14 KB
/
Neo4jPack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt.v1.messaging;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import org.neo4j.bolt.v1.messaging.infrastructure.ValueNode;
import org.neo4j.bolt.v1.messaging.infrastructure.ValueRelationship;
import org.neo4j.bolt.v1.packstream.PackInput;
import org.neo4j.bolt.v1.packstream.PackOutput;
import org.neo4j.bolt.v1.packstream.PackStream;
import org.neo4j.bolt.v1.packstream.PackType;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.kernel.api.exceptions.Status;
import static org.neo4j.bolt.v1.packstream.PackStream.UNKNOWN_SIZE;
/**
* Extended PackStream packer and unpacker classes for working
* with Neo4j-specific data types, represented as structures.
*/
public class Neo4jPack
{
public static final List<Object> EMPTY_LIST = new ArrayList<>();
public static final Map<String, Object> EMPTY_MAP = new HashMap<>();
public static final byte NODE = 'N';
public static final byte RELATIONSHIP = 'R';
public static final byte UNBOUND_RELATIONSHIP = 'r';
public static final byte PATH = 'P';
public static class Packer extends PackStream.Packer
{
private PathPack.Packer pathPacker = new PathPack.Packer();
private Optional<Error> error = Optional.empty();
public Packer( PackOutput output )
{
super( output );
}
@SuppressWarnings( "unchecked" )
public void pack( Object obj ) throws IOException
{
// Note: below uses instanceof for quick implementation, this should be swapped over
// to a dedicated
// visitable type that the serializer can simply visit. This would create explicit
// contract for what can
// be serialized and allow performant method dispatch rather than if branching.
if ( obj == null )
{
packNull();
}
else if ( obj instanceof Boolean )
{
pack( (boolean) obj );
}
else if ( obj instanceof Byte || obj instanceof Short || obj instanceof Integer ||
obj instanceof Long )
{
pack( ((Number) obj).longValue() );
}
else if ( obj instanceof Float || obj instanceof Double )
{
pack( ((Number) obj).doubleValue() );
}
else if ( obj instanceof String )
{
pack( (String) obj );
}
else if (obj instanceof Character )
{
pack( (Character) obj );
}
else if ( obj instanceof Map )
{
Map<Object, Object> map = (Map<Object, Object>) obj;
packMapHeader( map.size() );
for ( Map.Entry<?, ?> entry : map.entrySet() )
{
pack( entry.getKey().toString() );
pack( entry.getValue() );
}
}
else if ( obj instanceof Collection )
{
Collection list = (Collection) obj;
packListHeader( list.size() );
for ( Object item : list )
{
pack( item );
}
}
else if ( obj instanceof byte[] )
{
error = Optional.of(new Error( Status.Request.Invalid,
"Binary values is not yet supported in Bolt"));
packNull();
}
else if ( obj instanceof char[] )
{
char[] array = (char[]) obj;
packListHeader( array.length );
for ( char item : array )
{
pack( item );
}
}
else if ( obj instanceof short[] )
{
short[] array = (short[]) obj;
packListHeader( array.length );
for ( short item : array )
{
pack( item );
}
}
else if ( obj instanceof int[] )
{
int[] array = (int[]) obj;
packListHeader( array.length );
for ( int item : array )
{
pack( item );
}
}
else if ( obj instanceof long[] )
{
long[] array = (long[]) obj;
packListHeader( array.length );
for ( long item : array )
{
pack( item );
}
}
else if ( obj instanceof float[] )
{
float[] array = (float[]) obj;
packListHeader( array.length );
for ( float item : array )
{
pack( item );
}
}
else if ( obj instanceof double[] )
{
double[] array = (double[]) obj;
packListHeader( array.length );
for ( double item : array )
{
pack( item );
}
}
else if ( obj instanceof boolean[] )
{
boolean[] array = (boolean[]) obj;
packListHeader( array.length );
for ( boolean item : array )
{
pack( item );
}
}
else if ( obj.getClass().isArray() )
{
Object[] array = (Object[]) obj;
packListHeader( array.length );
for ( Object item : array )
{
pack( item );
}
}
else if ( obj instanceof Node )
{
ValueNode.pack( this, (Node) obj );
}
else if ( obj instanceof Relationship )
{
ValueRelationship.pack( this, (Relationship) obj );
}
else if ( obj instanceof Path )
{
pathPacker.pack( this, (Path) obj );
}
else if ( obj instanceof Point)
{
error = Optional.of(new Error( Status.Request.Invalid,
"Point is not yet supported as a return type in Bolt"));
packNull();
}
else
{
error = Optional.of(new Error( Status.Request.Invalid,
"Unpackable value " + obj + " of type " + obj.getClass().getName() ));
packNull();
}
}
public void packRawMap( Map<String,Object> map ) throws IOException
{
packMapHeader( map.size() );
for ( Map.Entry<String,Object> entry : map.entrySet() )
{
pack( entry.getKey() );
pack( entry.getValue() );
}
}
public void consumeError( ) throws BoltIOException
{
if (error.isPresent())
{
Error e = error.get();
error = Optional.empty();
throw new BoltIOException( e.status(), e.msg() );
}
}
public boolean hasErrors()
{
return error.isPresent();
}
}
public static class Unpacker extends PackStream.Unpacker
{
private PathPack.Unpacker pathUnpacker = new PathPack.Unpacker();
public Unpacker( PackInput input )
{
super( input );
}
public Object unpack() throws IOException
{
PackType valType = peekNextType();
switch ( valType )
{
case STRING:
return unpackString();
case INTEGER:
return unpackLong();
case FLOAT:
return unpackDouble();
case BOOLEAN:
return unpackBoolean();
case NULL:
// still need to move past the null value
unpackNull();
return null;
case LIST:
{
return unpackList();
}
case MAP:
{
return unpackMap();
}
case STRUCT:
{
unpackStructHeader();
char signature = unpackStructSignature();
switch ( signature )
{
case NODE:
{
throw new BoltIOException( Status.Request.Invalid, "Nodes cannot be unpacked." );
}
case RELATIONSHIP:
{
throw new BoltIOException( Status.Request.Invalid, "Relationships cannot be unpacked." );
}
case UNBOUND_RELATIONSHIP:
{
throw new BoltIOException( Status.Request.Invalid, "Relationships cannot be unpacked." );
}
case PATH:
{
throw new BoltIOException( Status.Request.Invalid, "Paths cannot be unpacked." );
}
default:
throw new BoltIOException( Status.Request.InvalidFormat,
"Unknown struct type: " + Integer.toHexString( signature ) );
}
}
case END_OF_STREAM:
{
unpackEndOfStream();
return null;
}
default:
throw new BoltIOException( Status.Request.InvalidFormat,
"Unknown value type: " + valType );
}
}
public List<Object> unpackList() throws IOException
{
int size = (int) unpackListHeader();
if ( size == 0 )
{
return EMPTY_LIST;
}
ArrayList<Object> list;
if ( size == UNKNOWN_SIZE )
{
list = new ArrayList<>();
boolean more = true;
while ( more )
{
PackType keyType = peekNextType();
switch ( keyType )
{
case END_OF_STREAM:
unpack();
more = false;
break;
default:
list.add( unpack() );
}
}
}
else
{
list = new ArrayList<>( size );
for ( int i = 0; i < size; i++ )
{
list.add( unpack() );
}
}
return list;
}
public Map<String, Object> unpackMap() throws IOException
{
int size = (int) unpackMapHeader();
if ( size == 0 )
{
return EMPTY_MAP;
}
Map<String, Object> map;
if ( size == UNKNOWN_SIZE ) {
map = new HashMap<>();
boolean more = true;
while ( more )
{
PackType keyType = peekNextType();
switch ( keyType )
{
case END_OF_STREAM:
unpack();
more = false;
break;
case STRING:
String key = unpackString();
Object val = unpack();
if( map.put( key, val ) != null )
{
throw new BoltIOException( Status.Request.Invalid, "Duplicate map key `" + key + "`." );
}
break;
default:
throw new PackStream.PackStreamException( "Bad key type" );
}
}
}
else
{
map = new HashMap<>( size, 1 );
for ( int i = 0; i < size; i++ )
{
String key = unpackString();
Object val = unpack();
if( map.put( key, val ) != null )
{
throw new BoltIOException( Status.Request.Invalid, "Duplicate map key `" + key + "`." );
}
}
}
return map;
}
}
private static class Error
{
private final Status status;
private final String msg;
private Error( Status status, String msg )
{
this.status = status;
this.msg = msg;
}
Status status()
{
return status;
}
String msg()
{
return msg;
}
}
}