Class Thrift::CompactProtocol
In: lib/thrift/protocol/compact_protocol.rb
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
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 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: 
102:       # Pre-allocated read buffer for read_double().
103:       @rbuf = "\0" * 8
104:       @rbuf.force_encoding("BINARY") if @rbuf.respond_to?(:force_encoding)
105:     end

[Source]

     # File 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: 
102:       # Pre-allocated read buffer for read_double().
103:       @rbuf = "\0" * 8
104:       @rbuf.force_encoding("BINARY") if @rbuf.respond_to?(:force_encoding)
105:     end

Public Instance methods

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 298
298:     def read_bool
299:       unless @bool_value.nil?
300:         bv = @bool_value
301:         @bool_value = nil
302:         bv
303:       else
304:         read_byte() == CompactTypes::BOOLEAN_TRUE
305:       end
306:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 298
298:     def read_bool
299:       unless @bool_value.nil?
300:         bv = @bool_value
301:         @bool_value = nil
302:         bv
303:       else
304:         read_byte() == CompactTypes::BOOLEAN_TRUE
305:       end
306:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 308
308:     def read_byte
309:       val = trans.read_byte
310:       if (val > 0x7f)
311:         val = 0 - ((val - 1) ^ 0xff)
312:       end
313:       val
314:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 308
308:     def read_byte
309:       val = trans.read_byte
310:       if (val > 0x7f)
311:         val = 0 - ((val - 1) ^ 0xff)
312:       end
313:       val
314:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 328
328:     def read_double
329:       trans.read_into_buffer(@rbuf, 8)
330:       val = @rbuf.reverse.unpack('G').first
331:       val
332:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 328
328:     def read_double
329:       trans.read_into_buffer(@rbuf, 8)
330:       val = @rbuf.reverse.unpack('G').first
331:       val
332:     end

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 320
320:     def read_i32
321:       zig_zag_to_int(read_varint32())
322:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 320
320:     def read_i32
321:       zig_zag_to_int(read_varint32())
322:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 324
324:     def read_i64
325:       zig_zag_to_long(read_varint64())
326:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 324
324:     def read_i64
325:       zig_zag_to_long(read_varint64())
326:     end

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 278
278:     def read_map_begin
279:       size = read_varint32()
280:       key_and_value_type = size == 0 ? 0 : read_byte()
281:       [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size]
282:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 278
278:     def read_map_begin
279:       size = read_varint32()
280:       key_and_value_type = size == 0 ? 0 : read_byte()
281:       [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size]
282:     end

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 294
294:     def read_set_begin
295:       read_list_begin
296:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 294
294:     def read_set_begin
295:       read_list_begin
296:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 334
334:     def read_string
335:       size = read_varint32()
336:       trans.read_all(size)
337:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 334
334:     def read_string
335:       size = read_varint32()
336:       trans.read_all(size)
337:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 236
236:     def read_struct_begin
237:       @last_field.push(0)
238:       ""
239:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 236
236:     def read_struct_begin
237:       @last_field.push(0)
238:       ""
239:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 241
241:     def read_struct_end
242:       @last_field.pop()
243:       nil
244:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 241
241:     def read_struct_end
242:       @last_field.pop()
243:       nil
244:     end

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 193
193:     def write_byte(byte)
194:       @trans.write([byte].pack('c'))
195:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 193
193:     def write_byte(byte)
194:       @trans.write([byte].pack('c'))
195:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 209
209:     def write_double(dub)
210:       @trans.write([dub].pack("G").reverse)
211:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 209
209:     def write_double(dub)
210:       @trans.write([dub].pack("G").reverse)
211:     end

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 125
125:     def write_field_begin(name, type, id)
126:       if type == Types::BOOL
127:         # we want to possibly include the value, so we'll wait.
128:         @boolean_field = [type, id]
129:       else
130:         write_field_begin_internal(type, id)
131:       end
132:       nil
133:     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 lib/thrift/protocol/compact_protocol.rb, line 140
140:     def write_field_begin_internal(type, id, type_override=nil)
141:       last_id = @last_field.pop
142:       
143:       # if there's a type override, use that.
144:       typeToWrite = type_override || CompactTypes.get_compact_type(type)
145: 
146:       # check if we can use delta encoding for the field id
147:       if id > last_id && id - last_id <= 15
148:         # write them together
149:         write_byte((id - last_id) << 4 | typeToWrite)
150:       else
151:         # write them separate
152:         write_byte(typeToWrite)
153:         write_i16(id)
154:       end
155: 
156:       @last_field.push(id)
157:       nil
158:     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 lib/thrift/protocol/compact_protocol.rb, line 140
140:     def write_field_begin_internal(type, id, type_override=nil)
141:       last_id = @last_field.pop
142:       
143:       # if there's a type override, use that.
144:       typeToWrite = type_override || CompactTypes.get_compact_type(type)
145: 
146:       # check if we can use delta encoding for the field id
147:       if id > last_id && id - last_id <= 15
148:         # write them together
149:         write_byte((id - last_id) << 4 | typeToWrite)
150:       else
151:         # write them separate
152:         write_byte(typeToWrite)
153:         write_i16(id)
154:       end
155: 
156:       @last_field.push(id)
157:       nil
158:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 160
160:     def write_field_stop
161:       write_byte(Types::STOP)
162:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 160
160:     def write_field_stop
161:       write_byte(Types::STOP)
162:     end

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 201
201:     def write_i32(i32)
202:       write_varint32(int_to_zig_zag(i32))
203:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 201
201:     def write_i32(i32)
202:       write_varint32(int_to_zig_zag(i32))
203:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 205
205:     def write_i64(i64)
206:       write_varint64(long_to_zig_zag(i64))
207:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 205
205:     def write_i64(i64)
206:       write_varint64(long_to_zig_zag(i64))
207:     end

[Source]

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

[Source]

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

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 164
164:     def write_map_begin(ktype, vtype, size)
165:       if (size == 0)
166:         write_byte(0)
167:       else
168:         write_varint32(size)
169:         write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype))
170:       end
171:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 164
164:     def write_map_begin(ktype, vtype, size)
165:       if (size == 0)
166:         write_byte(0)
167:       else
168:         write_varint32(size)
169:         write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype))
170:       end
171:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 107
107:     def write_message_begin(name, type, seqid)
108:       write_byte(PROTOCOL_ID)
109:       write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK))
110:       write_varint32(seqid)
111:       write_string(name)
112:       nil
113:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 107
107:     def write_message_begin(name, type, seqid)
108:       write_byte(PROTOCOL_ID)
109:       write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK))
110:       write_varint32(seqid)
111:       write_string(name)
112:       nil
113:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 177
177:     def write_set_begin(etype, size)
178:       write_collection_begin(etype, size);
179:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 177
177:     def write_set_begin(etype, size)
178:       write_collection_begin(etype, size);
179:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 213
213:     def write_string(str)
214:       write_varint32(str.length)
215:       @trans.write(str)
216:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 213
213:     def write_string(str)
214:       write_varint32(str.length)
215:       @trans.write(str)
216:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 115
115:     def write_struct_begin(name)
116:       @last_field.push(0)
117:       nil
118:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 115
115:     def write_struct_begin(name)
116:       @last_field.push(0)
117:       nil
118:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 120
120:     def write_struct_end
121:       @last_field.pop
122:       nil
123:     end

[Source]

     # File lib/thrift/protocol/compact_protocol.rb, line 120
120:     def write_struct_end
121:       @last_field.pop
122:       nil
123:     end

[Validate]