thrift 0.0.751142
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +2 -0
- data/COPYING +14 -0
- data/LICENSE +14 -0
- data/Makefile.am +15 -0
- data/Manifest +78 -0
- data/README +30 -0
- data/Rakefile +102 -0
- data/benchmark/Benchmark.thrift +5 -0
- data/benchmark/benchmark.rb +254 -0
- data/benchmark/client.rb +56 -0
- data/benchmark/gen-rb/BenchmarkService.rb +81 -0
- data/benchmark/gen-rb/Benchmark_constants.rb +11 -0
- data/benchmark/gen-rb/Benchmark_types.rb +10 -0
- data/benchmark/server.rb +64 -0
- data/benchmark/thin_server.rb +26 -0
- data/ext/binary_protocol_accelerated.c +463 -0
- data/ext/binary_protocol_accelerated.h +1 -0
- data/ext/constants.h +77 -0
- data/ext/extconf.rb +7 -0
- data/ext/memory_buffer.c +52 -0
- data/ext/memory_buffer.h +1 -0
- data/ext/protocol.c +166 -0
- data/ext/protocol.h +1 -0
- data/ext/struct.c +574 -0
- data/ext/struct.h +48 -0
- data/ext/thrift_native.c +173 -0
- data/lib/thrift/client.rb +44 -0
- data/lib/thrift/deprecation.rb +155 -0
- data/lib/thrift/exceptions.rb +65 -0
- data/lib/thrift/processor.rb +39 -0
- data/lib/thrift/protocol/binaryprotocol.rb +213 -0
- data/lib/thrift/protocol/binaryprotocolaccelerated.rb +19 -0
- data/lib/thrift/protocol/tbinaryprotocol.rb +2 -0
- data/lib/thrift/protocol/tprotocol.rb +2 -0
- data/lib/thrift/protocol.rb +270 -0
- data/lib/thrift/serializer.rb +27 -0
- data/lib/thrift/server/httpserver.rb +44 -0
- data/lib/thrift/server/nonblockingserver.rb +278 -0
- data/lib/thrift/server/thttpserver.rb +2 -0
- data/lib/thrift/server/tserver.rb +2 -0
- data/lib/thrift/server.rb +135 -0
- data/lib/thrift/struct.rb +272 -0
- data/lib/thrift/thrift.rb +14 -0
- data/lib/thrift/transport/httpclient.rb +29 -0
- data/lib/thrift/transport/socket.rb +167 -0
- data/lib/thrift/transport/thttpclient.rb +2 -0
- data/lib/thrift/transport/tsocket.rb +2 -0
- data/lib/thrift/transport/ttransport.rb +2 -0
- data/lib/thrift/transport/unixsocket.rb +58 -0
- data/lib/thrift/transport.rb +319 -0
- data/lib/thrift/types.rb +83 -0
- data/lib/thrift.rb +28 -0
- data/setup.rb +1585 -0
- data/spec/ThriftSpec.thrift +46 -0
- data/spec/backwards_compatibility_spec.rb +136 -0
- data/spec/binaryprotocol_spec.rb +45 -0
- data/spec/binaryprotocol_spec_shared.rb +274 -0
- data/spec/binaryprotocolaccelerated_spec.rb +101 -0
- data/spec/client_spec.rb +81 -0
- data/spec/deprecation_spec.rb +443 -0
- data/spec/exception_spec.rb +123 -0
- data/spec/gen-rb/NonblockingService.rb +268 -0
- data/spec/gen-rb/ThriftSpec_constants.rb +11 -0
- data/spec/gen-rb/ThriftSpec_types.rb +134 -0
- data/spec/httpclient_spec.rb +31 -0
- data/spec/httpserver_spec.rb +98 -0
- data/spec/nonblockingserver_spec.rb +245 -0
- data/spec/processor_spec.rb +64 -0
- data/spec/protocol_spec.rb +142 -0
- data/spec/serializer_spec.rb +52 -0
- data/spec/server_spec.rb +141 -0
- data/spec/socket_spec.rb +97 -0
- data/spec/socket_spec_shared.rb +85 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/struct_spec.rb +244 -0
- data/spec/transport_spec.rb +359 -0
- data/spec/types_spec.rb +98 -0
- data/spec/unixsocket_spec.rb +90 -0
- data/thrift.gemspec +33 -0
- data.tar.gz.sig +0 -0
- metadata +200 -0
- metadata.gz.sig +0 -0
data/benchmark/server.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require 'thrift'
|
3
|
+
require 'thrift/server/nonblockingserver'
|
4
|
+
$:.unshift File.dirname(__FILE__) + "/gen-rb"
|
5
|
+
require 'BenchmarkService'
|
6
|
+
|
7
|
+
module Server
|
8
|
+
include Thrift
|
9
|
+
|
10
|
+
class BenchmarkHandler
|
11
|
+
# 1-based index into the fibonacci sequence
|
12
|
+
def fibonacci(n)
|
13
|
+
seq = [1, 1]
|
14
|
+
3.upto(n) do
|
15
|
+
seq << seq[-1] + seq[-2]
|
16
|
+
end
|
17
|
+
seq[n-1] # n is 1-based
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.start_server(host, port, serverClass)
|
22
|
+
handler = BenchmarkHandler.new
|
23
|
+
processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
|
24
|
+
transport = ServerSocket.new(host, port)
|
25
|
+
transportFactory = FramedTransportFactory.new
|
26
|
+
args = [processor, transport, transportFactory, nil, 20]
|
27
|
+
if serverClass == NonblockingServer
|
28
|
+
logger = Logger.new(STDERR)
|
29
|
+
logger.level = Logger::WARN
|
30
|
+
args << logger
|
31
|
+
end
|
32
|
+
server = serverClass.new(*args)
|
33
|
+
@server_thread = Thread.new do
|
34
|
+
server.serve
|
35
|
+
end
|
36
|
+
@server = server
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.shutdown
|
40
|
+
return if @server.nil?
|
41
|
+
if @server.respond_to? :shutdown
|
42
|
+
@server.shutdown
|
43
|
+
else
|
44
|
+
@server_thread.kill
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def resolve_const(const)
|
50
|
+
const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
|
51
|
+
end
|
52
|
+
|
53
|
+
host, port, serverklass = ARGV
|
54
|
+
|
55
|
+
Server.start_server(host, port.to_i, resolve_const(serverklass))
|
56
|
+
|
57
|
+
# let our host know that the interpreter has started
|
58
|
+
# ideally we'd wait until the server was serving, but we don't have a hook for that
|
59
|
+
Marshal.dump(:started, STDOUT)
|
60
|
+
STDOUT.flush
|
61
|
+
|
62
|
+
Marshal.load(STDIN) # wait until we're instructed to shut down
|
63
|
+
|
64
|
+
Server.shutdown
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require 'thrift'
|
3
|
+
require 'thrift/server/nonblockingserver'
|
4
|
+
$:.unshift File.dirname(__FILE__) + "/gen-rb"
|
5
|
+
require 'BenchmarkService'
|
6
|
+
HOST = 'localhost'
|
7
|
+
PORT = 42587
|
8
|
+
|
9
|
+
class BenchmarkHandler
|
10
|
+
# 1-based index into the fibonacci sequence
|
11
|
+
def fibonacci(n)
|
12
|
+
seq = [1, 1]
|
13
|
+
3.upto(n) do
|
14
|
+
seq << seq[-1] + seq[-2]
|
15
|
+
end
|
16
|
+
seq[n-1] # n is 1-based
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
handler = BenchmarkHandler.new
|
21
|
+
processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
|
22
|
+
transport = Thrift::ServerSocket.new(HOST, PORT)
|
23
|
+
transportFactory = Thrift::FramedTransportFactory.new
|
24
|
+
logger = Logger.new(STDERR)
|
25
|
+
logger.level = Logger::WARN
|
26
|
+
Thrift::NonblockingServer.new(processor, transport, transportFactory, nil, 20, logger).serve
|
@@ -0,0 +1,463 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <stdbool.h>
|
3
|
+
#include <stdint.h>
|
4
|
+
#include <constants.h>
|
5
|
+
#include <struct.h>
|
6
|
+
|
7
|
+
#define GET_TRANSPORT(obj) rb_ivar_get(obj, transport_ivar_id)
|
8
|
+
#define GET_STRICT_READ(obj) rb_ivar_get(obj, strict_read_ivar_id)
|
9
|
+
#define GET_STRICT_WRITE(obj) rb_ivar_get(obj, strict_write_ivar_id)
|
10
|
+
#define WRITE(obj, data, length) rb_funcall(obj, write_method_id, 1, rb_str_new(data, length))
|
11
|
+
#define CHECK_NIL(obj) if (NIL_P(obj)) { rb_raise(rb_eStandardError, "nil argument not allowed!");}
|
12
|
+
|
13
|
+
VALUE rb_thrift_binary_proto_native_qmark(VALUE self) {
|
14
|
+
return Qtrue;
|
15
|
+
}
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
static int VERSION_1;
|
20
|
+
static int VERSION_MASK;
|
21
|
+
static int TYPE_MASK;
|
22
|
+
static int BAD_VERSION;
|
23
|
+
|
24
|
+
static void write_byte_direct(VALUE trans, int8_t b) {
|
25
|
+
WRITE(trans, (char*)&b, 1);
|
26
|
+
}
|
27
|
+
|
28
|
+
static void write_i16_direct(VALUE trans, int16_t value) {
|
29
|
+
char data[2];
|
30
|
+
|
31
|
+
data[1] = value;
|
32
|
+
data[0] = (value >> 8);
|
33
|
+
|
34
|
+
WRITE(trans, data, 2);
|
35
|
+
}
|
36
|
+
|
37
|
+
static void write_i32_direct(VALUE trans, int32_t value) {
|
38
|
+
char data[4];
|
39
|
+
|
40
|
+
data[3] = value;
|
41
|
+
data[2] = (value >> 8);
|
42
|
+
data[1] = (value >> 16);
|
43
|
+
data[0] = (value >> 24);
|
44
|
+
|
45
|
+
WRITE(trans, data, 4);
|
46
|
+
}
|
47
|
+
|
48
|
+
|
49
|
+
static void write_i64_direct(VALUE trans, int64_t value) {
|
50
|
+
char data[8];
|
51
|
+
|
52
|
+
data[7] = value;
|
53
|
+
data[6] = (value >> 8);
|
54
|
+
data[5] = (value >> 16);
|
55
|
+
data[4] = (value >> 24);
|
56
|
+
data[3] = (value >> 32);
|
57
|
+
data[2] = (value >> 40);
|
58
|
+
data[1] = (value >> 48);
|
59
|
+
data[0] = (value >> 56);
|
60
|
+
|
61
|
+
WRITE(trans, data, 8);
|
62
|
+
}
|
63
|
+
|
64
|
+
static void write_string_direct(VALUE trans, VALUE str) {
|
65
|
+
write_i32_direct(trans, RSTRING(str)->len);
|
66
|
+
rb_funcall(trans, write_method_id, 1, str);
|
67
|
+
}
|
68
|
+
|
69
|
+
//--------------------------------
|
70
|
+
// interface writing methods
|
71
|
+
//--------------------------------
|
72
|
+
|
73
|
+
VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
|
74
|
+
return Qnil;
|
75
|
+
}
|
76
|
+
|
77
|
+
VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
|
78
|
+
return Qnil;
|
79
|
+
}
|
80
|
+
|
81
|
+
VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
|
82
|
+
return Qnil;
|
83
|
+
}
|
84
|
+
|
85
|
+
VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
|
86
|
+
return Qnil;
|
87
|
+
}
|
88
|
+
|
89
|
+
VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
|
90
|
+
return Qnil;
|
91
|
+
}
|
92
|
+
|
93
|
+
VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
|
94
|
+
return Qnil;
|
95
|
+
}
|
96
|
+
|
97
|
+
VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
|
98
|
+
return Qnil;
|
99
|
+
}
|
100
|
+
|
101
|
+
VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
|
102
|
+
VALUE trans = GET_TRANSPORT(self);
|
103
|
+
VALUE strict_write = GET_STRICT_WRITE(self);
|
104
|
+
|
105
|
+
if (strict_write == Qtrue) {
|
106
|
+
write_i32_direct(trans, VERSION_1 | FIX2INT(type));
|
107
|
+
write_string_direct(trans, name);
|
108
|
+
write_i32_direct(trans, FIX2INT(seqid));
|
109
|
+
} else {
|
110
|
+
write_string_direct(trans, name);
|
111
|
+
write_byte_direct(trans, type);
|
112
|
+
write_i32_direct(trans, FIX2INT(seqid));
|
113
|
+
}
|
114
|
+
|
115
|
+
return Qnil;
|
116
|
+
}
|
117
|
+
|
118
|
+
VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
|
119
|
+
VALUE trans = GET_TRANSPORT(self);
|
120
|
+
write_byte_direct(trans, FIX2INT(type));
|
121
|
+
write_i16_direct(trans, FIX2INT(id));
|
122
|
+
|
123
|
+
return Qnil;
|
124
|
+
}
|
125
|
+
|
126
|
+
VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
|
127
|
+
write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
|
128
|
+
return Qnil;
|
129
|
+
}
|
130
|
+
|
131
|
+
VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
|
132
|
+
VALUE trans = GET_TRANSPORT(self);
|
133
|
+
write_byte_direct(trans, FIX2INT(ktype));
|
134
|
+
write_byte_direct(trans, FIX2INT(vtype));
|
135
|
+
write_i32_direct(trans, FIX2INT(size));
|
136
|
+
|
137
|
+
return Qnil;
|
138
|
+
}
|
139
|
+
|
140
|
+
VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
|
141
|
+
VALUE trans = GET_TRANSPORT(self);
|
142
|
+
write_byte_direct(trans, FIX2INT(etype));
|
143
|
+
write_i32_direct(trans, FIX2INT(size));
|
144
|
+
|
145
|
+
return Qnil;
|
146
|
+
}
|
147
|
+
|
148
|
+
VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
|
149
|
+
rb_thrift_binary_proto_write_list_begin(self, etype, size);
|
150
|
+
return Qnil;
|
151
|
+
}
|
152
|
+
|
153
|
+
VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
|
154
|
+
write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
|
155
|
+
return Qnil;
|
156
|
+
}
|
157
|
+
|
158
|
+
VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
|
159
|
+
CHECK_NIL(byte);
|
160
|
+
write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
|
161
|
+
return Qnil;
|
162
|
+
}
|
163
|
+
|
164
|
+
VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
|
165
|
+
CHECK_NIL(i16);
|
166
|
+
write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
|
167
|
+
return Qnil;
|
168
|
+
}
|
169
|
+
|
170
|
+
VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
|
171
|
+
CHECK_NIL(i32);
|
172
|
+
write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
|
173
|
+
return Qnil;
|
174
|
+
}
|
175
|
+
|
176
|
+
VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
|
177
|
+
CHECK_NIL(i64);
|
178
|
+
write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
|
179
|
+
return Qnil;
|
180
|
+
}
|
181
|
+
|
182
|
+
VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
|
183
|
+
CHECK_NIL(dub);
|
184
|
+
// Unfortunately, bitwise_cast doesn't work in C. Bad C!
|
185
|
+
union {
|
186
|
+
double f;
|
187
|
+
int64_t t;
|
188
|
+
} transfer;
|
189
|
+
transfer.f = RFLOAT(rb_Float(dub))->value;
|
190
|
+
write_i64_direct(GET_TRANSPORT(self), transfer.t);
|
191
|
+
|
192
|
+
return Qnil;
|
193
|
+
}
|
194
|
+
|
195
|
+
VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
|
196
|
+
CHECK_NIL(str);
|
197
|
+
VALUE trans = GET_TRANSPORT(self);
|
198
|
+
// write_i32_direct(trans, RSTRING(str)->len);
|
199
|
+
// rb_funcall(trans, write_method_id, 1, str);
|
200
|
+
write_string_direct(trans, str);
|
201
|
+
return Qnil;
|
202
|
+
}
|
203
|
+
|
204
|
+
//---------------------------------------
|
205
|
+
// interface reading methods
|
206
|
+
//---------------------------------------
|
207
|
+
|
208
|
+
#define READ(obj, length) rb_funcall(GET_TRANSPORT(obj), read_method_id, 1, INT2FIX(length))
|
209
|
+
|
210
|
+
VALUE rb_thrift_binary_proto_read_string(VALUE self);
|
211
|
+
VALUE rb_thrift_binary_proto_read_byte(VALUE self);
|
212
|
+
VALUE rb_thrift_binary_proto_read_i32(VALUE self);
|
213
|
+
VALUE rb_thrift_binary_proto_read_i16(VALUE self);
|
214
|
+
|
215
|
+
static char read_byte_direct(VALUE self) {
|
216
|
+
return (RSTRING(READ(self, 1))->ptr)[0];
|
217
|
+
}
|
218
|
+
|
219
|
+
static int16_t read_i16_direct(VALUE self) {
|
220
|
+
VALUE buf = READ(self, 2);
|
221
|
+
return (int16_t)(((uint8_t)(RSTRING(buf)->ptr[1])) | ((uint16_t)((RSTRING(buf)->ptr[0]) << 8)));
|
222
|
+
}
|
223
|
+
|
224
|
+
static int32_t read_i32_direct(VALUE self) {
|
225
|
+
VALUE buf = READ(self, 4);
|
226
|
+
return ((uint8_t)(RSTRING(buf)->ptr[3])) |
|
227
|
+
(((uint8_t)(RSTRING(buf)->ptr[2])) << 8) |
|
228
|
+
(((uint8_t)(RSTRING(buf)->ptr[1])) << 16) |
|
229
|
+
(((uint8_t)(RSTRING(buf)->ptr[0])) << 24);
|
230
|
+
}
|
231
|
+
|
232
|
+
static int64_t read_i64_direct(VALUE self) {
|
233
|
+
uint64_t hi = read_i32_direct(self);
|
234
|
+
uint32_t lo = read_i32_direct(self);
|
235
|
+
return (hi << 32) | lo;
|
236
|
+
}
|
237
|
+
|
238
|
+
static VALUE get_protocol_exception(VALUE code, VALUE message) {
|
239
|
+
VALUE args[2];
|
240
|
+
args[0] = code;
|
241
|
+
args[1] = message;
|
242
|
+
return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
|
243
|
+
}
|
244
|
+
|
245
|
+
VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
|
246
|
+
return Qnil;
|
247
|
+
}
|
248
|
+
|
249
|
+
VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
|
250
|
+
return Qnil;
|
251
|
+
}
|
252
|
+
|
253
|
+
VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
|
254
|
+
return Qnil;
|
255
|
+
}
|
256
|
+
|
257
|
+
VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
|
258
|
+
return Qnil;
|
259
|
+
}
|
260
|
+
|
261
|
+
VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
|
262
|
+
return Qnil;
|
263
|
+
}
|
264
|
+
|
265
|
+
VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
|
266
|
+
return Qnil;
|
267
|
+
}
|
268
|
+
|
269
|
+
VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
|
270
|
+
return Qnil;
|
271
|
+
}
|
272
|
+
|
273
|
+
VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
|
274
|
+
VALUE strict_read = GET_STRICT_READ(self);
|
275
|
+
VALUE name, seqid;
|
276
|
+
int type;
|
277
|
+
|
278
|
+
int version = read_i32_direct(self);
|
279
|
+
|
280
|
+
if (version < 0) {
|
281
|
+
if ((version & VERSION_MASK) != VERSION_1) {
|
282
|
+
rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
|
283
|
+
}
|
284
|
+
type = version & TYPE_MASK;
|
285
|
+
name = rb_thrift_binary_proto_read_string(self);
|
286
|
+
seqid = rb_thrift_binary_proto_read_i32(self);
|
287
|
+
} else {
|
288
|
+
if (strict_read == Qtrue) {
|
289
|
+
rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
|
290
|
+
}
|
291
|
+
name = READ(self, version);
|
292
|
+
type = rb_thrift_binary_proto_read_byte(self);
|
293
|
+
seqid = rb_thrift_binary_proto_read_i32(self);
|
294
|
+
}
|
295
|
+
|
296
|
+
return rb_ary_new3(3, name, INT2FIX(type), seqid);
|
297
|
+
}
|
298
|
+
|
299
|
+
VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
|
300
|
+
int type = read_byte_direct(self);
|
301
|
+
if (type == TTYPE_STOP) {
|
302
|
+
return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
|
303
|
+
} else {
|
304
|
+
VALUE id = rb_thrift_binary_proto_read_i16(self);
|
305
|
+
return rb_ary_new3(3, Qnil, INT2FIX(type), id);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
|
310
|
+
VALUE ktype = rb_thrift_binary_proto_read_byte(self);
|
311
|
+
VALUE vtype = rb_thrift_binary_proto_read_byte(self);
|
312
|
+
VALUE size = rb_thrift_binary_proto_read_i32(self);
|
313
|
+
return rb_ary_new3(3, ktype, vtype, size);
|
314
|
+
}
|
315
|
+
|
316
|
+
VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
|
317
|
+
VALUE etype = rb_thrift_binary_proto_read_byte(self);
|
318
|
+
VALUE size = rb_thrift_binary_proto_read_i32(self);
|
319
|
+
return rb_ary_new3(2, etype, size);
|
320
|
+
}
|
321
|
+
|
322
|
+
VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
|
323
|
+
return rb_thrift_binary_proto_read_list_begin(self);
|
324
|
+
}
|
325
|
+
|
326
|
+
VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
|
327
|
+
char byte = read_byte_direct(self);
|
328
|
+
return byte == 1 ? Qtrue : Qfalse;
|
329
|
+
}
|
330
|
+
|
331
|
+
VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
|
332
|
+
return INT2FIX(read_byte_direct(self));
|
333
|
+
}
|
334
|
+
|
335
|
+
VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
|
336
|
+
return INT2FIX(read_i16_direct(self));
|
337
|
+
}
|
338
|
+
|
339
|
+
VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
|
340
|
+
return INT2NUM(read_i32_direct(self));
|
341
|
+
}
|
342
|
+
|
343
|
+
VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
|
344
|
+
return LL2NUM(read_i64_direct(self));
|
345
|
+
}
|
346
|
+
|
347
|
+
VALUE rb_thrift_binary_proto_read_double(VALUE self) {
|
348
|
+
union {
|
349
|
+
double f;
|
350
|
+
int64_t t;
|
351
|
+
} transfer;
|
352
|
+
transfer.t = read_i64_direct(self);
|
353
|
+
return rb_float_new(transfer.f);
|
354
|
+
}
|
355
|
+
|
356
|
+
VALUE rb_thrift_binary_proto_read_string(VALUE self) {
|
357
|
+
int size = read_i32_direct(self);
|
358
|
+
return READ(self, size);
|
359
|
+
}
|
360
|
+
|
361
|
+
void Init_binary_protocol_accelerated() {
|
362
|
+
VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
|
363
|
+
|
364
|
+
VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
|
365
|
+
VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
|
366
|
+
TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
|
367
|
+
|
368
|
+
VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
|
369
|
+
|
370
|
+
rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
|
371
|
+
|
372
|
+
rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
|
373
|
+
rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
|
374
|
+
rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
|
375
|
+
rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
|
376
|
+
rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
|
377
|
+
rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
|
378
|
+
rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
|
379
|
+
rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
|
380
|
+
rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
|
381
|
+
rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
|
382
|
+
rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
|
383
|
+
rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
|
384
|
+
rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
|
385
|
+
// unused methods
|
386
|
+
rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
|
387
|
+
rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
|
388
|
+
rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
|
389
|
+
rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
|
390
|
+
rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
|
391
|
+
rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
|
392
|
+
rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
|
393
|
+
|
394
|
+
|
395
|
+
|
396
|
+
rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
|
397
|
+
rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
|
398
|
+
rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
|
399
|
+
rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
|
400
|
+
rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
|
401
|
+
rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
|
402
|
+
rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
|
403
|
+
rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
|
404
|
+
rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
|
405
|
+
rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
|
406
|
+
rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
|
407
|
+
rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
|
408
|
+
// unused methods
|
409
|
+
rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
|
410
|
+
rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
|
411
|
+
rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
|
412
|
+
rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
|
413
|
+
rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
|
414
|
+
rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
|
415
|
+
rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
|
416
|
+
|
417
|
+
// set up native method table
|
418
|
+
native_proto_method_table *npmt;
|
419
|
+
npmt = ALLOC(native_proto_method_table);
|
420
|
+
|
421
|
+
npmt->write_field_begin = rb_thrift_binary_proto_write_field_begin;
|
422
|
+
npmt->write_field_stop = rb_thrift_binary_proto_write_field_stop;
|
423
|
+
npmt->write_map_begin = rb_thrift_binary_proto_write_map_begin;
|
424
|
+
npmt->write_list_begin = rb_thrift_binary_proto_write_list_begin;
|
425
|
+
npmt->write_set_begin = rb_thrift_binary_proto_write_set_begin;
|
426
|
+
npmt->write_byte = rb_thrift_binary_proto_write_byte;
|
427
|
+
npmt->write_bool = rb_thrift_binary_proto_write_bool;
|
428
|
+
npmt->write_i16 = rb_thrift_binary_proto_write_i16;
|
429
|
+
npmt->write_i32 = rb_thrift_binary_proto_write_i32;
|
430
|
+
npmt->write_i64 = rb_thrift_binary_proto_write_i64;
|
431
|
+
npmt->write_double = rb_thrift_binary_proto_write_double;
|
432
|
+
npmt->write_string = rb_thrift_binary_proto_write_string;
|
433
|
+
npmt->write_message_end = rb_thrift_binary_proto_write_message_end;
|
434
|
+
npmt->write_struct_begin = rb_thrift_binary_proto_write_struct_begin;
|
435
|
+
npmt->write_struct_end = rb_thrift_binary_proto_write_struct_end;
|
436
|
+
npmt->write_field_end = rb_thrift_binary_proto_write_field_end;
|
437
|
+
npmt->write_map_end = rb_thrift_binary_proto_write_map_end;
|
438
|
+
npmt->write_list_end = rb_thrift_binary_proto_write_list_end;
|
439
|
+
npmt->write_set_end = rb_thrift_binary_proto_write_set_end;
|
440
|
+
|
441
|
+
npmt->read_message_begin = rb_thrift_binary_proto_read_message_begin;
|
442
|
+
npmt->read_field_begin = rb_thrift_binary_proto_read_field_begin;
|
443
|
+
npmt->read_map_begin = rb_thrift_binary_proto_read_map_begin;
|
444
|
+
npmt->read_list_begin = rb_thrift_binary_proto_read_list_begin;
|
445
|
+
npmt->read_set_begin = rb_thrift_binary_proto_read_set_begin;
|
446
|
+
npmt->read_byte = rb_thrift_binary_proto_read_byte;
|
447
|
+
npmt->read_bool = rb_thrift_binary_proto_read_bool;
|
448
|
+
npmt->read_i16 = rb_thrift_binary_proto_read_i16;
|
449
|
+
npmt->read_i32 = rb_thrift_binary_proto_read_i32;
|
450
|
+
npmt->read_i64 = rb_thrift_binary_proto_read_i64;
|
451
|
+
npmt->read_double = rb_thrift_binary_proto_read_double;
|
452
|
+
npmt->read_string = rb_thrift_binary_proto_read_string;
|
453
|
+
npmt->read_message_end = rb_thrift_binary_proto_read_message_end;
|
454
|
+
npmt->read_struct_begin = rb_thift_binary_proto_read_struct_begin;
|
455
|
+
npmt->read_struct_end = rb_thift_binary_proto_read_struct_end;
|
456
|
+
npmt->read_field_end = rb_thift_binary_proto_read_field_end;
|
457
|
+
npmt->read_map_end = rb_thift_binary_proto_read_map_end;
|
458
|
+
npmt->read_list_end = rb_thift_binary_proto_read_list_end;
|
459
|
+
npmt->read_set_end = rb_thift_binary_proto_read_set_end;
|
460
|
+
|
461
|
+
VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt);
|
462
|
+
rb_const_set(bpa_class, rb_intern("@native_method_table"), method_table_object);
|
463
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
void Init_binary_protocol_accelerated();
|
data/ext/constants.h
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
|
2
|
+
extern int TTYPE_STOP;
|
3
|
+
extern int TTYPE_BOOL;
|
4
|
+
extern int TTYPE_BYTE;
|
5
|
+
extern int TTYPE_I16;
|
6
|
+
extern int TTYPE_I32;
|
7
|
+
extern int TTYPE_I64;
|
8
|
+
extern int TTYPE_DOUBLE;
|
9
|
+
extern int TTYPE_STRING;
|
10
|
+
extern int TTYPE_MAP;
|
11
|
+
extern int TTYPE_SET;
|
12
|
+
extern int TTYPE_LIST;
|
13
|
+
extern int TTYPE_STRUCT;
|
14
|
+
|
15
|
+
extern ID validate_method_id;
|
16
|
+
extern ID write_struct_begin_method_id;
|
17
|
+
extern ID write_struct_end_method_id;
|
18
|
+
extern ID write_field_begin_method_id;
|
19
|
+
extern ID write_field_end_method_id;
|
20
|
+
extern ID write_boolean_method_id;
|
21
|
+
extern ID write_byte_method_id;
|
22
|
+
extern ID write_i16_method_id;
|
23
|
+
extern ID write_i32_method_id;
|
24
|
+
extern ID write_i64_method_id;
|
25
|
+
extern ID write_double_method_id;
|
26
|
+
extern ID write_string_method_id;
|
27
|
+
extern ID write_map_begin_method_id;
|
28
|
+
extern ID write_map_end_method_id;
|
29
|
+
extern ID write_list_begin_method_id;
|
30
|
+
extern ID write_list_end_method_id;
|
31
|
+
extern ID write_set_begin_method_id;
|
32
|
+
extern ID write_set_end_method_id;
|
33
|
+
extern ID size_method_id;
|
34
|
+
extern ID read_bool_method_id;
|
35
|
+
extern ID read_byte_method_id;
|
36
|
+
extern ID read_i16_method_id;
|
37
|
+
extern ID read_i32_method_id;
|
38
|
+
extern ID read_i64_method_id;
|
39
|
+
extern ID read_string_method_id;
|
40
|
+
extern ID read_double_method_id;
|
41
|
+
extern ID read_map_begin_method_id;
|
42
|
+
extern ID read_map_end_method_id;
|
43
|
+
extern ID read_list_begin_method_id;
|
44
|
+
extern ID read_list_end_method_id;
|
45
|
+
extern ID read_set_begin_method_id;
|
46
|
+
extern ID read_set_end_method_id;
|
47
|
+
extern ID read_struct_begin_method_id;
|
48
|
+
extern ID read_struct_end_method_id;
|
49
|
+
extern ID read_field_begin_method_id;
|
50
|
+
extern ID read_field_end_method_id;
|
51
|
+
extern ID keys_method_id;
|
52
|
+
extern ID entries_method_id;
|
53
|
+
extern ID name_method_id;
|
54
|
+
extern ID sort_method_id;
|
55
|
+
extern ID write_field_stop_method_id;
|
56
|
+
extern ID skip_method_id;
|
57
|
+
extern ID write_method_id;
|
58
|
+
extern ID read_method_id;
|
59
|
+
extern ID native_qmark_method_id;
|
60
|
+
|
61
|
+
extern ID fields_const_id;
|
62
|
+
extern ID transport_ivar_id;
|
63
|
+
extern ID strict_read_ivar_id;
|
64
|
+
extern ID strict_write_ivar_id;
|
65
|
+
|
66
|
+
extern VALUE type_sym;
|
67
|
+
extern VALUE name_sym;
|
68
|
+
extern VALUE key_sym;
|
69
|
+
extern VALUE value_sym;
|
70
|
+
extern VALUE element_sym;
|
71
|
+
extern VALUE class_sym;
|
72
|
+
|
73
|
+
extern VALUE rb_cSet;
|
74
|
+
extern VALUE thrift_module;
|
75
|
+
extern VALUE thrift_types_module;
|
76
|
+
extern VALUE class_thrift_protocol;
|
77
|
+
extern VALUE protocol_exception_class;
|
data/ext/extconf.rb
ADDED
data/ext/memory_buffer.c
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <constants.h>
|
3
|
+
|
4
|
+
ID buf_ivar_id;
|
5
|
+
ID index_ivar_id;
|
6
|
+
|
7
|
+
ID slice_method_id;
|
8
|
+
|
9
|
+
int GARBAGE_BUFFER_SIZE;
|
10
|
+
|
11
|
+
#define GET_BUF(self) rb_ivar_get(self, buf_ivar_id)
|
12
|
+
|
13
|
+
VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str) {
|
14
|
+
VALUE buf = GET_BUF(self);
|
15
|
+
rb_str_buf_cat(buf, RSTRING(str)->ptr, RSTRING(str)->len);
|
16
|
+
return Qnil;
|
17
|
+
}
|
18
|
+
|
19
|
+
VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value) {
|
20
|
+
int length = FIX2INT(length_value);
|
21
|
+
|
22
|
+
VALUE index_value = rb_ivar_get(self, index_ivar_id);
|
23
|
+
int index = FIX2INT(index_value);
|
24
|
+
|
25
|
+
VALUE buf = GET_BUF(self);
|
26
|
+
VALUE data = rb_funcall(buf, slice_method_id, 2, index_value, length_value);
|
27
|
+
|
28
|
+
index += length;
|
29
|
+
if (index > RSTRING(buf)->len) {
|
30
|
+
index = RSTRING(buf)->len;
|
31
|
+
}
|
32
|
+
if (index >= GARBAGE_BUFFER_SIZE) {
|
33
|
+
rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(-1)));
|
34
|
+
index = 0;
|
35
|
+
}
|
36
|
+
|
37
|
+
rb_ivar_set(self, index_ivar_id, INT2FIX(index));
|
38
|
+
return data;
|
39
|
+
}
|
40
|
+
|
41
|
+
void Init_memory_buffer() {
|
42
|
+
VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBuffer"));
|
43
|
+
rb_define_method(thrift_memory_buffer_class, "write", rb_thrift_memory_buffer_write, 1);
|
44
|
+
rb_define_method(thrift_memory_buffer_class, "read", rb_thrift_memory_buffer_read, 1);
|
45
|
+
|
46
|
+
buf_ivar_id = rb_intern("@buf");
|
47
|
+
index_ivar_id = rb_intern("@index");
|
48
|
+
|
49
|
+
slice_method_id = rb_intern("slice");
|
50
|
+
|
51
|
+
GARBAGE_BUFFER_SIZE = FIX2INT(rb_const_get(thrift_memory_buffer_class, rb_intern("GARBAGE_BUFFER_SIZE")));
|
52
|
+
}
|
data/ext/memory_buffer.h
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
void Init_memory_buffer();
|