Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the model definition into files that are written #358

Merged
merged 19 commits into from
Mar 7, 2023
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Rename EDMDefinitionRegistry and clarify documentation
tmadlener committed Mar 2, 2023
commit 27ab1bb76d1956e4401dfb292657c60491e7e16b
4 changes: 2 additions & 2 deletions include/podio/CollectionBase.h
Original file line number Diff line number Diff line change
@@ -77,8 +77,8 @@ class CollectionBase {
/// print this collection to the passed stream
virtual void print(std::ostream& os = std::cout, bool flush = true) const = 0;

/// Get the index in the EDMDefinitionRegistry of the EDM this collection belongs to
virtual size_t getDefinitionRegistryIndex() const = 0;
/// Get the index in the DatatypeRegistry of the EDM this collection belongs to
virtual size_t getDatamodelRegistryIndex() const = 0;
};

} // namespace podio
99 changes: 99 additions & 0 deletions include/podio/DatamodelRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#ifndef PODIO_DATAMODELREGISTRY_H
#define PODIO_DATAMODELREGISTRY_H

#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace podio {

/**
* Global registry holding information about datamodels and datatypes defined
* therein that are currently known by podio (i.e. which have been dynamically
* loaded).
*
* This is a singleton which is (statically) populated during dynamic loading of
* generated EDMs. In this context an **EDM refers to the shared library** that
* is compiled from the generated code from a datamodel definition in YAML
* format. When we refer to a **datamodel** in this context we talk about the
* entity as a whole, i.e. its definition in a YAML file, but also the concrete
* implementation as an EDM, as well as all other information that is related to
* it. In the API of this registry this will be used, unless we want to
* highlight that we are referring to a specific part of a datamodel.
*/
class DatamodelRegistry {
public:
/// Get the registry
static const DatamodelRegistry& instance();

// Mutable instance only used for the initial registration!
static DatamodelRegistry& mutInstance();

~DatamodelRegistry() = default;
DatamodelRegistry(const DatamodelRegistry&) = delete;
DatamodelRegistry& operator=(const DatamodelRegistry&) = delete;
DatamodelRegistry(DatamodelRegistry&&) = delete;
DatamodelRegistry& operator=(const DatamodelRegistry&&) = delete;

/// Dedicated index value for collections that don't have a datamodel
/// definition (e.g. UserDataCollection)
static constexpr size_t NoDefinitionNecessary = -1;
/// Dedicated index value for error checking, used to default init the generated RegistryIndex
static constexpr size_t NoDefinitionAvailable = -2;

/**
* Get the definition (in JSON format) of the datamodel with the given
* edmName.
*
* If no datamodel with the given name can be found, an empty datamodel
* definition, i.e. an empty JSON object ("{}"), is returned.
*
* @param name The name of the datamodel
*/
const std::string_view getDatamodelDefinition(std::string_view name) const;

/**
* Get the defintion (in JSON format) of the datamodel wth the given index.
*
* If no datamodel is found under the given index, an empty datamodel
* definition, i.e. an empty JSON object ("{}"), is returned.
*
* @param index The datamodel definition index that can be obtained from each
* collection
*/
const std::string_view getDatamodelDefinition(size_t index) const;

/**
* Get the name of the datamodel that is stored under the given index.
*
* If no datamodel is found under the given index, an empty string is returned
*
* @param index The datamodel definition index that can be obtained from each
* collection
*/
const std::string& getDatamodelName(size_t index) const;

/**
* Register a datamodel return the index in the registry.
*
* This is the hook that is called during dynamic loading of an EDM to
* register information for this EDM. If an EDM has already been registered
* under this name, than the index to the existing EDM in the registry will be
* returned.
*
* @param name The name of the EDM that should be registered
* @param definition The datamodel definition from which this EDM has been
* generated in JSON format
*
*/
size_t registerDatamodel(std::string name, std::string_view definition);

private:
DatamodelRegistry() = default;
/// The stored definitions
std::vector<std::pair<std::string, std::string_view>> m_definitions{};
};
} // namespace podio

