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
34 changes: 22 additions & 12 deletions form/root_storage/root_rfield_read_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ namespace form::detail::experimental {
}
}

if (!view_) {
create_view(type);
}
ensure_view(type);

if (!TDictionary::GetDictionary(type)) {
error("unsupported type");
Expand All @@ -95,9 +93,7 @@ namespace form::detail::experimental {
try {

//Connect to file at the last possible moment at the cost of a little run-time branching
if (!view_) {
create_view(type);
}
ensure_view(type);

if (std::cmp_greater_equal(id, reader_->GetNEntries())) {
return false;
Expand Down Expand Up @@ -144,6 +140,16 @@ namespace form::detail::experimental {
return static_cast<std::uint64_t>(reader_->GetNEntries());
}

void root_rfield_read_container_imp::ensure_view(std::type_info const& type)
{
if (!view_) {
create_view(type);
} else if (*view_type_ != type) {
error("type " + demangle_name(type) + " requested for a field named " + col_name() +
" that is already read as " + demangle_name(*view_type_));
}
}

void root_rfield_read_container_imp::create_view(std::type_info const& type)
{
if (!reader_) { //First time this RNTuple is read this job
Expand All @@ -160,22 +166,26 @@ namespace form::detail::experimental {
}
}

// Keep the new view local until it passes the type check, so a rejected type leaves no view
// behind for a later read to reuse unchecked.
std::unique_ptr<ROOT::RNTupleView<void>> view;
try {
view_ =
std::make_unique<ROOT::RNTupleView<void>>(reader_->GetView(col_name(), nullptr, type));
view = std::make_unique<ROOT::RNTupleView<void>>(reader_->GetView(col_name(), nullptr, type));
} catch (ROOT::RException const& e) {
//RNTupleView<void> will fail to create a field for fields written in streamer mode or for which type does not match the field's type on disk. Passing an empty string for type forces it to create the same type of field as the object on disk. Do this to handle streamer fields, then perform our own type check.
view_ = std::make_unique<ROOT::RNTupleView<void>>(reader_->GetView(col_name(), nullptr, ""));
view = std::make_unique<ROOT::RNTupleView<void>>(reader_->GetView(col_name(), nullptr, ""));
//TClass takes the "std::" off of "std::vector<>" when RNTuple's on-disk format doesn't. Convert RNTuple's type name to match TClass for manual type check because our dictionary of choice will likely be the same as TClass.
if (!TDictionary::GetDictionary(type) ||
!TDictionary::GetDictionary(view_->GetField().GetTypeName().c_str()) ||
(strcmp(TDictionary::GetDictionary(view_->GetField().GetTypeName().c_str())->GetName(),
!TDictionary::GetDictionary(view->GetField().GetTypeName().c_str()) ||
(strcmp(TDictionary::GetDictionary(view->GetField().GetTypeName().c_str())->GetName(),
TDictionary::GetDictionary(type)->GetName()) != 0)) {
handle_rexception(
"type " + demangle_name(type) + " requested for a field named " + col_name() +
" does not match the type in the file: " + view_->GetField().GetTypeName(),
" does not match the type in the file: " + view->GetField().GetTypeName(),
e);
}
}
view_ = std::move(view);
view_type_ = &type;
}
}
5 changes: 5 additions & 0 deletions form/root_storage/root_rfield_read_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <typeinfo>

// NOLINTBEGIN(readability-identifier-naming)
// Forward declarations of ROOT classes
Expand Down Expand Up @@ -44,7 +45,11 @@ namespace form::detail::experimental {
std::shared_ptr<TFile> tfile_;
std::unique_ptr<ROOT::RNTupleReader> reader_;
std::unique_ptr<ROOT::RNTupleView<void>> view_;
/// The type view_ was created and checked for; every later read must request the same type.
std::type_info const* view_type_ = nullptr;

/// Creates the view on first use, or throws if `type` differs from the type it was created for.
void ensure_view(std::type_info const& type);
void create_view(std::type_info const& type);
};
}
Expand Down
9 changes: 9 additions & 0 deletions form/storage/istorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ namespace form::detail::experimental {
std::type_info const& type,
form::experimental::config::tech_setting_config const& settings) = 0;

// Read all rows of a column in row order. The token identifies the column's container (e.g., a
// navigation table or product dictionary); its row id is ignored. Throws if the column cannot
// be read. Backends that store no rows (e.g., generic) return an empty column.
// Intended only for the navigation container, not for data product containers.
virtual std::vector<std::uint64_t> read_uint64_column(
token const& token, form::experimental::config::tech_setting_config const& settings) = 0;
virtual std::vector<std::string> read_string_column(
token const& token, form::experimental::config::tech_setting_config const& settings) = 0;

// Returns std::nullopt if no descriptor is present; throws if the file cannot be opened.
// The major technology selects the backend; the descriptor identifies the minor technologies.
virtual std::optional<std::vector<std::pair<std::string, std::string>>> read_descriptor(
Expand Down
102 changes: 54 additions & 48 deletions form/storage/storage_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include "form/config.hpp"
#include "storage/factories.hpp"
#include "storage/istorage.hpp"
#include "util/error.hpp"

#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <utility>
Expand Down Expand Up @@ -84,6 +84,42 @@ std::optional<std::vector<std::pair<std::string, std::string>>> storage_reader::
return create_file(technology, file_name, 'i')->read_descriptor();
}

i_storage_read_container& storage_reader::container_for(
token const& token, form::experimental::config::tech_setting_config const& settings)
{
auto const cont_key = std::make_pair(token.file_name(), token.container_name());
if (auto const found = read_containers_.find(cont_key); found != read_containers_.end()) {
return *found->second;
}
auto const& file = open_file(token.file_name(), token.technology(), settings);
auto container = create_read_container(token.technology(), token.container_name());
container->set_file(file);
for (auto const& [key, value] :
get_container_table(settings, token.technology(), token.container_name())) {
container->set_attribute(key, value);
}
// Cache the container only after its settings have been applied successfully.
return *read_containers_.emplace(cont_key, std::move(container)).first->second;
}

template <typename T>
std::vector<T> storage_reader::read_column(
token const& token, form::experimental::config::tech_setting_config const& settings)
{
auto& container = container_for(token, settings);
std::vector<T> values;
void const* raw = nullptr;
for (std::uint64_t row = 0; container.read(row, &raw, typeid(T)); ++row) {
std::unique_ptr<T const> const value{static_cast<T const*>(raw)};
Comment thread
wwuoneway marked this conversation as resolved.
Resolved
if (!value) {
error("row " + std::to_string(row) + " of container: " + token.container_name() +
" of file: " + token.file_name() + " was read but holds no value");
}
values.push_back(*value);
}
return values;
}

std::uint64_t storage_reader::get_index(
token const& token,
std::string const& id,
Expand All @@ -102,20 +138,7 @@ void storage_reader::prime(token const& token,
std::type_info const& type,
form::experimental::config::tech_setting_config const& settings)
{
auto cont_key = std::make_pair(token.file_name(), token.container_name());
auto cont = read_containers_.find(cont_key);
if (cont == read_containers_.end()) {
auto const& file = open_file(token.file_name(), token.technology(), settings);
cont = read_containers_
.insert({cont_key, create_read_container(token.technology(), token.container_name())})
.first;
cont->second->set_file(file);
for (auto const& [key, value] :
get_container_table(settings, token.technology(), token.container_name())) {
cont->second->set_attribute(key, value);
}
}
cont->second->prime(type);
container_for(token, settings).prime(type);
}

std::vector<std::string> storage_reader::list_indices(
Expand All @@ -137,27 +160,11 @@ std::map<std::string, std::uint64_t> const& storage_reader::index_map(
return built->second;
}

auto cont = read_containers_.find(cont_key);
if (cont == read_containers_.end()) {
auto const& file = open_file(token.file_name(), token.technology(), settings);
cont = read_containers_
.insert({cont_key, create_read_container(token.technology(), token.container_name())})
.first;
for (auto const& [key, value] :
get_container_table(settings, token.technology(), token.container_name())) {
cont->second->set_attribute(key, value);
}
cont->second->set_file(file);
}

std::map<std::string, std::uint64_t> index;
auto const& type = typeid(std::string);
std::uint64_t row = 0;
void const* raw_data = nullptr;
while (cont->second->read(row, &raw_data, type)) {
std::unique_ptr<std::string const> const id(static_cast<std::string const*>(raw_data));
if (!index.emplace(*id, row).second) {
error("Index id " + *id + " appears more than once in container: " + token.container_name() +
for (auto& id : read_column<std::string>(token, settings)) {
if (!index.try_emplace(std::move(id), row).second) {
error("Index id " + id + " appears more than once in container: " + token.container_name() +
" of file: " + token.file_name());
}
++row;
Expand All @@ -174,18 +181,17 @@ void storage_reader::read_container(token const& token,
std::type_info const& type,
form::experimental::config::tech_setting_config const& settings)
{
auto cont_key = std::make_pair(token.file_name(), token.container_name());
auto cont = read_containers_.find(cont_key);
if (cont == read_containers_.end()) {
auto const& file = open_file(token.file_name(), token.technology(), settings);
cont = read_containers_
.insert({cont_key, create_read_container(token.technology(), token.container_name())})
.first;
cont->second->set_file(file);
for (auto const& [key, value] :
get_container_table(settings, token.technology(), token.container_name())) {
cont->second->set_attribute(key, value);
}
}
cont->second->read(token.id(), data, type);
container_for(token, settings).read(token.id(), data, type);
}

std::vector<std::uint64_t> storage_reader::read_uint64_column(
token const& token, form::experimental::config::tech_setting_config const& settings)
{
return read_column<std::uint64_t>(token, settings);
}

std::vector<std::string> storage_reader::read_string_column(
token const& token, form::experimental::config::tech_setting_config const& settings)
{
return read_column<std::string>(token, settings);
}
10 changes: 10 additions & 0 deletions form/storage/storage_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace form::detail::experimental {
void const** data,
std::type_info const& type,
form::experimental::config::tech_setting_config const& settings) override;
std::vector<std::uint64_t> read_uint64_column(
token const& token, form::experimental::config::tech_setting_config const& settings) override;
std::vector<std::string> read_string_column(
token const& token, form::experimental::config::tech_setting_config const& settings) override;
std::optional<std::vector<std::pair<std::string, std::string>>> read_descriptor(
std::string const& file_name, form::technology::major technology) override;

Expand All @@ -43,6 +47,12 @@ namespace form::detail::experimental {
std::string const& file_name,
form::technology::id technology,
form::experimental::config::tech_setting_config const& settings);

i_storage_read_container& container_for(
token const& token, form::experimental::config::tech_setting_config const& settings);
template <typename T>
std::vector<T> read_column(token const& token,
form::experimental::config::tech_setting_config const& settings);
std::map<std::string, std::uint64_t> const& index_map(
token const& token, form::experimental::config::tech_setting_config const& settings);

Expand Down
11 changes: 11 additions & 0 deletions test/form/form_basics_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,17 @@ TEST_CASE("storage_reader basic operations", "[form]")
CHECK_THROWS_AS(storage->get_index(product_token, "[event:1]", settings), std::runtime_error);
}

TEST_CASE("storage_reader read columns: a backend without rows yields empty columns", "[form]")
{
auto reader = create_storage_reader();
REQUIRE(reader != nullptr);
form::experimental::config::tech_setting_config const settings;
token const column{"file.root", "table/column", form::technology::id{}};

CHECK(reader->read_uint64_column(column, settings).empty());
CHECK(reader->read_string_column(column, settings).empty());
}

TEST_CASE("storage_writer basic operations", "[form]")
{
auto storage = create_storage_writer();
Expand Down
Loading
Loading