Make get_metadata(Topic) return only topic metadata

This commit is contained in:
Matias Fontanini
2016-06-18 08:45:33 -07:00
parent 84d98b38e5
commit a3b311fe00
6 changed files with 22 additions and 6 deletions

View File

@@ -37,7 +37,6 @@
#include <mutex> #include <mutex>
#include <tuple> #include <tuple>
#include <librdkafka/rdkafka.h> #include <librdkafka/rdkafka.h>
#include "metadata.h"
#include "topic_partition.h" #include "topic_partition.h"
#include "topic_partition_list.h" #include "topic_partition_list.h"
#include "topic_configuration.h" #include "topic_configuration.h"
@@ -46,6 +45,8 @@
namespace cppkafka { namespace cppkafka {
class Topic; class Topic;
class Metadata;
class TopicMetadata;
/** /**
* Base class for kafka consumer/producer * Base class for kafka consumer/producer
@@ -137,7 +138,7 @@ public:
* *
* \param topic The topic to fetch information for * \param topic The topic to fetch information for
*/ */
Metadata get_metadata(const Topic& topic) const; TopicMetadata get_metadata(const Topic& topic) const;
/** /**
* Returns the kafka handle name * Returns the kafka handle name

View File

@@ -89,6 +89,11 @@ public:
*/ */
std::string get_error_string() const; std::string get_error_string() const;
/**
* Utility function to check for get_error() == RD_KAFKA_RESP_ERR__PARTITION_EOF
*/
bool is_eof() const;
/** /**
* Gets the topic that this message belongs to * Gets the topic that this message belongs to
*/ */

View File

@@ -39,7 +39,6 @@ using std::string;
using std::map; using std::map;
using std::move; using std::move;
using std::vector; using std::vector;
using std::unordered_set;
using boost::optional; using boost::optional;

View File

@@ -28,6 +28,7 @@
*/ */
#include "kafka_handle_base.h" #include "kafka_handle_base.h"
#include "metadata.h"
#include "exceptions.h" #include "exceptions.h"
#include "topic.h" #include "topic.h"
#include "topic_partition_list.h" #include "topic_partition_list.h"
@@ -105,8 +106,13 @@ Metadata KafkaHandleBase::get_metadata() const {
return get_metadata(nullptr); return get_metadata(nullptr);
} }
Metadata KafkaHandleBase::get_metadata(const Topic& topic) const { TopicMetadata KafkaHandleBase::get_metadata(const Topic& topic) const {
return get_metadata(topic.get_handle()); Metadata md = get_metadata(topic.get_handle());
auto topics = md.get_topics();
if (topics.empty()) {
throw Exception("Failed to find metadata for topic");
}
return topics.front();
} }
string KafkaHandleBase::get_name() const { string KafkaHandleBase::get_name() const {

View File

@@ -75,6 +75,10 @@ string Message::get_error_string() const {
return rd_kafka_err2str(handle_->err); return rd_kafka_err2str(handle_->err);
} }
bool Message::is_eof() const {
return get_error() == RD_KAFKA_RESP_ERR__PARTITION_EOF;
}
int Message::get_partition() const { int Message::get_partition() const {
return handle_->partition; return handle_->partition;
} }

View File

@@ -2,6 +2,7 @@
#include <unordered_set> #include <unordered_set>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "cppkafka/producer.h" #include "cppkafka/producer.h"
#include "cppkafka/metadata.h"
using std::vector; using std::vector;
using std::set; using std::set;
@@ -93,5 +94,5 @@ TEST_F(KafkaHandleBaseTest, TopicsMetadata) {
// Now get the whole metadata only for this topic // Now get the whole metadata only for this topic
Topic topic = producer.get_topic(KAFKA_TOPIC); Topic topic = producer.get_topic(KAFKA_TOPIC);
EXPECT_EQ(1, producer.get_metadata(topic).get_topics().size()); EXPECT_EQ(KAFKA_TOPIC, producer.get_metadata(topic).get_topic());
} }