1 /**
2 *
3 * Copyright 2007-2021 IMP Inventors. All rights reserved.
4 *
5 */
6
7 #ifndef RMF_ENCODE_DECODE_H
8 #define RMF_ENCODE_DECODE_H
9
10 #include <boost/any.hpp>
11 #include "avrocpp/api/Specific.hh"
12 #include "avrocpp/api/Encoder.hh"
13 #include "avrocpp/api/Decoder.hh"
14 #include "RMF/ID.h"
15 #include <boost/unordered_map.hpp>
16 #include <boost/unordered_set.hpp>
17 #include <set>
18 #include "types.h"
19 #include "RMF/constants.h"
20 #include "RMF/log.h"
21 #include "encode_decode_util.h"
22 #if BOOST_VERSION >= 104900
23 #include <boost/container/flat_set.hpp>
test_comparenull24 #include <boost/container/flat_map.hpp>
25 #endif
26
27 RMF_ENABLE_WARNINGS
28
29 namespace internal_avro {
30
31 template <class B>
32 struct codec_traits<RMF::avro2::Skip<B> > {
33 template <class Decoder>
34 static void decode(Decoder& d, RMF::avro2::Skip<B>&) {
35 B b;
36 internal_avro::decode(d, b);
37 }
38 };
39
40 template <>
41 struct codec_traits<RMF::avro2::Skip<std::string> > {
42 template <class Decoder>
43 static void decode(Decoder& d, RMF::avro2::Skip<std::string>&) {
44 d.skipString();
45 }
46 };
47
48 template <class B>
49 struct codec_traits<RMF::avro2::Skip<std::vector<B> > >;
50
51 template <class K, class V>
52 struct codec_traits<std::pair<K, V> > {
53 template <class Encoder>
54 static void encode(Encoder& e, const std::pair<K, V>& v) {
55 internal_avro::encode(e, v.first);
56 internal_avro::encode(e, v.second);
57 }
58 template <class Decoder>
59 static void decode(Decoder& d, std::pair<K, V>& v) {
60 internal_avro::decode(d, v.first);
61 internal_avro::decode(d, v.second);
62 }
63 };
64
65 template <class K, class V>
66 struct codec_traits<RMF::avro2::Skip<std::pair<K, V> > > {
67 template <class Decoder>
68 static void decode(Decoder& d, RMF::avro2::Skip<std::pair<K, V> >&) {
69 RMF::avro2::Skip<K> first;
70 internal_avro::decode(d, first);
71 RMF::avro2::Skip<V> second;
72 internal_avro::decode(d, second);
73 }
74 };
75
76 template <class B>
77 struct codec_traits<RMF::avro2::Skip<std::vector<B> > > {
78 template <class Decoder>
79 static void decode(Decoder& d, RMF::avro2::Skip<std::vector<B> >&) {
80 RMF::avro2::Skip<B> t;
81 size_t sa = d.skipArray();
82 if (sa != 0) {
83 for (size_t n = sa; n != 0; n = d.arrayNext()) {
84 for (size_t i = 0; i < n; ++i) {
85 internal_avro::decode(d, t);
86 }
87 }
88 }
89 }
90 };
91
92 template <RMF::VectorDimension V>
93 struct codec_traits<RMF_VECTOR<V> > {
94 template <class Encoder>
95 static void encode(Encoder& e, const RMF_VECTOR<V>& v) {
96 for (unsigned int i = 0; i < V; ++i) {
97 e.encodeFloat(v[i]);
98 }
99 }
100 template <class Decoder>
101 static void decode(Decoder& d, RMF_VECTOR<V>& v) {
102 for (unsigned int i = 0; i < V; ++i) {
103 v[i] = d.decodeFloat();
104 }
105 }
106 };
107
108 template <RMF::VectorDimension V>
109 struct codec_traits<RMF::avro2::Skip<RMF_VECTOR<V> > > {
110 template <class Decoder>
111 static void decode(Decoder& d, RMF::avro2::Skip<RMF_VECTOR<V> >&) {
112 for (unsigned int i = 0; i < V; ++i) {
113 float f;
114 internal_avro::decode(d, f);
115 }
116 }
117 };
118
119 template <>
120 struct codec_traits<RMF::NodeType> {
121 template <class Encoder>
122 static void encode(Encoder& e, const RMF::NodeType& v) {
123 internal_avro::encode(e, static_cast<int32_t>(v));
124 }
125 template <class Decoder>
126 static void decode(Decoder& d, RMF::NodeType& v) {
127 int32_t vv;
128 internal_avro::decode(d, vv);
129 // If the node type is unknown, map it to the 'invalid' type.
130 // This allows us to read in files generated by newer versions of
131 // RMF that have more node types.
132 v = RMF::NodeType(true, vv);
133 }
134 };
135
136 template <>
137 struct codec_traits<RMF::FrameType> {
138 template <class Encoder>
139 static void encode(Encoder& e, const RMF::FrameType& v) {
140 internal_avro::encode(e, static_cast<int32_t>(v));
141 }
142 template <class Decoder>
143 static void decode(Decoder& d, RMF::FrameType& v) {
144 int32_t vv;
145 internal_avro::decode(d, vv);
146 v = RMF::FrameType(vv);
147 }
148 };
149
150 template <class V>
151 struct codec_traits<RMF::ID<V> > {
152 template <class Encoder>
153 static void encode(Encoder& e, const RMF::ID<V>& v) {
154 RMF_INTERNAL_CHECK(v != RMF::ID<V>(), "Uninitialized ID");
155 internal_avro::encode(e, static_cast<int32_t>(v.get_index_always()));
156 }
157 template <class Decoder>
158 static void decode(Decoder& d, RMF::ID<V>& v) {
159 int32_t index;
160 internal_avro::decode(d, index);
161 if (index >= 0) {
162 v = RMF::ID<V>(index);
163 } else {
164 v = RMF::ID<V>(index, typename RMF::ID<V>::SpecialTag());
165 }
166 }
167 };
168
169 template <class K, class V>
170 struct codec_traits<boost::unordered_map<K, V> > {
171 typedef std::pair<K, V> KP;
172 template <class Encoder>
173 static void encode(Encoder& e, const boost::unordered_map<K, V>& v) {
174 std::vector<KP> pairs(v.begin(), v.end());
175 internal_avro::encode(e, pairs);
176 }
177 template <class Decoder>
178 static void decode(Decoder& d, boost::unordered_map<K, V>& v) {
179 std::vector<KP> values;
180 internal_avro::decode(d, values);
181 v.insert(values.begin(), values.end());
182 }
183 };
184
185 template <class K, class V>
186 struct codec_traits<RMF::avro2::Skip<boost::unordered_map<K, V> > > {
187 template <class Decoder>
188 static void decode(Decoder& d,
189 RMF::avro2::Skip<boost::unordered_map<K, V> >&) {
190 RMF::avro2::Skip<std::vector<std::pair<K, V> > > v;
191 internal_avro::decode(d, v);
192 }
193 };
194
195 template <class K>
196 struct codec_traits<boost::unordered_set<K> > {
197 template <class Encoder>
198 static void encode(Encoder& e, const boost::unordered_set<K>& v) {
199 std::vector<K> values(v.begin(), v.end());
200 internal_avro::encode(e, values);
201 }
202 template <class Decoder>
203 static void decode(Decoder& d, boost::unordered_set<K>& v) {
204 std::vector<K> values;
205 internal_avro::decode(d, values);
206 v.insert(values.begin(), values.end());
207 }
208 };
209
210 template <class K>
211 struct codec_traits<RMF::avro2::Skip<boost::unordered_set<K> > > {
212 template <class Decoder>
213 static void decode(Decoder& d, RMF::avro2::Skip<boost::unordered_set<K> >&) {
214 RMF::avro2::Skip<std::vector<K> > values;
215 internal_avro::decode(d, values);
216 }
217 };
218
219 #if BOOST_VERSION >= 104900
220 template <class K, class V>
221 struct codec_traits<boost::container::flat_map<K, V> > {
222 typedef std::pair<K, V> KP;
223 template <class Encoder>
224 static void encode(Encoder& e, const boost::container::flat_map<K, V>& v) {
225 std::vector<KP> pairs(v.begin(), v.end());
226 internal_avro::encode(e, pairs);
227 }
228 template <class Decoder>
229 static void decode(Decoder& d, boost::container::flat_map<K, V>& v) {
230 std::vector<KP> values;
231 internal_avro::decode(d, values);
232 v.insert(values.begin(), values.end());
233 }
234 };
235
236 template <class K, class V>
237 struct codec_traits<RMF::avro2::Skip<boost::container::flat_map<K, V> > > {
238 template <class Decoder>
239 static void decode(Decoder& d,
240 RMF::avro2::Skip<boost::container::flat_map<K, V> >&) {
241 RMF::avro2::Skip<std::vector<std::pair<K, V> > > values;
242 internal_avro::decode(d, values);
243 }
244 };
245
246 template <class K>
247 struct codec_traits<boost::container::flat_set<K> > {
248 template <class Encoder>
249 static void encode(Encoder& e, const boost::container::flat_set<K>& v) {
250 std::vector<K> values(v.begin(), v.end());
251 internal_avro::encode(e, values);
252 }
253 template <class Decoder>
254 static void decode(Decoder& d, boost::container::flat_set<K>& v) {
255 std::vector<K> values;
256 internal_avro::decode(d, values);
257 v.insert(values.begin(), values.end());
258 }
259 };
260
261 template <class K>
262 struct codec_traits<RMF::avro2::Skip<boost::container::flat_set<K> > > {
263 template <class Decoder>
264 static void decode(Decoder& d,
265 RMF::avro2::Skip<boost::container::flat_set<K> >&) {
266 RMF::avro2::Skip<std::vector<K> > values;
267 internal_avro::decode(d, values);
268 }
269 };
270 #endif
271
272 template <class K, class V>
273 struct codec_traits<std::map<K, V> > {
274 typedef std::pair<K, V> KP;
275 template <class Encoder>
276 static void encode(Encoder& e, const std::map<K, V>& v) {
277 std::vector<KP> pairs(v.begin(), v.end());
278 internal_avro::encode(e, pairs);
279 }
280 template <class Decoder>
281 static void decode(Decoder& d, std::map<K, V>& v) {
282 std::vector<KP> values;
283 internal_avro::decode(d, values);
284 v.insert(values.begin(), values.end());
285 }
286 };
287
288 template <class K>
289 struct codec_traits<std::set<K> > {
290 template <class Encoder>
291 static void encode(Encoder& e, const std::set<K>& v) {
292 std::vector<K> values(v.begin(), v.end());
293 internal_avro::encode(e, values);
294 }
295 template <class Decoder>
296 static void decode(Decoder& d, std::set<K>& v) {
297 std::vector<K> values;
298 internal_avro::decode(d, values);
299 v.insert(values.begin(), values.end());
300 }
301 };
302
303
304 template <>
305 struct codec_traits<RMF::avro2::StringAccumulator> {
306 template <class Encoder>
307 static void encode(Encoder& e, const RMF::avro2::StringAccumulator& v) {
308 internal_avro::encode(e, static_cast<const std::string&>(v));
309 }
310 template <class Decoder>
311 static void decode(Decoder& d, RMF::avro2::StringAccumulator& v) {
312 std::string cur;
313 internal_avro::decode(d, cur);
314 if (!cur.empty()) v.set(cur);
315 }
316 };
317
318 template <class T, class Ty>
319 struct codec_traits<RMF::internal::HierarchyNode<T, Ty> > {
320 template <class Encoder>
321 static void encode(Encoder& e, const RMF::internal::HierarchyNode<T, Ty>& v) {
322 internal_avro::encode(e, v.name);
323 internal_avro::encode(e, v.type);
324 internal_avro::encode(e, v.parents);
325 internal_avro::encode(e, v.children);
326 }
327 template <class Decoder>
328 static void decode(Decoder& d, RMF::internal::HierarchyNode<T, Ty>& v) {
329 internal_avro::decode(d, v.name);
330 internal_avro::decode(d, v.type);
331 internal_avro::decode(d, v.parents);
332 internal_avro::decode(d, v.children);
333 }
334 };
335
336 template <>
337 struct codec_traits<RMF::avro2::HierarchyNode> {
338 template <class Encoder>
339 static void encode(Encoder& e, const RMF::avro2::HierarchyNode& v) {
340 internal_avro::encode(e, v.id);
341 internal_avro::encode(e, v.name);
342 internal_avro::encode(e, v.type);
343 internal_avro::encode(e, v.parents);
344 }
345 template <class Decoder>
346 static void decode(Decoder& d, RMF::avro2::HierarchyNode& v) {
347 internal_avro::decode(d, v.id);
348 internal_avro::decode(d, v.name);
349 internal_avro::decode(d, v.type);
350 internal_avro::decode(d, v.parents);
351 }
352 };
353
354 template <>
355 struct codec_traits<RMF::avro2::KeyType> {
356 template <class Encoder>
357 static void encode(Encoder& e, RMF::avro2::KeyType v) {
358 e.encodeEnum(v);
359 }
360 template <class Decoder>
361 static void decode(Decoder& d, RMF::avro2::KeyType& v) {
362 v = static_cast<RMF::avro2::KeyType>(d.decodeEnum());
363 }
364 };
365
366 template <>
367 struct codec_traits<RMF::avro2::KeyInfo> {
368 template <class Encoder>
369 static void encode(Encoder& e, const RMF::avro2::KeyInfo& v) {
370 internal_avro::encode(e, v.id);
371 internal_avro::encode(e, v.name);
372 internal_avro::encode(e, v.category);
373 internal_avro::encode(e, v.type);
374 }
375 template <class Decoder>
376 static void decode(Decoder& d, RMF::avro2::KeyInfo& v) {
377 internal_avro::decode(d, v.id);
378 internal_avro::decode(d, v.name);
379 internal_avro::decode(d, v.category);
380 internal_avro::decode(d, v.type);
381 }
382 };
383
384 template <class Traits>
385 struct codec_traits<RMF::internal::KeyData<Traits> > {
386 template <class Encoder>
387 static void encode(Encoder& e, const RMF::internal::KeyData<Traits>& v) {
388 internal_avro::encode(
389 e, static_cast<const typename RMF::internal::KeyData<Traits>::P&>(v));
390 }
391 template <class Decoder>
392 static void decode(Decoder& d, RMF::internal::KeyData<Traits>& v) {
393 internal_avro::decode(
394 d, static_cast<typename RMF::internal::KeyData<Traits>::P&>(v));
395 }
396 };
397
398 template <class Traits>
399 struct codec_traits<RMF::avro2::Skip<RMF::internal::KeyData<Traits> > > {
400 template <class Decoder>
401 static void decode(Decoder& d,
402 RMF::avro2::Skip<RMF::internal::KeyData<Traits> >&) {
403 RMF::avro2::Skip<typename RMF::internal::KeyData<Traits>::P> v;
404 internal_avro::decode(d, v);
405 }
406 };
407
408 template <class Traits>
409 struct codec_traits<RMF::internal::TypeData<Traits> > {
410 template <class Encoder>
411 static void encode(Encoder& e, const RMF::internal::TypeData<Traits>& v) {
412 internal_avro::encode(
413 e, static_cast<const typename RMF::internal::TypeData<Traits>::P&>(v));
414 }
415 template <class Decoder>
416 static void decode(Decoder& d, RMF::internal::TypeData<Traits>& v) {
417 internal_avro::decode(
418 d, static_cast<typename RMF::internal::TypeData<Traits>::P&>(v));
419 }
420 };
421
422 template <class Traits>
423 struct codec_traits<RMF::avro2::Skip<RMF::internal::TypeData<Traits> > > {
424 template <class Decoder>
425 static void decode(Decoder& d,
426 RMF::avro2::Skip<RMF::internal::TypeData<Traits> >&) {
427 typename RMF::avro2::Skip<typename RMF::internal::TypeData<Traits>::P> v;
428 internal_avro::decode(d, v);
429 }
430 };
431
432 template <>
433 struct codec_traits<RMF::avro2::DataTypes> {
434 template <class Encoder>
435 static void encode(Encoder& e, const RMF::avro2::DataTypes& v) {
436 internal_avro::encode(e, v.int_data);
437 internal_avro::encode(e, v.float_data);
438 internal_avro::encode(e, v.string_data);
439 internal_avro::encode(e, v.vector3_data);
440 internal_avro::encode(e, v.vector4_data);
441 internal_avro::encode(e, v.ints_data);
442 internal_avro::encode(e, v.floats_data);
443 internal_avro::encode(e, v.strings_data);
444 internal_avro::encode(e, v.vector3s_data);
445 RMF::internal::TypeData<RMF::Vector4sTraits> empty;
446 internal_avro::encode(e, empty);
447 }
448 template <class Decoder>
449 static void decode(Decoder& d, RMF::avro2::DataTypes& v) {
450 internal_avro::decode(d, v.int_data);
451 internal_avro::decode(d, v.float_data);
452 internal_avro::decode(d, v.string_data);
453 internal_avro::decode(d, v.vector3_data);
454 internal_avro::decode(d, v.vector4_data);
455 internal_avro::decode(d, v.ints_data);
456 internal_avro::decode(d, v.floats_data);
457 internal_avro::decode(d, v.strings_data);
458 internal_avro::decode(d, v.vector3s_data);
459 RMF::internal::TypeData<RMF::Vector4sTraits> empty;
460 internal_avro::decode(d, empty);
461 }
462 };
463
464 template <>
465 struct codec_traits<RMF::avro2::Skip<RMF::avro2::DataTypes> > {
466 template <class Decoder>
467 static void decode(Decoder& d, RMF::avro2::Skip<RMF::avro2::DataTypes>&) {
468 RMF::avro2::Skip<RMF::internal::TypeData<RMF::IntTraits> > int_data;
469 internal_avro::decode(d, int_data);
470 RMF::avro2::Skip<RMF::internal::TypeData<RMF::FloatTraits> > float_data;
471 internal_avro::decode(d, float_data);
472 RMF::avro2::Skip<RMF::internal::TypeData<RMF::StringTraits> > string_data;
473 internal_avro::decode(d, string_data);
474 RMF::avro2::Skip<RMF::internal::TypeData<RMF::Vector3Traits> > vector3_data;
475 internal_avro::decode(d, vector3_data);
476 RMF::avro2::Skip<RMF::internal::TypeData<RMF::Vector4Traits> > vector4_data;
477 internal_avro::decode(d, vector4_data);
478 RMF::avro2::Skip<RMF::internal::TypeData<RMF::IntsTraits> > ints_data;
479 internal_avro::decode(d, ints_data);
480 RMF::avro2::Skip<RMF::internal::TypeData<RMF::FloatsTraits> > floats_data;
481 internal_avro::decode(d, floats_data);
482 RMF::avro2::Skip<RMF::internal::TypeData<RMF::StringsTraits> > strings_data;
483 internal_avro::decode(d, strings_data);
484 RMF::avro2::Skip<RMF::internal::TypeData<RMF::Vector3sTraits> >
485 vector3s_data;
486 internal_avro::decode(d, vector3s_data);
487 RMF::avro2::Skip<RMF::internal::TypeData<RMF::Vector4sTraits> >
488 vector4s_data;
489 internal_avro::decode(d, vector4s_data);
490 }
491 };
492
493 template <>
494 struct codec_traits<RMF::avro2::Frame> {
495 template <class Encoder>
496 static void encode(Encoder& e, const RMF::avro2::Frame& v) {
497 e.encodeUnionIndex(0);
498 internal_avro::encode(e, v.id);
499 internal_avro::encode(e, v.name);
500 internal_avro::encode(e, v.type);
501 internal_avro::encode(e, v.parents);
502
503 internal_avro::encode(e, v.nodes);
504 internal_avro::encode(e, v.keys);
505 internal_avro::encode(e, v.data);
506 }
507 template <class Decoder>
508 static void decode(Decoder& d, RMF::avro2::Frame& v) {
509 size_t n = d.decodeUnionIndex();
510 if (n == 0) {
511 internal_avro::decode(d, v.id);
512 internal_avro::decode(d, v.name);
513 internal_avro::decode(d, v.type);
514 internal_avro::decode(d, v.parents);
515 } else {
516 RMF::avro2::Skip<std::string> junk_string;
517 internal_avro::decode(d, junk_string);
518 internal_avro::decode(d, junk_string);
519 RMF::avro2::Skip<std::vector<std::pair<int32_t, std::string> > >
520 junk_labels;
521 internal_avro::decode(d, junk_labels);
522 internal_avro::decode(d, junk_labels);
523 internal_avro::decode(d, junk_labels);
524 RMF::avro2::Skip<std::vector<std::pair<int32_t, RMF::NodeIDs> > >
525 junk_node_sets;
526 internal_avro::decode(d, junk_node_sets);
527 }
528 RMF::avro2::Skip<std::vector<RMF::avro2::HierarchyNode> > nodes;
529 internal_avro::decode(d, nodes);
530 RMF::avro2::Skip<std::vector<RMF::avro2::KeyInfo> > keys;
531 internal_avro::decode(d, keys);
532
533 internal_avro::decode(d, v.data);
534 }
535 };
536
537 template <>
538 struct codec_traits<RMF::avro2::FileData> {
539 template <class Decoder>
540 static void decode(Decoder& d, RMF::avro2::FileData& v) {
541 size_t n = d.decodeUnionIndex();
542 if (n == 0) {
543 internal_avro::decode(d, v.cur_id);
544 v.max_id = std::max(v.max_id, v.cur_id);
545 internal_avro::decode(d, v.frames[v.cur_id].name);
546 internal_avro::decode(d, v.frames[v.cur_id].type);
547 internal_avro::decode(d, v.frames[v.cur_id].parents);
548 RMF_INFO("Found frame " << v.cur_id);
549 } else {
550 RMF_INFO("Found static frame");
551 v.cur_id = RMF::FrameID();
552 internal_avro::decode(d, v.description);
553 internal_avro::decode(d, v.producer);
554 std::vector<std::pair<RMF::Category, std::string> > categories;
555 internal_avro::decode(d, categories);
556 v.categories.insert(v.categories.end(), categories.begin(),
557 categories.end());
558 internal_avro::decode(d, v.extra_node_types);
559 internal_avro::decode(d, v.extra_frame_types);
560
561 internal_avro::decode(d, v.node_sets);
562 }
563 std::vector<RMF::avro2::HierarchyNode> nodes;
564 internal_avro::decode(d, nodes);
565 RMF_FOREACH(const RMF::avro2::HierarchyNode & hn, nodes) {
566 v.nodes.resize(
567 std::max<std::size_t>(v.nodes.size(), hn.id.get_index() + 1));
568 v.nodes[hn.id.get_index()].name = hn.name;
569 v.nodes[hn.id.get_index()].type = hn.type;
570 v.nodes[hn.id.get_index()]
571 .parents.insert(v.nodes[hn.id.get_index()].parents.end(),
572 hn.parents.begin(), hn.parents.end());
573 RMF_FOREACH(RMF::NodeID ch, hn.parents) {
574 v.nodes.resize(
575 std::max<std::size_t>(v.nodes.size(), ch.get_index() + 1));
576 v.nodes[ch.get_index()].children.push_back(hn.id);
577 }
578 }
579
580 std::vector<RMF::avro2::KeyInfo> keys;
581 internal_avro::decode(d, keys);
582 RMF::avro2::sort_keys(keys, v.keys);
583
584 if (n == 1) {
585 internal_avro::decode(d, v.data);
586 } else {
587 RMF::avro2::Skip<RMF::avro2::DataTypes> skip_data;
588 internal_avro::decode(d, skip_data);
589 }
590 }
591 };
592
593 template <>
594 struct codec_traits<RMF::avro2::FileDataChanges> {
595 template <class Encoder>
596 static void encode(Encoder& e, const RMF::avro2::FileDataChanges& v) {
597 e.encodeUnionIndex(1);
598 internal_avro::encode(e, v.description);
599 internal_avro::encode(e, v.producer);
600 internal_avro::encode(e, v.categories);
601 internal_avro::encode(e, v.node_types);
602 internal_avro::encode(e, v.frame_types);
603 internal_avro::encode(e, v.node_sets);
604
605 internal_avro::encode(e, v.nodes);
606 internal_avro::encode(e, v.keys);
607 internal_avro::encode(e, v.data);
608 }
609 };
610
611 }
612
613 RMF_DISABLE_WARNINGS
614
615 #endif /* RMF_ENCODE_DECODE_H */
616