This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMessage.cc
180 lines (145 loc) · 5.31 KB
/
Message.cc
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
/**
* 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/defines.h>
#include <pulsar/Message.h>
#include <pulsar/MessageBuilder.h>
#include "PulsarApi.pb.h"
#include "MessageImpl.h"
#include "SharedBuffer.h"
#include <iostream>
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(); }
std::string Message::getDataAsString() const { return std::string((const char*)getData(), getLength()); }
Message::Message() : impl_() {}
Message::Message(MessageImplPtr& impl) : impl_(impl) {}
Message::Message(const proto::CommandMessage& msg, proto::MessageMetadata& metadata, SharedBuffer& payload,
int32_t partition)
: impl_(std::make_shared<MessageImpl>()) {
impl_->messageId =
MessageId(partition, msg.message_id().ledgerid(), msg.message_id().entryid(), /* batchId */
-1);
impl_->metadata = metadata;
impl_->payload = payload;
}
Message::Message(const MessageId& messageID, proto::MessageMetadata& metadata, SharedBuffer& payload,
proto::SingleMessageMetadata& singleMetadata, const std::string& topicName)
: impl_(std::make_shared<MessageImpl>()) {
impl_->messageId = messageID;
impl_->metadata = metadata;
impl_->payload = payload;
impl_->metadata.mutable_properties()->CopyFrom(singleMetadata.properties());
impl_->topicName_ = &topicName;
if (singleMetadata.has_partition_key()) {
impl_->metadata.set_partition_key(singleMetadata.partition_key());
}
if (singleMetadata.has_event_time()) {
impl_->metadata.set_event_time(singleMetadata.event_time());
}
}
const MessageId& Message::getMessageId() const {
if (!impl_) {
return invalidMessageId;
} else {
return impl_->messageId;
}
}
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();
}
const int Message::getRedeliveryCount() const {
if (!impl_) {
return 0;
}
return impl_->getRedeliveryCount();
}
uint64_t Message::getPublishTimestamp() const { return impl_ ? impl_->getPublishTimestamp() : 0ull; }
uint64_t Message::getEventTimestamp() const { return impl_ ? impl_->getEventTimestamp() : 0ull; }
bool Message::operator==(const Message& msg) const { return getMessageId() == msg.getMessageId(); }
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