Class Thrift::CompactProtocol
In: ruby/lib/thrift/protocol/compact_protocol.rb
Parent: BaseProtocol

Methods

Classes and Modules

Class Thrift::CompactProtocol::CompactTypes

Constants

PROTOCOL_ID = [0x82].pack('c').unpack('c').first
VERSION = 1
VERSION_MASK = 0x1f
TYPE_MASK = 0xE0
TYPE_SHIFT_AMOUNT = 5
TSTOP = ["", Types::STOP, 0]
SEVEN_BIT_MASK = 0x7F
EVERYTHING_ELSE_MASK = ~SEVEN_BIT_MASK

Public Class methods

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 96
 96:     def initialize(transport)
 97:       super(transport)
 98: 
 99:       @last_field = [0]
100:       @boolean_value = nil
101:     end

Public Instance methods

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 294
294:     def read_bool
295:       unless @bool_value.nil?
296:         bv = @bool_value
297:         @bool_value = nil
298:         bv
299:       else
300:         read_byte() == CompactTypes::BOOLEAN_TRUE
301:       end
302:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 304
304:     def read_byte
305:       dat = trans.read_all(1)
306:       val = dat[0]
307:       if (val > 0x7f)
308:         val = 0 - ((val - 1) ^ 0xff)
309:       end
310:       val
311:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 325
325:     def read_double
326:       dat = trans.read_all(8)
327:       val = dat.reverse.unpack('G').first
328:       val
329:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 242
242:     def read_field_begin
243:       type = read_byte()
244: 
245:       # if it's a stop, then we can return immediately, as the struct is over.
246:       if (type & 0x0f) == Types::STOP
247:         TSTOP
248:       else
249:         field_id = nil
250: 
251:         # mask off the 4 MSB of the type header. it could contain a field id delta.
252:         modifier = (type & 0xf0) >> 4
253:         if modifier == 0
254:           # not a delta. look ahead for the zigzag varint field id.
255:           @last_field.pop
256:           field_id = read_i16()
257:         else
258:           # has a delta. add the delta to the last read field id.
259:           field_id = @last_field.pop + modifier
260:         end
261: 
262:         # if this happens to be a boolean field, the value is encoded in the type
263:         if CompactTypes.is_bool_type?(type)
264:           # save the boolean value in a special instance variable.
265:           @bool_value = (type & 0x0f) == CompactTypes::BOOLEAN_TRUE
266:         end
267: 
268:         # push the new field onto the field stack so we can keep the deltas going.
269:         @last_field.push(field_id)
270:         ["", CompactTypes.get_ttype(type & 0x0f), field_id]
271:       end
272:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 313
313:     def read_i16
314:       zig_zag_to_int(read_varint32())
315:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 317
317:     def read_i32
318:       zig_zag_to_int(read_varint32())
319:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 321
321:     def read_i64
322:       zig_zag_to_long(read_varint64())
323:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 280
280:     def read_list_begin
281:       size_and_type = read_byte()
282:       size = (size_and_type >> 4) & 0x0f
283:       if size == 15
284:         size = read_varint32()
285:       end
286:       type = CompactTypes.get_ttype(size_and_type)
287:       [type, size]
288:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 274
274:     def read_map_begin
275:       size = read_varint32()
276:       key_and_value_type = size == 0 ? 0 : read_byte()
277:       [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size]
278:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 214
214:     def read_message_begin
215:       protocol_id = read_byte()
216:       if protocol_id != PROTOCOL_ID
217:         raise ProtocolException.new("Expected protocol id #{PROTOCOL_ID} but got #{protocol_id}")
218:       end
219:       
220:       version_and_type = read_byte()
221:       version = version_and_type & VERSION_MASK
222:       if (version != VERSION)
223:         raise ProtocolException.new("Expected version #{VERSION} but got #{version}");
224:       end
225:       
226:       type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03
227:       seqid = read_varint32()
228:       messageName = read_string()
229:       [messageName, type, seqid]
230:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 290
290:     def read_set_begin
291:       read_list_begin
292:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 331
331:     def read_string
332:       size = read_varint32()
333:       trans.read_all(size)
334:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 232
232:     def read_struct_begin
233:       @last_field.push(0)
234:       ""
235:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 237
237:     def read_struct_end
238:       @last_field.pop()
239:       nil
240:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 177
177:     def write_bool(bool)
178:       type = bool ? CompactTypes::BOOLEAN_TRUE : CompactTypes::BOOLEAN_FALSE
179:       unless @boolean_field.nil?
180:         # we haven't written the field header yet
181:         write_field_begin_internal(@boolean_field.first, @boolean_field.last, type)
182:         @boolean_field = nil
183:       else
184:         # we're not part of a field, so just write the value.
185:         write_byte(type)
186:       end
187:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 189
189:     def write_byte(byte)
190:       @trans.write([byte].pack('c'))
191:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 205
205:     def write_double(dub)
206:       @trans.write([dub].pack("G").reverse)
207:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 121
121:     def write_field_begin(name, type, id)
122:       if type == Types::BOOL
123:         # we want to possibly include the value, so we'll wait.
124:         @boolean_field = [type, id]
125:       else
126:         write_field_begin_internal(type, id)
127:       end
128:       nil
129:     end

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.

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 136
136:     def write_field_begin_internal(type, id, type_override=nil)
137:       last_id = @last_field.pop
138:       
139:       # if there's a type override, use that.
140:       typeToWrite = type_override || CompactTypes.get_compact_type(type)
141: 
142:       # check if we can use delta encoding for the field id
143:       if id > last_id && id - last_id <= 15
144:         # write them together
145:         write_byte((id - last_id) << 4 | typeToWrite)
146:       else
147:         # write them separate
148:         write_byte(typeToWrite)
149:         write_i16(id)
150:       end
151: 
152:       @last_field.push(id)
153:       nil
154:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 156
156:     def write_field_stop
157:       write_byte(Types::STOP)
158:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 193
193:     def write_i16(i16)
194:       write_varint32(int_to_zig_zag(i16))
195:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 197
197:     def write_i32(i32)
198:       write_varint32(int_to_zig_zag(i32))
199:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 201
201:     def write_i64(i64)
202:       write_varint64(long_to_zig_zag(i64))
203:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 169
169:     def write_list_begin(etype, size)
170:       write_collection_begin(etype, size)
171:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 160
160:     def write_map_begin(ktype, vtype, size)
161:       if (size == 0)
162:         write_byte(0)
163:       else
164:         write_varint32(size)
165:         write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype))
166:       end
167:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 103
103:     def write_message_begin(name, type, seqid)
104:       write_byte(PROTOCOL_ID)
105:       write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK))
106:       write_varint32(seqid)
107:       write_string(name)
108:       nil
109:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 173
173:     def write_set_begin(etype, size)
174:       write_collection_begin(etype, size);
175:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 209
209:     def write_string(str)
210:       write_varint32(str.length)
211:       @trans.write(str)
212:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 111
111:     def write_struct_begin(name)
112:       @last_field.push(0)
113:       nil
114:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 116
116:     def write_struct_end
117:       @last_field.pop
118:       nil
119:     end

