Everett
Loading...
Searching...
No Matches
object_path.h
Go to the documentation of this file.
1
13#pragma once
14
15#include <filesystem>
16#include <initializer_list>
17#include <stdexcept>
18#include <string>
19#include <string_view>
20#include <utility>
21
22namespace everett {
23 // The immutable file layer owns data and fractional indexes. World roots,
24 // pins and merge progress belong to the separate metadata backend.
26
27 inline std::string_view file_extension(file_kind kind) {
28 switch (kind) {
29 case file_kind::native_blob: return ".kv";
30 case file_kind::fractional_index: return ".index";
31 }
32 throw std::invalid_argument("unsupported Everett file kind");
33 }
34
35 // Canonical representation of a physical 128-bit object identity. The
36 // allocator supplies well-distributed IDs; this type validates spelling,
37 // not entropy or uniqueness. Logical keys never enter the path builder.
38 struct object_id {
39 explicit object_id(std::string hex) : hex_(std::move(hex)) {
40 if (hex_.size() != 32) throw std::invalid_argument("object ID must contain 32 lowercase hex digits");
41 for (char c : hex_)
42 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
43 throw std::invalid_argument("noncanonical object ID");
44 }
45 static object_id from_hex(std::string_view hex) { return object_id(std::string(hex)); }
46 std::string const & hex() const noexcept { return hex_; }
47 bool operator==(object_id const &) const = default;
48 private:
49 std::string hex_;
50 };
51
52 inline std::filesystem::path object_path(object_id const & id, file_kind kind) {
53 auto const & hex = id.hex();
54 return std::filesystem::path(hex.substr(0, 2)) / hex.substr(2, 2) /
55 (hex.substr(4) + std::string(file_extension(kind)));
56 }
57
62
63 // Parse the canonical generic relative spelling before filesystem lexical
64 // normalization: ab/cd/<remaining 28 hex digits>.kv (or .index).
65 inline parsed_object_path parse_object_path(std::string_view path) {
66 if (path.size() < 37 || path.size() > 40 || path[2] != '/' || path[5] != '/' || path[34] != '.')
67 throw std::invalid_argument("noncanonical sharded object path");
68 auto id = object_id(std::string(path.substr(0, 2)) + std::string(path.substr(3, 2)) +
69 std::string(path.substr(6, 28)));
70 auto extension = path.substr(34);
72 if (extension == file_extension(kind)) return {std::move(id), kind};
73 throw std::invalid_argument("unsupported Everett object extension");
74 }
75}
Definition active_engine.h:18
parsed_object_path parse_object_path(std::string_view path)
Definition object_path.h:65
std::string_view file_extension(file_kind kind)
Definition object_path.h:27
std::filesystem::path object_path(object_id const &id, file_kind kind)
Definition object_path.h:52
file_kind
Definition object_path.h:25
Definition object_path.h:38
object_id(std::string hex)
Definition object_path.h:39
std::string const & hex() const noexcept
Definition object_path.h:46
std::string hex_
Definition object_path.h:49
bool operator==(object_id const &) const =default
static object_id from_hex(std::string_view hex)
Definition object_path.h:45
Definition object_path.h:58
file_kind kind
Definition object_path.h:60
object_id id
Definition object_path.h:59