1 | /* |
2 | * Copyright (c) 2017, Matias Fontanini |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * |
28 | */ |
29 | |
30 | #include <iostream> |
31 | #include <tuple> |
32 | #include <librdkafka/rdkafka.h> |
33 | #include "topic_partition.h" |
34 | |
35 | using std::string; |
36 | using std::to_string; |
37 | using std::ostream; |
38 | using std::tie; |
39 | |
40 | namespace cppkafka { |
41 | |
42 | TopicPartition::TopicPartition() |
43 | : TopicPartition("" ) { |
44 | |
45 | } |
46 | |
47 | TopicPartition::TopicPartition(const char* topic) |
48 | : TopicPartition(string(topic)) { |
49 | |
50 | } |
51 | |
52 | TopicPartition::TopicPartition(string topic) |
53 | : TopicPartition(move(topic), RD_KAFKA_PARTITION_UA) { |
54 | |
55 | } |
56 | |
57 | TopicPartition::TopicPartition(string topic, int partition) |
58 | : TopicPartition(move(topic), partition, RD_KAFKA_OFFSET_INVALID) { |
59 | |
60 | } |
61 | |
62 | TopicPartition::TopicPartition(string topic, int partition, int64_t offset) |
63 | : topic_(move(topic)), partition_(partition), offset_(offset) { |
64 | |
65 | } |
66 | |
67 | const string& TopicPartition::get_topic() const { |
68 | return topic_; |
69 | } |
70 | |
71 | int TopicPartition::get_partition() const { |
72 | return partition_; |
73 | } |
74 | |
75 | int64_t TopicPartition::get_offset() const { |
76 | return offset_; |
77 | } |
78 | |
79 | void TopicPartition::set_offset(int64_t offset) { |
80 | offset_ = offset; |
81 | } |
82 | |
83 | bool TopicPartition::operator<(const TopicPartition& rhs) const { |
84 | return tie(topic_, partition_) < tie(rhs.topic_, rhs.partition_); |
85 | } |
86 | |
87 | bool TopicPartition::operator==(const TopicPartition& rhs) const { |
88 | return tie(topic_, partition_) == tie(rhs.topic_, rhs.partition_); |
89 | } |
90 | |
91 | bool TopicPartition::operator!=(const TopicPartition& rhs) const { |
92 | return !(*this == rhs); |
93 | } |
94 | |
95 | ostream& operator<<(ostream& output, const TopicPartition& rhs) { |
96 | return output << rhs.get_topic() << "[" |
97 | << rhs.get_partition() << ":" |
98 | << (rhs.get_offset() == RD_KAFKA_OFFSET_INVALID ? "#" : to_string(rhs.get_offset())) |
99 | << "]" ; |
100 | } |
101 | |
102 | } // cppkafka |
103 | |