Avro C++
Writer.hh
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  * https://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef avro_Writer_hh__
20 #define avro_Writer_hh__
21 
22 #include <array>
23 #include <boost/noncopyable.hpp>
24 
25 #include "Config.hh"
26 #include "buffer/Buffer.hh"
27 #include "Zigzag.hh"
28 #include "Types.hh"
29 #include "Validator.hh"
30 
31 namespace avro {
32 
34 
35 template<class ValidatorType>
36 class WriterImpl : private boost::noncopyable
37 {
38 
39  public:
40 
41  WriterImpl() {}
42 
43  explicit WriterImpl(const ValidSchema &schema) :
44  validator_(schema)
45  {}
46 
47  void writeValue(const Null &) {
48  validator_.checkTypeExpected(AVRO_NULL);
49  }
50 
51  void writeValue(bool val) {
52  validator_.checkTypeExpected(AVRO_BOOL);
53  int8_t byte = (val != 0);
54  buffer_.writeTo(byte);
55  }
56 
57  void writeValue(int32_t val) {
58  validator_.checkTypeExpected(AVRO_INT);
59  std::array<uint8_t, 5> bytes;
60  size_t size = encodeInt32(val, bytes);
61  buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
62  }
63 
64  void writeValue(int64_t val) {
65  validator_.checkTypeExpected(AVRO_LONG);
66  putLong(val);
67  }
68 
69  void writeValue(float val) {
70  validator_.checkTypeExpected(AVRO_FLOAT);
71  union {
72  float f;
73  int32_t i;
74  } v;
75 
76  v.f = val;
77  buffer_.writeTo(v.i);
78  }
79 
80  void writeValue(double val) {
81  validator_.checkTypeExpected(AVRO_DOUBLE);
82  union {
83  double d;
84  int64_t i;
85  } v;
86 
87  v.d = val;
88  buffer_.writeTo(v.i);
89  }
90 
91  void writeValue(const std::string &val) {
92  validator_.checkTypeExpected(AVRO_STRING);
93  putBytes(val.c_str(), val.size());
94  }
95 
96  void writeBytes(const void *val, size_t size) {
97  validator_.checkTypeExpected(AVRO_BYTES);
98  putBytes(val, size);
99  }
100 
101  template <size_t N>
102  void writeFixed(const uint8_t (&val)[N]) {
103  validator_.checkFixedSizeExpected(N);
104  buffer_.writeTo(reinterpret_cast<const char *>(val), N);
105  }
106 
107  template <size_t N>
108  void writeFixed(const std::array<uint8_t, N> &val) {
109  validator_.checkFixedSizeExpected(val.size());
110  buffer_.writeTo(reinterpret_cast<const char *>(val.data()), val.size());
111  }
112 
113  void writeRecord() {
114  validator_.checkTypeExpected(AVRO_RECORD);
115  validator_.checkTypeExpected(AVRO_LONG);
116  validator_.setCount(1);
117  }
118 
119  void writeRecordEnd() {
120  validator_.checkTypeExpected(AVRO_RECORD);
121  validator_.checkTypeExpected(AVRO_LONG);
122  validator_.setCount(0);
123  }
124 
125  void writeArrayBlock(int64_t size) {
126  validator_.checkTypeExpected(AVRO_ARRAY);
127  writeCount(size);
128  }
129 
130  void writeArrayEnd() {
131  writeArrayBlock(0);
132  }
133 
134  void writeMapBlock(int64_t size) {
135  validator_.checkTypeExpected(AVRO_MAP);
136  writeCount(size);
137  }
138 
139  void writeMapEnd() {
140  writeMapBlock(0);
141  }
142 
143  void writeUnion(int64_t choice) {
144  validator_.checkTypeExpected(AVRO_UNION);
145  writeCount(choice);
146  }
147 
148  void writeEnum(int64_t choice) {
149  validator_.checkTypeExpected(AVRO_ENUM);
150  writeCount(choice);
151  }
152 
153  InputBuffer buffer() const {
154  return buffer_;
155  }
156 
157  private:
158 
159  void putLong(int64_t val) {
160  std::array<uint8_t, 10> bytes;
161  size_t size = encodeInt64(val, bytes);
162  buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
163  }
164 
165  void putBytes(const void *val, size_t size) {
166  putLong(size);
167  buffer_.writeTo(reinterpret_cast<const char *>(val), size);
168  }
169 
170  void writeCount(int64_t count) {
171  validator_.checkTypeExpected(AVRO_LONG);
172  validator_.setCount(count);
173  putLong(count);
174  }
175 
176  ValidatorType validator_;
177  OutputBuffer buffer_;
178 
179 };
180 
183 
184 } // namespace avro
185 
186 #endif
Definition: Types.hh:37
Definition: Types.hh:45
Definition: Types.hh:40
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
Definition: Types.hh:43
Definition: Types.hh:39
Definition: Types.hh:33
Definition: Types.hh:44
Class for writing avro data to a stream.
Definition: Writer.hh:36
Definition: Types.hh:35
Functions for encoding and decoding integers with zigzag compression.
Definition: Types.hh:34
Definition: Types.hh:38
define a type to identify Null in template functions
Definition: Types.hh:102
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
Definition: Types.hh:42
Definition: Types.hh:46
Definition: Types.hh:36