Skip to content

Instantly share code, notes, and snippets.

@langley
Created August 26, 2013 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langley/6345101 to your computer and use it in GitHub Desktop.
Save langley/6345101 to your computer and use it in GitHub Desktop.
UofARecs illustrates an attempt to create an avro specification for an array of values which can hold either an array of longs or an array of doubles
#include "UofARecs.hpp"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
int
main()
{
std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
avro_s11n::Array a1;
std::vector<double> values = a1.elements.get_doubleArray().elements;
values.push_back(3.14159265359);
values.push_back(2.71828);
avro::encode(*e, a1);
std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
avro::DecoderPtr d = avro::binaryDecoder();
d->init(*in);
avro_s11n::Array a2;
avro::decode(*d, a2);
for (std::vector<double>::iterator it = a2.elements.get_doubleArray().elements.begin();
it != a2.elements.get_doubleArray().elements.end(); ++it)
{
std::cout << *it << std::endl;
}
return 0;
}
/**
* 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
*
* http://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 UOFARECS_HPP_3776456281__H_
#define UOFARECS_HPP_3776456281__H_
#include "boost/any.hpp"
#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
namespace avro_s11n {
enum array_type_t {
Long,
Double,
};
struct longArray {
std::vector<int64_t > elements;
};
struct doubleArray {
std::vector<double > elements;
};
struct UofARecs_json_Union__0__ {
private:
size_t idx_;
boost::any value_;
public:
size_t idx() const { return idx_; }
longArray get_longArray() const;
void set_longArray(const longArray& v);
doubleArray get_doubleArray() const;
void set_doubleArray(const doubleArray& v);
UofARecs_json_Union__0__();
};
struct Array {
typedef UofARecs_json_Union__0__ elements_t;
array_type_t arrayType;
elements_t elements;
};
inline
longArray UofARecs_json_Union__0__::get_longArray() const {
if (idx_ != 0) {
throw avro::Exception("Invalid type for union");
}
return boost::any_cast<longArray >(value_);
}
inline
void UofARecs_json_Union__0__::set_longArray(const longArray& v) {
idx_ = 0;
value_ = v;
}
inline
doubleArray UofARecs_json_Union__0__::get_doubleArray() const {
if (idx_ != 1) {
throw avro::Exception("Invalid type for union");
}
return boost::any_cast<doubleArray >(value_);
}
inline
void UofARecs_json_Union__0__::set_doubleArray(const doubleArray& v) {
idx_ = 1;
value_ = v;
}
inline UofARecs_json_Union__0__::UofARecs_json_Union__0__() : idx_(0), value_(longArray()) { }
}
namespace avro {
template<> struct codec_traits<avro_s11n::array_type_t> {
static void encode(Encoder& e, avro_s11n::array_type_t v) {
e.encodeEnum(v);
}
static void decode(Decoder& d, avro_s11n::array_type_t& v) {
v = static_cast<avro_s11n::array_type_t>(d.decodeEnum());
}
};
template<> struct codec_traits<avro_s11n::longArray> {
static void encode(Encoder& e, const avro_s11n::longArray& v) {
avro::encode(e, v.elements);
}
static void decode(Decoder& d, avro_s11n::longArray& v) {
avro::decode(d, v.elements);
}
};
template<> struct codec_traits<avro_s11n::doubleArray> {
static void encode(Encoder& e, const avro_s11n::doubleArray& v) {
avro::encode(e, v.elements);
}
static void decode(Decoder& d, avro_s11n::doubleArray& v) {
avro::decode(d, v.elements);
}
};
template<> struct codec_traits<avro_s11n::UofARecs_json_Union__0__> {
static void encode(Encoder& e, avro_s11n::UofARecs_json_Union__0__ v) {
e.encodeUnionIndex(v.idx());
switch (v.idx()) {
case 0:
avro::encode(e, v.get_longArray());
break;
case 1:
avro::encode(e, v.get_doubleArray());
break;
}
}
static void decode(Decoder& d, avro_s11n::UofARecs_json_Union__0__& v) {
size_t n = d.decodeUnionIndex();
if (n >= 2) { throw avro::Exception("Union index too big"); }
switch (n) {
case 0:
{
avro_s11n::longArray vv;
avro::decode(d, vv);
v.set_longArray(vv);
}
break;
case 1:
{
avro_s11n::doubleArray vv;
avro::decode(d, vv);
v.set_doubleArray(vv);
}
break;
}
}
};
template<> struct codec_traits<avro_s11n::Array> {
static void encode(Encoder& e, const avro_s11n::Array& v) {
avro::encode(e, v.arrayType);
avro::encode(e, v.elements);
}
static void decode(Decoder& d, avro_s11n::Array& v) {
avro::decode(d, v.arrayType);
avro::decode(d, v.elements);
}
};
}
#endif
{
"type" : "record",
"name" : "Array",
"fields" : [
{ "name" : "arrayType", "type" : { "type" : "enum", "name" : "array_type_t", "symbols" : ["Long", "Double"]}},
{ "name" : "elements", "type" : [
{"type" : "record", "name": "longArray", "fields" : [ {"name" : "elements", "type" : { "type" : "array", "items" : "long"}}]},
{"type" : "record", "name": "doubleArray", "fields" : [ {"name" : "elements", "type" : { "type" : "array", "items" : "double"}}]}]}
]
}
# compile CXX with /usr/bin/c++
CXX_FLAGS = -Wall -O3 -DNDEBUG -I/usr/local/include/avro -L/usr/lib -L/usr/local/lib -lavrocpp
CXX_DEFINES =
UofARecs : UofARecs.cpp UofARecs.hpp
cc -o UofARecs UofARecs.cpp $(CXX_FLAGS)
UofARecs.hpp: UofARecs.json
avrogencpp -i UofARecs.json -o UofARecs.hpp -n "avro_s11n"
@langley
Copy link
Author

langley commented Aug 26, 2013

Beware, I "borked" the capitalization of the file names as I created the gist. The Makefile and the "includes" expects them to be slightly different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment