@@ -83,4 +83,33 @@ object_path_factory::level_zero_path_to_epoch(std::string_view key) {
8383 return cluster_epoch (epoch);
8484}
8585
86+ std::expected<decltype (object_id::prefix), std::string>
87+ object_path_factory::level_zero_path_to_prefix (std::string_view key) {
88+ // find the level zero prefix and chop it off
89+ auto name = key;
90+ auto it = name.find (level_zero_data_dir_str);
91+ if (it == std::string_view::npos) {
92+ return std::unexpected (
93+ fmt::format (" L0 object name missing prefix: {}" , key));
94+ }
95+ name.remove_prefix (it + std::strlen (level_zero_data_dir_str));
96+
97+ if (name.size () < prefix_digits + 1 ) {
98+ return std::unexpected (
99+ fmt::format (" L0 object name is too short: {}" , key));
100+ }
101+ name = name.substr (0 , prefix_digits);
102+ name.remove_suffix (name.size () - prefix_digits);
103+
104+ // parse the prefix into a uint16_t
105+ decltype (object_id::prefix) pfx{0 };
106+ auto res = std::from_chars (name.data (), name.data () + name.size (), pfx);
107+ if (res.ptr != name.data () + name.size () || res.ec != std::errc{}) {
108+ return std::unexpected (
109+ fmt::format (" L0 object name has invalid prefix: {}" , key));
110+ }
111+
112+ return pfx;
113+ }
114+
86115} // namespace cloud_topics
0 commit comments