aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/avro/api/GenericDatum.hh
blob: 15cd53becb28cd3bf28340d62aa1078f4f7f2a46 (plain) (blame)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* 
 * 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 
 * 
 *     https://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. 
 */ 
 
#ifndef avro_GenericDatum_hh__ 
#define avro_GenericDatum_hh__ 
 
#include <stdint.h> 
#include <vector> 
#include <map> 
#include <string> 
 
#if __cplusplus >= 201703L 
#include <any> 
#else 
#include "boost/any.hpp" 
#endif 
 
#include "LogicalType.hh" 
#include "Node.hh" 
#include "ValidSchema.hh" 
 
namespace avro { 
 
/** 
 * Generic datum which can hold any Avro type. The datum has a type 
 * and a value. The type is one of the Avro data types. The C++ type for 
 * value corresponds to the Avro type. 
 * \li An Avro <tt>null</tt> corresponds to no C++ type. It is illegal to 
 * to try to access values for <tt>null</tt>. 
 * \li Avro <tt>boolean</tt> maps to C++ <tt>bool</tt> 
 * \li Avro <tt>int</tt> maps to C++ <tt>int32_t</tt>. 
 * \li Avro <tt>long</tt> maps to C++ <tt>int64_t</tt>. 
 * \li Avro <tt>float</tt> maps to C++ <tt>float</tt>. 
 * \li Avro <tt>double</tt> maps to C++ <tt>double</tt>. 
 * \li Avro <tt>string</tt> maps to C++ <tt>std::string</tt>. 
 * \li Avro <tt>bytes</tt> maps to C++ <tt>std::vector&lt;uint_t&gt;</tt>. 
 * \li Avro <tt>fixed</tt> maps to C++ class <tt>GenericFixed</tt>. 
 * \li Avro <tt>enum</tt> maps to C++ class <tt>GenericEnum</tt>. 
 * \li Avro <tt>array</tt> maps to C++ class <tt>GenericArray</tt>. 
 * \li Avro <tt>map</tt> maps to C++ class <tt>GenericMap</tt>. 
 * \li There is no C++ type corresponding to Avro <tt>union</tt>. The 
 * object should have the C++ type corresponing to one of the constituent 
 * types of the union. 
 * 
 */ 
class AVRO_DECL GenericDatum { 
protected: 
    Type type_; 
    LogicalType logicalType_; 
#if __cplusplus >= 201703L 
    std::any value_; 
#else 
    boost::any value_; 
#endif 
 
    GenericDatum(Type t) 
        : type_(t), logicalType_(LogicalType::NONE) { } 
 
    GenericDatum(Type t, LogicalType logicalType) 
        : type_(t), logicalType_(logicalType) { } 
 
    template <typename T> 
    GenericDatum(Type t, LogicalType logicalType, const T& v) 
        : type_(t), logicalType_(logicalType), value_(v) { } 
 
    void init(const NodePtr& schema); 
public: 
    /** 
     * The avro data type this datum holds. 
     */ 
    Type type() const; 
 
    /** 
     * The avro logical type that augments the main data type this datum holds. 
     */ 
    LogicalType logicalType() const; 
 
    /** 
     * Returns the value held by this datum. 
     * T The type for the value. This must correspond to the 
     * avro type returned by type(). 
     */ 
    template<typename T> const T& value() const; 
 
    /** 
     * Returns the reference to the value held by this datum, which 
     * can be used to change the contents. Please note that only 
     * value can be changed, the data type of the value held cannot 
     * be changed. 
     * 
     * T The type for the value. This must correspond to the 
     * avro type returned by type(). 
     */ 
    template<typename T> T& value(); 
 
    /** 
     * Returns true if and only if this datum is a union. 
     */ 
    bool isUnion() const { return type_ == AVRO_UNION; } 
 
    /** 
     * Returns the index of the current branch, if this is a union. 
     * \sa isUnion(). 
     */ 
    size_t unionBranch() const; 
 
    /** 
     * Selects a new branch in the union if this is a union. 
     * \sa isUnion(). 
     */ 
    void selectBranch(size_t branch); 
 
    /// Makes a new AVRO_NULL datum. 
    GenericDatum() : type_(AVRO_NULL), logicalType_(LogicalType::NONE) { } 
 
