CCF
Loading...
Searching...
No Matches
ledger_filenames.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include <filesystem>
6#include <fmt/format.h>
7#include <optional>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11
12namespace asynchost
13{
14 namespace fs = std::filesystem;
15
16 static constexpr auto ledger_committed_suffix = ".committed";
17 static constexpr auto ledger_start_idx_delimiter = "_";
18 static constexpr auto ledger_last_idx_delimiter = "-";
19 static constexpr auto ledger_recovery_file_suffix = ".recovery";
20 static constexpr auto ledger_ignored_file_suffix = ".ignored";
21
22 static inline size_t get_start_idx_from_file_name(
23 const std::string& file_name)
24 {
25 auto pos = file_name.find(ledger_start_idx_delimiter);
26 if (pos == std::string::npos)
27 {
28 throw std::logic_error(fmt::format(
29 "Ledger file name {} does not contain a start seqno", file_name));
30 }
31
32 return std::stoull(file_name.substr(pos + 1));
33 }
34
35 static inline std::optional<size_t> get_last_idx_from_file_name(
36 const std::string& file_name)
37 {
38 auto pos = file_name.find(ledger_last_idx_delimiter);
39 if (pos == std::string::npos)
40 {
41 // Non-committed file names do not contain a last idx
42 return std::nullopt;
43 }
44
45 return std::stoull(file_name.substr(pos + 1));
46 }
47
48 static inline bool is_ledger_file_name_committed(const std::string& file_name)
49 {
50 return file_name.ends_with(ledger_committed_suffix);
51 }
52
53 static inline bool is_ledger_file_name_recovery(const std::string& file_name)
54 {
55 return file_name.ends_with(ledger_recovery_file_suffix);
56 }
57
58 static inline bool is_ledger_file_name_ignored(const std::string& file_name)
59 {
60 return file_name.ends_with(ledger_ignored_file_suffix);
61 }
62
63 static inline bool is_ledger_file_ignored(const std::string& file_name)
64 {
65 // Catch-all for all files that should be ignored
66 return is_ledger_file_name_recovery(file_name) ||
67 is_ledger_file_name_ignored(file_name);
68 }
69
70 static inline fs::path remove_suffix(
71 std::string_view file_name, const std::string& suffix)
72 {
73 if (file_name.ends_with(suffix))
74 {
75 file_name.remove_suffix(suffix.size());
76 }
77 return file_name;
78 }
79
80 static inline fs::path remove_recovery_suffix(std::string_view file_name)
81 {
82 return remove_suffix(file_name, ledger_recovery_file_suffix);
83 }
84}
Definition after_io.h:8