mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
synced 2025-11-04 12:37:54 +00:00
Header support implementation (#115)
* header support implementation * Fixed issue when ptr is null and doesn't have a cloner function * Code complete with test cases updated travis file with v0.11.5 * Added compile time check for rdkafka header support version * Changes per last code review * Using brace list initializers
This commit is contained in:
committed by
Matias Fontanini
parent
9af4330c6d
commit
fbe3759fed
@@ -35,6 +35,7 @@
|
||||
#include "topic.h"
|
||||
#include "macros.h"
|
||||
#include "message.h"
|
||||
#include "header_list.h"
|
||||
|
||||
namespace cppkafka {
|
||||
|
||||
@@ -44,6 +45,10 @@ namespace cppkafka {
|
||||
template <typename BufferType, typename Concrete>
|
||||
class BasicMessageBuilder {
|
||||
public:
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
using HeaderType = Header<BufferType>;
|
||||
using HeaderListType = HeaderList<HeaderType>;
|
||||
#endif
|
||||
/**
|
||||
* Construct a BasicMessageBuilder
|
||||
*
|
||||
@@ -99,6 +104,15 @@ public:
|
||||
*/
|
||||
Concrete& key(BufferType&& value);
|
||||
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
/**
|
||||
* Add a header to the message
|
||||
*
|
||||
* \param header The header to be used
|
||||
*/
|
||||
Concrete& header(const HeaderType& header);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Sets the message's payload
|
||||
*
|
||||
@@ -146,7 +160,19 @@ public:
|
||||
* Gets the message's key
|
||||
*/
|
||||
BufferType& key();
|
||||
|
||||
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
/**
|
||||
* Gets the list of headers
|
||||
*/
|
||||
const HeaderListType& header_list() const;
|
||||
|
||||
/**
|
||||
* Gets the list of headers
|
||||
*/
|
||||
HeaderListType& header_list();
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Gets the message's payload
|
||||
*/
|
||||
@@ -180,6 +206,9 @@ private:
|
||||
std::string topic_;
|
||||
int partition_{-1};
|
||||
BufferType key_;
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
HeaderListType header_list_;
|
||||
#endif
|
||||
BufferType payload_;
|
||||
std::chrono::milliseconds timestamp_{0};
|
||||
void* user_data_;
|
||||
@@ -237,6 +266,17 @@ C& BasicMessageBuilder<T, C>::key(T&& value) {
|
||||
return get_concrete();
|
||||
}
|
||||
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
template <typename T, typename C>
|
||||
C& BasicMessageBuilder<T, C>::header(const HeaderType& header) {
|
||||
if (!header_list_) {
|
||||
header_list_ = HeaderListType(5);
|
||||
}
|
||||
header_list_.add(header);
|
||||
return get_concrete();
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T, typename C>
|
||||
C& BasicMessageBuilder<T, C>::payload(const T& value) {
|
||||
get_concrete().construct_buffer(payload_, value);
|
||||
@@ -281,6 +321,20 @@ T& BasicMessageBuilder<T, C>::key() {
|
||||
return key_;
|
||||
}
|
||||
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
template <typename T, typename C>
|
||||
const typename BasicMessageBuilder<T, C>::HeaderListType&
|
||||
BasicMessageBuilder<T, C>::header_list() const {
|
||||
return header_list_;
|
||||
}
|
||||
|
||||
template <typename T, typename C>
|
||||
typename BasicMessageBuilder<T, C>::HeaderListType&
|
||||
BasicMessageBuilder<T, C>::header_list() {
|
||||
return header_list_;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T, typename C>
|
||||
const T& BasicMessageBuilder<T, C>::payload() const {
|
||||
return payload_;
|
||||
@@ -338,7 +392,12 @@ C& BasicMessageBuilder<T, C>::get_concrete() {
|
||||
*/
|
||||
class MessageBuilder : public BasicMessageBuilder<Buffer, MessageBuilder> {
|
||||
public:
|
||||
using Base = BasicMessageBuilder<Buffer, MessageBuilder>;
|
||||
using BasicMessageBuilder::BasicMessageBuilder;
|
||||
#if (RD_KAFKA_VERSION >= RD_KAFKA_HEADERS_SUPPORT_VERSION)
|
||||
using HeaderType = Base::HeaderType;
|
||||
using HeaderListType = Base::HeaderListType;
|
||||
#endif
|
||||
|
||||
void construct_buffer(Buffer& lhs, const Buffer& rhs) {
|
||||
lhs = Buffer(rhs.get_data(), rhs.get_size());
|
||||
|
||||
Reference in New Issue
Block a user