Skip to content

Commit 0c1a92d

Browse files
committed
thrift 0.7.0 from trunk, patched for low mem usage
1 parent eb15310 commit 0c1a92d

19 files changed

+1722
-6959
lines changed

module/lib/thrift/Thrift.py

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ class TType:
3838
UTF8 = 16
3939
UTF16 = 17
4040

41+
_VALUES_TO_NAMES = ( 'STOP',
42+
'VOID',
43+
'BOOL',
44+
'BYTE',
45+
'DOUBLE',
46+
None,
47+
'I16',
48+
None,
49+
'I32',
50+
None,
51+
'I64',
52+
'STRING',
53+
'STRUCT',
54+
'MAP',
55+
'SET',
56+
'LIST',
57+
'UTF8',
58+
'UTF16' )
59+
4160
class TMessageType:
4261
CALL = 1
4362
REPLY = 2

module/lib/thrift/protocol/TBase.py

+298
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
#
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+
20+
from thrift.Thrift import *
21+
from thrift.protocol import TBinaryProtocol
22+
from thrift.transport import TTransport
23+
24+
try:
25+
from thrift.protocol import fastbinary
26+
except:
27+
fastbinary = None
28+
29+
class TBase(object):
30+
__slots__ = []
31+
32+
def __repr__(self):
33+
L = ['%s=%r' % (key, getattr(self, key))
34+
for key in self.__slots__ ]
35+
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
36+
37+
def __eq__(self, other):
38+
if not isinstance(other, self.__class__):
39+
return False
40+
for attr in self.__slots__:
41+
my_val = getattr(self, attr)
42+
other_val = getattr(other, attr)
43+
if my_val != other_val:
44+
return False
45+
return True
46+
47+
def __ne__(self, other):
48+
return not (self == other)
49+
50+
def read(self, iprot):
51+
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
52+
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
53+
return
54+
iprot.readStruct(self, self.thrift_spec)
55+
56+
def write(self, oprot):
57+
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
58+
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
59+
return
60+
oprot.writeStruct(self, self.thrift_spec)
61+
62+
class TExceptionBase(Exception):
63+
# old style class so python2.4 can raise exceptions derived from this
64+
# This can't inherit from TBase because of that limitation.
65+
__slots__ = []
66+
67+
__repr__ = TBase.__repr__.im_func
68+
__eq__ = TBase.__eq__.im_func
69+
__ne__ = TBase.__ne__.im_func
70+
read = TBase.read.im_func
71+
write = TBase.write.im_func
72+
73+
#
74+
# Licensed to the Apache Software Foundation (ASF) under one
75+
# or more contributor license agreements. See the NOTICE file
76+
# distributed with this work for additional information
77+
# regarding copyright ownership. The ASF licenses this file
78+
# to you under the Apache License, Version 2.0 (the
79+
# "License"); you may not use this file except in compliance
80+
# with the License. You may obtain a copy of the License at
81+
#
82+
# http://www.apache.org/licenses/LICENSE-2.0
83+
#
84+
# Unless required by applicable law or agreed to in writing,
85+
# software distributed under the License is distributed on an
86+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
87+
# KIND, either express or implied. See the License for the
88+
# specific language governing permissions and limitations
89+
# under the License.
90+
#
91+
92+
from thrift.Thrift import *
93+
from thrift.protocol import TBinaryProtocol
94+
from thrift.transport import TTransport
95+
96+
try:
97+
from thrift.protocol import fastbinary
98+
except:
99+
fastbinary = None
100+
101+
def read(iprot, types, ftype, spec):
102+
try:
103+
return types[ftype][0]()
104+
except KeyError:
105+
if ftype == TType.LIST:
106+
ltype, lsize = iprot.readListBegin()
107+
108+
value = [read(iprot, types, spec[0], spec[1]) for i in range(lsize)]
109+
110+
iprot.readListEnd()
111+
return value
112+
113+
elif ftype == TType.SET:
114+
ltype, lsize = iprot.readSetBegin()
115+
116+
value = set([read(iprot, types, spec[0], spec[1]) for i in range(lsize)])
117+
118+
iprot.readSetEnd()
119+
return value
120+
121+
elif ftype == TType.MAP:
122+
key_type, key_spec = spec[0], spec[1]
123+
val_type, val_spec = spec[2], spec[3]
124+
125+
ktype, vtype, mlen = iprot.readMapBegin()
126+
res = dict()
127+
128+
for i in xrange(mlen):
129+
key = read(iprot, types, key_type, key_spec)
130+
res[key] = read(iprot, types, val_type, val_spec)
131+
132+
iprot.readMapEnd()
133+
return res
134+
135+
elif ftype == TType.STRUCT:
136+
return spec[0]().read(iprot)
137+
138+
139+
140+
141+
def write(oprot, types, ftype, spec, value):
142+
try:
143+
types[ftype][1](value)
144+
except KeyError:
145+
if ftype == TType.LIST:
146+
oprot.writeListBegin(spec[0], len(value))
147+
148+
for elem in value:
149+
write(oprot, types, spec[0], spec[1], elem)
150+
151+
oprot.writeListEnd()
152+
elif ftype == TType.SET:
153+
oprot.writeSetBegin(spec[0], len(value))
154+
155+
for elem in value:
156+
write(oprot, types, spec[0], spec[1], elem)
157+
158+
oprot.writeSetEnd()
159+
elif ftype == TType.MAP:
160+
key_type, key_spec = spec[0], spec[1]
161+
val_type, val_spec = spec[2], spec[3]
162+
163+
oprot.writeMapBegin(key_type, val_type, len(value))
164+
for key, val in value.iteritems():
165+
write(oprot, types, key_type, key_spec, key)
166+
write(oprot, types, val_type, val_spec, val)
167+
168+
oprot.writeMapEnd()
169+
elif ftype == TType.STRUCT:
170+
value.write(oprot)
171+
172+
173+
class TBase2(object):
174+
__slots__ = ("thrift_spec")
175+
176+
#subclasses provides this information
177+
thrift_spec = ()
178+
179+
def __repr__(self):
180+
L = ['%s=%r' % (key, getattr(self, key))
181+
for key in self.__slots__ ]
182+
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
183+
184+
def __eq__(self, other):
185+
if not isinstance(other, self.__class__):
186+
return False
187+
for attr in self.__slots__:
188+
my_val = getattr(self, attr)
189+
other_val = getattr(other, attr)
190+
if my_val != other_val:
191+
return False
192+
return True
193+
194+
def __ne__(self, other):
195+
return not (self == other)
196+
197+
def read(self, iprot):
198+
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
199+
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
200+
return
201+
202+
#local copies for faster access
203+
thrift_spec = self.thrift_spec
204+
setter = self.__setattr__
205+
206+
iprot.readStructBegin()
207+
while True:
208+
(fname, ftype, fid) = iprot.readFieldBegin()
209+
if ftype == TType.STOP:
210+
break
211+
212+
try:
213+
specs = thrift_spec[fid]
214+
if not specs or specs[1] != ftype:
215+
iprot.skip(ftype)
216+
217+
else:
218+
pos, etype, ename, espec, unk = specs
219+
value = read(iprot, iprot.primTypes, etype, espec)
220+
setter(ename, value)
221+
222+
except IndexError:
223+
iprot.skip()
224+
225+
iprot.readFieldEnd()
226+
227+
iprot.readStructEnd()
228+
229+
def write(self, oprot):
230+
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
231+
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
232+
return
233+
234+
#local copies for faster access
235+
oprot.writeStructBegin(self.__class__.__name__)
236+
getter = self.__getattribute__
237+
238+
for spec in self.thrift_spec:
239+
if spec is None: continue
240+
# element attributes
241+
pos, etype, ename, espec, unk = spec
242+
value = getter(ename)
243+
if value is None: continue
244+
245+
oprot.writeFieldBegin(ename, etype, pos)
246+
write(oprot, oprot.primTypes, etype, espec, value)
247+
oprot.writeFieldEnd()
248+
249+
oprot.writeFieldStop()
250+
oprot.writeStructEnd()
251+
252+
class TBase(object):
253+
__slots__ = ('thrift_spec',)
254+
255+
#provides by subclasses
256+
thrift_spec = ()
257+
258+
def __repr__(self):
259+
L = ['%s=%r' % (key, getattr(self, key))
260+
for key in self.__slots__ ]
261+
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
262+
263+
def __eq__(self, other):
264+
if not isinstance(other, self.__class__):
265+
return False
266+
for attr in self.__slots__:
267+
my_val = getattr(self, attr)
268+
other_val = getattr(other, attr)
269+
if my_val != other_val:
270+
return False
271+
return True
272+
273+
def __ne__(self, other):
274+
return not (self == other)
275+
276+
def read(self, iprot):
277+
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
278+
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
279+
return
280+
iprot.readStruct(self, self.thrift_spec)
281+
282+
def write(self, oprot):
283+
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
284+
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
285+
return
286+
oprot.writeStruct(self, self.thrift_spec)
287+
288+
class TExceptionBase(Exception):
289+
# old style class so python2.4 can raise exceptions derived from this
290+
# This can't inherit from TBase because of that limitation.
291+
__slots__ = []
292+
293+
__repr__ = TBase.__repr__.im_func
294+
__eq__ = TBase.__eq__.im_func
295+
__ne__ = TBase.__ne__.im_func
296+
read = TBase.read.im_func
297+
write = TBase.write.im_func
298+

