Skip to content

Commit

Permalink
Rename the SIO default reader / writer
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jan 31, 2024
1 parent 44f81a3 commit 9b483c2
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 145 deletions.
86 changes: 3 additions & 83 deletions include/podio/SIOFrameReader.h
Original file line number Diff line number Diff line change
@@ -1,90 +1,10 @@
#ifndef PODIO_SIOFRAMEREADER_H
#define PODIO_SIOFRAMEREADER_H

#include "podio/SIOBlock.h"
#include "podio/SIOFrameData.h"
#include "podio/podioVersion.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <sio/definitions.h>

#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include "podio/SIOReader.h"

namespace podio {

class CollectionIDTable;

class SIOFrameReader {

public:
SIOFrameReader();
~SIOFrameReader() = default;

// non copyable
SIOFrameReader(const SIOFrameReader&) = delete;
SIOFrameReader& operator=(const SIOFrameReader&) = delete;

/**
* Read the next data entry from which a Frame can be constructed for the
* given name. In case there are no more entries left for this name or in
* case there is no data for this name, this returns a nullptr.
*/
std::unique_ptr<podio::SIOFrameData> readNextEntry(const std::string& name);

/**
* Read the specified data entry from which a Frame can be constructed for
* the given name. In case the entry does not exist for this name or in
* case there is no data for this name, this returns a nullptr.
*/
std::unique_ptr<podio::SIOFrameData> readEntry(const std::string& name, const unsigned entry);

/// Returns number of entries for the given name
unsigned getEntries(const std::string& name) const;

void openFile(const std::string& filename);

/// Get the build version of podio that has been used to write the current file
podio::version::Version currentFileVersion() const {
return m_fileVersion;
}

/// Get the names of all the available Frame categories in the current file(s)
std::vector<std::string_view> getAvailableCategories() const;

/// Get the datamodel definition for the given name
const std::string_view getDatamodelDefinition(const std::string& name) const {
return m_datamodelHolder.getDatamodelDefinition(name);
}

/// Get all names of the datamodels that ara available from this reader
std::vector<std::string> getAvailableDatamodels() const {
return m_datamodelHolder.getAvailableDatamodels();
}

private:
void readPodioHeader();

/// read the TOC record
bool readFileTOCRecord();

void readEDMDefinitions();

sio::ifstream m_stream{}; ///< The stream from which we read

/// Count how many times each an entry of this name has been read already
std::unordered_map<std::string, unsigned> m_nameCtr{};

/// Table of content record where starting points of named entries can be read from
SIOFileTOCRecord m_tocRecord{};
/// The podio version that has been used to write the file
podio::version::Version m_fileVersion{0};

DatamodelDefinitionHolder m_datamodelHolder{};
};

} // namespace podio
using SIOFrameReader [[deprecated("Will be removed in v1.0 switch podio::SIOReader")]] = podio::SIOReader;
}

#endif // PODIO_SIOFRAMEREADER_H
40 changes: 3 additions & 37 deletions include/podio/SIOFrameWriter.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
#ifndef PODIO_SIOFRAMEWRITER_H
#define PODIO_SIOFRAMEWRITER_H

#include "podio/SIOBlock.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <sio/definitions.h>

#include <string>
#include <utility>
#include <vector>
#include "podio/SIOWriter.h"

namespace podio {

class Frame;

class SIOFrameWriter {
public:
SIOFrameWriter(const std::string& filename);
~SIOFrameWriter();

SIOFrameWriter(const SIOFrameWriter&) = delete;
SIOFrameWriter& operator=(const SIOFrameWriter&) = delete;

/** Write the given Frame with the given category
*/
void writeFrame(const podio::Frame& frame, const std::string& category);

/** Write the given Frame with the given category only storing the collections
* that are desired via collsToWrite
*/
void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collsToWrite);

void finish();

private:
sio::ofstream m_stream{}; ///< The output file stream
SIOFileTOCRecord m_tocRecord{}; ///< The "table of contents" of the written file
DatamodelDefinitionCollector m_datamodelCollector{};
bool m_finished{false}; ///< Has finish been called already?
};
} // namespace podio
using SIOFrameWriter [[deprecated("Will be removed in v1.0 switch podio::SIOWriter")]] = podio::SIOWriter;
}

