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