You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by th...@apache.org on 2019/01/17 18:30:16 UTC
[avro] branch master updated: Added byteCount() to Encoder
interface in C++
This is an automated email from the ASF dual-hosted git repository.
thiru pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 5a64ea5 Added byteCount() to Encoder interface in C++
new 6d0323c Merge pull request #429 from thiru-apache/AVRO-2300
5a64ea5 is described below
commit 5a64ea53d2e37449295c8d29f3d33ce849691155
Author: Thiruvalluvan M G <th...@startsmartlabs.com>
AuthorDate: Thu Jan 17 14:28:58 2019 +0530
Added byteCount() to Encoder interface in C++
---
lang/c++/api/Encoder.hh | 4 ++++
lang/c++/api/Stream.hh | 9 ++++++++-
lang/c++/impl/BinaryEncoder.cc | 6 ++++++
lang/c++/impl/json/JsonIO.hh | 4 ++++
lang/c++/impl/parsing/JsonCodec.cc | 7 +++++++
lang/c++/impl/parsing/ValidatingCodec.cc | 7 +++++++
lang/c++/test/CodecTests.cc | 16 +++++++++++++++-
7 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/lang/c++/api/Encoder.hh b/lang/c++/api/Encoder.hh
index 137e904..02d4265 100644
--- a/lang/c++/api/Encoder.hh
+++ b/lang/c++/api/Encoder.hh
@@ -60,6 +60,10 @@ public:
/// Flushes any data in internal buffers.
virtual void flush() = 0;
+ /// Returns the number of bytes produced so far.
+ /// For a meaningful value, do a flush() before invoking this function.
+ virtual int64_t byteCount() const = 0;
+
/// Encodes a null to the current stream.
virtual void encodeNull() = 0;
diff --git a/lang/c++/api/Stream.hh b/lang/c++/api/Stream.hh
index 24be184..fd60b1e 100644
--- a/lang/c++/api/Stream.hh
+++ b/lang/c++/api/Stream.hh
@@ -440,6 +440,14 @@ struct StreamWriter {
}
/**
+ * Return the number of bytes written so far. For a meaningful
+ * result, call this after a flush().
+ */
+ int64_t byteCount() const {
+ return out_->byteCount();
+ }
+
+ /**
* Gets more space to write to. Throws an exception it cannot.
*/
void more() {
@@ -452,7 +460,6 @@ struct StreamWriter {
}
throw Exception("EOF reached");
}
-
};
/**
diff --git a/lang/c++/impl/BinaryEncoder.cc b/lang/c++/impl/BinaryEncoder.cc
index 6da5c03..ee2eea0 100644
--- a/lang/c++/impl/BinaryEncoder.cc
+++ b/lang/c++/impl/BinaryEncoder.cc
@@ -29,6 +29,7 @@ class BinaryEncoder : public Encoder {
void init(OutputStream& os);
void flush();
+ int64_t byteCount() const;
void encodeNull();
void encodeBool(bool b);
void encodeInt(int32_t i);
@@ -153,6 +154,11 @@ void BinaryEncoder::encodeUnionIndex(size_t e)
doEncodeLong(e);
}
+int64_t BinaryEncoder::byteCount() const {
+ return out_.byteCount();
+}
+
+
void BinaryEncoder::doEncodeLong(int64_t l)
{
std::array<uint8_t, 10> bytes;
diff --git a/lang/c++/impl/json/JsonIO.hh b/lang/c++/impl/json/JsonIO.hh
index eeb7e34..971fc61 100644
--- a/lang/c++/impl/json/JsonIO.hh
+++ b/lang/c++/impl/json/JsonIO.hh
@@ -369,6 +369,10 @@ public:
out_.flush();
}
+ int64_t byteCount() const {
+ return out_.byteCount();
+ }
+
void encodeNull() {
sep();
out_.writeBytes(reinterpret_cast<const uint8_t*>("null"), 4);
diff --git a/lang/c++/impl/parsing/JsonCodec.cc b/lang/c++/impl/parsing/JsonCodec.cc
index 2d2a5b3..7fb82ca 100644
--- a/lang/c++/impl/parsing/JsonCodec.cc
+++ b/lang/c++/impl/parsing/JsonCodec.cc
@@ -502,6 +502,7 @@ class JsonEncoder : public Encoder {
void init(OutputStream& os);
void flush();
+ int64_t byteCount() const;
void encodeNull();
void encodeBool(bool b);
void encodeInt(int32_t i);
@@ -539,6 +540,12 @@ void JsonEncoder<P, F>::flush()
}
template<typename P, typename F>
+int64_t JsonEncoder<P, F>::byteCount() const
+{
+ return out_.byteCount();
+}
+
+template<typename P, typename F>
void JsonEncoder<P, F>::encodeNull()
{
parser_.advance(Symbol::sNull);
diff --git a/lang/c++/impl/parsing/ValidatingCodec.cc b/lang/c++/impl/parsing/ValidatingCodec.cc
index 0ce2203..3a4d4f5 100644
--- a/lang/c++/impl/parsing/ValidatingCodec.cc
+++ b/lang/c++/impl/parsing/ValidatingCodec.cc
@@ -404,6 +404,7 @@ class ValidatingEncoder : public Encoder {
void init(OutputStream& os);
void flush();
+ int64_t byteCount() const;
void encodeNull();
void encodeBool(bool b);
void encodeInt(int32_t i);
@@ -567,6 +568,12 @@ void ValidatingEncoder<P>::encodeUnionIndex(size_t e)
base_->encodeUnionIndex(e);
}
+template<typename P>
+int64_t ValidatingEncoder<P>::byteCount() const
+{
+ return base_->byteCount();
+}
+
} // namespace parsing
DecoderPtr validatingDecoder(const ValidSchema& s,
diff --git a/lang/c++/test/CodecTests.cc b/lang/c++/test/CodecTests.cc
index 71eb7bc..94cb675 100644
--- a/lang/c++/test/CodecTests.cc
+++ b/lang/c++/test/CodecTests.cc
@@ -1692,7 +1692,8 @@ static void testJson(const JsonData& data)
EncoderPtr e = jsonEncoder(schema);
}
-static void testJsonCodecReinit() {
+static void testJsonCodecReinit()
+{
const char *schemaStr = "{\"type\":\"record\",\"name\":\"r\",\"fields\":["
"{\"name\":\"f1\", \"type\":\"boolean\"},"
"{\"name\":\"f2\", \"type\":\"long\"}"
@@ -1730,6 +1731,18 @@ static void testJsonCodecReinit() {
}
}
+static void testByteCount()
+{
+ OutputStreamPtr os1 = memoryOutputStream();
+ EncoderPtr e1 = binaryEncoder();
+ e1->init(*os1);
+ e1->encodeBool(true);
+ e1->encodeLong(1000);
+ e1->flush();
+ BOOST_CHECK_EQUAL(e1->byteCount(), 3);
+ BOOST_CHECK_EQUAL(os1->byteCount(), 3);
+}
+
} // namespace avro
boost::unit_test::test_suite*
@@ -1745,6 +1758,7 @@ init_unit_test_suite(int argc, char* argv[])
ts->add(BOOST_PARAM_TEST_CASE(&avro::testJson, avro::jsonData,
ENDOF(avro::jsonData)));
ts->add(BOOST_TEST_CASE(avro::testJsonCodecReinit));
+ ts->add(BOOST_TEST_CASE(avro::testByteCount));
return ts;
}