thrift-mavericks 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG +1 -0
- data/README +43 -0
- data/benchmark/Benchmark.thrift +24 -0
- data/benchmark/benchmark.rb +271 -0
- data/benchmark/client.rb +74 -0
- data/benchmark/gen-rb/benchmark_constants.rb +11 -0
- data/benchmark/gen-rb/benchmark_service.rb +80 -0
- data/benchmark/gen-rb/benchmark_types.rb +10 -0
- data/benchmark/server.rb +82 -0
- data/benchmark/thin_server.rb +44 -0
- data/ext/binary_protocol_accelerated.c +441 -0
- data/ext/binary_protocol_accelerated.h +20 -0
- data/ext/compact_protocol.c +618 -0
- data/ext/compact_protocol.h +20 -0
- data/ext/constants.h +96 -0
- data/ext/extconf.rb +30 -0
- data/ext/macros.h +41 -0
- data/ext/memory_buffer.c +131 -0
- data/ext/memory_buffer.h +20 -0
- data/ext/protocol.c +185 -0
- data/ext/protocol.h +20 -0
- data/ext/strlcpy.c +41 -0
- data/ext/strlcpy.h +32 -0
- data/ext/struct.c +691 -0
- data/ext/struct.h +25 -0
- data/ext/thrift_native.c +196 -0
- data/lib/thrift.rb +64 -0
- data/lib/thrift/client.rb +62 -0
- data/lib/thrift/core_ext.rb +23 -0
- data/lib/thrift/core_ext/fixnum.rb +29 -0
- data/lib/thrift/exceptions.rb +84 -0
- data/lib/thrift/processor.rb +57 -0
- data/lib/thrift/protocol/base_protocol.rb +290 -0
- data/lib/thrift/protocol/binary_protocol.rb +229 -0
- data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
- data/lib/thrift/protocol/compact_protocol.rb +426 -0
- data/lib/thrift/serializer/deserializer.rb +33 -0
- data/lib/thrift/serializer/serializer.rb +34 -0
- data/lib/thrift/server/base_server.rb +31 -0
- data/lib/thrift/server/mongrel_http_server.rb +58 -0
- data/lib/thrift/server/nonblocking_server.rb +305 -0
- data/lib/thrift/server/simple_server.rb +43 -0
- data/lib/thrift/server/thread_pool_server.rb +75 -0
- data/lib/thrift/server/threaded_server.rb +47 -0
- data/lib/thrift/struct.rb +237 -0
- data/lib/thrift/struct_union.rb +192 -0
- data/lib/thrift/thrift_native.rb +24 -0
- data/lib/thrift/transport/base_server_transport.rb +37 -0
- data/lib/thrift/transport/base_transport.rb +107 -0
- data/lib/thrift/transport/buffered_transport.rb +108 -0
- data/lib/thrift/transport/framed_transport.rb +116 -0
- data/lib/thrift/transport/http_client_transport.rb +51 -0
- data/lib/thrift/transport/io_stream_transport.rb +39 -0
- data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
- data/lib/thrift/transport/server_socket.rb +63 -0
- data/lib/thrift/transport/socket.rb +137 -0
- data/lib/thrift/transport/unix_server_socket.rb +60 -0
- data/lib/thrift/transport/unix_socket.rb +40 -0
- data/lib/thrift/types.rb +101 -0
- data/lib/thrift/union.rb +179 -0
- data/spec/ThriftSpec.thrift +132 -0
- data/spec/base_protocol_spec.rb +160 -0
- data/spec/base_transport_spec.rb +351 -0
- data/spec/binary_protocol_accelerated_spec.rb +46 -0
- data/spec/binary_protocol_spec.rb +61 -0
- data/spec/binary_protocol_spec_shared.rb +375 -0
- data/spec/client_spec.rb +100 -0
- data/spec/compact_protocol_spec.rb +144 -0
- data/spec/exception_spec.rb +142 -0
- data/spec/gen-rb/nonblocking_service.rb +272 -0
- data/spec/gen-rb/thrift_spec_constants.rb +11 -0
- data/spec/gen-rb/thrift_spec_types.rb +346 -0
- data/spec/http_client_spec.rb +64 -0
- data/spec/mongrel_http_server_spec.rb +117 -0
- data/spec/nonblocking_server_spec.rb +265 -0
- data/spec/processor_spec.rb +83 -0
- data/spec/serializer_spec.rb +69 -0
- data/spec/server_socket_spec.rb +80 -0
- data/spec/server_spec.rb +159 -0
- data/spec/socket_spec.rb +61 -0
- data/spec/socket_spec_shared.rb +104 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/struct_spec.rb +295 -0
- data/spec/types_spec.rb +116 -0
- data/spec/union_spec.rb +193 -0
- data/spec/unix_socket_spec.rb +108 -0
- data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
- data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
- data/test/debug_proto/gen-rb/empty_service.rb +24 -0
- data/test/debug_proto/gen-rb/inherited.rb +79 -0
- data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
- data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
- data/test/debug_proto/gen-rb/srv.rb +330 -0
- metadata +281 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
/*
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
|
20
|
+
void Init_binary_protocol_accelerated();
|
@@ -0,0 +1,618 @@
|
|
1
|
+
/**
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include <ruby.h>
|
21
|
+
#include <stdbool.h>
|
22
|
+
#include <stdint.h>
|
23
|
+
#include "constants.h"
|
24
|
+
#include "struct.h"
|
25
|
+
#include "macros.h"
|
26
|
+
|
27
|
+
#define LAST_ID(obj) FIX2INT(rb_ary_pop(rb_ivar_get(obj, last_field_id)))
|
28
|
+
#define SET_LAST_ID(obj, val) rb_ary_push(rb_ivar_get(obj, last_field_id), val)
|
29
|
+
|
30
|
+
VALUE rb_thrift_compact_proto_native_qmark(VALUE self) {
|
31
|
+
return Qtrue;
|
32
|
+
}
|
33
|
+
|
34
|
+
static ID last_field_id;
|
35
|
+
static ID boolean_field_id;
|
36
|
+
static ID bool_value_id;
|
37
|
+
static ID rbuf_ivar_id;
|
38
|
+
|
39
|
+
static int VERSION;
|
40
|
+
static int VERSION_MASK;
|
41
|
+
static int TYPE_MASK;
|
42
|
+
static int TYPE_SHIFT_AMOUNT;
|
43
|
+
static int PROTOCOL_ID;
|
44
|
+
|
45
|
+
static VALUE thrift_compact_protocol_class;
|
46
|
+
|
47
|
+
static int CTYPE_BOOLEAN_TRUE = 0x01;
|
48
|
+
static int CTYPE_BOOLEAN_FALSE = 0x02;
|
49
|
+
static int CTYPE_BYTE = 0x03;
|
50
|
+
static int CTYPE_I16 = 0x04;
|
51
|
+
static int CTYPE_I32 = 0x05;
|
52
|
+
static int CTYPE_I64 = 0x06;
|
53
|
+
static int CTYPE_DOUBLE = 0x07;
|
54
|
+
static int CTYPE_BINARY = 0x08;
|
55
|
+
static int CTYPE_LIST = 0x09;
|
56
|
+
static int CTYPE_SET = 0x0A;
|
57
|
+
static int CTYPE_MAP = 0x0B;
|
58
|
+
static int CTYPE_STRUCT = 0x0C;
|
59
|
+
|
60
|
+
VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16);
|
61
|
+
|
62
|
+
// TODO: implement this
|
63
|
+
static int get_compact_type(VALUE type_value) {
|
64
|
+
int type = FIX2INT(type_value);
|
65
|
+
if (type == TTYPE_BOOL) {
|
66
|
+
return CTYPE_BOOLEAN_TRUE;
|
67
|
+
} else if (type == TTYPE_BYTE) {
|
68
|
+
return CTYPE_BYTE;
|
69
|
+
} else if (type == TTYPE_I16) {
|
70
|
+
return CTYPE_I16;
|
71
|
+
} else if (type == TTYPE_I32) {
|
72
|
+
return CTYPE_I32;
|
73
|
+
} else if (type == TTYPE_I64) {
|
74
|
+
return CTYPE_I64;
|
75
|
+
} else if (type == TTYPE_DOUBLE) {
|
76
|
+
return CTYPE_DOUBLE;
|
77
|
+
} else if (type == TTYPE_STRING) {
|
78
|
+
return CTYPE_BINARY;
|
79
|
+
} else if (type == TTYPE_LIST) {
|
80
|
+
return CTYPE_LIST;
|
81
|
+
} else if (type == TTYPE_SET) {
|
82
|
+
return CTYPE_SET;
|
83
|
+
} else if (type == TTYPE_MAP) {
|
84
|
+
return CTYPE_MAP;
|
85
|
+
} else if (type == TTYPE_STRUCT) {
|
86
|
+
return CTYPE_STRUCT;
|
87
|
+
} else {
|
88
|
+
char str[50];
|
89
|
+
sprintf(str, "don't know what type: %d", type);
|
90
|
+
rb_raise(rb_eStandardError, "%s", str);
|
91
|
+
return 0;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
static void write_byte_direct(VALUE transport, int8_t b) {
|
96
|
+
WRITE(transport, (char*)&b, 1);
|
97
|
+
}
|
98
|
+
|
99
|
+
static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, VALUE type_override) {
|
100
|
+
int id = FIX2INT(id_value);
|
101
|
+
int last_id = LAST_ID(self);
|
102
|
+
VALUE transport = GET_TRANSPORT(self);
|
103
|
+
|
104
|
+
// if there's a type override, use that.
|
105
|
+
int8_t type_to_write = RTEST(type_override) ? FIX2INT(type_override) : get_compact_type(type);
|
106
|
+
// check if we can use delta encoding for the field id
|
107
|
+
int diff = id - last_id;
|
108
|
+
if (diff > 0 && diff <= 15) {
|
109
|
+
// write them together
|
110
|
+
write_byte_direct(transport, diff << 4 | (type_to_write & 0x0f));
|
111
|
+
} else {
|
112
|
+
// write them separate
|
113
|
+
write_byte_direct(transport, type_to_write & 0x0f);
|
114
|
+
rb_thrift_compact_proto_write_i16(self, id_value);
|
115
|
+
}
|
116
|
+
|
117
|
+
SET_LAST_ID(self, id_value);
|
118
|
+
}
|
119
|
+
|
120
|
+
static int32_t int_to_zig_zag(int32_t n) {
|
121
|
+
return (n << 1) ^ (n >> 31);
|
122
|
+
}
|
123
|
+
|
124
|
+
static uint64_t ll_to_zig_zag(int64_t n) {
|
125
|
+
return (n << 1) ^ (n >> 63);
|
126
|
+
}
|
127
|
+
|
128
|
+
static void write_varint32(VALUE transport, uint32_t n) {
|
129
|
+
while (true) {
|
130
|
+
if ((n & ~0x7F) == 0) {
|
131
|
+
write_byte_direct(transport, n & 0x7f);
|
132
|
+
break;
|
133
|
+
} else {
|
134
|
+
write_byte_direct(transport, (n & 0x7F) | 0x80);
|
135
|
+
n = n >> 7;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
static void write_varint64(VALUE transport, uint64_t n) {
|
141
|
+
while (true) {
|
142
|
+
if ((n & ~0x7F) == 0) {
|
143
|
+
write_byte_direct(transport, n & 0x7f);
|
144
|
+
break;
|
145
|
+
} else {
|
146
|
+
write_byte_direct(transport, (n & 0x7F) | 0x80);
|
147
|
+
n = n >> 7;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
static void write_collection_begin(VALUE transport, VALUE elem_type, VALUE size_value) {
|
153
|
+
int size = FIX2INT(size_value);
|
154
|
+
if (size <= 14) {
|
155
|
+
write_byte_direct(transport, size << 4 | get_compact_type(elem_type));
|
156
|
+
} else {
|
157
|
+
write_byte_direct(transport, 0xf0 | get_compact_type(elem_type));
|
158
|
+
write_varint32(transport, size);
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
//--------------------------------
|
164
|
+
// interface writing methods
|
165
|
+
//--------------------------------
|
166
|
+
|
167
|
+
VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
|
168
|
+
VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
|
169
|
+
|
170
|
+
VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
|
171
|
+
return Qnil;
|
172
|
+
}
|
173
|
+
|
174
|
+
VALUE rb_thrift_compact_proto_write_struct_begin(VALUE self, VALUE name) {
|
175
|
+
rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
|
176
|
+
return Qnil;
|
177
|
+
}
|
178
|
+
|
179
|
+
VALUE rb_thrift_compact_proto_write_struct_end(VALUE self) {
|
180
|
+
rb_ary_pop(rb_ivar_get(self, last_field_id));
|
181
|
+
return Qnil;
|
182
|
+
}
|
183
|
+
|
184
|
+
VALUE rb_thrift_compact_proto_write_field_end(VALUE self) {
|
185
|
+
return Qnil;
|
186
|
+
}
|
187
|
+
|
188
|
+
VALUE rb_thrift_compact_proto_write_map_end(VALUE self) {
|
189
|
+
return Qnil;
|
190
|
+
}
|
191
|
+
|
192
|
+
VALUE rb_thrift_compact_proto_write_list_end(VALUE self) {
|
193
|
+
return Qnil;
|
194
|
+
}
|
195
|
+
|
196
|
+
VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
|
197
|
+
return Qnil;
|
198
|
+
}
|
199
|
+
|
200
|
+
VALUE rb_thrift_compact_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
|
201
|
+
VALUE transport = GET_TRANSPORT(self);
|
202
|
+
write_byte_direct(transport, PROTOCOL_ID);
|
203
|
+
write_byte_direct(transport, (VERSION & VERSION_MASK) | ((FIX2INT(type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
|
204
|
+
write_varint32(transport, FIX2INT(seqid));
|
205
|
+
rb_thrift_compact_proto_write_string(self, name);
|
206
|
+
|
207
|
+
return Qnil;
|
208
|
+
}
|
209
|
+
|
210
|
+
VALUE rb_thrift_compact_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
|
211
|
+
if (FIX2INT(type) == TTYPE_BOOL) {
|
212
|
+
// we want to possibly include the value, so we'll wait.
|
213
|
+
rb_ivar_set(self, boolean_field_id, rb_ary_new3(2, type, id));
|
214
|
+
} else {
|
215
|
+
write_field_begin_internal(self, type, id, Qnil);
|
216
|
+
}
|
217
|
+
|
218
|
+
return Qnil;
|
219
|
+
}
|
220
|
+
|
221
|
+
VALUE rb_thrift_compact_proto_write_field_stop(VALUE self) {
|
222
|
+
write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
|
223
|
+
return Qnil;
|
224
|
+
}
|
225
|
+
|
226
|
+
VALUE rb_thrift_compact_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size_value) {
|
227
|
+
int size = FIX2INT(size_value);
|
228
|
+
VALUE transport = GET_TRANSPORT(self);
|
229
|
+
if (size == 0) {
|
230
|
+
write_byte_direct(transport, 0);
|
231
|
+
} else {
|
232
|
+
write_varint32(transport, size);
|
233
|
+
write_byte_direct(transport, get_compact_type(ktype) << 4 | get_compact_type(vtype));
|
234
|
+
}
|
235
|
+
return Qnil;
|
236
|
+
}
|
237
|
+
|
238
|
+
VALUE rb_thrift_compact_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
|
239
|
+
write_collection_begin(GET_TRANSPORT(self), etype, size);
|
240
|
+
return Qnil;
|
241
|
+
}
|
242
|
+
|
243
|
+
VALUE rb_thrift_compact_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
|
244
|
+
write_collection_begin(GET_TRANSPORT(self), etype, size);
|
245
|
+
return Qnil;
|
246
|
+
}
|
247
|
+
|
248
|
+
VALUE rb_thrift_compact_proto_write_bool(VALUE self, VALUE b) {
|
249
|
+
int8_t type = b == Qtrue ? CTYPE_BOOLEAN_TRUE : CTYPE_BOOLEAN_FALSE;
|
250
|
+
VALUE boolean_field = rb_ivar_get(self, boolean_field_id);
|
251
|
+
if (NIL_P(boolean_field)) {
|
252
|
+
// we're not part of a field, so just write the value.
|
253
|
+
write_byte_direct(GET_TRANSPORT(self), type);
|
254
|
+
} else {
|
255
|
+
// we haven't written the field header yet
|
256
|
+
write_field_begin_internal(self, rb_ary_entry(boolean_field, 0), rb_ary_entry(boolean_field, 1), INT2FIX(type));
|
257
|
+
rb_ivar_set(self, boolean_field_id, Qnil);
|
258
|
+
}
|
259
|
+
return Qnil;
|
260
|
+
}
|
261
|
+
|
262
|
+
VALUE rb_thrift_compact_proto_write_byte(VALUE self, VALUE byte) {
|
263
|
+
CHECK_NIL(byte);
|
264
|
+
write_byte_direct(GET_TRANSPORT(self), FIX2INT(byte));
|
265
|
+
return Qnil;
|
266
|
+
}
|
267
|
+
|
268
|
+
VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16) {
|
269
|
+
rb_thrift_compact_proto_write_i32(self, i16);
|
270
|
+
return Qnil;
|
271
|
+
}
|
272
|
+
|
273
|
+
VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32) {
|
274
|
+
CHECK_NIL(i32);
|
275
|
+
write_varint32(GET_TRANSPORT(self), int_to_zig_zag(NUM2INT(i32)));
|
276
|
+
return Qnil;
|
277
|
+
}
|
278
|
+
|
279
|
+
VALUE rb_thrift_compact_proto_write_i64(VALUE self, VALUE i64) {
|
280
|
+
CHECK_NIL(i64);
|
281
|
+
write_varint64(GET_TRANSPORT(self), ll_to_zig_zag(NUM2LL(i64)));
|
282
|
+
return Qnil;
|
283
|
+
}
|
284
|
+
|
285
|
+
VALUE rb_thrift_compact_proto_write_double(VALUE self, VALUE dub) {
|
286
|
+
CHECK_NIL(dub);
|
287
|
+
// Unfortunately, bitwise_cast doesn't work in C. Bad C!
|
288
|
+
union {
|
289
|
+
double f;
|
290
|
+
int64_t l;
|
291
|
+
} transfer;
|
292
|
+
transfer.f = RFLOAT_VALUE(rb_Float(dub));
|
293
|
+
char buf[8];
|
294
|
+
buf[0] = transfer.l & 0xff;
|
295
|
+
buf[1] = (transfer.l >> 8) & 0xff;
|
296
|
+
buf[2] = (transfer.l >> 16) & 0xff;
|
297
|
+
buf[3] = (transfer.l >> 24) & 0xff;
|
298
|
+
buf[4] = (transfer.l >> 32) & 0xff;
|
299
|
+
buf[5] = (transfer.l >> 40) & 0xff;
|
300
|
+
buf[6] = (transfer.l >> 48) & 0xff;
|
301
|
+
buf[7] = (transfer.l >> 56) & 0xff;
|
302
|
+
WRITE(GET_TRANSPORT(self), buf, 8);
|
303
|
+
return Qnil;
|
304
|
+
}
|
305
|
+
|
306
|
+
VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
|
307
|
+
VALUE transport = GET_TRANSPORT(self);
|
308
|
+
write_varint32(transport, RSTRING_LEN(str));
|
309
|
+
WRITE(transport, RSTRING_PTR(str), RSTRING_LEN(str));
|
310
|
+
return Qnil;
|
311
|
+
}
|
312
|
+
|
313
|
+
//---------------------------------------
|
314
|
+
// interface reading methods
|
315
|
+
//---------------------------------------
|
316
|
+
|
317
|
+
#define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
|
318
|
+
|
319
|
+
VALUE rb_thrift_compact_proto_read_string(VALUE self);
|
320
|
+
VALUE rb_thrift_compact_proto_read_byte(VALUE self);
|
321
|
+
VALUE rb_thrift_compact_proto_read_i32(VALUE self);
|
322
|
+
VALUE rb_thrift_compact_proto_read_i16(VALUE self);
|
323
|
+
|
324
|
+
static int8_t get_ttype(int8_t ctype) {
|
325
|
+
if (ctype == TTYPE_STOP) {
|
326
|
+
return TTYPE_STOP;
|
327
|
+
} else if (ctype == CTYPE_BOOLEAN_TRUE || ctype == CTYPE_BOOLEAN_FALSE) {
|
328
|
+
return TTYPE_BOOL;
|
329
|
+
} else if (ctype == CTYPE_BYTE) {
|
330
|
+
return TTYPE_BYTE;
|
331
|
+
} else if (ctype == CTYPE_I16) {
|
332
|
+
return TTYPE_I16;
|
333
|
+
} else if (ctype == CTYPE_I32) {
|
334
|
+
return TTYPE_I32;
|
335
|
+
} else if (ctype == CTYPE_I64) {
|
336
|
+
return TTYPE_I64;
|
337
|
+
} else if (ctype == CTYPE_DOUBLE) {
|
338
|
+
return TTYPE_DOUBLE;
|
339
|
+
} else if (ctype == CTYPE_BINARY) {
|
340
|
+
return TTYPE_STRING;
|
341
|
+
} else if (ctype == CTYPE_LIST) {
|
342
|
+
return TTYPE_LIST;
|
343
|
+
} else if (ctype == CTYPE_SET) {
|
344
|
+
return TTYPE_SET;
|
345
|
+
} else if (ctype == CTYPE_MAP) {
|
346
|
+
return TTYPE_MAP;
|
347
|
+
} else if (ctype == CTYPE_STRUCT) {
|
348
|
+
return TTYPE_STRUCT;
|
349
|
+
} else {
|
350
|
+
char str[50];
|
351
|
+
sprintf(str, "don't know what type: %d", ctype);
|
352
|
+
rb_raise(rb_eStandardError, "%s", str);
|
353
|
+
return 0;
|
354
|
+
}
|
355
|
+
}
|
356
|
+
|
357
|
+
static char read_byte_direct(VALUE self) {
|
358
|
+
VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
|
359
|
+
return (char)(FIX2INT(byte));
|
360
|
+
}
|
361
|
+
|
362
|
+
static int64_t zig_zag_to_ll(int64_t n) {
|
363
|
+
return (((uint64_t)n) >> 1) ^ -(n & 1);
|
364
|
+
}
|
365
|
+
|
366
|
+
static int32_t zig_zag_to_int(int32_t n) {
|
367
|
+
return (((uint32_t)n) >> 1) ^ -(n & 1);
|
368
|
+
}
|
369
|
+
|
370
|
+
static int64_t read_varint64(VALUE self) {
|
371
|
+
int shift = 0;
|
372
|
+
int64_t result = 0;
|
373
|
+
while (true) {
|
374
|
+
int8_t b = read_byte_direct(self);
|
375
|
+
result = result | ((uint64_t)(b & 0x7f) << shift);
|
376
|
+
if ((b & 0x80) != 0x80) {
|
377
|
+
break;
|
378
|
+
}
|
379
|
+
shift += 7;
|
380
|
+
}
|
381
|
+
return result;
|
382
|
+
}
|
383
|
+
|
384
|
+
static int16_t read_i16(VALUE self) {
|
385
|
+
return zig_zag_to_int((int32_t)read_varint64(self));
|
386
|
+
}
|
387
|
+
|
388
|
+
static VALUE get_protocol_exception(VALUE code, VALUE message) {
|
389
|
+
VALUE args[2];
|
390
|
+
args[0] = code;
|
391
|
+
args[1] = message;
|
392
|
+
return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
|
393
|
+
}
|
394
|
+
|
395
|
+
VALUE rb_thrift_compact_proto_read_message_end(VALUE self) {
|
396
|
+
return Qnil;
|
397
|
+
}
|
398
|
+
|
399
|
+
VALUE rb_thrift_compact_proto_read_struct_begin(VALUE self) {
|
400
|
+
rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
|
401
|
+
return Qnil;
|
402
|
+
}
|
403
|
+
|
404
|
+
VALUE rb_thrift_compact_proto_read_struct_end(VALUE self) {
|
405
|
+
rb_ary_pop(rb_ivar_get(self, last_field_id));
|
406
|
+
return Qnil;
|
407
|
+
}
|
408
|
+
|
409
|
+
VALUE rb_thrift_compact_proto_read_field_end(VALUE self) {
|
410
|
+
return Qnil;
|
411
|
+
}
|
412
|
+
|
413
|
+
VALUE rb_thrift_compact_proto_read_map_end(VALUE self) {
|
414
|
+
return Qnil;
|
415
|
+
}
|
416
|
+
|
417
|
+
VALUE rb_thrift_compact_proto_read_list_end(VALUE self) {
|
418
|
+
return Qnil;
|
419
|
+
}
|
420
|
+
|
421
|
+
VALUE rb_thrift_compact_proto_read_set_end(VALUE self) {
|
422
|
+
return Qnil;
|
423
|
+
}
|
424
|
+
|
425
|
+
VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
|
426
|
+
int8_t protocol_id = read_byte_direct(self);
|
427
|
+
if (protocol_id != PROTOCOL_ID) {
|
428
|
+
char buf[100];
|
429
|
+
int len = sprintf(buf, "Expected protocol id %d but got %d", PROTOCOL_ID, protocol_id);
|
430
|
+
buf[len] = 0;
|
431
|
+
rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
|
432
|
+
}
|
433
|
+
|
434
|
+
int8_t version_and_type = read_byte_direct(self);
|
435
|
+
int8_t version = version_and_type & VERSION_MASK;
|
436
|
+
if (version != VERSION) {
|
437
|
+
char buf[100];
|
438
|
+
int len = sprintf(buf, "Expected version id %d but got %d", version, VERSION);
|
439
|
+
buf[len] = 0;
|
440
|
+
rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
|
441
|
+
}
|
442
|
+
|
443
|
+
int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03;
|
444
|
+
int32_t seqid = read_varint64(self);
|
445
|
+
VALUE messageName = rb_thrift_compact_proto_read_string(self);
|
446
|
+
return rb_ary_new3(3, messageName, INT2FIX(type), INT2NUM(seqid));
|
447
|
+
}
|
448
|
+
|
449
|
+
VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
|
450
|
+
int8_t type = read_byte_direct(self);
|
451
|
+
// if it's a stop, then we can return immediately, as the struct is over.
|
452
|
+
if ((type & 0x0f) == TTYPE_STOP) {
|
453
|
+
return rb_ary_new3(3, Qnil, INT2FIX(0), INT2FIX(0));
|
454
|
+
} else {
|
455
|
+
int field_id = 0;
|
456
|
+
|
457
|
+
// mask off the 4 MSB of the type header. it could contain a field id delta.
|
458
|
+
uint8_t modifier = ((type & 0xf0) >> 4);
|
459
|
+
|
460
|
+
if (modifier == 0) {
|
461
|
+
// not a delta. look ahead for the zigzag varint field id.
|
462
|
+
(void) LAST_ID(self);
|
463
|
+
field_id = read_i16(self);
|
464
|
+
} else {
|
465
|
+
// has a delta. add the delta to the last read field id.
|
466
|
+
field_id = LAST_ID(self) + modifier;
|
467
|
+
}
|
468
|
+
|
469
|
+
// if this happens to be a boolean field, the value is encoded in the type
|
470
|
+
if (is_bool_type(type)) {
|
471
|
+
// save the boolean value in a special instance variable.
|
472
|
+
rb_ivar_set(self, bool_value_id, (type & 0x0f) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse);
|
473
|
+
}
|
474
|
+
|
475
|
+
// push the new field onto the field stack so we can keep the deltas going.
|
476
|
+
SET_LAST_ID(self, INT2FIX(field_id));
|
477
|
+
return rb_ary_new3(3, Qnil, INT2FIX(get_ttype(type & 0x0f)), INT2FIX(field_id));
|
478
|
+
}
|
479
|
+
}
|
480
|
+
|
481
|
+
VALUE rb_thrift_compact_proto_read_map_begin(VALUE self) {
|
482
|
+
int32_t size = read_varint64(self);
|
483
|
+
uint8_t key_and_value_type = size == 0 ? 0 : read_byte_direct(self);
|
484
|
+
return rb_ary_new3(3, INT2FIX(get_ttype(key_and_value_type >> 4)), INT2FIX(get_ttype(key_and_value_type & 0xf)), INT2FIX(size));
|
485
|
+
}
|
486
|
+
|
487
|
+
VALUE rb_thrift_compact_proto_read_list_begin(VALUE self) {
|
488
|
+
uint8_t size_and_type = read_byte_direct(self);
|
489
|
+
int32_t size = (size_and_type >> 4) & 0x0f;
|
490
|
+
if (size == 15) {
|
491
|
+
size = read_varint64(self);
|
492
|
+
}
|
493
|
+
uint8_t type = get_ttype(size_and_type & 0x0f);
|
494
|
+
return rb_ary_new3(2, INT2FIX(type), INT2FIX(size));
|
495
|
+
}
|
496
|
+
|
497
|
+
VALUE rb_thrift_compact_proto_read_set_begin(VALUE self) {
|
498
|
+
return rb_thrift_compact_proto_read_list_begin(self);
|
499
|
+
}
|
500
|
+
|
501
|
+
VALUE rb_thrift_compact_proto_read_bool(VALUE self) {
|
502
|
+
VALUE bool_value = rb_ivar_get(self, bool_value_id);
|
503
|
+
if (NIL_P(bool_value)) {
|
504
|
+
return read_byte_direct(self) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse;
|
505
|
+
} else {
|
506
|
+
rb_ivar_set(self, bool_value_id, Qnil);
|
507
|
+
return bool_value;
|
508
|
+
}
|
509
|
+
}
|
510
|
+
|
511
|
+
VALUE rb_thrift_compact_proto_read_byte(VALUE self) {
|
512
|
+
return INT2FIX(read_byte_direct(self));
|
513
|
+
}
|
514
|
+
|
515
|
+
VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
|
516
|
+
return INT2FIX(read_i16(self));
|
517
|
+
}
|
518
|
+
|
519
|
+
VALUE rb_thrift_compact_proto_read_i32(VALUE self) {
|
520
|
+
return INT2NUM(zig_zag_to_int(read_varint64(self)));
|
521
|
+
}
|
522
|
+
|
523
|
+
VALUE rb_thrift_compact_proto_read_i64(VALUE self) {
|
524
|
+
return LL2NUM(zig_zag_to_ll(read_varint64(self)));
|
525
|
+
}
|
526
|
+
|
527
|
+
VALUE rb_thrift_compact_proto_read_double(VALUE self) {
|
528
|
+
union {
|
529
|
+
double f;
|
530
|
+
int64_t l;
|
531
|
+
} transfer;
|
532
|
+
VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
|
533
|
+
rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
|
534
|
+
uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[0]))
|
535
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 8)
|
536
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 16)
|
537
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[3])) << 24);
|
538
|
+
uint64_t hi = (((uint8_t)(RSTRING_PTR(rbuf)[4])))
|
539
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 8)
|
540
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 16)
|
541
|
+
| (((uint8_t)(RSTRING_PTR(rbuf)[7])) << 24);
|
542
|
+
transfer.l = (hi << 32) | lo;
|
543
|
+
|
544
|
+
return rb_float_new(transfer.f);
|
545
|
+
}
|
546
|
+
|
547
|
+
VALUE rb_thrift_compact_proto_read_string(VALUE self) {
|
548
|
+
int64_t size = read_varint64(self);
|
549
|
+
return READ(self, size);
|
550
|
+
}
|
551
|
+
|
552
|
+
static void Init_constants() {
|
553
|
+
thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
|
554
|
+
|
555
|
+
VERSION = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
|
556
|
+
VERSION_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
|
557
|
+
TYPE_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
|
558
|
+
TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
|
559
|
+
PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
|
560
|
+
|
561
|
+
last_field_id = rb_intern("@last_field");
|
562
|
+
boolean_field_id = rb_intern("@boolean_field");
|
563
|
+
bool_value_id = rb_intern("@bool_value");
|
564
|
+
rbuf_ivar_id = rb_intern("@rbuf");
|
565
|
+
}
|
566
|
+
|
567
|
+
static void Init_rb_methods() {
|
568
|
+
rb_define_method(thrift_compact_protocol_class, "native?", rb_thrift_compact_proto_native_qmark, 0);
|
569
|
+
|
570
|
+
rb_define_method(thrift_compact_protocol_class, "write_message_begin", rb_thrift_compact_proto_write_message_begin, 3);
|
571
|
+
rb_define_method(thrift_compact_protocol_class, "write_field_begin", rb_thrift_compact_proto_write_field_begin, 3);
|
572
|
+
rb_define_method(thrift_compact_protocol_class, "write_field_stop", rb_thrift_compact_proto_write_field_stop, 0);
|
573
|
+
rb_define_method(thrift_compact_protocol_class, "write_map_begin", rb_thrift_compact_proto_write_map_begin, 3);
|
574
|
+
rb_define_method(thrift_compact_protocol_class, "write_list_begin", rb_thrift_compact_proto_write_list_begin, 2);
|
575
|
+
rb_define_method(thrift_compact_protocol_class, "write_set_begin", rb_thrift_compact_proto_write_set_begin, 2);
|
576
|
+
rb_define_method(thrift_compact_protocol_class, "write_byte", rb_thrift_compact_proto_write_byte, 1);
|
577
|
+
rb_define_method(thrift_compact_protocol_class, "write_bool", rb_thrift_compact_proto_write_bool, 1);
|
578
|
+
rb_define_method(thrift_compact_protocol_class, "write_i16", rb_thrift_compact_proto_write_i16, 1);
|
579
|
+
rb_define_method(thrift_compact_protocol_class, "write_i32", rb_thrift_compact_proto_write_i32, 1);
|
580
|
+
rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
|
581
|
+
rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
|
582
|
+
rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
|
583
|
+
|
584
|
+
rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
|
585
|
+
rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
|
586
|
+
rb_define_method(thrift_compact_protocol_class, "write_struct_end", rb_thrift_compact_proto_write_struct_end, 0);
|
587
|
+
rb_define_method(thrift_compact_protocol_class, "write_field_end", rb_thrift_compact_proto_write_field_end, 0);
|
588
|
+
rb_define_method(thrift_compact_protocol_class, "write_map_end", rb_thrift_compact_proto_write_map_end, 0);
|
589
|
+
rb_define_method(thrift_compact_protocol_class, "write_list_end", rb_thrift_compact_proto_write_list_end, 0);
|
590
|
+
rb_define_method(thrift_compact_protocol_class, "write_set_end", rb_thrift_compact_proto_write_set_end, 0);
|
591
|
+
|
592
|
+
|
593
|
+
rb_define_method(thrift_compact_protocol_class, "read_message_begin", rb_thrift_compact_proto_read_message_begin, 0);
|
594
|
+
rb_define_method(thrift_compact_protocol_class, "read_field_begin", rb_thrift_compact_proto_read_field_begin, 0);
|
595
|
+
rb_define_method(thrift_compact_protocol_class, "read_map_begin", rb_thrift_compact_proto_read_map_begin, 0);
|
596
|
+
rb_define_method(thrift_compact_protocol_class, "read_list_begin", rb_thrift_compact_proto_read_list_begin, 0);
|
597
|
+
rb_define_method(thrift_compact_protocol_class, "read_set_begin", rb_thrift_compact_proto_read_set_begin, 0);
|
598
|
+
rb_define_method(thrift_compact_protocol_class, "read_byte", rb_thrift_compact_proto_read_byte, 0);
|
599
|
+
rb_define_method(thrift_compact_protocol_class, "read_bool", rb_thrift_compact_proto_read_bool, 0);
|
600
|
+
rb_define_method(thrift_compact_protocol_class, "read_i16", rb_thrift_compact_proto_read_i16, 0);
|
601
|
+
rb_define_method(thrift_compact_protocol_class, "read_i32", rb_thrift_compact_proto_read_i32, 0);
|
602
|
+
rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
|
603
|
+
rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
|
604
|
+
rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
|
605
|
+
|
606
|
+
rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
|
607
|
+
rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);
|
608
|
+
rb_define_method(thrift_compact_protocol_class, "read_struct_end", rb_thrift_compact_proto_read_struct_end, 0);
|
609
|
+
rb_define_method(thrift_compact_protocol_class, "read_field_end", rb_thrift_compact_proto_read_field_end, 0);
|
610
|
+
rb_define_method(thrift_compact_protocol_class, "read_map_end", rb_thrift_compact_proto_read_map_end, 0);
|
611
|
+
rb_define_method(thrift_compact_protocol_class, "read_list_end", rb_thrift_compact_proto_read_list_end, 0);
|
612
|
+
rb_define_method(thrift_compact_protocol_class, "read_set_end", rb_thrift_compact_proto_read_set_end, 0);
|
613
|
+
}
|
614
|
+
|
615
|
+
void Init_compact_protocol() {
|
616
|
+
Init_constants();
|
617
|
+
Init_rb_methods();
|
618
|
+
}
|