client-cpp  0.6.3
AvroGenericUtils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AVROGENERICUTILS_HPP_
18 #define AVROGENERICUTILS_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_CONFIGURATION
23 
24 #include <algorithm>
25 #include <stdexcept>
26 
27 #include <avro/Generic.hh>
28 
32 
33 namespace kaa {
34 
38 class AvroGenericUtils {
39 public:
47  static bool isUuid(const avro::GenericDatum &d) {
48  if (d.type() == avro::AVRO_FIXED) {
49  auto f = d.value<avro::GenericFixed>();
50  return f.schema()->name().fullname().compare(AvroGenericUtils::UUIDT) == 0;
51  }
52  return false;
53  }
54 
62  static bool isFixed(const avro::GenericDatum &d) {
63  return (d.type() == avro::AVRO_FIXED);
64  }
65 
73  static bool isEnum(const avro::GenericDatum &d) {
74  return (d.type() == avro::AVRO_ENUM);
75  }
76 
84  static bool isRecord(const avro::GenericDatum &d) {
85  return (d.type() == avro::AVRO_RECORD);
86  }
87 
95  static bool isArray(const avro::GenericDatum &d) {
96  return (d.type() == avro::AVRO_ARRAY);
97  }
98 
106  static bool isNull(const avro::GenericDatum &d) {
107  return d.type() == avro::AVRO_NULL;
108  }
109 
117  static bool isReset(const avro::GenericDatum &d) {
118  if (d.type() != avro::AVRO_ENUM) {
119  return false;
120  }
121  const avro::GenericEnum &e = d.value<avro::GenericEnum>();
122  return e.schema()->name().fullname().compare(AvroGenericUtils::RESETT) == 0;
123  }
124 
132  static bool isUnchanged(const avro::GenericDatum &d) {
133  if (d.type() != avro::AVRO_ENUM) {
134  return false;
135  }
136  const avro::GenericEnum &e = d.value<avro::GenericEnum>();
137  return e.schema()->name().fullname().compare(AvroGenericUtils::UNCHANGEDT) == 0;
138  }
139 
146  static uuid_t getUuidFromDatum(const avro::GenericDatum& datum) {
147  uuid_t uuid;
148  const avro::GenericFixed& uuidFixed = datum.value<avro::GenericRecord>().
149  field("__uuid").value<avro::GenericFixed>();
150 
151  if (uuidFixed.value().size() != uuid.size()) {
152  throw KaaException("invalid uuid data");
153  }
154 
155  std::copy(uuidFixed.value().begin(), uuidFixed.value().end(), uuid.begin());
156  return uuid;
157  }
158 
165  static DeltaHandlerId getDeltaIDFromDatum(const avro::GenericDatum& datum) {
166  uuid_t uuid;
167  const avro::GenericFixed& uuidFixed = datum.value<avro::GenericFixed>();
168 
169  if (uuidFixed.value().size() != uuid.size()) {
170  throw KaaException("invalid uuid data");
171  }
172 
173  std::copy(uuidFixed.value().begin(), uuidFixed.value().end(), uuid.begin());
174 
175  DeltaHandlerId deltaId(uuid);
176  return deltaId;
177  }
178 
179 private:
180  static const std::string RESETT;
181  static const std::string UNCHANGEDT;
182  static const std::string UUIDT;
183 };
184 
185 } // namespace kaa
186 
187 #endif
188 
189 #endif /* AVROGENERICUTILS_HPP_ */