-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PackStreamMessageFormatV1.java
349 lines (304 loc) · 11.5 KB
/
PackStreamMessageFormatV1.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
/*
* 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.Map;
import org.neo4j.bolt.v1.messaging.message.Message;
import org.neo4j.bolt.v1.packstream.PackStream;
import org.neo4j.bolt.v1.runtime.spi.Record;
import org.neo4j.kernel.api.exceptions.Status;
import static org.neo4j.bolt.v1.runtime.internal.Neo4jError.codeFromString;
import static org.neo4j.bolt.v1.runtime.spi.Records.record;
public class PackStreamMessageFormatV1 implements MessageFormat
{
public static final int VERSION = 1;
@Override
public int version()
{
return VERSION;
}
public interface MessageTypes
{
byte MSG_INIT = 0x01;
byte MSG_RESET = 0x0F;
byte MSG_RUN = 0x10;
byte MSG_DISCARD_ALL = 0x2F;
byte MSG_PULL_ALL = 0x3F;
byte MSG_RECORD = 0x71;
byte MSG_SUCCESS = 0x70;
byte MSG_IGNORED = 0x7E;
byte MSG_FAILURE = 0x7F;
}
static String messageTypeName( int type )
{
switch( type )
{
case MessageTypes.MSG_INIT: return "MSG_INIT";
case MessageTypes.MSG_RESET: return "MSG_RESET";
case MessageTypes.MSG_RUN: return "MSG_RUN";
case MessageTypes.MSG_DISCARD_ALL: return "MSG_DISCARD_ALL";
case MessageTypes.MSG_PULL_ALL: return "MSG_PULL_ALL";
case MessageTypes.MSG_RECORD: return "MSG_RECORD";
case MessageTypes.MSG_SUCCESS: return "MSG_SUCCESS";
case MessageTypes.MSG_IGNORED: return "MSG_IGNORED";
case MessageTypes.MSG_FAILURE: return "MSG_FAILURE";
default: return "0x" + Integer.toHexString(type);
}
}
public static class Writer implements MessageFormat.Writer
{
public static final MessageBoundaryHook NO_OP = new MessageBoundaryHook()
{
@Override
public void onMessageComplete() throws IOException
{
}
};
private final Neo4jPack.Packer packer;
private final MessageBoundaryHook onMessageComplete;
/**
* @param packer serializer to output channel
* @param onMessageComplete invoked for each message, after it's done writing to the output
*/
public Writer( Neo4jPack.Packer packer, MessageBoundaryHook onMessageComplete )
{
this.packer = packer;
this.onMessageComplete = onMessageComplete;
}
@Override
public Writer write( Message message ) throws IOException
{
message.dispatch( this );
return this;
}
@Override
public void handleRunMessage( String statement, Map<String,Object> params )
throws IOException
{
packer.packStructHeader( 2, MessageTypes.MSG_RUN );
packer.pack( statement );
packer.packRawMap( params );
onMessageComplete.onMessageComplete();
}
@Override
public void handlePullAllMessage()
throws IOException
{
packer.packStructHeader( 0, MessageTypes.MSG_PULL_ALL );
onMessageComplete.onMessageComplete();
}
@Override
public void handleDiscardAllMessage()
throws IOException
{
packer.packStructHeader( 0, MessageTypes.MSG_DISCARD_ALL );
onMessageComplete.onMessageComplete();
}
@Override
public void handleRecordMessage( Record item )
throws IOException
{
Object[] fields = item.fields();
packer.packStructHeader( 1, MessageTypes.MSG_RECORD );
packer.packListHeader( fields.length );
for ( Object field : fields )
{
packer.pack( field );
}
onMessageComplete.onMessageComplete();
}
@Override
public void handleSuccessMessage( Map<String,Object> metadata )
throws IOException
{
packer.packStructHeader( 1, MessageTypes.MSG_SUCCESS );
packer.packRawMap( metadata );
onMessageComplete.onMessageComplete();
}
@Override
public void handleFailureMessage( Status status, String message )
throws IOException
{
packer.packStructHeader( 1, MessageTypes.MSG_FAILURE );
packer.packMapHeader( 2 );
packer.pack( "code" );
packer.pack( status.code().serialize() );
packer.pack( "message" );
packer.pack( message );
onMessageComplete.onMessageComplete();
}
@Override
public void handleIgnoredMessage() throws IOException
{
packer.packStructHeader( 0, MessageTypes.MSG_IGNORED );
onMessageComplete.onMessageComplete();
}
@Override
public void handleInitMessage( String clientName ) throws IOException
{
packer.packStructHeader( 1, MessageTypes.MSG_INIT );
packer.pack( clientName );
onMessageComplete.onMessageComplete();
}
@Override
public void handleResetMessage() throws IOException
{
packer.packStructHeader( 0, MessageTypes.MSG_RESET );
onMessageComplete.onMessageComplete();
}
@Override
public void flush() throws IOException
{
packer.flush();
}
}
public static class Reader implements MessageFormat.Reader
{
private final Neo4jPack.Unpacker unpacker;
public Reader( Neo4jPack.Unpacker unpacker )
{
this.unpacker = unpacker;
}
@Override
public boolean hasNext() throws IOException
{
return unpacker.hasNext();
}
/**
* Parse a single message into the given consumer.
*/
@Override
public <E extends Exception> void read( MessageHandler<E> output ) throws IOException, E
{
try
{
unpacker.unpackStructHeader();
int type = (int) unpacker.unpackLong();
try
{
switch ( type )
{
case MessageTypes.MSG_RUN:
unpackRunMessage( output );
break;
case MessageTypes.MSG_DISCARD_ALL:
unpackDiscardAllMessage( output );
break;
case MessageTypes.MSG_PULL_ALL:
unpackPullAllMessage( output );
break;
case MessageTypes.MSG_RECORD:
unpackRecordMessage( output );
break;
case MessageTypes.MSG_SUCCESS:
unpackSuccessMessage( output );
break;
case MessageTypes.MSG_FAILURE:
unpackFailureMessage( output );
break;
case MessageTypes.MSG_IGNORED:
unpackIgnoredMessage( output );
break;
case MessageTypes.MSG_INIT:
unpackInitMessage( output );
break;
case MessageTypes.MSG_RESET:
unpackResetMessage( output );
break;
default:
throw new BoltIOException( Status.Request.Invalid,
"0x" + Integer.toHexString(type) + " is not a valid message type." );
}
}
catch( PackStream.PackStreamException e )
{
throw new BoltIOException( Status.Request.InvalidFormat,
"Unable to read " + messageTypeName (type) + " message. " +
"Error was: " + e.getMessage(), e );
}
}
catch( PackStream.PackStreamException e )
{
throw new BoltIOException( Status.Request.InvalidFormat, "Unable to read message type. " +
"Error was: " + e.getMessage(), e );
}
}
private <E extends Exception> void unpackSuccessMessage( MessageHandler<E> output )
throws E, IOException
{
Map<String,Object> map = unpacker.unpackMap();
output.handleSuccessMessage( map );
}
private <E extends Exception> void unpackFailureMessage( MessageHandler<E> output )
throws E, IOException
{
Map<String,Object> map = unpacker.unpackMap();
String codeStr = map.containsKey( "code" ) ?
(String) map.get( "code" ) :
Status.General.UnknownFailure.name();
String msg = map.containsKey( "message" ) ?
(String) map.get( "message" ) :
"<No message supplied>";
output.handleFailureMessage( codeFromString( codeStr ), msg );
}
private <E extends Exception> void unpackIgnoredMessage( MessageHandler<E> output )
throws E
{
output.handleIgnoredMessage();
}
private <E extends Exception> void unpackRecordMessage( MessageHandler<E> output )
throws E, IOException
{
long length = unpacker.unpackListHeader();
final Object[] fields = new Object[(int) length];
for ( int i = 0; i < length; i++ )
{
fields[i] = unpacker.unpack();
}
output.handleRecordMessage( record( fields ) );
}
private <E extends Exception> void unpackRunMessage( MessageHandler<E> output )
throws E, IOException
{
String statement = unpacker.unpackString();
Map<String,Object> params = unpacker.unpackMap();
output.handleRunMessage( statement, params );
}
private <E extends Exception> void unpackDiscardAllMessage( MessageHandler<E> output )
throws E, IOException
{
output.handleDiscardAllMessage();
}
private <E extends Exception> void unpackPullAllMessage( MessageHandler<E> output )
throws E, IOException
{
output.handlePullAllMessage();
}
private <E extends Exception> void unpackInitMessage( MessageHandler<E> output ) throws IOException, E
{
String clientName = unpacker.unpackString();
output.handleInitMessage( clientName );
}
private <E extends Exception> void unpackResetMessage( MessageHandler<E> output ) throws IOException, E
{
output.handleResetMessage();
}
}
}