mirror of
				https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
				synced 2025-11-04 04:27:48 +00:00 
			
		
		
		
	Add partitioner callback to topic configuration
This commit is contained in:
		@@ -4,16 +4,18 @@
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <unordered_map>
 | 
			
		||||
#include <mutex>
 | 
			
		||||
#include <librdkafka/rdkafka.h>
 | 
			
		||||
#include "metadata.h"
 | 
			
		||||
#include "topic_partition.h"
 | 
			
		||||
#include "topic_partition_list.h"
 | 
			
		||||
#include "topic_configuration.h"
 | 
			
		||||
#include "configuration.h"
 | 
			
		||||
 | 
			
		||||
namespace cppkafka {
 | 
			
		||||
 | 
			
		||||
class Topic;
 | 
			
		||||
class TopicConfiguration;
 | 
			
		||||
 | 
			
		||||
class KafkaHandleBase {
 | 
			
		||||
public:
 | 
			
		||||
@@ -46,13 +48,17 @@ private:
 | 
			
		||||
    static const std::chrono::milliseconds DEFAULT_TIMEOUT;
 | 
			
		||||
 | 
			
		||||
    using HandlePtr = std::unique_ptr<rd_kafka_t, decltype(&rd_kafka_destroy)>;
 | 
			
		||||
    using TopicConfigurationMap = std::unordered_map<std::string, TopicConfiguration>;
 | 
			
		||||
 | 
			
		||||
    Topic get_topic(const std::string& name, rd_kafka_topic_conf_t* conf);
 | 
			
		||||
    Metadata get_metadata(rd_kafka_topic_t* topic_ptr);
 | 
			
		||||
    void save_topic_config(const std::string& topic_name, TopicConfiguration config);
 | 
			
		||||
 | 
			
		||||
    HandlePtr handle_;
 | 
			
		||||
    std::chrono::milliseconds timeout_ms_;
 | 
			
		||||
    Configuration config_;
 | 
			
		||||
    TopicConfigurationMap topic_configurations_;
 | 
			
		||||
    std::mutex topic_configurations_mutex_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // cppkafka
 | 
			
		||||
 
 | 
			
		||||
@@ -10,14 +10,19 @@ namespace cppkafka {
 | 
			
		||||
 | 
			
		||||
class Topic {
 | 
			
		||||
public:
 | 
			
		||||
    static Topic make_non_owning(rd_kafka_topic_t* handle);
 | 
			
		||||
 | 
			
		||||
    Topic(rd_kafka_topic_t* handle);
 | 
			
		||||
 | 
			
		||||
    std::string get_name() const;
 | 
			
		||||
 | 
			
		||||
    rd_kafka_topic_t* get_handle() const;
 | 
			
		||||
private:
 | 
			
		||||
    using HandlePtr = std::unique_ptr<rd_kafka_topic_t, decltype(&rd_kafka_topic_destroy)>;
 | 
			
		||||
 | 
			
		||||
    struct NonOwningTag { };
 | 
			
		||||
 | 
			
		||||
    Topic(rd_kafka_topic_t* handle, NonOwningTag);
 | 
			
		||||
 | 
			
		||||
    HandlePtr handle_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,26 +2,40 @@
 | 
			
		||||
#define CPPKAFKA_TOPIC_CONFIGURATION_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <functional>
 | 
			
		||||
#include <librdkafka/rdkafka.h>
 | 
			
		||||
#include "clonable_ptr.h"
 | 
			
		||||
 | 
			
		||||
namespace cppkafka {
 | 
			
		||||
 | 
			
		||||
class Topic;
 | 
			
		||||
class Buffer;
 | 
			
		||||
 | 
			
		||||
class TopicConfiguration {
 | 
			
		||||
public:
 | 
			
		||||
    using PartitionerCallback = std::function<int32_t(const Topic&, const Buffer& key,
 | 
			
		||||
                                                      int32_t partition_count)>;
 | 
			
		||||
 | 
			
		||||
    TopicConfiguration();
 | 
			
		||||
 | 
			
		||||
    void set(const std::string& name, const std::string& value);
 | 
			
		||||
 | 
			
		||||
    void set_partitioner_callback(PartitionerCallback callback);
 | 
			
		||||
 | 
			
		||||
    void set_as_opaque();
 | 
			
		||||
 | 
			
		||||
    const PartitionerCallback& get_partitioner_callback() const;
 | 
			
		||||
    rd_kafka_topic_conf_t* get_handle() const;
 | 
			
		||||
private:
 | 
			
		||||
    using HandlePtr = ClonablePtr<rd_kafka_topic_conf_t, decltype(&rd_kafka_topic_conf_destroy),
 | 
			
		||||
    using HandlePtr = ClonablePtr<rd_kafka_topic_conf_t,
 | 
			
		||||
                                  decltype(&rd_kafka_topic_conf_destroy),
 | 
			
		||||
                                  decltype(&rd_kafka_topic_conf_dup)>;
 | 
			
		||||
 | 
			
		||||
    TopicConfiguration(rd_kafka_topic_conf_t* ptr);
 | 
			
		||||
    static HandlePtr make_handle(rd_kafka_topic_conf_t* ptr);
 | 
			
		||||
 | 
			
		||||
    HandlePtr handle_;
 | 
			
		||||
    PartitionerCallback partitioner_callback_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // cppkafka
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user