#endif // PODIO_DATAMODELREGISTRY_H
62 changes: 0 additions & 62 deletions include/podio/EDMDefinitionRegistry.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/podio/ROOTFrameReader.h
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ struct CollectionReadBuffers;
* This class has the function to read available data from disk
* and to prepare collections and buffers.
**/
class ROOTFrameReader : public EDMDefinitionHolder {
class ROOTFrameReader : public DatamodelDefinitionHolder {

public:
ROOTFrameReader() = default;
2 changes: 1 addition & 1 deletion include/podio/ROOTFrameWriter.h
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ class Frame;
class CollectionBase;
class GenericParameters;

class ROOTFrameWriter : EDMDefinitionCollector {
class ROOTFrameWriter : DatamodelDefinitionCollector {
public:
ROOTFrameWriter(const std::string& filename);
~ROOTFrameWriter() = default;
2 changes: 1 addition & 1 deletion include/podio/SIOFrameReader.h
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ namespace podio {

class CollectionIDTable;

class SIOFrameReader : public EDMDefinitionHolder {
class SIOFrameReader : public DatamodelDefinitionHolder {

public:
SIOFrameReader();
2 changes: 1 addition & 1 deletion include/podio/SIOFrameWriter.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ namespace podio {

class Frame;

class SIOFrameWriter : EDMDefinitionCollector {
class SIOFrameWriter : DatamodelDefinitionCollector {
public:
SIOFrameWriter(const std::string& filename);
~SIOFrameWriter() = default;
6 changes: 3 additions & 3 deletions include/podio/UserDataCollection.h
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

#include "podio/CollectionBase.h"
#include "podio/CollectionBuffers.h"
#include "podio/EDMDefinitionRegistry.h"
#include "podio/DatamodelRegistry.h"
#include "podio/utilities/TypeHelpers.h"

#include <map>
@@ -173,8 +173,8 @@ class UserDataCollection : public CollectionBase {
}
}

size_t getDefinitionRegistryIndex() const override {
return EDMDefinitionRegistry::NoDefinitionNecessary;
size_t getDatamodelRegistryIndex() const override {
return DatamodelRegistry::NoDefinitionNecessary;
}

// ----- some wrapers for std::vector and access to the complete std::vector (if really needed)
37 changes: 25 additions & 12 deletions include/podio/utilities/EDMRegistryIOHelpers.h
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
#define PODIO_UTILITIES_EDMREGISTRYIOHELPERS_H

#include "podio/CollectionBase.h"
#include "podio/EDMDefinitionRegistry.h"
#include "podio/DatamodelRegistry.h"

#include <set>
#include <string>
@@ -12,34 +12,47 @@
namespace podio {

/**
* Helper class (mixin) to collect the EDM (JSON) definitions that should be
* Helper class (mixin) to collect the datamodel (JSON) definitions that should be
* written.
*/
class EDMDefinitionCollector {
class DatamodelDefinitionCollector {
public:
/// Register the EDM where this collection is from to be written
void registerEDMDef(const podio::CollectionBase* coll, const std::string& name);
/**
* Register the datamodel definition of the EDM this collection is from to be
* written.
*
* @param coll A collection of an EDM
* @param name The name under which this collection is stored on file
*/
void registerDatamodelDefinition(const podio::CollectionBase* coll, const std::string& name);

/// Get all the names and JSON definitions that need to be written
std::vector<std::tuple<std::string, std::string>> getEDMDefinitionsToWrite() const;
std::vector<std::tuple<std::string, std::string>> getDatamodelDefinitionsToWrite() const;

private:
std::set<size_t> m_edmDefRegistryIdcs{}; ///< The indices in the EDM definition registry that need to be written
};

/**
* Helper class (mixin) to hold and provide the EDM (JSON) definitions for
* Helper class (mixin) to hold and provide the datamodel (JSON) definitions for
* reader classes.
*/
class EDMDefinitionHolder {
class DatamodelDefinitionHolder {
public:
/**
* Get the EDM definition for the given EDM name. Returns an empty model
* definition if no model is stored under the given name.
* Get the datamodel definition for the given datamodel name.
*
* Returns an empty model definition if no model is stored under the given
* name.
*
* @param name The name of the datamodel
*/
const std::string_view getEDMDefinition(const std::string& edmName) const;
const std::string_view getDatamodelDefinition(const std::string& name) const;

std::vector<std::string> getAvailableEDMDefinitions() const;
/**
* Get all names of the datamodels that have been read from file
*/
std::vector<std::string> getAvailableDatamodels() const;

protected:
std::vector<std::tuple<std::string, std::string>> m_availEDMDefs{};
4 changes: 2 additions & 2 deletions python/templates/Collection.cc.jinja2
Original file line number Diff line number Diff line change
@@ -179,8 +179,8 @@ podio::CollectionReadBuffers {{ collection_type }}::createBuffers() /*const*/ {
{{ macros.vectorized_access(class, member) }}
{% endfor %}

size_t {{ collection_type }}::getDefinitionRegistryIndex() const {
return {{ package_name }}::meta::DefinitionRegistryIndex::value();
size_t {{ collection_type }}::getDatamodelRegistryIndex() const {
return {{ package_name }}::meta::DatamodelRegistryIndex::value();
}

#ifdef PODIO_JSON_OUTPUT
2 changes: 1 addition & 1 deletion python/templates/Collection.h.jinja2
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ public:
return m_isValid;
}

size_t getDefinitionRegistryIndex() const final;
size_t getDatamodelRegistryIndex() const final;

// support for the iterator protocol
iterator begin() {
18 changes: 9 additions & 9 deletions python/templates/DatamodelDefinition.h.jinja2
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// AUTOMATICALLY GENERATED FILE - DO NOT EDIT

#include "podio/EDMDefinitionRegistry.h"
#include "podio/DatamodelRegistry.h"

namespace {{ package_name }}::meta {
/**
* The complete definition of the datamodel at generation time in JSON format.
*/
static constexpr auto {{ package_name }}__JSONDefinition = R"EDMDEFINITION({{ edm_definition }})EDMDEFINITION";
static constexpr auto {{ package_name }}__JSONDefinition = R"DATAMODELDEF({{ edm_definition }})DATAMODELDEF";

/**
* The helper class that takes care of registering the EDM definition to the
* EDMDefinitionRegistry and to provide the index in that registry.
* The helper class that takes care of registering the datamodel definition to
* the DatamodelRegistry and to provide the index in that registry.
*
* Implemented as a singleton mainly to ensure only a single registration of
* each EDM, during the constructor
* each datamodel, during the constructor
*/
class DefinitionRegistryIndex {
class DatamodelRegistryIndex {
public:
static size_t value() {
static auto index = DefinitionRegistryIndex(podio::EDMDefinitionRegistry::mutInstance().registerEDM("{{ package_name }}", {{ package_name }}__JSONDefinition));
static auto index = DatamodelRegistryIndex(podio::DatamodelRegistry::mutInstance().registerDatamodel("{{ package_name }}", {{ package_name }}__JSONDefinition));
return index.m_value;
}

private:
DefinitionRegistryIndex(size_t v) : m_value(v) {}
size_t m_value{podio::EDMDefinitionRegistry::NoDefinitionAvailable};
DatamodelRegistryIndex(size_t v) : m_value(v) {}
size_t m_value{podio::DatamodelRegistry::NoDefinitionAvailable};
};

} // namespace {{ package_name }}::meta
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ SET(core_sources
GenericParameters.cc
ASCIIWriter.cc
EventStore.cc
EDMDefinitionRegistry.cc
DatamodelRegistry.cc
EDMRegistryIOHelpers.cc
)

@@ -62,7 +62,7 @@ SET(core_headers
${CMAKE_SOURCE_DIR}/include/podio/ObjectID.h
${CMAKE_SOURCE_DIR}/include/podio/UserDataCollection.h
${CMAKE_SOURCE_DIR}/include/podio/podioVersion.h
${CMAKE_SOURCE_DIR}/include/podio/EDMDefinitionRegistry.h
${CMAKE_SOURCE_DIR}/include/podio/DatamodelRegistry.h
${CMAKE_SOURCE_DIR}/include/podio/utilities/EDMRegistryIOHelpers.h
)

Loading