#endif // PODIO_SIOFRAMEWRITER_H
90 changes: 90 additions & 0 deletions include/podio/SIOReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef PODIO_SIOREADER_H
#define PODIO_SIOREADER_H

#include "podio/SIOBlock.h"
#include "podio/SIOFrameData.h"
#include "podio/podioVersion.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <sio/definitions.h>

#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>

namespace podio {

class CollectionIDTable;

class SIOReader {

public:
SIOReader();
~SIOReader() = default;

// non copyable
SIOReader(const SIOReader&) = delete;
SIOReader& operator=(const SIOReader&) = delete;

/**
* Read the next data entry from which a Frame can be constructed for the
* given name. In case there are no more entries left for this name or in
* case there is no data for this name, this returns a nullptr.
*/
std::unique_ptr<podio::SIOFrameData> readNextEntry(const std::string& name);

/**
* Read the specified data entry from which a Frame can be constructed for
* the given name. In case the entry does not exist for this name or in
* case there is no data for this name, this returns a nullptr.
*/
std::unique_ptr<podio::SIOFrameData> readEntry(const std::string& name, const unsigned entry);

/// Returns number of entries for the given name
unsigned getEntries(const std::string& name) const;

void openFile(const std::string& filename);

/// Get the build version of podio that has been used to write the current file
podio::version::Version currentFileVersion() const {
return m_fileVersion;
}

/// Get the names of all the available Frame categories in the current file(s)
std::vector<std::string_view> getAvailableCategories() const;

/// Get the datamodel definition for the given name
const std::string_view getDatamodelDefinition(const std::string& name) const {
return m_datamodelHolder.getDatamodelDefinition(name);
}

/// Get all names of the datamodels that ara available from this reader
std::vector<std::string> getAvailableDatamodels() const {
return m_datamodelHolder.getAvailableDatamodels();
}

private:
void readPodioHeader();

/// read the TOC record
bool readFileTOCRecord();

void readEDMDefinitions();

sio::ifstream m_stream{}; ///< The stream from which we read

/// Count how many times each an entry of this name has been read already
std::unordered_map<std::string, unsigned> m_nameCtr{};

/// Table of content record where starting points of named entries can be read from
SIOFileTOCRecord m_tocRecord{};
/// The podio version that has been used to write the file
podio::version::Version m_fileVersion{0};

DatamodelDefinitionHolder m_datamodelHolder{};
};

} // namespace podio

#endif // PODIO_SIOREADER_H
44 changes: 44 additions & 0 deletions include/podio/SIOWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef PODIO_SIOWRITER_H
#define PODIO_SIOWRITER_H

#include "podio/SIOBlock.h"
#include "podio/utilities/DatamodelRegistryIOHelpers.h"

#include <sio/definitions.h>

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

namespace podio {

class Frame;

class SIOWriter {
public:
SIOWriter(const std::string& filename);
~SIOWriter();

SIOWriter(const SIOWriter&) = delete;
SIOWriter& operator=(const SIOWriter&) = delete;

/** Write the given Frame with the given category
*/
void writeFrame(const podio::Frame& frame, const std::string& category);

/** Write the given Frame with the given category only storing the collections
* that are desired via collsToWrite
*/
void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collsToWrite);

void finish();

private:
sio::ofstream m_stream{}; ///< The output file stream
SIOFileTOCRecord m_tocRecord{}; ///< The "table of contents" of the written file
DatamodelDefinitionCollector m_datamodelCollector{};
bool m_finished{false}; ///< Has finish been called already?
};
} // namespace podio

#endif // PODIO_SIOWRITER_H
4 changes: 2 additions & 2 deletions python/podio/sio_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, filename):
Args:
filename (str): File to open and read data from
"""
self._reader = podio.SIOFrameReader()
self._reader = podio.SIOReader()
self._reader.openFile(filename)

super().__init__()
Expand Down Expand Up @@ -57,5 +57,5 @@ def __init__(self, filename):
Args:
filename (str): The name of the output file
"""
self._writer = podio.SIOFrameWriter(filename)
self._writer = podio.SIOWriter(filename)
super().__init__()
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ if(ENABLE_SIO)
SET(sio_sources
SIOBlockUserData.cc
SIOBlock.cc
SIOFrameWriter.cc
SIOFrameReader.cc
SIOWriter.cc
SIOReader.cc
SIOFrameData.cc
sioUtils.h
SIOLegacyReader.cc
)

