Lightweight 0.20261105.1
Loading...
Searching...
No Matches
RestoreFailureLog.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../Api.hpp"
6#include "SqlBackup.hpp"
7
8#include <cstdint>
9#include <map>
10#include <mutex>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace Lightweight::SqlBackup::detail
16{
17
18/// Thread-safe, size-capped collector of restore failures.
19///
20/// Memory is bounded by the report caps (ranges, reasons) and by the number of tables and chunks,
21/// never by the number of failures.
22class LIGHTWEIGHT_API RestoreFailureLog
23{
24 public:
25 /// Records one row that failed to restore.
26 /// @param table Table name.
27 /// @param chunkPath Archive path of the chunk holding the row.
28 /// @param rowInChunk 1-based row number within the chunk.
29 /// @param sqlState SQLSTATE of the failure.
30 /// @param message Failure message; digit runs are collapsed when grouping reasons.
31 /// @param sampleKey Identifying key of the row, kept for the first few occurrences of a reason.
32 void RecordRowFailure(std::string_view table,
33 std::string_view chunkPath,
34 std::uint64_t rowInChunk,
35 std::string_view sqlState,
36 std::string_view message,
37 std::string sampleKey);
38
39 /// Records a cell that was set to NULL because its value could not be converted.
40 /// @param table Table name.
41 /// @param column Column name.
42 /// @param reason Why the value was dropped.
43 void RecordNulledCell(std::string_view table, std::string_view column, std::string_view reason);
44
45 /// Records that a whole table failed. Its failed row count is settled by SetFailedTableRowCount().
46 /// @param table Table name.
47 /// @param reason Failure reason (the first one wins).
48 void RecordTableFailure(std::string_view table, std::string_view reason);
49
50 /// Sets the failed row count of a table that failed as a whole to @p missingRows, the rows of the
51 /// table that did not reach the target. That replaces the count of its individually failed rows,
52 /// which are among them. Does nothing for a table without a table failure.
53 /// @param table Table name.
54 /// @param missingRows The table's rows that are not in the target.
55 void SetFailedTableRowCount(std::string_view table, std::uint64_t missingRows);
56
57 /// Records a chunk that could not be read.
58 /// @param table Table the chunk belongs to; empty if unknown.
59 void RecordUnreadableChunk(std::string_view table);
60
61 /// @param table Table name.
62 /// @return Snapshot of the table's report; an empty report if nothing failed.
63 [[nodiscard]] TableRestoreReport TableSnapshot(std::string_view table) const;
64
65 /// @return Snapshot of all failures; incompleteTables is left for the caller to set.
66 [[nodiscard]] RestoreReport Snapshot() const;
67
68 private:
69 struct TableState
70 {
71 TableRestoreReport report;
72 /// Index into report.failedRowRanges of each chunk's most recent range.
73 std::map<std::string, std::size_t, std::less<>> lastRangeOfChunk;
74 /// The message of each of report.reasons with its digit runs collapsed, by which it groups rows.
75 std::vector<std::string> reasonKeys;
76 std::map<std::string, std::size_t, std::less<>> columnIndex;
77 };
78
79 mutable std::mutex _mutex;
80 std::map<std::string, TableState, std::less<>> _tables;
81 std::uint64_t _unattributedChunkErrors {};
82
83 TableState& StateFor(std::string_view table);
84};
85
86} // namespace Lightweight::SqlBackup::detail