takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
atomic_vector.hpp
Go to the documentation of this file.
1#ifndef TAKANE_ATOMIC_VECTOR_HPP
2#define TAKANE_ATOMIC_VECTOR_HPP
3
4#include <string>
5#include <stdexcept>
6#include <filesystem>
7
8#include "ritsuko/hdf5/hdf5.hpp"
9
10#include "utils_public.hpp"
11#include "utils_string.hpp"
12#include "utils_json.hpp"
13
19namespace takane {
20
25namespace atomic_vector {
26
32inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
33 const auto& vstring = internal_json::extract_version_for_type(metadata.other, "atomic_vector");
34 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
35 if (version.major != 1) {
36 throw std::runtime_error("unsupported version string '" + vstring + "'");
37 }
38
39 auto handle = ritsuko::hdf5::open_file(path / "contents.h5");
40 auto ghandle = ritsuko::hdf5::open_group(handle, "atomic_vector");
41 auto dhandle = ritsuko::hdf5::open_dataset(ghandle, "values");
42 auto vlen = ritsuko::hdf5::get_1d_length(dhandle.getSpace(), false);
43 auto type = ritsuko::hdf5::open_and_load_scalar_string_attribute(ghandle, "type");
44
45 const char* missing_attr_name = "missing-value-placeholder";
46
47 if (type == "string") {
48 if (!ritsuko::hdf5::is_utf8_string(dhandle)) {
49 throw std::runtime_error("expected a datatype for 'values' that can be represented by a UTF-8 encoded string");
50 }
51 auto missingness = ritsuko::hdf5::open_and_load_optional_string_missing_placeholder(dhandle, missing_attr_name);
52 std::string format = internal_string::fetch_format_attribute(ghandle);
53 internal_string::validate_string_format(dhandle, vlen, format, missingness.first, missingness.second, options.hdf5_buffer_size);
54
55 } else {
56 if (type == "integer") {
57 if (ritsuko::hdf5::exceeds_integer_limit(dhandle, 32, true)) {
58 throw std::runtime_error("expected a datatype for 'values' that fits in a 32-bit signed integer");
59 }
60 } else if (type == "boolean") {
61 if (ritsuko::hdf5::exceeds_integer_limit(dhandle, 32, true)) {
62 throw std::runtime_error("expected a datatype for 'values' that fits in a 32-bit signed integer");
63 }
64 } else if (type == "number") {
65 if (ritsuko::hdf5::exceeds_float_limit(dhandle, 64)) {
66 throw std::runtime_error("expected a datatype for 'values' that fits in a 64-bit float");
67 }
68 } else {
69 throw std::runtime_error("unsupported type '" + type + "'");
70 }
71
72 if (dhandle.attrExists(missing_attr_name)) {
73 auto missing_attr = dhandle.openAttribute(missing_attr_name);
74 ritsuko::hdf5::check_missing_placeholder_attribute(dhandle, missing_attr);
75 }
76 }
77
78 internal_string::validate_names(ghandle, "names", vlen, options.hdf5_buffer_size);
79}
80
87inline size_t height(const std::filesystem::path& path, [[maybe_unused]] const ObjectMetadata& metadata, [[maybe_unused]] Options& options) {
88 auto handle = ritsuko::hdf5::open_file(path / "contents.h5");
89 auto ghandle = handle.openGroup("atomic_vector");
90 auto dhandle = ghandle.openDataSet("values");
91 return ritsuko::hdf5::get_1d_length(dhandle.getSpace(), false);
92}
93
94}
95
96}
97
98#endif
size_t height(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition atomic_vector.hpp:87
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition atomic_vector.hpp:32
takane validation functions.
Definition _derived_from.hpp:15
Object metadata, including the type and other fields.
Definition utils_public.hpp:26
std::unordered_map< std::string, std::shared_ptr< millijson::Base > > other
Definition utils_public.hpp:35
Validation options.
Definition utils_public.hpp:94
hsize_t hdf5_buffer_size
Definition utils_public.hpp:103
Exported utilities.