    /// Makes a new AVRO_BOOL datum whose value is of type bool. 
    GenericDatum(bool v) 
        : type_(AVRO_BOOL), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_INT datum whose value is of type int32_t. 
    GenericDatum(int32_t v) 
        : type_(AVRO_INT), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_LONG datum whose value is of type int64_t. 
    GenericDatum(int64_t v) 
        : type_(AVRO_LONG), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_FLOAT datum whose value is of type float. 
    GenericDatum(float v) 
        : type_(AVRO_FLOAT), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_DOUBLE datum whose value is of type double. 
    GenericDatum(double v) 
        : type_(AVRO_DOUBLE), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_STRING datum whose value is of type std::string. 
    GenericDatum(const std::string& v) 
        : type_(AVRO_STRING), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /// Makes a new AVRO_BYTES datum whose value is of type 
    /// std::vector<uint8_t>. 
    GenericDatum(const std::vector<uint8_t>& v) : 
        type_(AVRO_BYTES), logicalType_(LogicalType::NONE), value_(v) { } 
 
    /** 
     * Constructs a datum corresponding to the given avro type. 
     * The value will the appropriate default corresponding to the 
     * data type. 
     * \param schema The schema that defines the avro type. 
     */ 
    GenericDatum(const NodePtr& schema); 
 
    /** 
     * Constructs a datum corresponding to the given avro type and set 
     * the value. 
     * \param schema The schema that defines the avro type. 
     * \param v The value for this type. 
     */ 
    template<typename T> 
    GenericDatum(const NodePtr& schema, const T& v) : 
        type_(schema->type()), logicalType_(schema->logicalType()) { 
        init(schema); 
#if __cplusplus >= 201703L 
        *std::any_cast<T>(&value_) = v; 
#else 
        *boost::any_cast<T>(&value_) = v; 
#endif 
    } 
 
    /** 
     * Constructs a datum corresponding to the given avro type. 
     * The value will the appropriate default corresponding to the 
     * data type. 
     * \param schema The schema that defines the avro type. 
     */ 
    GenericDatum(const ValidSchema& schema); 
}; 
 
/** 
 * The base class for all generic type for containers. 
 */ 
class AVRO_DECL GenericContainer { 
    NodePtr schema_; 
    static void assertType(const NodePtr& schema, Type type); 
protected: 
    /** 
     * Constructs a container corresponding to the given schema. 
     */ 
    GenericContainer(Type type, const NodePtr& s) : schema_(s) { 
        assertType(s, type); 
    } 
 
public: 
    /// Returns the schema for this object 
    const NodePtr& schema() const { 
        return schema_; 
    } 
}; 
 
/** 
 * Generic container for unions. 
 */ 
class AVRO_DECL GenericUnion : public GenericContainer { 
    size_t curBranch_; 
    GenericDatum datum_; 
 
public: 
    /** 
     * Constructs a generic union corresponding to the given schema \p schema, 
     * and the given value. The schema should be of Avro type union 
     * and the value should correspond to one of the branches of the union. 
     */ 
    GenericUnion(const NodePtr& schema) : 
        GenericContainer(AVRO_UNION, schema), curBranch_(schema->leaves()) { 
        selectBranch(0); 
    } 
 
    /** 
     * Returns the index of the current branch. 
     */ 
    size_t currentBranch() const { return curBranch_; } 
 
    /** 
     * Selects a new branch. The type for the value is changed accordingly. 
     * \param branch The index for the selected branch. 
     */ 
    void selectBranch(size_t branch) { 
        if (curBranch_ != branch) { 
            datum_ = GenericDatum(schema()->leafAt(branch)); 
            curBranch_ = branch; 
        } 
    } 
 
    /** 
     * Returns the datum corresponding to the currently selected branch 
     * in this union. 
     */ 
    GenericDatum& datum() { 
        return datum_; 
    } 
 
    /** 
     * Returns the datum corresponding to the currently selected branch 
     * in this union. 
     */ 
    const GenericDatum& datum() const { 
        return datum_; 
    } 
}; 
 
/** 
 * The generic container for Avro records. 
 */ 
class AVRO_DECL GenericRecord : public GenericContainer { 
    std::vector<GenericDatum> fields_; 
public: 
    /** 
     * Constructs a generic record corresponding to the given schema \p schema, 
     * which should be of Avro type record. 
     */ 
    GenericRecord(const NodePtr& schema); 
 
