Lightweight 0.20251202.0
Loading...
Searching...
No Matches
SqlBackupFormats.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include <cstdint>
5#include <iostream>
6#include <memory>
7#include <span>
8#include <string>
9#include <variant>
10#include <vector>
11
12namespace Lightweight::SqlBackup
13{
14
15/// Represents a value in a backup row.
16using BackupValue = std::variant<std::monostate, // NULL
17 bool,
18 int64_t,
19 double,
20 std::string,
21 std::vector<uint8_t> // Binary
22 >;
23
24/// Represents a batch of backup data in column-oriented format.
26{
27 using ColumnData =
28 std::variant<std::monostate, // Placeholder (e.g. for pure NULL columns if we optimize that later, or initialization)
29 std::vector<int64_t>,
30 std::vector<double>,
31 std::vector<std::string>,
32 std::vector<std::vector<uint8_t>>, // Binary
33 std::vector<bool>>;
34
35 size_t rowCount = 0;
36 std::vector<ColumnData> columns;
37 std::vector<std::vector<bool>> nullIndicators; // Parallel to columns: true if NULL
38
39 /// Clears the internal buffer after a flush and releases memory.
40 void Clear()
41 {
42 rowCount = 0;
43 for (auto& col: columns)
44 {
45 std::visit(
46 [](auto& v) {
47 if constexpr (!std::is_same_v<std::decay_t<decltype(v)>, std::monostate>)
48 {
49 v.clear();
50 v.shrink_to_fit();
51 }
52 },
53 col);
54 }
55 for (auto& inds: nullIndicators)
56 {
57 inds.clear();
58 inds.shrink_to_fit();
59 }
60 }
61};
62
63/// Interface for writing backup chunks.
65{
66 virtual ~ChunkWriter() = default;
67
68 ChunkWriter(ChunkWriter const&) = delete;
69 ChunkWriter(ChunkWriter&&) = delete;
70 ChunkWriter& operator=(ChunkWriter const&) = delete;
71 ChunkWriter& operator=(ChunkWriter&&) = delete;
72 ChunkWriter() = default;
73
74 /// Writes a single row to the chunk (buffers it).
75 ///
76 /// @param row The row data to write.
77 virtual void WriteRow(std::span<BackupValue const> row) = 0;
78
79 /// Flushes any buffered data to the output.
80 ///
81 /// @return The formatted data chunk.
82 virtual std::string Flush() = 0;
83
84 /// Checks if the current chunk should be finalized (e.g. size limit reached).
85 ///
86 /// @return True if the chunk is full.
87 [[nodiscard]] virtual bool IsChunkFull() const = 0;
88
89 /// Clears the internal buffer after a flush.
90 virtual void Clear() = 0;
91};
92
93/// Interface for reading backup chunks.
95{
96 virtual ~ChunkReader() = default;
97
98 ChunkReader(ChunkReader const&) = delete;
99 ChunkReader(ChunkReader&&) = delete;
100 ChunkReader& operator=(ChunkReader const&) = delete;
101 ChunkReader& operator=(ChunkReader&&) = delete;
102 ChunkReader() = default;
103
104 /// Reads the next batch of data from the chunk.
105 ///
106 /// @param[out] batch The batch structure to populate.
107 /// @return True if data was read, false if end of stream.
108 virtual bool ReadBatch(ColumnBatch& batch) = 0;
109};
110
111} // namespace Lightweight::SqlBackup
Interface for reading backup chunks.
virtual bool ReadBatch(ColumnBatch &batch)=0
Interface for writing backup chunks.
virtual bool IsChunkFull() const =0
virtual void Clear()=0
Clears the internal buffer after a flush.
virtual std::string Flush()=0
virtual void WriteRow(std::span< BackupValue const > row)=0
Represents a batch of backup data in column-oriented format.
void Clear()
Clears the internal buffer after a flush and releases memory.