diff --git a/form/root_storage/root_rfield_read_container.cpp b/form/root_storage/root_rfield_read_container.cpp index 9f9719c6e..bdc3f2823 100644 --- a/form/root_storage/root_rfield_read_container.cpp +++ b/form/root_storage/root_rfield_read_container.cpp @@ -78,9 +78,7 @@ namespace form::detail::experimental { } } - if (!view_) { - create_view(type); - } + ensure_view(type); if (!TDictionary::GetDictionary(type)) { error("unsupported type"); @@ -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; @@ -144,6 +140,16 @@ namespace form::detail::experimental { return static_cast(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 @@ -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> view; try { - view_ = - std::make_unique>(reader_->GetView(col_name(), nullptr, type)); + view = std::make_unique>(reader_->GetView(col_name(), nullptr, type)); } catch (ROOT::RException const& e) { //RNTupleView 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>(reader_->GetView(col_name(), nullptr, "")); + view = std::make_unique>(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; } } diff --git a/form/root_storage/root_rfield_read_container.hpp b/form/root_storage/root_rfield_read_container.hpp index 724175970..0e70094de 100644 --- a/form/root_storage/root_rfield_read_container.hpp +++ b/form/root_storage/root_rfield_read_container.hpp @@ -8,6 +8,7 @@ #include #include #include +#include // NOLINTBEGIN(readability-identifier-naming) // Forward declarations of ROOT classes @@ -44,7 +45,11 @@ namespace form::detail::experimental { std::shared_ptr tfile_; std::unique_ptr reader_; std::unique_ptr> 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); }; } diff --git a/form/storage/istorage.hpp b/form/storage/istorage.hpp index 22173f2d1..a14f0f7ac 100644 --- a/form/storage/istorage.hpp +++ b/form/storage/istorage.hpp @@ -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 read_uint64_column( + token const& token, form::experimental::config::tech_setting_config const& settings) = 0; + virtual std::vector 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>> read_descriptor( diff --git a/form/storage/storage_reader.cpp b/form/storage/storage_reader.cpp index 970c88cd5..c49693c24 100644 --- a/form/storage/storage_reader.cpp +++ b/form/storage/storage_reader.cpp @@ -7,13 +7,13 @@ #include "form/config.hpp" #include "storage/factories.hpp" #include "storage/istorage.hpp" +#include "util/error.hpp" #include #include #include #include #include -#include #include #include #include @@ -84,6 +84,42 @@ std::optional>> 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 +std::vector storage_reader::read_column( + token const& token, form::experimental::config::tech_setting_config const& settings) +{ + auto& container = container_for(token, settings); + std::vector values; + void const* raw = nullptr; + for (std::uint64_t row = 0; container.read(row, &raw, typeid(T)); ++row) { + std::unique_ptr const value{static_cast(raw)}; + 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, @@ -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 storage_reader::list_indices( @@ -137,27 +160,11 @@ std::map 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 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 const id(static_cast(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(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; @@ -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 storage_reader::read_uint64_column( + token const& token, form::experimental::config::tech_setting_config const& settings) +{ + return read_column(token, settings); +} + +std::vector storage_reader::read_string_column( + token const& token, form::experimental::config::tech_setting_config const& settings) +{ + return read_column(token, settings); } diff --git a/form/storage/storage_reader.hpp b/form/storage/storage_reader.hpp index 3fd5a9759..28c9a44c8 100644 --- a/form/storage/storage_reader.hpp +++ b/form/storage/storage_reader.hpp @@ -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 read_uint64_column( + token const& token, form::experimental::config::tech_setting_config const& settings) override; + std::vector read_string_column( + token const& token, form::experimental::config::tech_setting_config const& settings) override; std::optional>> read_descriptor( std::string const& file_name, form::technology::major technology) override; @@ -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 + std::vector read_column(token const& token, + form::experimental::config::tech_setting_config const& settings); std::map const& index_map( token const& token, form::experimental::config::tech_setting_config const& settings); diff --git a/test/form/form_basics_test.cpp b/test/form/form_basics_test.cpp index 857bdfd37..932780ad3 100644 --- a/test/form/form_basics_test.cpp +++ b/test/form/form_basics_test.cpp @@ -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(); diff --git a/test/form/form_storage_test.cpp b/test/form/form_storage_test.cpp index 33630b008..799895be6 100644 --- a/test/form/form_storage_test.cpp +++ b/test/form/form_storage_test.cpp @@ -9,6 +9,7 @@ #include "form/config.hpp" #include "persistence/ipersistence_reader.hpp" #include "persistence/ipersistence_writer.hpp" +#include "persistence/navigation_naming.hpp" #include "persistence/persistence_reader.hpp" #include "root_storage/root_descriptor.hpp" #include "root_storage/root_tfile.hpp" @@ -1007,6 +1008,106 @@ TEST_CASE("storage_reader get_index: the job cell resolves through the index", " CHECK(read_cell(reader, file_name, creator, "[]").empty()); } +TEST_CASE("storage_reader read columns: navigation table and product dictionary", "[form]") +{ + std::string const file_name = + "storage_reader_columns_" + form::technology::to_string(technology) + ".root"; + std::string const creator = "columns_creator"; + // Written out of cell order; the navigation table is ordered by layer values. + std::vector const cells{make_cell(3, 1), make_cell(1, 2), make_cell(2, 0)}; + write_cells(file_name, creator, cells); + + storage_reader reader; + form::experimental::config::tech_setting_config const settings{}; + auto const column = [&file_name](std::string const& container, std::string const& name) { + return token{file_name, build_full_label(container, name), technology}; + }; + + SECTION("navigation table") + { + auto const table = navigation_table_name(cells.front().hierarchy, technology); + auto const events = reader.read_uint64_column(column(table, "event"), settings); + auto const segments = reader.read_uint64_column(column(table, "segment"), settings); + auto const rows = reader.read_uint64_column( + column(table, navigation_row_column(creator, test_stage)), settings); + + CHECK(events == std::vector{1, 2, 3}); + CHECK(segments == std::vector{2, 0, 1}); + // Each cell's payload row is the position it was written at. + CHECK(rows == std::vector{1, 2, 0}); + + // The navigated row holds the payload of the cell named by the layer columns. + for (std::size_t i = 0; i != rows.size(); ++i) { + void const* raw = nullptr; + reader.read_container( + token{file_name, build_full_label(row_space(creator), "prod"), technology, rows[i]}, + &raw, + typeid(std::vector), + settings); + std::unique_ptr const> const payload{ + static_cast const*>(raw)}; + REQUIRE(payload != nullptr); + CHECK(*payload == + std::vector{static_cast(events[i]), static_cast(segments[i])}); + } + } + + SECTION("product dictionary") + { + auto const dictionary = navigation_dictionary_name(technology); + auto const entry = [&](std::string const& name) { + return reader.read_string_column(column(dictionary, name), settings); + }; + using strings = std::vector; + CHECK(entry("product_name") == strings{"prod"}); + CHECK(entry("creator") == strings{creator}); + CHECK(entry("stage") == strings{test_stage}); + CHECK(entry("container_name") == strings{build_full_label(row_space(creator), "prod")}); + CHECK(entry("hierarchy_key") == strings{"event_segment"}); + CHECK(entry("navigation_container") == + strings{navigation_table_name(cells.front().hierarchy, technology)}); + CHECK(entry("navigation_column") == strings{navigation_row_column(creator, test_stage)}); + } + + SECTION("index container") + { + auto const index = column(row_space(creator), "index"); + CHECK(reader.read_string_column(index, settings) == ids_of(cells)); + CHECK(reader.list_indices(index, settings) == ids_of(cells)); + } + + SECTION("a column is read only as the type it stores") + { + auto const table = navigation_table_name(cells.front().hierarchy, technology); + auto const numbers = column(table, "event"); + auto const names = column(navigation_dictionary_name(technology), "product_name"); + std::vector const events{1, 2, 3}; + + // A matching read first, then a mismatched one, then the matching type again. + storage_reader first_match; + CHECK(first_match.read_uint64_column(numbers, settings) == events); + CHECK_THROWS(first_match.read_string_column(numbers, settings)); + CHECK(first_match.read_uint64_column(numbers, settings) == events); + CHECK(first_match.read_string_column(names, settings) == std::vector{"prod"}); + CHECK_THROWS(first_match.read_uint64_column(names, settings)); + CHECK(first_match.read_string_column(names, settings) == std::vector{"prod"}); + + // A mismatched read first must not leave anything behind for a later read. + storage_reader first_mismatch; + CHECK_THROWS(first_mismatch.read_string_column(numbers, settings)); + CHECK(first_mismatch.read_uint64_column(numbers, settings) == events); + CHECK_THROWS(first_mismatch.read_uint64_column(names, settings)); + CHECK(first_mismatch.read_string_column(names, settings) == std::vector{"prod"}); + } + + SECTION("a missing table or column throws") + { + auto const table = navigation_table_name(cells.front().hierarchy, technology); + CHECK_THROWS(reader.read_uint64_column(column(table, "no_such_column"), settings)); + CHECK_THROWS(reader.read_string_column(column("nav_no_such_table", "product_name"), settings)); + } +} + TEST_CASE("storage_reader get_index: index maps are kept per file", "[form]") { auto const tech = form::technology::to_string(technology);