SET(sio_headers
${PROJECT_SOURCE_DIR}/include/podio/SIOFrameReader.h
${PROJECT_SOURCE_DIR}/include/podio/SIOReader.h
${PROJECT_SOURCE_DIR}/include/podio/SIOLegacyReader.h
${PROJECT_SOURCE_DIR}/include/podio/SIOFrameWriter.h
${PROJECT_SOURCE_DIR}/include/podio/SIOWriter.h
)

PODIO_ADD_LIB_AND_DICT(podioSioIO "${sio_headers}" "${sio_sources}" sio_selection.xml)
Expand Down
20 changes: 10 additions & 10 deletions src/SIOFrameReader.cc → src/SIOReader.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "podio/SIOFrameReader.h"
#include "podio/SIOReader.h"
#include "podio/SIOBlock.h"

#include "sioUtils.h"
Expand All @@ -11,11 +11,11 @@

namespace podio {

SIOFrameReader::SIOFrameReader() {
SIOReader::SIOReader() {
auto& libLoader [[maybe_unused]] = SIOBlockLibraryLoader::instance();
}

void SIOFrameReader::openFile(const std::string& filename) {
void SIOReader::openFile(const std::string& filename) {
m_stream.open(filename, std::ios::binary);
if (!m_stream.is_open()) {
throw std::runtime_error("File " + filename + " couldn't be opened");
Expand All @@ -27,7 +27,7 @@ void SIOFrameReader::openFile(const std::string& filename) {
readEDMDefinitions(); // Potentially could do this lazily
}

std::unique_ptr<SIOFrameData> SIOFrameReader::readNextEntry(const std::string& name) {
std::unique_ptr<SIOFrameData> SIOReader::readNextEntry(const std::string& name) {
// Skip to where the next record of this name starts in the file, based on
// how many times we have already read this name
//
Expand All @@ -48,14 +48,14 @@ std::unique_ptr<SIOFrameData> SIOFrameReader::readNextEntry(const std::string& n
tableInfo._uncompressed_length);
}

std::unique_ptr<SIOFrameData> SIOFrameReader::readEntry(const std::string& name, const unsigned entry) {
std::unique_ptr<SIOFrameData> SIOReader::readEntry(const std::string& name, const unsigned entry) {
// NOTE: Will create or overwrite the entry counter
// All checks are done in the following function
m_nameCtr[name] = entry;
return readNextEntry(name);
}

std::vector<std::string_view> SIOFrameReader::getAvailableCategories() const {
std::vector<std::string_view> SIOReader::getAvailableCategories() const {
// Filter the available records from the TOC to remove records that are
// stored, but use reserved record names for podio meta data
auto recordNames = m_tocRecord.getRecordNames();
Expand All @@ -65,11 +65,11 @@ std::vector<std::string_view> SIOFrameReader::getAvailableCategories() const {
return recordNames;
}

unsigned SIOFrameReader::getEntries(const std::string& name) const {
unsigned SIOReader::getEntries(const std::string& name) const {
return m_tocRecord.getNRecords(name);
}

bool SIOFrameReader::readFileTOCRecord() {
bool SIOReader::readFileTOCRecord() {
// Check if there is a dedicated marker at the end of the file that tells us
// where the TOC actually starts
m_stream.seekg(-sio_helpers::SIOTocInfoSize, std::ios_base::end);
Expand Down Expand Up @@ -99,7 +99,7 @@ bool SIOFrameReader::readFileTOCRecord() {
return false;
}

void SIOFrameReader::readPodioHeader() {
void SIOReader::readPodioHeader() {
const auto& [buffer, _] = sio_utils::readRecord(m_stream, false, sizeof(podio::version::Version));

sio::block_list blocks;
Expand All @@ -109,7 +109,7 @@ void SIOFrameReader::readPodioHeader() {
m_fileVersion = static_cast<SIOVersionBlock*>(blocks[0].get())->version;
}

void SIOFrameReader::readEDMDefinitions() {
void SIOReader::readEDMDefinitions() {
const auto recordPos = m_tocRecord.getPosition(sio_helpers::SIOEDMDefinitionName);
if (recordPos == 0) {
// No EDM definitions found
Expand Down
Loading

0 comments on commit 9b483c2

Please sign in to comment.