blob: cdec60773a5f6b604a7bcb3e62b9f83f328732fb [file] [log] [blame]
Roger Meierf4eec7a2011-09-11 18:16:21 +00001#
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
David Reissabafd792010-09-27 17:28:15 +000020from TProtocol import *
21from struct import pack, unpack
22
23__all__ = ['TCompactProtocol', 'TCompactProtocolFactory']
24
25CLEAR = 0
26FIELD_WRITE = 1
27VALUE_WRITE = 2
28CONTAINER_WRITE = 3
29BOOL_WRITE = 4
30FIELD_READ = 5
31CONTAINER_READ = 6
32VALUE_READ = 7
33BOOL_READ = 8
34
Bryan Duxbury69720412012-01-03 17:32:30 +000035
David Reissabafd792010-09-27 17:28:15 +000036def make_helper(v_from, container):
37 def helper(func):
38 def nested(self, *args, **kwargs):
39 assert self.state in (v_from, container), (self.state, v_from, container)
40 return func(self, *args, **kwargs)
41 return nested
42 return helper
43writer = make_helper(VALUE_WRITE, CONTAINER_WRITE)
44reader = make_helper(VALUE_READ, CONTAINER_READ)
45
Bryan Duxbury69720412012-01-03 17:32:30 +000046
David Reissabafd792010-09-27 17:28:15 +000047def makeZigZag(n, bits):
48 return (n << 1) ^ (n >> (bits - 1))
49
Bryan Duxbury69720412012-01-03 17:32:30 +000050
David Reissabafd792010-09-27 17:28:15 +000051def fromZigZag(n):
52 return (n >> 1) ^ -(n & 1)
53
Bryan Duxbury69720412012-01-03 17:32:30 +000054
David Reissabafd792010-09-27 17:28:15 +000055def writeVarint(trans, n):
56 out = []
57 while True:
58 if n & ~0x7f == 0:
59 out.append(n)
60 break
61 else:
62 out.append((n & 0xff) | 0x80)
63 n = n >> 7
64 trans.write(''.join(map(chr, out)))
65
Bryan Duxbury69720412012-01-03 17:32:30 +000066
David Reissabafd792010-09-27 17:28:15 +000067def readVarint(trans):
68 result = 0
69 shift = 0
70 while True:
71 x = trans.readAll(1)
72 byte = ord(x)
73 result |= (byte & 0x7f) << shift
74 if byte >> 7 == 0:
75 return result
76 shift += 7
77
Bryan Duxbury69720412012-01-03 17:32:30 +000078
David Reissabafd792010-09-27 17:28:15 +000079class CompactType:
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000080 STOP = 0x00
81 TRUE = 0x01
82 FALSE = 0x02
David Reissabafd792010-09-27 17:28:15 +000083 BYTE = 0x03
84 I16 = 0x04
85 I32 = 0x05
86 I64 = 0x06
87 DOUBLE = 0x07
88 BINARY = 0x08
89 LIST = 0x09
90 SET = 0x0A
91 MAP = 0x0B
92 STRUCT = 0x0C
93
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000094CTYPES = {TType.STOP: CompactType.STOP,
Bryan Duxbury69720412012-01-03 17:32:30 +000095 TType.BOOL: CompactType.TRUE, # used for collection
David Reissabafd792010-09-27 17:28:15 +000096 TType.BYTE: CompactType.BYTE,
97 TType.I16: CompactType.I16,
98 TType.I32: CompactType.I32,
99 TType.I64: CompactType.I64,
100 TType.DOUBLE: CompactType.DOUBLE,
101 TType.STRING: CompactType.BINARY,
102 TType.STRUCT: CompactType.STRUCT,
103 TType.LIST: CompactType.LIST,
104 TType.SET: CompactType.SET,
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000105 TType.MAP: CompactType.MAP
David Reissabafd792010-09-27 17:28:15 +0000106 }
107
108TTYPES = {}
109for k, v in CTYPES.items():
110 TTYPES[v] = k
111TTYPES[CompactType.FALSE] = TType.BOOL
112del k
113del v
114
Bryan Duxbury69720412012-01-03 17:32:30 +0000115
David Reissabafd792010-09-27 17:28:15 +0000116class TCompactProtocol(TProtocolBase):
Bryan Duxbury69720412012-01-03 17:32:30 +0000117 """Compact implementation of the Thrift protocol driver."""
David Reissabafd792010-09-27 17:28:15 +0000118
119 PROTOCOL_ID = 0x82
120 VERSION = 1
121 VERSION_MASK = 0x1f
122 TYPE_MASK = 0xe0
123 TYPE_SHIFT_AMOUNT = 5
124
125 def __init__(self, trans):
126 TProtocolBase.__init__(self, trans)
127 self.state = CLEAR
128 self.__last_fid = 0
129 self.__bool_fid = None
130 self.__bool_value = None
131 self.__structs = []
132 self.__containers = []
133
134 def __writeVarint(self, n):
135 writeVarint(self.trans, n)
136
137 def writeMessageBegin(self, name, type, seqid):
138 assert self.state == CLEAR
139 self.__writeUByte(self.PROTOCOL_ID)
140 self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT))
141 self.__writeVarint(seqid)
142 self.__writeString(name)
143 self.state = VALUE_WRITE
144
145 def writeMessageEnd(self):
146 assert self.state == VALUE_WRITE
147 self.state = CLEAR
148
149 def writeStructBegin(self, name):
150 assert self.state in (CLEAR, CONTAINER_WRITE, VALUE_WRITE), self.state
151 self.__structs.append((self.state, self.__last_fid))
152 self.state = FIELD_WRITE
153 self.__last_fid = 0
154
155 def writeStructEnd(self):
156 assert self.state == FIELD_WRITE
157 self.state, self.__last_fid = self.__structs.pop()
158
159 def writeFieldStop(self):
160 self.__writeByte(0)
161
162 def __writeFieldHeader(self, type, fid):
163 delta = fid - self.__last_fid
164 if 0 < delta <= 15:
165 self.__writeUByte(delta << 4 | type)
166 else:
167 self.__writeByte(type)
168 self.__writeI16(fid)
169 self.__last_fid = fid
170
171 def writeFieldBegin(self, name, type, fid):
172 assert self.state == FIELD_WRITE, self.state
173 if type == TType.BOOL:
174 self.state = BOOL_WRITE
175 self.__bool_fid = fid
176 else:
177 self.state = VALUE_WRITE
178 self.__writeFieldHeader(CTYPES[type], fid)
179
180 def writeFieldEnd(self):
181 assert self.state in (VALUE_WRITE, BOOL_WRITE), self.state
182 self.state = FIELD_WRITE
183
184 def __writeUByte(self, byte):
185 self.trans.write(pack('!B', byte))
186
187 def __writeByte(self, byte):
188 self.trans.write(pack('!b', byte))
189
190 def __writeI16(self, i16):
191 self.__writeVarint(makeZigZag(i16, 16))
192
193 def __writeSize(self, i32):
194 self.__writeVarint(i32)
195
196 def writeCollectionBegin(self, etype, size):
197 assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
198 if size <= 14:
199 self.__writeUByte(size << 4 | CTYPES[etype])
200 else:
201 self.__writeUByte(0xf0 | CTYPES[etype])
202 self.__writeSize(size)
203 self.__containers.append(self.state)
204 self.state = CONTAINER_WRITE
205 writeSetBegin = writeCollectionBegin
206 writeListBegin = writeCollectionBegin
207
208 def writeMapBegin(self, ktype, vtype, size):
209 assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
210 if size == 0:
211 self.__writeByte(0)
212 else:
213 self.__writeSize(size)
214 self.__writeUByte(CTYPES[ktype] << 4 | CTYPES[vtype])
215 self.__containers.append(self.state)
216 self.state = CONTAINER_WRITE
217
218 def writeCollectionEnd(self):
219 assert self.state == CONTAINER_WRITE, self.state
220 self.state = self.__containers.pop()
221 writeMapEnd = writeCollectionEnd
222 writeSetEnd = writeCollectionEnd
223 writeListEnd = writeCollectionEnd
224
225 def writeBool(self, bool):
226 if self.state == BOOL_WRITE:
Bryan Duxbury69720412012-01-03 17:32:30 +0000227 if bool:
228 ctype = CompactType.TRUE
229 else:
230 ctype = CompactType.FALSE
231 self.__writeFieldHeader(ctype, self.__bool_fid)
David Reissabafd792010-09-27 17:28:15 +0000232 elif self.state == CONTAINER_WRITE:
Bryan Duxbury69720412012-01-03 17:32:30 +0000233 if bool:
234 self.__writeByte(CompactType.TRUE)
235 else:
236 self.__writeByte(CompactType.FALSE)
David Reissabafd792010-09-27 17:28:15 +0000237 else:
Bryan Duxbury69720412012-01-03 17:32:30 +0000238 raise AssertionError("Invalid state in compact protocol")
David Reissabafd792010-09-27 17:28:15 +0000239
240 writeByte = writer(__writeByte)
241 writeI16 = writer(__writeI16)
242
243 @writer
244 def writeI32(self, i32):
245 self.__writeVarint(makeZigZag(i32, 32))
246
247 @writer
248 def writeI64(self, i64):
249 self.__writeVarint(makeZigZag(i64, 64))
250
251 @writer
252 def writeDouble(self, dub):
253 self.trans.write(pack('!d', dub))
254
255 def __writeString(self, s):
256 self.__writeSize(len(s))
257 self.trans.write(s)
258 writeString = writer(__writeString)
259
260 def readFieldBegin(self):
261 assert self.state == FIELD_READ, self.state
262 type = self.__readUByte()
263 if type & 0x0f == TType.STOP:
264 return (None, 0, 0)
265 delta = type >> 4
266 if delta == 0:
267 fid = self.__readI16()
268 else:
269 fid = self.__last_fid + delta
270 self.__last_fid = fid
271 type = type & 0x0f
272 if type == CompactType.TRUE:
273 self.state = BOOL_READ
274 self.__bool_value = True
275 elif type == CompactType.FALSE:
276 self.state = BOOL_READ
277 self.__bool_value = False
278 else:
279 self.state = VALUE_READ
280 return (None, self.__getTType(type), fid)
281
282 def readFieldEnd(self):
283 assert self.state in (VALUE_READ, BOOL_READ), self.state
284 self.state = FIELD_READ
285
286 def __readUByte(self):
287 result, = unpack('!B', self.trans.readAll(1))
288 return result
289
290 def __readByte(self):
291 result, = unpack('!b', self.trans.readAll(1))
292 return result
293
294 def __readVarint(self):
295 return readVarint(self.trans)
296
297 def __readZigZag(self):
298 return fromZigZag(self.__readVarint())
299
300 def __readSize(self):
301 result = self.__readVarint()
302 if result < 0:
303 raise TException("Length < 0")
304 return result
305
306 def readMessageBegin(self):
307 assert self.state == CLEAR
308 proto_id = self.__readUByte()
309 if proto_id != self.PROTOCOL_ID:
310 raise TProtocolException(TProtocolException.BAD_VERSION,
311 'Bad protocol id in the message: %d' % proto_id)
312 ver_type = self.__readUByte()
313 type = (ver_type & self.TYPE_MASK) >> self.TYPE_SHIFT_AMOUNT
314 version = ver_type & self.VERSION_MASK
315 if version != self.VERSION:
316 raise TProtocolException(TProtocolException.BAD_VERSION,
317 'Bad version: %d (expect %d)' % (version, self.VERSION))
318 seqid = self.__readVarint()
319 name = self.__readString()
320 return (name, type, seqid)
321
322 def readMessageEnd(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000323 assert self.state == CLEAR
David Reissabafd792010-09-27 17:28:15 +0000324 assert len(self.__structs) == 0
David Reissabafd792010-09-27 17:28:15 +0000325
326 def readStructBegin(self):
327 assert self.state in (CLEAR, CONTAINER_READ, VALUE_READ), self.state
328 self.__structs.append((self.state, self.__last_fid))
329 self.state = FIELD_READ
330 self.__last_fid = 0
331
332 def readStructEnd(self):
333 assert self.state == FIELD_READ
334 self.state, self.__last_fid = self.__structs.pop()
335
336 def readCollectionBegin(self):
337 assert self.state in (VALUE_READ, CONTAINER_READ), self.state
338 size_type = self.__readUByte()
339 size = size_type >> 4
340 type = self.__getTType(size_type)
341 if size == 15:
342 size = self.__readSize()
343 self.__containers.append(self.state)
344 self.state = CONTAINER_READ
345 return type, size
346 readSetBegin = readCollectionBegin
347 readListBegin = readCollectionBegin
348
349 def readMapBegin(self):
350 assert self.state in (VALUE_READ, CONTAINER_READ), self.state
351 size = self.__readSize()
352 types = 0
353 if size > 0:
354 types = self.__readUByte()
355 vtype = self.__getTType(types)
356 ktype = self.__getTType(types >> 4)
357 self.__containers.append(self.state)
358 self.state = CONTAINER_READ
359 return (ktype, vtype, size)
360
361 def readCollectionEnd(self):
362 assert self.state == CONTAINER_READ, self.state
363 self.state = self.__containers.pop()
364 readSetEnd = readCollectionEnd
365 readListEnd = readCollectionEnd
366 readMapEnd = readCollectionEnd
367
368 def readBool(self):
369 if self.state == BOOL_READ:
Bryan Duxbury54df97c2011-07-13 18:11:29 +0000370 return self.__bool_value == CompactType.TRUE
David Reissabafd792010-09-27 17:28:15 +0000371 elif self.state == CONTAINER_READ:
Bryan Duxbury54df97c2011-07-13 18:11:29 +0000372 return self.__readByte() == CompactType.TRUE
David Reissabafd792010-09-27 17:28:15 +0000373 else:
Bryan Duxbury69720412012-01-03 17:32:30 +0000374 raise AssertionError("Invalid state in compact protocol: %d" %
375 self.state)
David Reissabafd792010-09-27 17:28:15 +0000376
377 readByte = reader(__readByte)
378 __readI16 = __readZigZag
379 readI16 = reader(__readZigZag)
380 readI32 = reader(__readZigZag)
381 readI64 = reader(__readZigZag)
382
383 @reader
384 def readDouble(self):
385 buff = self.trans.readAll(8)
386 val, = unpack('!d', buff)
387 return val
388
389 def __readString(self):
390 len = self.__readSize()
391 return self.trans.readAll(len)
392 readString = reader(__readString)
393
394 def __getTType(self, byte):
395 return TTYPES[byte & 0x0f]
396
397
398class TCompactProtocolFactory:
399 def __init__(self):
400 pass
401
402 def getProtocol(self, trans):
403 return TCompactProtocol(trans)