Class: Thrift::CompactProtocol
Defined Under Namespace
Classes: CompactTypes
Constant Summary
collapse
- PROTOCOL_ID =
[0x82].pack('c').unpack('c').first
- VERSION =
1
- VERSION_MASK =
0x1f
- TYPE_MASK =
0xE0
- TYPE_BITS =
0x07
- TYPE_SHIFT_AMOUNT =
5
- TSTOP =
[nil, Types::STOP, 0]
Instance Attribute Summary
Attributes inherited from BaseProtocol
#trans
Instance Method Summary
collapse
-
#initialize(transport) ⇒ CompactProtocol
constructor
A new instance of CompactProtocol.
-
#read_binary ⇒ Object
-
#read_bool ⇒ Object
-
#read_byte ⇒ Object
-
#read_double ⇒ Object
-
#read_field_begin ⇒ Object
-
#read_i16 ⇒ Object
-
#read_i32 ⇒ Object
-
#read_i64 ⇒ Object
-
#read_list_begin ⇒ Object
-
#read_map_begin ⇒ Object
-
#read_message_begin ⇒ Object
-
#read_set_begin ⇒ Object
-
#read_string ⇒ Object
-
#read_struct_begin ⇒ Object
-
#read_struct_end ⇒ Object
-
#read_uuid ⇒ Object
-
#to_s ⇒ Object
-
#write_binary(buf) ⇒ Object
-
#write_bool(bool) ⇒ Object
-
#write_byte(byte) ⇒ Object
-
#write_double(dub) ⇒ Object
-
#write_field_begin(name, type, id) ⇒ Object
-
#write_field_begin_internal(type, id, type_override = nil) ⇒ Object
The workhorse of writeFieldBegin.
-
#write_field_stop ⇒ Object
-
#write_i16(i16) ⇒ Object
-
#write_i32(i32) ⇒ Object
-
#write_i64(i64) ⇒ Object
-
#write_list_begin(etype, size) ⇒ Object
-
#write_map_begin(ktype, vtype, size) ⇒ Object
-
#write_message_begin(name, type, seqid) ⇒ Object
-
#write_set_begin(etype, size) ⇒ Object
-
#write_string(str) ⇒ Object
-
#write_struct_begin(name) ⇒ Object
-
#write_struct_end ⇒ Object
-
#write_uuid(uuid) ⇒ Object
#native?, #read_field_end, #read_list_end, #read_map_end, #read_message_end, #read_set_end, #read_type, #skip, #write_field, #write_field_end, #write_list_end, #write_map_end, #write_message_end, #write_set_end, #write_type
Constructor Details
Returns a new instance of CompactProtocol.
100
101
102
103
104
105
106
107
108
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 100
def initialize(transport)
super(transport)
@last_field = [0]
@boolean_value = nil
@rbuf = Bytes.empty_byte_buffer(8)
end
|
Instance Method Details
#read_binary ⇒ Object
352
353
354
355
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 352
def read_binary
size = read_varint32()
trans.read_all(size)
end
|
#read_bool ⇒ Object
311
312
313
314
315
316
317
318
319
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 311
def read_bool
unless @bool_value.nil?
bv = @bool_value
@bool_value = nil
bv
else
read_byte() == CompactTypes::BOOLEAN_TRUE
end
end
|
#read_byte ⇒ Object
321
322
323
324
325
326
327
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 321
def read_byte
val = trans.read_byte
if (val > 0x7f)
val = 0 - ((val - 1) ^ 0xff)
end
val
end
|
#read_double ⇒ Object
341
342
343
344
345
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 341
def read_double
trans.read_into_buffer(@rbuf, 8)
val = @rbuf.reverse.unpack('G').first
val
end
|
#read_field_begin ⇒ Object
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
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 259
def read_field_begin
type = read_byte()
if (type & 0x0f) == Types::STOP
TSTOP
else
field_id = nil
modifier = (type & 0xf0) >> 4
if modifier == 0
@last_field.pop
field_id = read_i16()
else
field_id = @last_field.pop + modifier
end
if CompactTypes.is_bool_type?(type)
@bool_value = (type & 0x0f) == CompactTypes::BOOLEAN_TRUE
end
@last_field.push(field_id)
[nil, CompactTypes.get_ttype(type & 0x0f), field_id]
end
end
|
#read_i16 ⇒ Object
329
330
331
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 329
def read_i16
zig_zag_to_int(read_varint32())
end
|
#read_i32 ⇒ Object
333
334
335
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 333
def read_i32
zig_zag_to_int(read_varint32())
end
|
#read_i64 ⇒ Object
337
338
339
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 337
def read_i64
zig_zag_to_long(read_varint64())
end
|
#read_list_begin ⇒ Object
297
298
299
300
301
302
303
304
305
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 297
def read_list_begin
size_and_type = read_byte()
size = (size_and_type >> 4) & 0x0f
if size == 15
size = read_varint32()
end
type = CompactTypes.get_ttype(size_and_type)
[type, size]
end
|
#read_map_begin ⇒ Object
291
292
293
294
295
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 291
def read_map_begin
size = read_varint32()
key_and_value_type = size == 0 ? 0 : read_byte()
[CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size]
end
|
#read_message_begin ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 231
def read_message_begin
protocol_id = read_byte()
if protocol_id != PROTOCOL_ID
raise ProtocolException.new("Expected protocol id #{PROTOCOL_ID} but got #{protocol_id}")
end
version_and_type = read_byte()
version = version_and_type & VERSION_MASK
if (version != VERSION)
raise ProtocolException.new("Expected version #{VERSION} but got #{version}");
end
type = (version_and_type >> TYPE_SHIFT_AMOUNT) & TYPE_BITS
seqid = message_seqid_from_varint32(read_varint32())
messageName = read_string()
[messageName, type, seqid]
end
|
#read_set_begin ⇒ Object
307
308
309
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 307
def read_set_begin
read_list_begin
end
|
#read_string ⇒ Object
347
348
349
350
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 347
def read_string
buffer = read_binary
Bytes.convert_to_string(buffer)
end
|
#read_struct_begin ⇒ Object
249
250
251
252
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 249
def read_struct_begin
@last_field.push(0)
""
end
|
#read_struct_end ⇒ Object
254
255
256
257
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 254
def read_struct_end
@last_field.pop()
nil
end
|
#read_uuid ⇒ Object
357
358
359
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 357
def read_uuid
UUID.uuid_from_bytes(trans.read_all(16))
end
|
#to_s ⇒ Object
361
362
363
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 361
def to_s
"compact(#{super.to_s})"
end
|
#write_binary(buf) ⇒ Object
221
222
223
224
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 221
def write_binary(buf)
write_varint32(buf.bytesize)
@trans.write(buf)
end
|
#write_bool(bool) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 184
def write_bool(bool)
type = bool ? CompactTypes::BOOLEAN_TRUE : CompactTypes::BOOLEAN_FALSE
unless @boolean_field.nil?
write_field_begin_internal(@boolean_field.first, @boolean_field.last, type)
@boolean_field = nil
else
write_byte(type)
end
end
|
#write_byte(byte) ⇒ Object
196
197
198
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 196
def write_byte(byte)
@trans.write([byte].pack('c'))
end
|
#write_double(dub) ⇒ Object
212
213
214
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 212
def write_double(dub)
@trans.write([dub].pack("G").reverse)
end
|
#write_field_begin(name, type, id) ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 128
def write_field_begin(name, type, id)
if type == Types::BOOL
@boolean_field = [type, id]
else
write_field_begin_internal(type, id)
end
nil
end
|
#write_field_begin_internal(type, id, type_override = nil) ⇒ Object
The workhorse of writeFieldBegin. It has the option of doing a ‘type override’ of the type header. This is used specifically in the boolean field case.
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 143
def write_field_begin_internal(type, id, type_override = nil)
last_id = @last_field.pop
typeToWrite = type_override || CompactTypes.get_compact_type(type)
if id > last_id && id - last_id <= 15
write_byte((id - last_id) << 4 | typeToWrite)
else
write_byte(typeToWrite)
write_i16(id)
end
@last_field.push(id)
nil
end
|
#write_field_stop ⇒ Object
163
164
165
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 163
def write_field_stop
write_byte(Types::STOP)
end
|
#write_i16(i16) ⇒ Object
200
201
202
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 200
def write_i16(i16)
write_varint32(int_to_zig_zag(i16))
end
|
#write_i32(i32) ⇒ Object
204
205
206
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 204
def write_i32(i32)
write_varint32(int_to_zig_zag(i32))
end
|
#write_i64(i64) ⇒ Object
208
209
210
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 208
def write_i64(i64)
write_varint64(long_to_zig_zag(i64))
end
|
#write_list_begin(etype, size) ⇒ Object
176
177
178
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 176
def write_list_begin(etype, size)
write_collection_begin(etype, size)
end
|
#write_map_begin(ktype, vtype, size) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 167
def write_map_begin(ktype, vtype, size)
if (size == 0)
write_byte(0)
else
write_varint32(size)
write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype))
end
end
|
#write_message_begin(name, type, seqid) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 110
def write_message_begin(name, type, seqid)
write_byte(PROTOCOL_ID)
write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK))
write_varint32(message_seqid_to_varint32(seqid))
write_string(name)
nil
end
|
#write_set_begin(etype, size) ⇒ Object
180
181
182
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 180
def write_set_begin(etype, size)
write_collection_begin(etype, size);
end
|
#write_string(str) ⇒ Object
216
217
218
219
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 216
def write_string(str)
buf = Bytes.convert_to_utf8_byte_buffer(str)
write_binary(buf)
end
|
#write_struct_begin(name) ⇒ Object
118
119
120
121
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 118
def write_struct_begin(name)
@last_field.push(0)
nil
end
|
#write_struct_end ⇒ Object
123
124
125
126
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 123
def write_struct_end
@last_field.pop
nil
end
|
#write_uuid(uuid) ⇒ Object
226
227
228
229
|
# File 'lib/thrift/protocol/compact_protocol.rb', line 226
def write_uuid(uuid)
UUID.validate_uuid!(uuid)
trans.write(UUID.uuid_bytes(uuid))
end
|