18 namespace sort_profile_detail {
19 template <
class P,
class Ops,
class Stream =
object_stream<P, Ops>>
struct file_bit_sink {
33 stream_.append(std::span(
buffer_).first(
static_cast<std::size_t
>(bytes)));
39 if (next > std::numeric_limits<std::uint64_t>::max() - 7)
40 throw std::length_error(
"sort file bit extent");
41 while (!source.
empty()) {
44 auto bytes = source.
size() >> 3;
46 static_cast<std::size_t
>(bytes)));
47 source = source.
subview(bytes << 3, source.
size() - (bytes << 3));
58 if (width > 64 || (width < 64 && (value >> width)))
throw std::invalid_argument(
"sort file bit field");
59 std::array<std::byte, 8> bytes{};
61 auto aligned = value << (64 - width);
62 for (
unsigned i = 0; i != 8; ++i) bytes[i] = std::byte(aligned >> (56 - (i << 3)));
70 auto quotient = value >> code::backspace_parameter;
71 if (quotient == std::numeric_limits<std::uint64_t>::max()) {
74 auto prefix = quotient + 1;
auto width = unsigned(std::bit_width(prefix));
77 if constexpr (code::backspace_parameter)
78 write_bits(value & ((std::uint64_t{1} << code::backspace_parameter) - 1), unsigned(code::backspace_parameter));
80 constexpr auto modulus = code::backspace_parameter;
81 constexpr auto width = unsigned(std::bit_width(modulus - 1));
82 constexpr auto cutoff = profile_detail::golomb_cutoff<code>();
84 auto remainder = value % modulus;
85 if (remainder < cutoff)
write_bits(remainder, width - 1);
92 static constexpr std::array<std::byte, 8> zero{};
93 stream_.append(std::span(zero).first(
static_cast<std::size_t
>((0 -
stream_.body_bytes()) & 7)));
95 void words(std::span<std::uint64_t const> input) {
96 if constexpr (std::endian::native == std::endian::little)
stream_.append(std::as_bytes(input));
97 else while (!input.empty()) {
100 stream_.append(std::span(
buffer_).first(count << 3)); input = input.subspan(count);
103 void samples(std::span<elias_fano_sample const> input) {
105 if constexpr (std::endian::native == std::endian::little)
stream_.append(std::as_bytes(input));
106 else while (!input.empty()) {
108 for (std::size_t i = 0; i != count; ++i) {
112 stream_.append(std::span(
buffer_).first(count << 4)); input = input.subspan(count);
117 std::array<std::byte, buffer_bytes>
buffer_{};
120 static constexpr std::array<std::byte, 4096> zero{};
122 auto part = std::min<std::uint64_t>(count, zero.size() * 8);
append(
bit_view(zero, part)); count -= part;
133 template <
class P,
class Selector,
class Sink,
class Stream>
136 std::array<std::byte, 192> directory{};
137 for (
unsigned i = 0; i != 4; ++i) directory[i] = std::byte(
"KV03"[i]);
144 directory[48] = std::byte(ef.low_width);
149 std::uint64_t end = directory.size();
150 for (std::size_t i = 0; i != lengths.size(); ++i) {
156 sink.finish_payload();
157 sink.align(); sink.words(ef.low);
158 sink.align(); sink.words(ef.high);
159 sink.align(); sink.samples(ef.samples);
160 sink.align(); sink.words(ef.sparse);
164 return stream.finish(header, directory);
172 template <
class P,
class Selector = registry_selector<
typename P::registry_type>,
class Ops = posix_
object_ops>
191 std::uint64_t common,
bit_view value) {
194 catch (...) {
failed_ =
true;
throw; }
200 if (
frame_only_)
throw std::logic_error(
"encoded append after trusted frame stream");
201 auto comparison = compare_common_bits<typename P::architecture>(
previous_.
view(), key);
202 if (
size() && comparison.order >= 0)
throw std::invalid_argument(
"sort file keys must be strictly ordered");
205 Selector::select(input, [&]<
class S>(std::type_identity<S>,
auto &) {
206 std::array<bit_view, 1> spans{key};
207 encoder_.template append<S>(
sink_, key.prefix(input.position()), input.remaining(), spans, value, comparison.common_bits, retained_limit_bits);
210 }
catch (...) {
failed_ =
true;
throw; }
218 }
catch (...) {
failed_ =
true;
throw; }
void put(std::span< std::byte > bytes, std::size_t at, unsigned width, std::uint64_t value) noexcept
Definition file.h:70
void validate_metadata(file_header< P > const &header)
Definition file.h:93
std::uint64_t add(std::uint64_t a, std::uint64_t b)
Definition profile.h:39
std::uint64_t multiply(std::uint64_t a, std::uint64_t b)
Definition profile.h:44
std::uint64_t byte_count(std::uint64_t bits) noexcept
Definition profile.h:50
void copy_bits(std::byte *target, std::uint64_t first, bit_view source) noexcept
Definition profile.h:123
object_seal_receipt seal(encoder< P, Selector > const &encoder, Sink &sink, Stream &stream)
Definition sort_profile_file_writer.h:134
Definition active_engine.h:18
file_kind
Definition object_path.h:25
Appends immutable object bodies before their final envelope is known.
Navigates sort-owned bit records using shared sort seeds and sparse offsets.
std::vector< std::byte > bytes
Definition profile.h:167
bit_view view() const &
Definition profile.h:178
static bit_string copy(bit_view source)
Definition profile.h:181
std::uint64_t bit_size
Definition profile.h:168
std::span< std::byte const > storage() const noexcept
Definition profile.h:67
std::uint64_t size() const noexcept
Definition profile.h:64
std::uint64_t offset() const noexcept
Definition profile.h:66
bool empty() const noexcept
Definition profile.h:65
bit_view subview(std::uint64_t first, std::uint64_t count) const
Definition profile.h:73
bit_view prefix(std::uint64_t count) const
Definition profile.h:80
Definition elias_fano.h:195
Definition object_writer.h:39
Definition object_path.h:38
Definition object_writer.h:88
Definition object_stream.h:32
object_write_paths const & paths() const &noexcept
Definition object_stream.h:62
bool failed() const noexcept
Definition object_stream.h:60
bool finished() const noexcept
Definition object_stream.h:61
Definition object_writer.h:53
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
Definition sort_codec.h:56
Definition sort_codec.h:26
Definition sort_profile.h:443
bit_string dictionary
Definition sort_profile.h:447
profile_metadata metadata
Definition sort_profile.h:446
std::vector< std::uint64_t > dictionary_offsets
Definition sort_profile.h:448
elias_fano offsets
Definition sort_profile.h:449
bit_string seeds
Definition sort_profile.h:447
std::uint64_t size() const noexcept
Definition sort_profile.h:450
Definition sort_profile_file_writer.h:19
void write_bits(std::uint64_t value, unsigned width)
Definition sort_profile_file_writer.h:57
void samples(std::span< elias_fano_sample const > input)
Definition sort_profile_file_writer.h:103
std::uint64_t position() const noexcept
Definition sort_profile_file_writer.h:22
std::uint64_t bits_
Definition sort_profile_file_writer.h:118
bit_view buffered_payload() const &&=delete
bit_view buffered_payload() const &noexcept
Definition sort_profile_file_writer.h:26
std::uint64_t buffered_bits() const noexcept
Definition sort_profile_file_writer.h:23
std::array< std::byte, buffer_bytes > buffer_
Definition sort_profile_file_writer.h:117
void write_count(std::uint64_t value)
Definition sort_profile_file_writer.h:66
void finish_payload()
Definition sort_profile_file_writer.h:90
void zeroes(std::uint64_t count)
Definition sort_profile_file_writer.h:119
void append(bit_view source)
Definition sort_profile_file_writer.h:37
void align()
Definition sort_profile_file_writer.h:91
std::uint64_t buffered_
Definition sort_profile_file_writer.h:118
file_bit_sink(Stream &stream)
Definition sort_profile_file_writer.h:21
void flush_complete()
Definition sort_profile_file_writer.h:30
Stream & stream_
Definition sort_profile_file_writer.h:116
static constexpr std::size_t buffer_bytes
Definition sort_profile_file_writer.h:20
void words(std::span< std::uint64_t const > input)
Definition sort_profile_file_writer.h:95
void flush()
Definition sort_profile_file_writer.h:125
Definition sort_profile_file_writer.h:173
void append_encoded(bit_view key, bit_view value, std::optional< std::uint64_t > retained_limit_bits={})
Definition sort_profile_file_writer.h:198
static constexpr std::size_t buffer_bytes
Definition sort_profile_file_writer.h:175
void append_frame(sort_profile_frame const &frame, std::span< bit_view const > key, std::uint64_t common, bit_view value)
Definition sort_profile_file_writer.h:190
object_write_paths const & paths() const &&=delete
void require_active() const
Definition sort_profile_file_writer.h:226
std::uint64_t size() const noexcept
Definition sort_profile_file_writer.h:184
auto common_value_width() const noexcept
Definition sort_profile_file_writer.h:187
sort_profile_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt, Ops &ops)
Definition sort_profile_file_writer.h:178
bit_string previous_
Definition sort_profile_file_writer.h:224
sort_profile_file_writer & operator=(sort_profile_file_writer &&)=delete
sort_profile_file_writer(std::filesystem::path root, object_id id, object_attempt_id attempt)
Definition sort_profile_file_writer.h:176
bool finished() const noexcept
Definition sort_profile_file_writer.h:186
sort_profile_file_writer & operator=(sort_profile_file_writer const &)=delete
object_seal_receipt finish()
Definition sort_profile_file_writer.h:213
sort_profile_detail::encoder< P, Selector > encoder_
Definition sort_profile_file_writer.h:223
sort_profile_file_writer(sort_profile_file_writer &&)=delete
void append_encoded(profile_record const &record)
Definition sort_profile_file_writer.h:212
sort_profile_file_writer(sort_profile_file_writer const &)=delete
object_write_paths const & paths() const &noexcept
Definition sort_profile_file_writer.h:188
bool frame_only_
Definition sort_profile_file_writer.h:225
object_stream< P, Ops > stream_
Definition sort_profile_file_writer.h:221
sort_profile_detail::file_bit_sink< P, Ops > sink_
Definition sort_profile_file_writer.h:222
bool failed() const noexcept
Definition sort_profile_file_writer.h:185
bool failed_
Definition sort_profile_file_writer.h:225
P policy_type
Definition sort_profile_file_writer.h:174
Definition sort_profile.h:163