    /** 
     * Returns the number of fields in the current record. 
     */ 
    size_t fieldCount() const { 
        return fields_.size(); 
    } 
 
    /** 
     * Returns index of the field with the given name \p name 
     */ 
    size_t fieldIndex(const std::string& name) const { 
        size_t index = 0; 
        if (!schema()->nameIndex(name, index)) { 
            throw Exception("Invalid field name: " + name); 
        } 
        return index; 
    } 
 
    /** 
     * Returns true if a field with the given name \p name is located in this r 
     * false otherwise 
     */ 
    bool hasField(const std::string& name) const { 
        size_t index = 0; 
        return schema()->nameIndex(name, index); 
    } 
 
    /** 
     * Returns the field with the given name \p name. 
     */ 
    const GenericDatum& field(const std::string& name) const { 
        return fieldAt(fieldIndex(name)); 
    } 
 
    /** 
     * Returns the reference to the field with the given name \p name, 
     * which can be used to change the contents. 
     */ 
    GenericDatum& field(const std::string& name) { 
        return fieldAt(fieldIndex(name)); 
    } 
 
    /** 
     * Returns the field at the given position \p pos. 
     */ 
    const GenericDatum& fieldAt(size_t pos) const { 
        return fields_[pos]; 
    } 
 
    /** 
     * Returns the reference to the field at the given position \p pos, 
     * which can be used to change the contents. 
     */ 
    GenericDatum& fieldAt(size_t pos) { 
        return fields_[pos]; 
    } 
 
    /** 
     * Replaces the field at the given position \p pos with \p v. 
     */ 
    void setFieldAt(size_t pos, const GenericDatum& v) { 
        // assertSameType(v, schema()->leafAt(pos)); 
        fields_[pos] = v; 
    } 
}; 
 
/** 
 * The generic container for Avro arrays. 
 */ 
class AVRO_DECL GenericArray : public GenericContainer { 
public: 
    /** 
     * The contents type for the array. 
     */ 
    typedef std::vector<GenericDatum> Value; 
 
    /** 
     * Constructs a generic array corresponding to the given schema \p schema, 
     * which should be of Avro type array. 
     */ 
    GenericArray(const NodePtr& schema) : GenericContainer(AVRO_ARRAY, schema) { 
    } 
 
    /** 
     * Returns the contents of this array. 
     */ 
    const Value& value() const { 
        return value_; 
    } 
 
    /** 
     * Returns the reference to the contents of this array. 
     */ 
    Value& value() { 
        return value_; 
    } 
private: 
    Value value_; 
}; 
 
/** 
 * The generic container for Avro maps. 
 */ 
class AVRO_DECL GenericMap : public GenericContainer { 
public: 
    /** 
     * The contents type for the map. 
     */ 
    typedef std::vector<std::pair<std::string, GenericDatum> > Value; 
 
    /** 
     * Constructs a generic map corresponding to the given schema \p schema, 
     * which should be of Avro type map. 
     */ 
    GenericMap(const NodePtr& schema) : GenericContainer(AVRO_MAP, schema) { 
    } 
 
    /** 
     * Returns the contents of this map. 
     */ 
    const Value& value() const { 
        return value_; 
    } 
 
    /** 
     * Returns the reference to the contents of this map. 
     */ 
    Value& value() { 
        return value_; 
    } 
private: 
    Value value_; 
}; 
 
/** 
 * Generic container for Avro enum. 
 */ 
class AVRO_DECL GenericEnum : public GenericContainer { 
    size_t value_; 
 
    static size_t index(const NodePtr& schema, const std::string& symbol) { 
        size_t result; 
        if (schema->nameIndex(symbol, result)) { 
            return result; 
        } 
        throw Exception("No such symbol"); 
    } 
 
public: 
    /** 
     * Constructs a generic enum corresponding to the given schema \p schema, 
     * which should be of Avro type enum. 
     */ 
    GenericEnum(const NodePtr& schema) : 
        GenericContainer(AVRO_ENUM, schema), value_(0) { 
    } 
 
    GenericEnum(const NodePtr& schema, const std::string& symbol) : 
        GenericContainer(AVRO_ENUM, schema), value_(index(schema, symbol)) { 
    } 
 
