forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.pxi
More file actions
320 lines (264 loc) · 10.4 KB
/
serialization.pxi
File metadata and controls
320 lines (264 loc) · 10.4 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
317
318
319
320
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
from cpython.ref cimport PyObject
from pyarrow.compat import pickle
def is_named_tuple(cls):
"""Return True if cls is a namedtuple and False otherwise."""
b = cls.__bases__
if len(b) != 1 or b[0] != tuple:
return False
f = getattr(cls, "_fields", None)
if not isinstance(f, tuple):
return False
return all(type(n) == str for n in f)
class SerializationCallbackError(ArrowSerializationError):
def __init__(self, message, example_object):
ArrowSerializationError.__init__(self, message)
self.example_object = example_object
class DeserializationCallbackError(ArrowSerializationError):
def __init__(self, message, type_id):
ArrowSerializationError.__init__(self, message)
self.type_id = type_id
cdef class SerializationContext:
cdef:
object type_to_type_id
object whitelisted_types
object types_to_pickle
object custom_serializers
object custom_deserializers
def __init__(self):
# Types with special serialization handlers
self.type_to_type_id = dict()
self.whitelisted_types = dict()
self.types_to_pickle = set()
self.custom_serializers = dict()
self.custom_deserializers = dict()
def register_type(self, type_, type_id, pickle=False,
custom_serializer=None, custom_deserializer=None):
"""EXPERIMENTAL: Add type to the list of types we can serialize.
Parameters
----------
type_ : TypeType
The type that we can serialize.
type_id : bytes
A string of bytes used to identify the type.
pickle : bool
True if the serialization should be done with pickle.
False if it should be done efficiently with Arrow.
custom_serializer : callable
This argument is optional, but can be provided to
serialize objects of the class in a particular way.
custom_deserializer : callable
This argument is optional, but can be provided to
deserialize objects of the class in a particular way.
"""
self.type_to_type_id[type_] = type_id
self.whitelisted_types[type_id] = type_
if pickle:
self.types_to_pickle.add(type_id)
if custom_serializer is not None:
self.custom_serializers[type_id] = custom_serializer
self.custom_deserializers[type_id] = custom_deserializer
def _serialize_callback(self, obj):
found = False
for type_ in type(obj).__mro__:
if type_ in self.type_to_type_id:
found = True
break
if not found:
raise SerializationCallbackError(
"pyarrow does not know how to "
"serialize objects of type {}.".format(type(obj)), obj
)
# use the closest match to type(obj)
type_id = self.type_to_type_id[type_]
if type_id in self.types_to_pickle:
serialized_obj = {"data": pickle.dumps(obj), "pickle": True}
elif type_id in self.custom_serializers:
serialized_obj = {"data": self.custom_serializers[type_id](obj)}
else:
if is_named_tuple(type_):
serialized_obj = {}
serialized_obj["_pa_getnewargs_"] = obj.__getnewargs__()
elif hasattr(obj, "__dict__"):
serialized_obj = obj.__dict__
else:
msg = "We do not know how to serialize " \
"the object '{}'".format(obj)
raise SerializationCallbackError(msg, obj)
return dict(serialized_obj, **{"_pytype_": type_id})
def _deserialize_callback(self, serialized_obj):
type_id = serialized_obj["_pytype_"]
if "pickle" in serialized_obj:
# The object was pickled, so unpickle it.
obj = pickle.loads(serialized_obj["data"])
else:
assert type_id not in self.types_to_pickle
if type_id not in self.whitelisted_types:
msg = "Type ID " + str(type_id) + " not registered in " \
"deserialization callback"
raise DeserializationCallbackError(msg, type_id)
type_ = self.whitelisted_types[type_id]
if type_id in self.custom_deserializers:
obj = self.custom_deserializers[type_id](
serialized_obj["data"])
else:
# In this case, serialized_obj should just be
# the __dict__ field.
if "_pa_getnewargs_" in serialized_obj:
obj = type_.__new__(
type_, *serialized_obj["_pa_getnewargs_"])
else:
obj = type_.__new__(type_)
serialized_obj.pop("_pytype_")
obj.__dict__.update(serialized_obj)
return obj
_default_serialization_context = SerializationContext()
cdef class SerializedPyObject:
"""
Arrow-serialized representation of Python object
"""
cdef:
CSerializedPyObject data
cdef readonly:
object base
property total_bytes:
def __get__(self):
cdef CMockOutputStream mock_stream
with nogil:
check_status(WriteSerializedObject(self.data, &mock_stream))
return mock_stream.GetExtentBytesWritten()
def write_to(self, sink):
"""
Write serialized object to a sink
"""
cdef shared_ptr[OutputStream] stream
get_writer(sink, &stream)
self._write_to(stream.get())
cdef _write_to(self, OutputStream* stream):
with nogil:
check_status(WriteSerializedObject(self.data, stream))
def deserialize(self, SerializationContext context=None):
"""
Convert back to Python object
"""
cdef PyObject* result
if context is None:
context = _default_serialization_context
with nogil:
check_status(DeserializeObject(context, self.data,
<PyObject*> self.base, &result))
# PyObject_to_object is necessary to avoid a memory leak;
# also unpack the list the object was wrapped in in serialize
return PyObject_to_object(result)[0]
def to_buffer(self, nthreads=1):
"""
Write serialized data as Buffer
"""
cdef Buffer output = allocate_buffer(self.total_bytes)
sink = FixedSizeBufferWriter(output)
if nthreads > 1:
sink.set_memcopy_threads(nthreads)
self.write_to(sink)
return output
def serialize(object value, SerializationContext context=None):
"""EXPERIMENTAL: Serialize a Python sequence
Parameters
----------
value: object
Python object for the sequence that is to be serialized.
context : SerializationContext
Custom serialization and deserialization context, uses a default
context with some standard type handlers if not specified
Returns
-------
serialized : SerializedPyObject
"""
cdef SerializedPyObject serialized = SerializedPyObject()
wrapped_value = [value]
if context is None:
context = _default_serialization_context
with nogil:
check_status(SerializeObject(context, wrapped_value, &serialized.data))
return serialized
def serialize_to(object value, sink, SerializationContext context=None):
"""EXPERIMENTAL: Serialize a Python sequence to a file.
Parameters
----------
value: object
Python object for the sequence that is to be serialized.
sink: NativeFile or file-like
File the sequence will be written to.
context : SerializationContext
Custom serialization and deserialization context, uses a default
context with some standard type handlers if not specified
"""
serialized = serialize(value, context)
serialized.write_to(sink)
def read_serialized(source, base=None):
"""EXPERIMENTAL: Read serialized Python sequence from file-like object
Parameters
----------
source: NativeFile
File to read the sequence from.
base: object
This object will be the base object of all the numpy arrays
contained in the sequence.
Returns
-------
serialized : the serialized data
"""
cdef shared_ptr[RandomAccessFile] stream
get_reader(source, &stream)
cdef SerializedPyObject serialized = SerializedPyObject()
serialized.base = base
with nogil:
check_status(ReadSerializedObject(stream.get(), &serialized.data))
return serialized
def deserialize_from(source, object base, SerializationContext context=None):
"""EXPERIMENTAL: Deserialize a Python sequence from a file.
Parameters
----------
source: NativeFile
File to read the sequence from.
base: object
This object will be the base object of all the numpy arrays
contained in the sequence.
context : SerializationContext
Custom serialization and deserialization context
Returns
-------
object
Python object for the deserialized sequence.
"""
serialized = read_serialized(source, base=base)
return serialized.deserialize(context)
def deserialize(obj, SerializationContext context=None):
"""
EXPERIMENTAL: Deserialize Python object from Buffer or other Python object
supporting the buffer protocol
Parameters
----------
obj : pyarrow.Buffer or Python object supporting buffer protocol
context : SerializationContext
Custom serialization and deserialization context
Returns
-------
deserialized : object
"""
source = BufferReader(obj)
return deserialize_from(source, obj, context)