module/lib/thrift/protocol/TCompactProtocol.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def readVarint(trans):
5252
shift += 7
5353

5454
class CompactType:
55-
TRUE = 1
56-
FALSE = 2
55+
STOP = 0x00
56+
TRUE = 0x01
57+
FALSE = 0x02
5758
BYTE = 0x03
5859
I16 = 0x04
5960
I32 = 0x05
@@ -65,7 +66,8 @@ class CompactType:
6566
MAP = 0x0B
6667
STRUCT = 0x0C
6768

68-
CTYPES = {TType.BOOL: CompactType.TRUE, # used for collection
69+
CTYPES = {TType.STOP: CompactType.STOP,
70+
TType.BOOL: CompactType.TRUE, # used for collection
6971
TType.BYTE: CompactType.BYTE,
7072
TType.I16: CompactType.I16,
7173
TType.I32: CompactType.I32,
@@ -75,7 +77,7 @@ class CompactType:
7577
TType.STRUCT: CompactType.STRUCT,
7678
TType.LIST: CompactType.LIST,
7779
TType.SET: CompactType.SET,
78-
TType.MAP: CompactType.MAP,
80+
TType.MAP: CompactType.MAP
7981
}
8082

8183
TTYPES = {}
@@ -196,11 +198,15 @@ def writeCollectionEnd(self):
196198

197199
def writeBool(self, bool):
198200
if self.state == BOOL_WRITE:
199-
self.__writeFieldHeader(types[bool], self.__bool_fid)
201+
if bool:
202+
ctype = CompactType.TRUE
203+
else:
204+
ctype = CompactType.FALSE
205+
self.__writeFieldHeader(ctype, self.__bool_fid)
200206
elif self.state == CONTAINER_WRITE:
201207
self.__writeByte(int(bool))
202208
else:
203-
raise AssertetionError, "Invalid state in compact protocol"
209+
raise AssertionError, "Invalid state in compact protocol"
204210

205211
writeByte = writer(__writeByte)
206212
writeI16 = writer(__writeI16)
@@ -285,9 +291,8 @@ def readMessageBegin(self):
285291
return (name, type, seqid)
286292

287293
def readMessageEnd(self):
288-
assert self.state == VALUE_READ
294+
assert self.state == CLEAR
289295
assert len(self.__structs) == 0
290-
self.state = CLEAR
291296

292297
def readStructBegin(self):
293298
assert self.state in (CLEAR, CONTAINER_READ, VALUE_READ), self.state

0 commit comments

Comments
 (0)