Lightweight 0.20260921.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/// @ingroup Backup
25/// Represents a batch of backup data in column-oriented format.
27{
28 /// Type representing columnar data storage for a batch.
29 using ColumnData =
30 std::variant<std::monostate, // Placeholder (e.g. for pure NULL columns if we optimize that later, or initialization)
31 std::vector<int64_t>,
32 std::vector<double>,
33 std::vector<std::string>,
34 std::vector<std::vector<uint8_t>>, // Binary
35 std::vector<bool>>;
36
37 /// The number of rows in the batch.
38 size_t rowCount = 0;
39 /// The column data arrays.
40 std::vector<ColumnData> columns;
41 /// Null indicators for each column, parallel to columns.
42 std::vector<std::vector<bool>> nullIndicators; // Parallel to columns: true if NULL
43
44 /// Clears the internal buffer after a flush and releases memory.
45 void Clear()
46 {
47 rowCount = 0;
48 for (auto& col: columns)
49 {
50 std::visit(
51 [](auto& v) {
52 if constexpr (!std::is_same_v<std::decay_t<decltype(v)>, std::monostate>)
53 {
54 v.clear();
55 v.shrink_to_fit();
56 }
57 },
58 col);
59 }
60 for (auto& inds: nullIndicators)
61 {
62 inds.clear();
63 inds.shrink_to_fit();
64 }
65 }
66};
67
68/// @ingroup Backup
69/// Interface for writing backup chunks.
71{
72 virtual ~ChunkWriter() = default;
73
74 ChunkWriter(ChunkWriter const&) = delete;
75 ChunkWriter(ChunkWriter&&) = delete;
76 ChunkWriter& operator=(ChunkWriter const&) = delete;
77 ChunkWriter& operator=(ChunkWriter&&) = delete;
78 ChunkWriter() = default;
79
80 /// Writes a single row to the chunk (buffers it).
81 ///
82 /// @param row The row data to write.
83 virtual void WriteRow(std::span<BackupValue const> row) = 0;
84
85 /// Flushes any buffered data to the output.
86 ///
87 /// @return The formatted data chunk.
88 virtual std::string Flush() = 0;
89
90 /// Checks if the current chunk should be finalized (e.g. size limit reached).
91 ///
92 /// @return True if the chunk is full.
93 [[nodiscard]] virtual bool IsChunkFull() const = 0;
94
95 /// Clears the internal buffer after a flush.
96 virtual void Clear() = 0;
97};
98
99/// @ingroup Backup
100/// Interface for reading backup chunks.
102{
103 virtual ~ChunkReader() = default;
104
105 ChunkReader(ChunkReader const&) = delete;
106 ChunkReader(ChunkReader&&) = delete;
107 ChunkReader& operator=(ChunkReader const&) = delete;
108 ChunkReader& operator=(ChunkReader&&) = delete;
109 ChunkReader() = default;
110
111 /// Reads the next batch of data from the chunk.
112 ///
113 /// @param[out] batch The batch structure to populate.
114 /// @return True if data was read, false if end of stream.
115 virtual bool ReadBatch(ColumnBatch& batch) = 0;
116};
117
118} // namespace Lightweight::SqlBackup
virtual bool ReadBatch(ColumnBatch &batch)=0
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
std::vector< std::vector< bool > > nullIndicators
Null indicators for each column, parallel to columns.
std::vector< ColumnData > columns
The column data arrays.
void Clear()
Clears the internal buffer after a flush and releases memory.
size_t rowCount
The number of rows in the batch.
std::variant< std::monostate, std::vector< int64_t >, std::vector< double >, std::vector< std::string >, std::vector< std::vector< uint8_t > >, std::vector< bool > > ColumnData
Type representing columnar data storage for a batch.