Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
output_directory.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//output_directory.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//output_directory.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with output_directory.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
39
40#pragma once
41
42#include <cstddef>
43#include <cstdint>
44#include <filesystem>
45#include <fstream>
46#include <map>
47#include <memory>
48#include <mutex>
49#include <optional>
50#include <string>
51#include <vector>
52
53#include <boost/json.hpp>
54
56
57namespace bertini {
58namespace records {
59
62constexpr char RecordSchemaVersion[] = "b2rec/1";
63
73{
74public:
77 explicit OutputDirectory(std::filesystem::path root);
78
107 static std::shared_ptr<OutputDirectory> Shared(std::filesystem::path const& root);
108
110 std::filesystem::path const& Root() const { return root_; }
111
112 // ---- definitions (content-addressed, grouped by kind) ----
113
133 std::string PutDefinition(std::string const& content, std::string const& kind,
134 std::optional<std::string> external_id = std::nullopt,
135 std::string const& label = {});
136
138 std::string GetDefinition(std::string const& id) const;
139
141 bool HasDefinition(std::string const& id) const;
142
143 // ---- history (append-only JSONL) ----
144
153 void Append(boost::json::object const& record);
154
168 void Annotate(std::string const& run_id, std::int64_t index,
169 std::string const& key, boost::json::value const& value);
170
177 std::vector<boost::json::object> Scan() const;
178
179 // ---- results (per-run append-only JSONL payload files; truth) ----
180
194 void EnsureResultsFile(std::string const& run_id, boost::json::object const& header);
195
207 void AppendResult(std::string const& run_id, boost::json::object const& record);
208
218 std::vector<boost::json::object> ResultsOf(std::string const& run_id) const;
219
220 // ---- derived views (rebuildable at will) ----
221
225 void RefreshIndex() const;
226
228 std::string Describe() const;
229
230private:
231 std::filesystem::path DefinitionPath(std::string const& kind, std::string const& id,
232 std::string const& label) const;
233 std::optional<std::filesystem::path> FindDefinition(std::string const& id) const;
234 std::filesystem::path ResultsPath(std::string const& run_id) const;
235 void EnsureSessionFile();
236
240 void ReleaseFiles();
241
242 std::filesystem::path root_;
243 long pid_;
244 std::ofstream session_;
245 std::filesystem::path session_path_;
246 std::mutex append_mutex_;
247 std::map<std::string, std::ofstream> results_streams_;
248
252 static constexpr std::size_t kMaxOpenResultsFiles = 16;
253};
254
272std::string SystemEncodingAsJson(std::string const& encoding_text,
273 std::string const& digest_hex,
274 boost::json::object const& parts);
275
278std::string TimeStampNow();
279
292std::optional<std::string> AmbientRecordsPath();
293
294} // namespace records
295} // namespace bertini
std::vector< boost::json::object > Scan() const
Every record from every history file, in filename-then-line order.
OutputDirectory(std::filesystem::path root)
Open (creating if needed) the output directory at root, writing the self-documenting README....
void AppendResult(std::string const &run_id, boost::json::object const &record)
Append one payload record to the run's results file (flushed per record, so a kill loses at most the ...
std::string Describe() const
One human line: how much is here (record/file/definition counts + path).
void RefreshIndex() const
(Re)write INDEX.txt: one line per run — when, paths done, op, target description, run id.
std::string GetDefinition(std::string const &id) const
Read a definition's bytes by id (searched across all kinds). Throws if absent.
std::filesystem::path const & Root() const
The directory's root path.
Definition output_directory.hpp:110
void EnsureResultsFile(std::string const &run_id, boost::json::object const &header)
Ensure the run's results file exists, with its self-description header as line 1: {"kind":"results_he...
std::string PutDefinition(std::string const &content, std::string const &kind, std::optional< std::string > external_id=std::nullopt, std::string const &label={})
Store a definition content-addressed; returns its id.
void Annotate(std::string const &run_id, std::int64_t index, std::string const &key, boost::json::value const &value)
Attach metadata to a recorded point: appends an annotation record {"kind":"annotation",...
void Append(boost::json::object const &record)
Append one record to this session's history file.
bool HasDefinition(std::string const &id) const
Whether a definition with this id is present (searched across all kinds).
static std::shared_ptr< OutputDirectory > Shared(std::filesystem::path const &root)
The process-shared OutputDirectory at root: one instance – hence one session history file – per direc...
std::vector< boost::json::object > ResultsOf(std::string const &run_id) const
Every record in the run's results file, header line included, in order.
Definition config_encoding.hpp:66
std::string TimeStampNow()
The record timestamp, now: YYYY-MM-DD HH:MM local time – the one clock format every record kind uses ...
std::string SystemEncodingAsJson(std::string const &encoding_text, std::string const &digest_hex, boost::json::object const &parts)
The archived form of a system definition: a machine-parseable JSON document {"schema",...
std::optional< std::string > AmbientRecordsPath()
Resolve the ambient records directory from the environment – the ONE place the BERTINI_RECORDS_DIR se...
constexpr char RecordSchemaVersion[]
The record-format version tag written on run headers; see docs/records/b2rec-1.md.
Definition output_directory.hpp:62
Definition algorithm_builder.hpp:36
Self-contained SHA-256 (FIPS 180-4) and the Digest256 value type.