Everett
Loading...
Searching...
No Matches
file_index_pipeline.h
Go to the documentation of this file.
1
13#pragma once
14
17#include <everett/mapped_blob.h>
18
19#include <filesystem>
20#include <memory>
21#include <span>
22#include <stdexcept>
23#include <unordered_set>
24#include <utility>
25#include <vector>
26
27namespace everett {
28 // A reserved new index over an existing immutable native object. The caller
29 // authenticates that source belongs to identity.native, as for mapped_blob.
30 template <class P> struct file_index_stage {
31 std::shared_ptr<mapped_native<P> const> source;
34 };
35
36 // Stages are ordered nearest the existing target first, new head last.
37 // Sample the exact mapped target once and pass front-coded samples directly
38 // between stages. All native objects are retained unchanged. Each stage has
39 // bounded payload buffering, current key contexts and compact navigation.
40 //
41 // Construction opens private attempts under caller-reserved identities.
42 // step budgets the same entry events as index_pipeline. After it completes,
43 // seal_next finalizes one stage, target outward. It does not publish a catalog
44 // root: record each receipt and register the dependencies before publication.
45 // Failures preserve earlier receipts and surviving names for reconciliation.
46 // A supplied Ops owner must outlive this nonmovable pipeline.
47 template <class P, class Ops = posix_object_ops>
49 : index_detail::pipeline_driver<P, mapped_blob<P>, file_index_builder<P, mapped_native<P>, Ops>> {
50 using policy_type = P;
52 using pair_type = std::shared_ptr<blob_type const>;
54
55 file_index_pipeline(std::filesystem::path root, pair_type target, std::vector<stage_type> stages)
56 : base(target, make_stages<false>(root, target, stages, nullptr)),
57 identities_(identities(stages)), head_(identities_.empty() ? target->identity() : identities_.back()) {
58 receipts_.reserve(stages.size());
59 }
60 file_index_pipeline(std::filesystem::path root, pair_type target, std::vector<stage_type> stages, Ops & ops)
61 : base(target, make_stages<true>(root, target, stages, &ops)),
62 identities_(identities(stages)), head_(identities_.empty() ? target->identity() : identities_.back()) {
63 receipts_.reserve(stages.size());
64 }
69
70 // Returns true after sealing one object, false when all are already sealed.
71 // The returned receipt is appended to completed_receipts before success.
72 bool seal_next() {
73 if (this->failed_) error_detail::raise<std::logic_error>("file index pipeline has failed");
74 if (!this->done()) error_detail::raise<std::logic_error>("file index pipeline still has input");
75 auto i = receipts_.size();
76 if (i == this->stages_.size()) { this->finished_ = true; return false; }
77 try {
78 auto count = i ? this->stages_[i - 1]->size() : this->target_->virtual_size();
79 receipts_.push_back(this->stages_[i]->finish(count));
80 this->finished_ = receipts_.size() == this->stages_.size();
81 return true;
82 } catch (...) {
83 this->failed_ = true;
84 throw;
85 }
86 }
87
88 std::span<object_seal_receipt const> finish() & {
89 while (seal_next()) {}
90 return receipts_;
91 }
92 std::span<object_seal_receipt const> finish() && = delete;
93 std::span<object_seal_receipt const> completed_receipts() const & noexcept { return receipts_; }
94 std::span<object_seal_receipt const> completed_receipts() const && = delete;
95 std::span<blob_identity const> stage_identities() const & noexcept { return identities_; }
96 std::span<blob_identity const> stage_identities() const && = delete;
97 blob_identity const & planned_head() const & noexcept { return head_; }
98 blob_identity const & planned_head() const && = delete;
99 object_write_paths const & paths(std::size_t stage) const & {
100 if (stage >= this->stages_.size())
101 error_detail::raise<std::out_of_range>("file index pipeline stage is out of range");
102 return this->stages_[stage]->paths();
103 }
104 object_write_paths const & paths(std::size_t) const && = delete;
105
106 private:
109 std::vector<blob_identity> identities_;
111 std::vector<object_seal_receipt> receipts_;
112
113 static std::vector<blob_identity> identities(std::span<stage_type const> stages) {
114 std::vector<blob_identity> result;
115 result.reserve(stages.size());
116 for (auto const & stage : stages) result.push_back(stage.identity);
117 return result;
118 }
119 template <bool ExternalOps>
120 static std::vector<std::unique_ptr<builder_type>> make_stages(std::filesystem::path const & root,
121 pair_type const & target, std::span<stage_type const> stages, Ops * ops) {
122 if (!target) error_detail::raise<std::invalid_argument>("null file index pipeline target");
123 std::unordered_set<std::string> ids;
124 ids.insert(target->identity().index.hex());
125 for (auto const & stage : stages) {
126 if (!stage.source || stage.identity.native.hex().size() != 32 ||
127 stage.identity.index.hex().size() != 32 || stage.attempt.hex().size() != 32)
128 error_detail::raise<std::invalid_argument>("invalid file index pipeline stage");
129 if (!ids.insert(stage.identity.index.hex()).second)
130 error_detail::raise<std::invalid_argument>("file index pipeline reuses an index identity");
131 }
132 std::vector<std::unique_ptr<builder_type>> result;
133 result.reserve(stages.size());
134 auto downstream = target->identity();
135 for (auto const & stage : stages) {
136 if constexpr (ExternalOps) result.push_back(std::make_unique<builder_type>(root, stage.identity.index, stage.attempt,
137 stage.source, stage.identity.native, downstream, *ops));
138 else result.push_back(std::make_unique<builder_type>(root, stage.identity.index, stage.attempt,
139 stage.source, stage.identity.native, downstream));
140 downstream = stage.identity;
141 }
142 return result;
143 }
144 };
145}
Builds fractional indexes over pinned native storage into immutable files.
Shares bounded front-coded sample handoff between index pipelines.
Binds immutable mapped blobs to exact dependency chains and validates their samples.
Definition active_engine.h:18
Definition sections.h:34
Definition file_index_builder.h:87
Definition file_index_pipeline.h:49
blob_identity const & planned_head() const &&=delete
object_write_paths const & paths(std::size_t) const &&=delete
std::span< object_seal_receipt const > finish() &
Definition file_index_pipeline.h:88
file_index_pipeline(std::filesystem::path root, pair_type target, std::vector< stage_type > stages)
Definition file_index_pipeline.h:55
static std::vector< std::unique_ptr< builder_type > > make_stages(std::filesystem::path const &root, pair_type const &target, std::span< stage_type const > stages, Ops *ops)
Definition file_index_pipeline.h:120
object_write_paths const & paths(std::size_t stage) const &
Definition file_index_pipeline.h:99
bool seal_next()
Definition file_index_pipeline.h:72
file_index_pipeline & operator=(file_index_pipeline const &)=delete
std::span< object_seal_receipt const > completed_receipts() const &noexcept
Definition file_index_pipeline.h:93
static std::vector< blob_identity > identities(std::span< stage_type const > stages)
Definition file_index_pipeline.h:113
file_index_pipeline(file_index_pipeline const &)=delete
file_index_pipeline & operator=(file_index_pipeline &&)=delete
P policy_type
Definition file_index_pipeline.h:50
blob_identity head_
Definition file_index_pipeline.h:110
std::vector< blob_identity > identities_
Definition file_index_pipeline.h:109
file_index_pipeline(file_index_pipeline &&)=delete
std::vector< object_seal_receipt > receipts_
Definition file_index_pipeline.h:111
blob_identity const & planned_head() const &noexcept
Definition file_index_pipeline.h:97
std::span< object_seal_receipt const > finish() &&=delete
std::shared_ptr< blob_type const > pair_type
Definition file_index_pipeline.h:52
std::span< blob_identity const > stage_identities() const &&=delete
file_index_pipeline(std::filesystem::path root, pair_type target, std::vector< stage_type > stages, Ops &ops)
Definition file_index_pipeline.h:60
std::span< object_seal_receipt const > completed_receipts() const &&=delete
std::span< blob_identity const > stage_identities() const &noexcept
Definition file_index_pipeline.h:95
Definition file_index_pipeline.h:30
blob_identity identity
Definition file_index_pipeline.h:32
std::shared_ptr< mapped_native< P > const > source
Definition file_index_pipeline.h:31
object_attempt_id attempt
Definition file_index_pipeline.h:33
Definition index_pipeline_detail.h:39
std::vector< std::unique_ptr< Stage > > stages_
Definition index_pipeline_detail.h:93
bool finished_
Definition index_pipeline_detail.h:98
pair_type target_
Definition index_pipeline_detail.h:90
bool done() const noexcept
Definition index_pipeline_detail.h:57
bool failed_
Definition index_pipeline_detail.h:99
Definition mapped_blob.h:31
Definition object_writer.h:39
Definition object_writer.h:53