Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/pulsar/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ class PULSAR_PUBLIC Message {
*/
uint64_t getEventTimestamp() const;

/**
* Check whether the event timestamp was set by the client producer.
*
* @return true if the event timestamp was set while creating the message
* false if the event timestamp was not set while creating the message
*
* This is distinct from `getEventTimestamp() != 0`, because a producer may legitimately set an
* event timestamp of 0 (the Unix epoch).
*/
bool hasEventTimestamp() const;

/**
* Get the topic Name from which this message originated from
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/Message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ uint64_t Message::getPublishTimestamp() const { return impl_ ? impl_->getPublish

uint64_t Message::getEventTimestamp() const { return impl_ ? impl_->getEventTimestamp() : 0ull; }

bool Message::hasEventTimestamp() const { return impl_ ? impl_->hasEventTimestamp() : false; }

const std::string& Message::getProducerName() const noexcept {
if (!impl_) {
return emptyString;
Expand Down
2 changes: 2 additions & 0 deletions lib/MessageImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ uint64_t MessageImpl::getEventTimestamp() const {
}
}

bool MessageImpl::hasEventTimestamp() const { return metadata.has_event_time(); }

void MessageImpl::setReplicationClusters(const std::vector<std::string>& clusters) {
google::protobuf::RepeatedPtrField<std::string> r(clusters.begin(), clusters.end());
r.Swap(metadata.mutable_replicate_to());
Expand Down
1 change: 1 addition & 0 deletions lib/MessageImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class MessageImpl {

uint64_t getPublishTimestamp() const;
uint64_t getEventTimestamp() const;
bool hasEventTimestamp() const;

/**
* Get the topic Name from which this message originated from
Expand Down
14 changes: 14 additions & 0 deletions tests/MessageTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ bool compareMaps(const Map& lhs, const Map& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}

TEST(MessageTest, testHasEventTimestamp) {
ASSERT_FALSE(Message().hasEventTimestamp());
ASSERT_FALSE(MessageBuilder().setContent("msg").build().hasEventTimestamp());

// The Unix epoch is a valid event time, so it must not be reported as an unset one.
Message msg = MessageBuilder().setContent("msg").setEventTimestamp(0).build();
ASSERT_TRUE(msg.hasEventTimestamp());
ASSERT_EQ(msg.getEventTimestamp(), 0u);

msg = MessageBuilder().setContent("msg").setEventTimestamp(1234).build();
ASSERT_TRUE(msg.hasEventTimestamp());
ASSERT_EQ(msg.getEventTimestamp(), 1234u);
}

TEST(MessageTest, testProperties) {
MessageBuilder msgBuilder1;
msgBuilder1.setProperty("property1", "value1");
Expand Down