forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.h
More file actions
244 lines (209 loc) · 6.99 KB
/
Message.h
File metadata and controls
244 lines (209 loc) · 6.99 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
/**
* 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.
*/
#ifndef MESSAGE_HPP_
#define MESSAGE_HPP_
#include <pulsar/EncryptionContext.h>
#include <pulsar/defines.h>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include "KeyValue.h"
#include "MessageId.h"
namespace pulsar {
namespace proto {
class CommandMessage;
class BrokerEntryMetadata;
class MessageMetadata;
class SingleMessageMetadata;
} // namespace proto
class SharedBuffer;
class MessageBuilder;
class MessageImpl;
class PulsarWrapper;
class PULSAR_PUBLIC Message {
public:
typedef std::map<std::string, std::string> StringMap;
Message();
/**
* Return the properties attached to the message.
* Properties are application defined key/value pairs that will be attached to the message
*
* @return an unmodifiable view of the properties map
*/
const StringMap& getProperties() const;
/**
* Check whether the message has a specific property attached.
*
* @param name the name of the property to check
* @return true if the message has the specified property
* @return false if the property is not defined
*/
bool hasProperty(const std::string& name) const;
/**
* Get the value of a specific property
*
* @param name the name of the property
* @return the value of the property or null if the property was not defined
*/
const std::string& getProperty(const std::string& name) const;
/**
* Get the content of the message
*
*
* @return the pointer to the message payload
*/
const void* getData() const;
/**
* Get the length of the message
*
* @return the length of the message payload
*/
std::size_t getLength() const;
/**
* Get string representation of the message
*
* @return the string representation of the message payload
*
* NOTE: For MSVC with debug mode, return a thread local std::string object to avoid memory allocation
* across DLLs and applications, which could lead to a crash.
*/
#if defined(_MSC_VER) && !defined(NDEBUG)
const std::string& getDataAsString() const;
#else
std::string getDataAsString() const;
#endif
/**
* Get key value message.
*
* @return key value message.
*/
KeyValue getKeyValueData() const;
/**
* Get the unique message ID associated with this message.
*
* The message id can be used to univocally refer to a message without having to keep the entire payload
* in memory.
*
* Only messages received from the consumer will have a message id assigned.
*
*/
const MessageId& getMessageId() const;
/**
* Set the unique message ID.
*
*/
void setMessageId(const MessageId& messageId) const;
/**
* Get the index of this message, if it doesn't exist, return -1
* @return
*/
int64_t getIndex() const;
/**
* Get the partition key for this message
* @return key string that is hashed to determine message's topic partition
*/
const std::string& getPartitionKey() const;
/**
* @return true if the message has a partition key
*/
bool hasPartitionKey() const;
/**
* Get the ordering key of the message
*
* @return the ordering key of the message
*/
const std::string& getOrderingKey() const;
/**
* Check whether the message has a ordering key
*
* @return true if the ordering key was set while creating the message
* false if the ordering key was not set while creating the message
*/
bool hasOrderingKey() const;
/**
* Get the UTC based timestamp in milliseconds referring to when the message was published by the client
* producer
*/
uint64_t getPublishTimestamp() const;
/**
* Get the event timestamp associated with this message. It is set by the client producer.
*/
uint64_t getEventTimestamp() const;
/**
* Get the topic Name from which this message originated from
*/
const std::string& getTopicName() const;
/**
* Get the redelivery count for this message
*/
int getRedeliveryCount() const;
/**
* Check if schema version exists
*/
bool hasSchemaVersion() const;
/**
* Get the schema version.
*
* @return the the schema version on success or -1 if the message does not have the schema version
*/
int64_t getLongSchemaVersion() const;
/**
* Get the schema version of the raw bytes.
*/
const std::string& getSchemaVersion() const;
/**
* Get the producer name which produced this message.
*
* @return the producer name or empty string if not available
*/
const std::string& getProducerName() const noexcept;
/**
* @return the optional encryption context that is present when the message is encrypted, the pointer is
* valid as the Message instance is alive
*/
std::optional<const EncryptionContext*> getEncryptionContext() const;
bool operator==(const Message& msg) const;
protected:
typedef std::shared_ptr<MessageImpl> MessageImplPtr;
MessageImplPtr impl_;
Message(MessageImplPtr& impl);
Message(const MessageId& messageId, proto::BrokerEntryMetadata& brokerEntryMetadata,
proto::MessageMetadata& metadata, SharedBuffer& payload);
/// Used for Batch Messages
Message(const MessageId& messageId, proto::BrokerEntryMetadata& brokerEntryMetadata,
proto::MessageMetadata& metadata, SharedBuffer& payload,
proto::SingleMessageMetadata& singleMetadata, const std::shared_ptr<std::string>& topicName);
friend class PartitionedProducerImpl;
friend class MultiTopicsConsumerImpl;
friend class MessageBuilder;
friend class ConsumerImpl;
friend class ProducerImpl;
friend class Commands;
friend class BatchMessageContainerBase;
friend class BatchAcknowledgementTracker;
friend class PulsarWrapper;
friend class MessageBatch;
friend struct OpSendMsg;
friend PULSAR_PUBLIC std::ostream& operator<<(std::ostream& s, const StringMap& map);
friend PULSAR_PUBLIC std::ostream& operator<<(std::ostream& s, const Message& msg);
friend class PulsarFriend;
};
} // namespace pulsar
#endif /* MESSAGE_HPP_ */