Skip to content

Commit

Permalink
Add check for existence before getting collections
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 18, 2023
1 parent 294ef3f commit 3f1dbb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/podio/CollectionIDTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class CollectionIDTable {
/// Check if collection name is known
bool present(const std::string& name) const;

/// Check if collection ID is known
bool present(uint32_t collectionID) const;

/// return registered names
const std::vector<std::string>& names() const {
return m_names;
Expand Down
3 changes: 3 additions & 0 deletions include/podio/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ podio::CollectionBase* Frame::FrameModel<FrameDataT>::doGet(const std::string& n

template <typename FrameDataT>
bool Frame::FrameModel<FrameDataT>::get(uint32_t collectionID, CollectionBase*& collection) const {
if (!m_idTable.present(collectionID)) {
return false;
}
const auto& name = m_idTable.name(collectionID);
const auto& [_, inserted] = m_retrievedIDs.insert(collectionID);

Expand Down
10 changes: 8 additions & 2 deletions src/CollectionIDTable.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// podio specific includes
#include "podio/CollectionIDTable.h"
#include <algorithm>
#include <iostream>

#include "MurmurHash3.h"

#include <algorithm>
#include <iostream>

namespace podio {

CollectionIDTable::CollectionIDTable() : m_mutex(std::make_unique<std::mutex>()) {
Expand Down Expand Up @@ -46,6 +47,11 @@ bool CollectionIDTable::present(const std::string& name) const {
return result != end(m_names);
}

bool CollectionIDTable::present(uint32_t collectionID) const {
std::lock_guard<std::mutex> lock{*m_mutex};
return std::find(m_collectionIDs.begin(), m_collectionIDs.end(), collectionID) != m_collectionIDs.end();
}

uint32_t CollectionIDTable::add(const std::string& name) {
std::lock_guard<std::mutex> lock(*m_mutex);
const auto result = std::find(begin(m_names), end(m_names), name);
Expand Down

0 comments on commit 3f1dbb3

Please sign in to comment.