Skip to content
Merged
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
6 changes: 6 additions & 0 deletions pj_datastore/include/pj_datastore/object_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <optional>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -91,6 +92,11 @@ class ObjectStore {

Expected<ObjectTopicId> registerTopic(const ObjectTopicDescriptor& descriptor);

// Resolve a topic id by (dataset_id, topic_name) without registering. Returns
// nullopt if no topic with that key exists. Used by hosts that need to bind a
// parser-side write surface to a topic the source already registered.
std::optional<ObjectTopicId> findTopic(DatasetId dataset_id, std::string_view topic_name) const;

const ObjectTopicDescriptor& descriptor(ObjectTopicId id) const;

std::vector<ObjectTopicId> listTopics() const;
Expand Down
11 changes: 11 additions & 0 deletions pj_datastore/src/object_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ Expected<ObjectTopicId> ObjectStore::registerTopic(const ObjectTopicDescriptor&
return id;
}

std::optional<ObjectTopicId> ObjectStore::findTopic(
DatasetId dataset_id, std::string_view topic_name) const {
std::shared_lock lock(store_mutex_);
for (const auto& [tid, series] : topics_) {
if (series->descriptor.dataset_id == dataset_id && series->descriptor.topic_name == topic_name) {
return tid;
}
}
return std::nullopt;
}

const ObjectTopicDescriptor& ObjectStore::descriptor(ObjectTopicId id) const {
std::shared_lock lock(store_mutex_);
const auto* s = findSeries(id);
Expand Down
15 changes: 15 additions & 0 deletions pj_datastore/tests/object_store_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ TEST(ObjectStoreTest, SameNameDifferentDatasetOk) {
EXPECT_NE(id1.id, id2_or->id);
}

TEST(ObjectStoreTest, FindTopicReturnsRegisteredId) {
ObjectStore store;
auto id = registerTestTopic(store, "cam/image");
auto found = store.findTopic(1, "cam/image");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->id, id.id);
}

TEST(ObjectStoreTest, FindTopicMissingReturnsNullopt) {
ObjectStore store;
registerTestTopic(store, "cam/image");
EXPECT_FALSE(store.findTopic(1, "other/topic").has_value());
EXPECT_FALSE(store.findTopic(99, "cam/image").has_value());
}

TEST(ObjectStoreTest, ListTopics) {
ObjectStore store;
auto id1 = registerTestTopic(store, "topic_a");
Expand Down
Loading