    /** 
     * Returns the symbol corresponding to the cardinal \p n. If the 
     * value for \p n is not within the limits an exception is thrown. 
     */ 
    const std::string& symbol(size_t n) { 
        if (n < schema()->names()) { 
            return schema()->nameAt(n); 
        } 
        throw Exception("Not as many symbols"); 
    } 
 
    /** 
     * Returns the cardinal for the given symbol \c symbol. If the symbol 
     * is not defined for this enum and exception is thrown. 
     */ 
    size_t index(const std::string& symbol) const { 
        return index(schema(), symbol); 
    } 
 
    /** 
     * Set the value for this enum corresponding to the given symbol \c symbol. 
     */ 
    size_t set(const std::string& symbol) { 
        return value_ = index(symbol); 
    } 
 
    /** 
     * Set the value for this enum corresponding to the given cardinal \c n. 
     */ 
    void set(size_t n) { 
        if (n < schema()->names()) { 
            value_ = n; 
            return; 
        } 
        throw Exception("Not as many symbols"); 
    } 
 
    /** 
     * Returns the cardinal for the current value of this enum. 
     */ 
    size_t value() const { 
        return value_; 
    } 
 
    /** 
     * Returns the symbol for the current value of this enum. 
     */ 
    const std::string& symbol() const { 
        return schema()->nameAt(value_); 
    } 
}; 
 
/** 
 * Generic container for Avro fixed. 
 */ 
class AVRO_DECL GenericFixed : public GenericContainer { 
    std::vector<uint8_t> value_; 
public: 
    /** 
     * Constructs a generic enum corresponding to the given schema \p schema, 
     * which should be of Avro type fixed. 
     */ 
    GenericFixed(const NodePtr& schema) : GenericContainer(AVRO_FIXED, schema) { 
        value_.resize(schema->fixedSize()); 
    } 
 
    GenericFixed(const NodePtr& schema, const std::vector<uint8_t>& v) : 
        GenericContainer(AVRO_FIXED, schema), value_(v) { } 
 
    /** 
     * Returns the contents of this fixed. 
     */ 
    const std::vector<uint8_t>& value() const { 
        return value_; 
    } 
 
    /** 
     * Returns the reference to the contents of this fixed. 
     */ 
    std::vector<uint8_t>& value() { 
        return value_; 
    } 
}; 
 
inline Type GenericDatum::type() const { 
    return (type_ == AVRO_UNION) ? 
#if __cplusplus >= 201703L 
        std::any_cast<GenericUnion>(&value_)->datum().type() : 
#else 
        boost::any_cast<GenericUnion>(&value_)->datum().type() : 
#endif 
        type_; 
} 
 
inline LogicalType GenericDatum::logicalType() const { 
    return logicalType_; 
} 
 
template<typename T> T& GenericDatum::value() { 
    return (type_ == AVRO_UNION) ? 
#if __cplusplus >= 201703L 
        std::any_cast<GenericUnion>(&value_)->datum().value<T>() : 
        *std::any_cast<T>(&value_); 
#else 
        boost::any_cast<GenericUnion>(&value_)->datum().value<T>() : 
        *boost::any_cast<T>(&value_); 
#endif 
} 
 
template<typename T> const T& GenericDatum::value() const { 
    return (type_ == AVRO_UNION) ? 
#if __cplusplus >= 201703L 
        std::any_cast<GenericUnion>(&value_)->datum().value<T>() : 
        *std::any_cast<T>(&value_); 
#else 
        boost::any_cast<GenericUnion>(&value_)->datum().value<T>() : 
        *boost::any_cast<T>(&value_); 
#endif 
} 
 
inline size_t GenericDatum::unionBranch() const { 
#if __cplusplus >= 201703L 
    return std::any_cast<GenericUnion>(&value_)->currentBranch(); 
#else 
    return boost::any_cast<GenericUnion>(&value_)->currentBranch(); 
#endif 
} 
 
inline void GenericDatum::selectBranch(size_t branch) { 
#if __cplusplus >= 201703L 
    std::any_cast<GenericUnion>(&value_)->selectBranch(branch); 
#else 
    boost::any_cast<GenericUnion>(&value_)->selectBranch(branch); 
#endif 
} 
 
}   // namespace avro 
#endif // avro_GenericDatum_hh__