Everett
Loading...
Searching...
No Matches
native_file_writer.h
Go to the documentation of this file.
1
13#pragma once
14
16
17namespace everett {
18 namespace profile_detail {
19 template <class P, class Ops = posix_object_ops>
21 }
22
23 // Checked incremental native output to a private immutable-object attempt.
24 // Retains only the preceding full key, bounded FC/control scratch, and block
25 // offsets; values are consumed synchronously. Invalid input is retryable.
26 // Output I/O failure poisons this attempt and preserves surviving names.
27 // Finalization builds EF and seals the existing portable native container;
28 // this is not a durable partial-output checkpoint or a resumable job.
29 template <class P, class Ops = posix_object_ops> struct native_file_writer {
30 using policy_type = P;
31 native_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt,
32 std::optional<std::uint64_t> common = P::value_width)
33 : output_(std::move(root), std::move(id), std::move(attempt), common) {}
34 native_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt,
35 std::optional<std::uint64_t> common, Ops & ops)
36 : output_(std::move(root), std::move(id), std::move(attempt), common, ops) {}
41 std::uint64_t size() const noexcept { return output_.size(); }
42 bool failed() const noexcept { return output_.failed(); }
43 bool finished() const noexcept { return output_.finished(); }
44 std::optional<std::uint64_t> common_value_width() const noexcept { return output_.common_value_width(); }
45 object_write_paths const & paths() const & noexcept { return output_.paths(); }
46 object_write_paths const & paths() const && = delete;
47
48 void append(bit_view key, bit_view value, std::optional<std::uint64_t> retained_limit_bits = {}) {
49 output_.require_active();
50 if ((key.size() & (P::bits_per_unit - 1)) || (value.size() & (P::bits_per_unit - 1)))
51 error_detail::raise<std::invalid_argument>("native file record disagrees with policy units");
52 auto common = output_.common_value_width();
53 if (common && (value.size() >> P::unit_shift) != *common)
54 error_detail::raise<std::invalid_argument>("native file value disagrees with common width");
55 auto comparison = compare_common_bits<typename P::architecture>(previous_.view(), key);
56 if (size() && comparison.order >= 0)
57 error_detail::raise<std::invalid_argument>("native file keys must be strictly increasing");
58 auto retained = std::min(comparison.common_bits, retained_limit_bits.value_or(comparison.common_bits)) >> P::unit_shift;
59 auto retained_bits = retained << P::unit_shift;
60 if (key.size() > std::numeric_limits<std::uint64_t>::max() - 7)
61 error_detail::raise<std::length_error>("native file key is too large");
62 auto bytes = profile_detail::byte_count(key.size());
63 if (bytes > previous_.bytes.max_size())
64 error_detail::raise<std::length_error>("native file key is too large");
65 if (bytes > previous_.bytes.capacity()) {
66 auto capacity = previous_.bytes.capacity(), maximum = previous_.bytes.max_size();
67 auto grown = capacity > maximum / 2 ? maximum : 2 * capacity;
68 previous_.bytes.reserve(std::max(static_cast<std::size_t>(bytes), grown));
69 }
70 auto literal = key.subview(retained_bits, key.size() - retained_bits);
71 output_.append(retained, literal, value);
73 profile_detail::copy_into(previous_, retained_bits, literal);
74 }
75 void append(profile_record const & record) { append(record.key.view(), record.value.view(), record.retained_limit_bits); }
77 auto receipt = output_.finish(); previous_ = {}; return receipt;
78 }
79 private:
82 };
83}
std::uint64_t byte_count(std::uint64_t bits) noexcept
Definition profile.h:50
void resize(bit_string &value, std::uint64_t bits)
Definition profile.h:465
void copy_into(bit_string &target, std::uint64_t first, bit_view source)
Definition profile.h:474
Definition active_engine.h:18
Streams native and borrowed profile sections with bounded payload buffering.
Definition profile.h:166
std::vector< std::byte > bytes
Definition profile.h:167
bit_view view() const &
Definition profile.h:178
Definition profile.h:56
Definition native_file_writer.h:29
native_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt, std::optional< std::uint64_t > common, Ops &ops)
Definition native_file_writer.h:34
profile_detail::native_file_output< P, Ops > output_
Definition native_file_writer.h:80
object_write_paths const & paths() const &&=delete
bool finished() const noexcept
Definition native_file_writer.h:43
object_write_paths const & paths() const &noexcept
Definition native_file_writer.h:45
object_seal_receipt finish()
Definition native_file_writer.h:76
bool failed() const noexcept
Definition native_file_writer.h:42
void append(profile_record const &record)
Definition native_file_writer.h:75
native_file_writer & operator=(native_file_writer const &)=delete
native_file_writer(native_file_writer const &)=delete
native_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt, std::optional< std::uint64_t > common=P::value_width)
Definition native_file_writer.h:31
native_file_writer(native_file_writer &&)=delete
bit_string previous_
Definition native_file_writer.h:81
std::uint64_t size() const noexcept
Definition native_file_writer.h:41
std::optional< std::uint64_t > common_value_width() const noexcept
Definition native_file_writer.h:44
P policy_type
Definition native_file_writer.h:30
native_file_writer & operator=(native_file_writer &&)=delete
void append(bit_view key, bit_view value, std::optional< std::uint64_t > retained_limit_bits={})
Definition native_file_writer.h:48
Definition object_writer.h:39
Definition object_path.h:38
Definition object_writer.h:88
Definition object_writer.h:53
Definition profile_file_output.h:44
Definition profile.h:294
std::optional< std::uint64_t > retained_limit_bits
Definition profile.h:299
bit_string key
Definition profile.h:295
bit_string value
Definition profile.h:296