Private Instance methods

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 400
400:     def int_to_zig_zag(n)
401:       (n << 1) ^ (n >> 31)
402:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 404
404:     def long_to_zig_zag(l)
405:       # puts "zz encoded #{l} to #{(l << 1) ^ (l >> 63)}"
406:       (l << 1) ^ (l >> 63)
407:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 384
384:     def read_varint32()
385:       read_varint64()
386:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 388
388:     def read_varint64()
389:       shift = 0
390:       result = 0
391:       while true
392:         b = read_byte()
393:         result |= (b & 0x7f) << shift
394:         break if (b & 0x80) != 0x80
395:         shift += 7
396:       end
397:       result
398:     end

Abstract method for writing the start of lists and sets. List and sets on the wire differ only by the type indicator.

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 343
343:     def write_collection_begin(elem_type, size)
344:       if size <= 14
345:         write_byte(size << 4 | CompactTypes.get_compact_type(elem_type))
346:       else
347:         write_byte(0xf0 | CompactTypes.get_compact_type(elem_type))
348:         write_varint32(size)
349:       end
350:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 352
352:     def write_varint32(n)
353:       # int idx = 0;
354:       while true
355:         if (n & ~0x7F) == 0
356:           # i32buf[idx++] = (byte)n;
357:           write_byte(n)
358:           break
359:           # return;
360:         else
361:           # i32buf[idx++] = (byte)((n & 0x7F) | 0x80);
362:           write_byte((n & 0x7F) | 0x80)
363:           n = n >> 7
364:         end
365:       end
366:       # trans_.write(i32buf, 0, idx);
367:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 372
372:     def write_varint64(n)
373:       while true
374:         if (n & EVERYTHING_ELSE_MASK) == 0 #TODO need to find a way to make this into a long...
375:           write_byte(n)
376:           break
377:         else
378:           write_byte((n & SEVEN_BIT_MASK) | 0x80)
379:           n >>= 7
380:         end
381:       end
382:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 409
409:     def zig_zag_to_int(n)
410:       (n >> 1) ^ -(n & 1)
411:     end

[Source]

     # File ruby/lib/thrift/protocol/compact_protocol.rb, line 413
413:     def zig_zag_to_long(n)
414:       (n >> 1) ^ -(n & 1)
415:     end

[Validate]