GCC Code Coverage Report


Directory: libimgdoc2/
File: libimgdoc2/src/db/database_configuration.h
Date: 2025-02-03 12:41:04
Exec Total Coverage
Lines: 17 17 100.0%
Functions: 11 11 100.0%
Branches: 6 8 75.0%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
2 //
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <vector>
8 #include <unordered_set>
9 #include <map>
10 #include <algorithm>
11 #include <iterator>
12 #include <string>
13 #include <stdexcept>
14 #include <imgdoc2.h>
15
16 /// The purpose of this class is to represent all information/configuration required to operate
17 /// on the database. This includes
18 /// - names of the various tables/column in use
19 /// - certain options or "information about the state" like which columns are indexed or not
20 /// - options/configurations like "spatial index active or not"
21 /// The idea is that "DatabaseConfigurationCommon" covers all options which are common for all supported
22 /// "document types", and that we have derived classes specific for a document type.
23 class DatabaseConfigurationCommon
24 {
25 public:
26 /// Values that represent the tables (which are of relevance to us).
27 enum class TableTypeCommon
28 {
29 GeneralInfo,
30 TilesData,
31 TilesInfo,
32 TilesSpatialIndex,
33 Metadata,
34 Blobs
35 };
36
37 static constexpr int kGeneralInfoTable_Column_Key = 1; ///< Identifier for the "key column" in the "general" table.
38 static constexpr int kGeneralInfoTable_Column_ValueString = 2; ///< Identifier for the "value(string) column" in the "general" table.
39
40 static constexpr int kBlobTable_Column_Pk = 1; ///< Identifier for the "primary key column" in the "blobs" table.
41 static constexpr int kBlobTable_Column_Data = 2; ///< Identifier for the "data column" in the "blobs" table.
42
43 static constexpr int kMetadataTable_Column_Pk = 1; ///< Identifier for the "primary key" column in the "metadata" table.
44 static constexpr int kMetadataTable_Column_Name = 2; ///< Identifier for the "name" column in the "metadata" table.
45 static constexpr int kMetadataTable_Column_AncestorId = 3; ///< Identifier for the "ancestor-id" column in the "metadata" table.
46 static constexpr int kMetadataTable_Column_TypeDiscriminator = 4; ///< Identifier for the "type discriminator" column in the "metadata" table.
47 static constexpr int kMetadataTable_Column_ValueDouble = 5; ///< Identifier for the "value(double) column" in the "metadata" table.
48 static constexpr int kMetadataTable_Column_ValueInteger = 6; ///< Identifier for the "value(integer) column" in the "metadata" table.
49 static constexpr int kMetadataTable_Column_ValueString = 7; ///< Identifier for the "value(string) column" in the "metadata" table.
50 private:
51 std::unordered_set<imgdoc2::Dimension> dimensions_;
52 std::unordered_set<imgdoc2::Dimension> indexed_dimensions_;
53 std::map<TableTypeCommon, std::string> map_tabletype_to_tablename_;
54 std::string dimension_column_prefix_;
55 std::string index_for_dimension_prefix_;
56 std::map<int, std::string> map_blobtable_columnids_to_columnname_;
57 std::map<int, std::string> map_metadatatable_columnids_to_columnname_;
58 public:
59 template<typename ForwardIterator>
60 272 void SetTileDimensions(ForwardIterator begin, ForwardIterator end)
61 {
62 272 this->dimensions_.clear();
63
2/2
✓ Branch 2 taken 342 times.
✓ Branch 3 taken 272 times.
614 for (auto it = begin; it != end; ++it)
64 {
65
1/2
✓ Branch 2 taken 342 times.
✗ Branch 3 not taken.
342 this->dimensions_.insert(*it);
66 }
67 272 }
68
69 template<typename ForwardIterator>
70 272 void SetIndexedTileDimensions(ForwardIterator begin, ForwardIterator end)
71 {
72 272 this->indexed_dimensions_.clear();
73
2/2
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 272 times.
312 for (auto it = begin; it != end; ++it)
74 {
75
1/2
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 this->indexed_dimensions_.insert(*it);
76 }
77 272 }
78
79 11204 const std::unordered_set<imgdoc2::Dimension>& GetTileDimensions() const { return this->dimensions_; }
80 274 const std::unordered_set<imgdoc2::Dimension>& GetIndexedTileDimensions() const { return this->indexed_dimensions_; }
81
82 bool IsDimensionIndexed(imgdoc2::Dimension) const;
83
84 /// Queries if the specified tile dimension is valid (for this document).
85 /// \param dimension The dimension to check.
86 /// \returns True if the tile dimension is valid for this document, false if not.
87 bool IsTileDimensionValid(imgdoc2::Dimension dimension) const;
88
89 272 void SetDimensionColumnPrefix(const char* prefix) { this->dimension_column_prefix_ = prefix; }
90 272 void SetIndexForDimensionColumnPrefix(const char* prefix) { this->index_for_dimension_prefix_ = prefix; }
91 48208 const std::string& GetDimensionsColumnPrefix() const { return this->dimension_column_prefix_; }
92 20 const std::string& GetIndexForDimensionColumnPrefix() const { return this->index_for_dimension_prefix_; }
93
94 void SetTableName(TableTypeCommon tableType, const char* name);
95
96 virtual bool TryGetTableName(TableTypeCommon table_type, std::string* name) const;
97
98 void SetColumnNameForBlobTable(int column_identifier, const char* column_name);
99 void SetColumnNameForMetadataTable(int column_identifier, const char* column_name);
100
101 bool TryGetColumnNameOfGeneralInfoTable(int columnIdentifier, std::string* column_name) const;
102 bool TryGetColumnNameOfBlobTable(int column_identifier, std::string* column_name) const;
103 bool TryGetColumnNameOfMetadataTable(int column_identifier, std::string* column_name) const;
104
105 /// Gets document type constant - which document-type is represented by this configuration.
106 ///
107 /// \returns The document type constant.
108 [[nodiscard]] virtual imgdoc2::DocumentType GetDocumentType() const = 0;
109
110 544 virtual ~DatabaseConfigurationCommon() = default;
111 public:
112 std::string GetTableNameOrThrow(TableTypeCommon table_type) const;
113 std::string GetTableNameForTilesDataOrThrow() const;
114 std::string GetTableNameForTilesInfoOrThrow() const;
115 std::string GetTableNameForGeneralTableOrThrow() const;
116 std::string GetTableNameForTilesSpatialIndexTableOrThrow() const;
117 std::string GetTableNameForBlobTableOrThrow() const;
118 std::string GetTableNameForMetadataTableOrThrow() const;
119
120 std::string GetColumnNameOfGeneralInfoTableOrThrow(int column_identifier) const;
121 std::string GetColumnNameOfBlobTableOrThrow(int column_identifier) const;
122 std::string GetColumnNameOfMetadataTableOrThrow(int column_identifier) const;
123
124 void SetDefaultColumnNamesForMetadataTable();
125
126 bool GetIsUsingSpatialIndex() const;
127 bool GetHasBlobsTable() const;
128 bool GetHasMetadataTable() const;
129
130 protected:
131 static void SetColumnName(std::map<int, std::string>& map, int columnIdentifier, const char* column_name);
132 static bool GetColumnName(const std::map<int, std::string>& map, int columnIdentifier, std::string* column_name);
133 };
134
135 /// This class is intended to capture the "state of the database configuration" for 2D-documents.
136 class DatabaseConfiguration2D : public DatabaseConfigurationCommon
137 {
138 private:
139 std::map<int, std::string> map_tilesinfotable_columnids_to_columnname_;
140 std::map<int, std::string> map_tilesdatatable_columnids_to_columnname_;
141 std::map<int, std::string> map_tilespatialindextable_columnids_to_columnname_;
142 public:
143 static constexpr int kTilesInfoTable_Column_Pk = 1;
144 static constexpr int kTilesInfoTable_Column_TileX = 2;
145 static constexpr int kTilesInfoTable_Column_TileY = 3;
146 static constexpr int kTilesInfoTable_Column_TileW = 4;
147 static constexpr int kTilesInfoTable_Column_TileH = 5;
148 static constexpr int kTilesInfoTable_Column_PyramidLevel = 6;
149 static constexpr int kTilesInfoTable_Column_TileDataId = 7;
150
151 static constexpr int kTilesDataTable_Column_Pk = 1;
152 static constexpr int kTilesDataTable_Column_PixelWidth = 2;
153 static constexpr int kTilesDataTable_Column_PixelHeight = 3;
154 static constexpr int kTilesDataTable_Column_PixelType = 4;
155 static constexpr int kTilesDataTable_Column_TileDataType = 5;
156 static constexpr int kTilesDataTable_Column_BinDataStorageType = 6;
157 static constexpr int kTilesDataTable_Column_BinDataId = 7;
158
159 static constexpr int kTilesSpatialIndexTable_Column_Pk = 1;
160 static constexpr int kTilesSpatialIndexTable_Column_MinX = 2;
161 static constexpr int kTilesSpatialIndexTable_Column_MaxX = 3;
162 static constexpr int kTilesSpatialIndexTable_Column_MinY = 4;
163 static constexpr int kTilesSpatialIndexTable_Column_MaxY = 5;
164
165 [[nodiscard]] imgdoc2::DocumentType GetDocumentType() const override;
166
167 void SetColumnNameForTilesInfoTable(int columnIdentifier, const char* column_name);
168 bool TryGetColumnNameOfTilesInfoTable(int columnIdentifier, std::string* column_name) const;
169
170 void SetColumnNameForTilesDataTable(int columnIdentifier, const char* column_name);
171 bool TryGetColumnNameOfTilesDataTable(int columnIdentifier, std::string* column_name) const;
172
173 void SetColumnNameForTilesSpatialIndexTable(int columnIdentifier, const char* column_name);
174 bool TryGetColumnNameOfTilesSpatialIndexTable(int columnIdentifier, std::string* column_name) const;
175
176 void SetDefaultColumnNamesForTilesInfoTable();
177 void SetDefaultColumnNamesForTilesDataTable();
178 public:
179 [[nodiscard]] std::string GetColumnNameOfTilesInfoTableOrThrow(int columnIdentifier) const;
180 [[nodiscard]] std::string GetColumnNameOfTilesDataTableOrThrow(int column_identifier) const;
181 [[nodiscard]] std::string GetColumnNameOfTilesSpatialIndexTableOrThrow(int columnIdentifier) const;
182 };
183
184 /// This class is intended to capture the "state of the database configuration" for 3D-documents.
185 class DatabaseConfiguration3D : public DatabaseConfigurationCommon
186 {
187 private:
188 std::map<int, std::string> map_tilesinfotable_columnids_to_columnname_;
189 std::map<int, std::string> map_tilesdatatable_columnids_to_columnname_;
190 std::map<int, std::string> map_tilespatialindextable_columnids_to_columnname_;
191 public:
192 static constexpr int kTilesInfoTable_Column_Pk = 1;
193 static constexpr int kTilesInfoTable_Column_TileX = 2; ///< (Immutable) The brick's X-position.
194 static constexpr int kTilesInfoTable_Column_TileY = 3; ///< (Immutable) The brick's Y-position.
195 static constexpr int kTilesInfoTable_Column_TileZ = 4; ///< (Immutable) The brick's Z-position.
196 static constexpr int kTilesInfoTable_Column_TileW = 5; ///< (Immutable) The brick's width.
197 static constexpr int kTilesInfoTable_Column_TileH = 6; ///< (Immutable) The brick's height.
198 static constexpr int kTilesInfoTable_Column_TileD = 7; ///< (Immutable) The brick's depth.
199 static constexpr int kTilesInfoTable_Column_PyramidLevel = 8;
200 static constexpr int kTilesInfoTable_Column_TileDataId = 9;
201
202 static constexpr int kTilesDataTable_Column_Pk = 1;
203 static constexpr int kTilesDataTable_Column_PixelWidth = 2;
204 static constexpr int kTilesDataTable_Column_PixelHeight = 3;
205 static constexpr int kTilesDataTable_Column_PixelDepth = 4;
206 static constexpr int kTilesDataTable_Column_PixelType = 5;
207 static constexpr int kTilesDataTable_Column_TileDataType = 6;
208 static constexpr int kTilesDataTable_Column_BinDataStorageType = 7;
209 static constexpr int kTilesDataTable_Column_BinDataId = 8;
210
211 static constexpr int kTilesSpatialIndexTable_Column_Pk = 1;
212 static constexpr int kTilesSpatialIndexTable_Column_MinX = 2;
213 static constexpr int kTilesSpatialIndexTable_Column_MaxX = 3;
214 static constexpr int kTilesSpatialIndexTable_Column_MinY = 4;
215 static constexpr int kTilesSpatialIndexTable_Column_MaxY = 5;
216 static constexpr int kTilesSpatialIndexTable_Column_MinZ = 6;
217 static constexpr int kTilesSpatialIndexTable_Column_MaxZ = 7;
218
219 [[nodiscard]] imgdoc2::DocumentType GetDocumentType() const override;
220
221 void SetColumnNameForTilesInfoTable(int columnIdentifier, const char* column_name);
222 bool TryGetColumnNameOfTilesInfoTable(int columnIdentifier, std::string* column_name) const;
223
224 void SetColumnNameForTilesDataTable(int columnIdentifier, const char* column_name);
225 bool TryGetColumnNameOfTilesDataTable(int columnIdentifier, std::string* column_name) const;
226
227 void SetColumnNameForTilesSpatialIndexTable(int columnIdentifier, const char* column_name);
228 bool TryGetColumnNameOfTilesSpatialIndexTable(int columnIdentifier, std::string* column_name) const;
229
230 void SetDefaultColumnNamesForTilesInfoTable();
231 void SetDefaultColumnNamesForTilesDataTable();
232 public:
233 [[nodiscard]] std::string GetColumnNameOfTilesInfoTableOrThrow(int columnIdentifier) const;
234 [[nodiscard]] std::string GetColumnNameOfTilesDataTableOrThrow(int column_identifier) const;
235 [[nodiscard]] std::string GetColumnNameOfTilesSpatialIndexTableOrThrow(int columnIdentifier) const;
236 };
237