diff --git a/include/pulsar/Message.h b/include/pulsar/Message.h index 1c906e8a..6d5c3afd 100644 --- a/include/pulsar/Message.h +++ b/include/pulsar/Message.h @@ -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 */ diff --git a/lib/Message.cc b/lib/Message.cc index 8bde683f..156bc59c 100644 --- a/lib/Message.cc +++ b/lib/Message.cc @@ -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; diff --git a/lib/MessageImpl.cc b/lib/MessageImpl.cc index cfc573a7..2ef48d16 100644 --- a/lib/MessageImpl.cc +++ b/lib/MessageImpl.cc @@ -55,6 +55,8 @@ uint64_t MessageImpl::getEventTimestamp() const { } } +bool MessageImpl::hasEventTimestamp() const { return metadata.has_event_time(); } + void MessageImpl::setReplicationClusters(const std::vector& clusters) { google::protobuf::RepeatedPtrField r(clusters.begin(), clusters.end()); r.Swap(metadata.mutable_replicate_to()); diff --git a/lib/MessageImpl.h b/lib/MessageImpl.h index a234ca45..db974894 100644 --- a/lib/MessageImpl.h +++ b/lib/MessageImpl.h @@ -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 diff --git a/tests/MessageTest.cc b/tests/MessageTest.cc index 7f1ae4d6..a178bddc 100644 --- a/tests/MessageTest.cc +++ b/tests/MessageTest.cc @@ -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");