client-cpp  0.0.1-SNAPSHOT
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 <algorithm>
21 #include <stdexcept>
22 
23 #include <avro/Generic.hh>
24 
28 
29 namespace kaa {
30 
35 public:
43  static bool isUuid(const avro::GenericDatum &d) {
44  if (d.type() == avro::AVRO_FIXED) {
45  auto f = d.value<avro::GenericFixed>();
46  return f.schema()->name().fullname().compare(AvroGenericUtils::UUIDT) == 0;
47  }
48  return false;
49  }
50 
58  static bool isFixed(const avro::GenericDatum &d) {
59  return (d.type() == avro::AVRO_FIXED);
60  }
61 
69  static bool isEnum(const avro::GenericDatum &d) {
70  return (d.type() == avro::AVRO_ENUM);
71  }
72 
80  static bool isRecord(const avro::GenericDatum &d) {
81  return (d.type() == avro::AVRO_RECORD);
82  }
83 
91  static bool isArray(const avro::GenericDatum &d) {
92  return (d.type() == avro::AVRO_ARRAY);
93  }
94 
102  static bool isNull(const avro::GenericDatum &d) {
103  return d.type() == avro::AVRO_NULL;
104  }
105 
113  static bool isReset(const avro::GenericDatum &d) {
114  if (d.type() != avro::AVRO_ENUM) {
115  return false;
116  }
117  const avro::GenericEnum &e = d.value<avro::GenericEnum>();
118  return e.schema()->name().fullname().compare(AvroGenericUtils::RESETT) == 0;
119  }
120 
128  static bool isUnchanged(const avro::GenericDatum &d) {
129  if (d.type() != avro::AVRO_ENUM) {
130  return false;
131  }
132  const avro::GenericEnum &e = d.value<avro::GenericEnum>();
133  return e.schema()->name().fullname().compare(AvroGenericUtils::UNCHANGEDT) == 0;
134  }
135 
142  static uuid_t getUuidFromDatum(const avro::GenericDatum& datum) {
143  uuid_t uuid;
144  const avro::GenericFixed& uuidFixed = datum.value<avro::GenericRecord>().
145  field("__uuid").value<avro::GenericFixed>();
146 
147  if (uuidFixed.value().size() != uuid.size()) {
148  throw KaaException("invalid uuid data");
149  }
150 
151  std::copy(uuidFixed.value().begin(), uuidFixed.value().end(), uuid.begin());
152  return uuid;
153  }
154 
161  static DeltaHandlerId getDeltaIDFromDatum(const avro::GenericDatum& datum) {
162  uuid_t uuid;
163  const avro::GenericFixed& uuidFixed = datum.value<avro::GenericFixed>();
164 
165  if (uuidFixed.value().size() != uuid.size()) {
166  throw KaaException("invalid uuid data");
167  }
168 
169  std::copy(uuidFixed.value().begin(), uuidFixed.value().end(), uuid.begin());
170 
171  DeltaHandlerId deltaId(uuid);
172  return deltaId;
173  }
174 
175 private:
176  static const std::string RESETT;
177  static const std::string UNCHANGEDT;
178  static const std::string UUIDT;
179 };
180 
181 } // namespace kaa
182 
183 
184 #endif /* AVROGENERICUTILS_HPP_ */
static bool isEnum(const avro::GenericDatum &d)
static bool isReset(const avro::GenericDatum &d)
static bool isNull(const avro::GenericDatum &d)
static bool isUnchanged(const avro::GenericDatum &d)
static bool isArray(const avro::GenericDatum &d)
boost::uuids::uuid uuid_t
static uuid_t getUuidFromDatum(const avro::GenericDatum &datum)
static DeltaHandlerId getDeltaIDFromDatum(const avro::GenericDatum &datum)
static bool isRecord(const avro::GenericDatum &d)
static bool isFixed(const avro::GenericDatum &d)
static bool isUuid(const avro::GenericDatum &d)