forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.cc
More file actions
267 lines (222 loc) · 7.93 KB
/
Message.cc
File metadata and controls
267 lines (222 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <pulsar/Message.h>
#include <pulsar/MessageBuilder.h>
#include <pulsar/MessageIdBuilder.h>
#include <pulsar/defines.h>
#include <iostream>
#include "Int64SerDes.h"
#include "KeyValueImpl.h"
#include "MessageImpl.h"
#include "PulsarApi.pb.h"
#include "SharedBuffer.h"
using namespace pulsar;
namespace pulsar {
const static std::string emptyString;
const static MessageId invalidMessageId;
const Message::StringMap& Message::getProperties() const { return impl_->properties(); }
bool Message::hasProperty(const std::string& name) const {
const StringMap& m = impl_->properties();
return m.find(name) != m.end();
}
const std::string& Message::getProperty(const std::string& name) const {
if (hasProperty(name)) {
const StringMap& m = impl_->properties();
return m.at(name);
} else {
return emptyString;
}
}
const void* Message::getData() const { return impl_->payload.data(); }
std::size_t Message::getLength() const { return impl_->payload.readableBytes(); }
#if defined(_MSC_VER) && !defined(NDEBUG)
const std::string& Message::getDataAsString() const {
thread_local std::string value;
value = std::string{static_cast<const char*>(getData()), getLength()};
return value;
}
#else
std::string Message::getDataAsString() const { return std::string((const char*)getData(), getLength()); }
#endif
Message::Message() : impl_() {}
Message::Message(MessageImplPtr& impl) : impl_(impl) {}
Message::Message(const MessageId& messageId, proto::BrokerEntryMetadata& brokerEntryMetadata,
proto::MessageMetadata& metadata, SharedBuffer& payload)
: impl_(std::make_shared<MessageImpl>()) {
impl_->messageId = messageId;
impl_->brokerEntryMetadata = brokerEntryMetadata;
impl_->metadata = metadata;
impl_->payload = payload;
}
Message::Message(const MessageId& messageID, proto::BrokerEntryMetadata& brokerEntryMetadata,
proto::MessageMetadata& metadata, SharedBuffer& payload,
proto::SingleMessageMetadata& singleMetadata, const std::shared_ptr<std::string>& topicName)
: impl_(std::make_shared<MessageImpl>()) {
impl_->messageId = messageID;
impl_->brokerEntryMetadata = brokerEntryMetadata;
impl_->metadata = metadata;
impl_->payload = payload;
impl_->metadata.mutable_properties()->CopyFrom(singleMetadata.properties());
impl_->topicName_ = topicName;
impl_->metadata.clear_properties();
if (singleMetadata.properties_size() > 0) {
impl_->metadata.mutable_properties()->Reserve(singleMetadata.properties_size());
for (int i = 0; i < singleMetadata.properties_size(); i++) {
auto keyValue = proto::KeyValue().New();
*keyValue = singleMetadata.properties(i);
impl_->metadata.mutable_properties()->AddAllocated(keyValue);
}
}
if (singleMetadata.has_partition_key()) {
impl_->metadata.set_partition_key(singleMetadata.partition_key());
} else {
impl_->metadata.clear_partition_key();
}
if (singleMetadata.has_ordering_key()) {
impl_->metadata.set_ordering_key(singleMetadata.ordering_key());
} else {
impl_->metadata.clear_ordering_key();
}
if (singleMetadata.has_event_time()) {
impl_->metadata.set_event_time(singleMetadata.event_time());
} else {
impl_->metadata.clear_event_time();
}
if (singleMetadata.has_sequence_id()) {
impl_->metadata.set_sequence_id(singleMetadata.sequence_id());
} else {
impl_->metadata.clear_sequence_id();
}
}
const MessageId& Message::getMessageId() const {
if (!impl_) {
return invalidMessageId;
} else {
return impl_->messageId;
}
}
void Message::setMessageId(const MessageId& messageID) const {
if (impl_) {
impl_->messageId = messageID;
}
return;
}
int64_t Message::getIndex() const {
if (!impl_ || !impl_->brokerEntryMetadata.has_index()) {
return -1;
} else {
// casting uint64_t to int64_t, server definition ensures that's safe
return static_cast<int64_t>(impl_->brokerEntryMetadata.index());
}
}
bool Message::hasPartitionKey() const {
if (impl_) {
return impl_->hasPartitionKey();
}
return false;
}
const std::string& Message::getPartitionKey() const {
if (!impl_) {
return emptyString;
}
return impl_->getPartitionKey();
}
bool Message::hasOrderingKey() const {
if (impl_) {
return impl_->hasOrderingKey();
}
return false;
}
const std::string& Message::getOrderingKey() const {
if (!impl_) {
return emptyString;
}
return impl_->getOrderingKey();
}
const std::string& Message::getTopicName() const {
if (!impl_) {
return emptyString;
}
return impl_->getTopicName();
}
int Message::getRedeliveryCount() const {
if (!impl_) {
return 0;
}
return impl_->getRedeliveryCount();
}
bool Message::hasSchemaVersion() const {
if (impl_) {
return impl_->hasSchemaVersion();
}
return false;
}
int64_t Message::getLongSchemaVersion() const {
return (impl_ && impl_->hasSchemaVersion()) ? fromBigEndianBytes(impl_->getSchemaVersion()) : -1L;
}
const std::string& Message::getSchemaVersion() const {
if (!impl_) {
return emptyString;
}
return impl_->getSchemaVersion();
}
uint64_t Message::getPublishTimestamp() const { return impl_ ? impl_->getPublishTimestamp() : 0ull; }
uint64_t Message::getEventTimestamp() const { return impl_ ? impl_->getEventTimestamp() : 0ull; }
const std::string& Message::getProducerName() const noexcept {
if (!impl_) {
return emptyString;
}
return impl_->metadata.producer_name();
}
std::optional<const EncryptionContext*> Message::getEncryptionContext() const {
if (!impl_ || !impl_->encryptionContext_.has_value()) {
return std::nullopt;
}
return &impl_->encryptionContext_.value();
}
bool Message::operator==(const Message& msg) const { return getMessageId() == msg.getMessageId(); }
KeyValue Message::getKeyValueData() const { return KeyValue(impl_->keyValuePtr); }
PULSAR_PUBLIC std::ostream& operator<<(std::ostream& s, const Message::StringMap& map) {
// Output at most 10 elements -- appropriate if used for logging.
s << '{';
Message::StringMap::const_iterator begin = map.begin();
Message::StringMap::const_iterator end = map.end();
for (int i = 0; begin != end && i < 10; ++i, ++begin) {
if (i > 0) {
s << ", ";
}
s << "'" << begin->first << "':'" << begin->second << "'";
}
if (begin != end) {
s << " ...";
}
s << '}';
return s;
}
PULSAR_PUBLIC std::ostream& operator<<(std::ostream& s, const Message& msg) {
assert(msg.impl_.get());
assert(msg.impl_->metadata.has_sequence_id());
assert(msg.impl_->metadata.has_publish_time());
s << "Message(prod=" << msg.impl_->metadata.producer_name()
<< ", seq=" << msg.impl_->metadata.sequence_id()
<< ", publish_time=" << msg.impl_->metadata.publish_time() << ", payload_size=" << msg.getLength()
<< ", msg_id=" << msg.getMessageId() << ", props=" << msg.getProperties() << ')';
return s;
}
} // namespace pulsar