forked from GoogleCloudPlatform/DataflowPythonSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoder_impl.py
More file actions
316 lines (224 loc) · 9.23 KB
/
coder_impl.py
File metadata and controls
316 lines (224 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Coder implementations.
The actual encode/decode implementations are split off from coders to
allow conditional (compiled/pure) implementations, which can be used to
encode many elements with minimal overhead.
This module may be optionally compiled with Cython, using the corresponding
coder_impl.pxd file for type hints.
"""
import collections
from cPickle import loads, dumps
# pylint: disable=g-import-not-at-top
try:
# Don't depend on the full dataflow sdk to test coders.
from google.cloud.dataflow.transforms.window import WindowedValue
except ImportError:
WindowedValue = collections.namedtuple(
'WindowedValue', ('value', 'timestamp', 'windows'))
try:
from stream import InputStream as create_InputStream
from stream import OutputStream as create_OutputStream
except ImportError:
from slow_stream import InputStream as create_InputStream
from slow_stream import OutputStream as create_OutputStream
# pylint: enable=g-import-not-at-top
class CoderImpl(object):
def encode_to_stream(self, value, stream, nested):
"""Reads object from potentially-nested encoding in stream."""
raise NotImplementedError
def decode_from_stream(self, stream, nested):
"""Reads object from potentially-nested encoding in stream."""
raise NotImplementedError
def encode(self, value):
"""Encodes an object to an unnested string."""
raise NotImplementedError
def decode(self, encoded):
"""Encodes an object to an unnested string."""
raise NotImplementedError
class SimpleCoderImpl(CoderImpl):
"""Subclass of CoderImpl implementing stream methods using encode/decode."""
def encode_to_stream(self, value, stream, nested):
"""Reads object from potentially-nested encoding in stream."""
stream.write(self.encode(value), nested)
def decode_from_stream(self, stream, nested):
"""Reads object from potentially-nested encoding in stream."""
return self.decode(stream.read_all(nested))
class StreamCoderImpl(CoderImpl):
"""Subclass of CoderImpl implementing encode/decode using stream methods."""
def encode(self, value):
out = create_OutputStream()
self.encode_to_stream(value, out, False)
return out.get()
def decode(self, encoded):
return self.decode_from_stream(create_InputStream(encoded), False)
class CallbackCoderImpl(CoderImpl):
"""A CoderImpl that calls back to the _impl methods on the Coder itself.
This is the default implementation used if Coder._get_impl()
is not overwritten.
"""
def __init__(self, encoder, decoder):
self._encoder = encoder
self._decoder = decoder
def encode_to_stream(self, value, stream, nested):
return stream.write(self._encoder(value), nested)
def decode_from_stream(self, stream, nested):
return self._decoder(stream.read_all(nested))
def encode(self, value):
return self._encoder(value)
def decode(self, encoded):
return self._decoder(encoded)
class DeterministicPickleCoderImpl(CoderImpl):
def __init__(self, pickle_coder, step_label):
self._pickle_coder = pickle_coder
self._step_label = step_label
def _check_safe(self, value):
if isinstance(value, (str, unicode, long, int, float)):
pass
elif value is None:
pass
elif isinstance(value, (tuple, list)):
for x in value:
self._check_safe(x)
else:
raise TypeError(
"Unable to deterministically code '%s' of type '%s', "
"please provide a type hint for the input of '%s'" % (
value, type(value), self._step_label))
def encode_to_stream(self, value, stream, nested):
self._check_safe(value)
return self._pickle_coder.encode_to_stream(value, stream, nested)
def decode_from_stream(self, stream, nested):
return self._pickle_coder.decode_from_stream(stream, nested)
def encode(self, value):
self._check_safe(value)
return self._pickle_coder.encode(value)
def decode(self, encoded):
return self._pickle_coder.decode(encoded)
class BytesCoderImpl(CoderImpl):
"""A coder for bytes/str objects."""
def encode_to_stream(self, value, out, nested):
out.write(value, nested)
def decode_from_stream(self, in_stream, nested):
return in_stream.read_all(nested)
def encode(self, value):
assert isinstance(value, bytes), (value, type(value))
return value
def decode(self, encoded):
return encoded
class FloatCoderImpl(StreamCoderImpl):
def encode_to_stream(self, value, out, nested):
out.write_bigendian_double(value)
def decode_from_stream(self, in_stream, nested):
return in_stream.read_bigendian_double()
class TimestampCoderImpl(StreamCoderImpl):
def __init__(self, timestamp_class):
self.timestamp_class = timestamp_class
def encode_to_stream(self, value, out, nested):
out.write_bigendian_int64(value.micros)
def decode_from_stream(self, in_stream, nested):
return self.timestamp_class(micros=in_stream.read_bigendian_int64())
small_ints = [chr(_) for _ in range(128)]
class VarIntCoderImpl(StreamCoderImpl):
"""A coder for long/int objects."""
def encode_to_stream(self, value, out, nested):
out.write_var_int64(value)
def decode_from_stream(self, in_stream, nested):
return in_stream.read_var_int64()
def encode(self, value):
ivalue = value # type cast
if 0 <= ivalue < len(small_ints):
return small_ints[ivalue]
else:
return StreamCoderImpl.encode(self, value)
def decode(self, encoded):
if len(encoded) == 1:
i = ord(encoded)
if 0 <= i < 128:
return i
return StreamCoderImpl.decode(self, encoded)
class SingletonCoderImpl(CoderImpl):
"""A coder that always encodes exactly one value."""
def __init__(self, value):
self._value = value
def encode_to_stream(self, value, stream, nested):
pass
def decode_from_stream(self, stream, nested):
return self._value
def encode(self, value):
b = '' # avoid byte vs str vs unicode error
return b
def decode(self, encoded):
return self._value
class AbstractComponentCoderImpl(StreamCoderImpl):
def __init__(self, coder_impls):
for c in coder_impls:
assert isinstance(c, CoderImpl), c
self._coder_impls = tuple(coder_impls)
def _extract_components(self, value):
raise NotImplementedError
def _construct_from_components(self, components):
raise NotImplementedError
def encode_to_stream(self, value, out, nested):
values = self._extract_components(value)
if len(self._coder_impls) != len(values):
raise ValueError(
'Number of components does not match number of coders.')
for i in range(0, len(self._coder_impls)):
c = self._coder_impls[i] # type cast
c.encode_to_stream(values[i], out, True)
def decode_from_stream(self, in_stream, nested):
return self._construct_from_components(
[c.decode_from_stream(in_stream, True) for c in self._coder_impls])
class TupleCoderImpl(AbstractComponentCoderImpl):
"""A coder for tuple objects."""
def _extract_components(self, value):
return value
def _construct_from_components(self, components):
return tuple(components)
class SequenceCoderImpl(StreamCoderImpl):
"""A coder for sequences of known length."""
def __init__(self, elem_coder):
self._elem_coder = elem_coder
def _construct_from_sequence(self, values):
raise NotImplementedError
def encode_to_stream(self, value, out, nested):
# Compatible with Java's IterableLikeCoder.
out.write_bigendian_int32(len(value))
for elem in value:
self._elem_coder.encode_to_stream(elem, out, True)
def decode_from_stream(self, in_stream, nested):
size = in_stream.read_bigendian_int32()
return self._construct_from_sequence(
[self._elem_coder.decode_from_stream(in_stream, True)
for _ in range(size)])
class TupleSequenceCoderImpl(SequenceCoderImpl):
"""A coder for homogeneous tuple objects."""
def _construct_from_sequence(self, components):
return tuple(components)
class WindowedValueCoderImpl(StreamCoderImpl):
"""A coder for windowed values."""
def __init__(self, value_coder, timestamp_coder, window_coder):
self._value_coder = value_coder
self._timestamp_coder = timestamp_coder
self._windows_coder = TupleSequenceCoderImpl(window_coder)
def encode_to_stream(self, value, out, nested):
self._value_coder.encode_to_stream(value.value, out, True)
self._timestamp_coder.encode_to_stream(value.timestamp, out, True)
self._windows_coder.encode_to_stream(value.windows, out, True)
def decode_from_stream(self, in_stream, nested):
return WindowedValue(
self._value_coder.decode_from_stream(in_stream, True),
self._timestamp_coder.decode_from_stream(in_stream, True),
self._windows_coder.decode_from_stream(in_stream, True))