Skip to content

Commit 16d95b1

Browse files
refactor(core): Use C++20 concepts to enforce requirements for logtype and variable dictionary entries, readers, and writers. (#1267)
Co-authored-by: Lin Zhihao <59785146+LinZhihao-723@users.noreply.github.com>
1 parent 9888c11 commit 16d95b1

17 files changed

+387
-40
lines changed

components/core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,9 @@ set(SOURCE_FILES_unitTest
580580
src/clp/LogSurgeonReader.hpp
581581
src/clp/LogTypeDictionaryEntry.cpp
582582
src/clp/LogTypeDictionaryEntry.hpp
583+
src/clp/LogTypeDictionaryEntryReq.hpp
583584
src/clp/LogTypeDictionaryReader.hpp
585+
src/clp/LogTypeDictionaryReaderReq.hpp
584586
src/clp/LogTypeDictionaryWriter.cpp
585587
src/clp/LogTypeDictionaryWriter.hpp
586588
src/clp/math_utils.hpp
@@ -673,9 +675,12 @@ set(SOURCE_FILES_unitTest
673675
src/clp/Utils.hpp
674676
src/clp/VariableDictionaryEntry.cpp
675677
src/clp/VariableDictionaryEntry.hpp
678+
src/clp/VariableDictionaryEntryReq.hpp
676679
src/clp/VariableDictionaryReader.hpp
680+
src/clp/VariableDictionaryReaderReq.hpp
677681
src/clp/VariableDictionaryWriter.cpp
678682
src/clp/VariableDictionaryWriter.hpp
683+
src/clp/VariableDictionaryWriterReq.hpp
679684
src/clp/version.hpp
680685
src/clp/WriterInterface.cpp
681686
src/clp/WriterInterface.hpp

components/core/src/clp/DictionaryReader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DictionaryReader {
3838
};
3939

4040
using dictionary_id_t = DictionaryIdType;
41-
using entry_t = EntryType;
41+
using Entry = EntryType;
4242

4343
// Constructors
4444
DictionaryReader() : m_is_open(false), m_num_segments_read_from_index(0) {

components/core/src/clp/DictionaryWriter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DictionaryWriter {
3636
};
3737

3838
using dictionary_id_t = DictionaryIdType;
39-
using entry_t = EntryType;
39+
using Entry = EntryType;
4040

4141
// Constructors
4242
DictionaryWriter() : m_is_open(false) {}

components/core/src/clp/EncodedVariableInterpreter.hpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include "ffi/ir_stream/decoding_methods.hpp"
1212
#include "ir/LogEvent.hpp"
1313
#include "ir/types.hpp"
14+
#include "LogTypeDictionaryEntryReq.hpp"
1415
#include "Query.hpp"
1516
#include "spdlog_with_specializations.hpp"
1617
#include "TraceableException.hpp"
1718
#include "type_utils.hpp"
19+
#include "VariableDictionaryReaderReq.hpp"
20+
#include "VariableDictionaryWriterReq.hpp"
1821

1922
namespace clp {
2023
/**
@@ -123,7 +126,9 @@ class EncodedVariableInterpreter {
123126
* @param encoded_vars
124127
* @param var_ids
125128
*/
126-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
129+
template <
130+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
131+
VariableDictionaryWriterReq VariableDictionaryWriterType>
127132
static void encode_and_add_to_dictionary(
128133
std::string_view message,
129134
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -149,8 +154,8 @@ class EncodedVariableInterpreter {
149154
*/
150155
template <
151156
typename EncodedVariableType,
152-
typename LogTypeDictionaryEntryType,
153-
typename VariableDictionaryWriterType>
157+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
158+
VariableDictionaryWriterReq VariableDictionaryWriterType>
154159
static void encode_and_add_to_dictionary(
155160
ir::LogEvent<EncodedVariableType> const& log_event,
156161
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -172,8 +177,8 @@ class EncodedVariableInterpreter {
172177
* @return true if successful, false otherwise
173178
*/
174179
template <
175-
typename LogTypeDictionaryEntryType,
176-
typename VariableDictionaryReaderType,
180+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
181+
VariableDictionaryReaderReq VariableDictionaryReaderType,
177182
typename EncodedVariableContainerType>
178183
static bool decode_variables_into_message(
179184
LogTypeDictionaryEntryType const& logtype_dict_entry,
@@ -195,7 +200,7 @@ class EncodedVariableInterpreter {
195200
* dictionary
196201
* @return false otherwise
197202
*/
198-
template <typename VariableDictionaryReaderType>
203+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
199204
static bool encode_and_search_dictionary(
200205
std::string_view var_str,
201206
VariableDictionaryReaderType const& var_dict,
@@ -207,16 +212,13 @@ class EncodedVariableInterpreter {
207212
* Search for the given string-form variable in the variable dictionary, encode any matches, and
208213
* add them to the given sub-query.
209214
* @tparam VariableDictionaryReaderType
210-
* @tparam VariableDictionaryEntryType
211215
* @param var_wildcard_str
212216
* @param var_dict
213217
* @param ignore_case
214218
* @param sub_query
215219
* @return true if any match found, false otherwise
216220
*/
217-
template <
218-
typename VariableDictionaryReaderType,
219-
typename VariableDictionaryEntryType = typename VariableDictionaryReaderType::entry_t>
221+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
220222
static bool wildcard_search_dictionary_and_get_encoded_matches(
221223
std::string_view var_wildcard_str,
222224
VariableDictionaryReaderType const& var_dict,
@@ -237,7 +239,9 @@ class EncodedVariableInterpreter {
237239
* variable)
238240
* @return The encoded variable
239241
*/
240-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
242+
template <
243+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
244+
VariableDictionaryWriterReq VariableDictionaryWriterType>
241245
static encoded_variable_t encode_var(
242246
std::string_view var,
243247
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -256,7 +260,9 @@ class EncodedVariableInterpreter {
256260
* @param var_ids A container to add the dictionary ID to
257261
* @return The dictionary ID
258262
*/
259-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
263+
template <
264+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
265+
VariableDictionaryWriterReq VariableDictionaryWriterType>
260266
static variable_dictionary_id_t add_dict_var(
261267
std::string_view var,
262268
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -265,7 +271,9 @@ class EncodedVariableInterpreter {
265271
);
266272
};
267273

268-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
274+
template <
275+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
276+
VariableDictionaryWriterReq VariableDictionaryWriterType>
269277
void EncodedVariableInterpreter::encode_and_add_to_dictionary(
270278
std::string_view message,
271279
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -289,8 +297,8 @@ void EncodedVariableInterpreter::encode_and_add_to_dictionary(
289297

290298
template <
291299
typename EncodedVariableType,
292-
typename LogTypeDictionaryEntryType,
293-
typename VariableDictionaryWriterType>
300+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
301+
VariableDictionaryWriterReq VariableDictionaryWriterType>
294302
void EncodedVariableInterpreter::encode_and_add_to_dictionary(
295303
ir::LogEvent<EncodedVariableType> const& log_event,
296304
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -362,8 +370,8 @@ void EncodedVariableInterpreter::encode_and_add_to_dictionary(
362370
}
363371

364372
template <
365-
typename LogTypeDictionaryEntryType,
366-
typename VariableDictionaryReaderType,
373+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
374+
VariableDictionaryReaderReq VariableDictionaryReaderType,
367375
typename EncodedVariableContainerType>
368376
bool EncodedVariableInterpreter::decode_variables_into_message(
369377
LogTypeDictionaryEntryType const& logtype_dict_entry,
@@ -436,7 +444,7 @@ bool EncodedVariableInterpreter::decode_variables_into_message(
436444
return true;
437445
}
438446

439-
template <typename VariableDictionaryReaderType>
447+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
440448
bool EncodedVariableInterpreter::encode_and_search_dictionary(
441449
std::string_view var_str,
442450
VariableDictionaryReaderType const& var_dict,
@@ -485,15 +493,15 @@ bool EncodedVariableInterpreter::encode_and_search_dictionary(
485493
return true;
486494
}
487495

488-
template <typename VariableDictionaryReaderType, typename VariableDictionaryEntryType>
496+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
489497
bool EncodedVariableInterpreter::wildcard_search_dictionary_and_get_encoded_matches(
490498
std::string_view var_wildcard_str,
491499
VariableDictionaryReaderType const& var_dict,
492500
bool ignore_case,
493501
SubQuery& sub_query
494502
) {
495503
// Find matches
496-
std::unordered_set<VariableDictionaryEntryType const*> var_dict_entries;
504+
std::unordered_set<typename VariableDictionaryReaderType::Entry const*> var_dict_entries;
497505
var_dict.get_entries_matching_wildcard_string(var_wildcard_str, ignore_case, var_dict_entries);
498506
if (var_dict_entries.empty()) {
499507
// Not in dictionary
@@ -513,7 +521,9 @@ bool EncodedVariableInterpreter::wildcard_search_dictionary_and_get_encoded_matc
513521
return true;
514522
}
515523

516-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
524+
template <
525+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
526+
VariableDictionaryWriterReq VariableDictionaryWriterType>
517527
encoded_variable_t EncodedVariableInterpreter::encode_var(
518528
std::string_view var,
519529
LogTypeDictionaryEntryType& logtype_dict_entry,
@@ -532,7 +542,9 @@ encoded_variable_t EncodedVariableInterpreter::encode_var(
532542
return encoded_var;
533543
}
534544

535-
template <typename LogTypeDictionaryEntryType, typename VariableDictionaryWriterType>
545+
template <
546+
LogTypeDictionaryEntryReq LogTypeDictionaryEntryType,
547+
VariableDictionaryWriterReq VariableDictionaryWriterType>
536548
variable_dictionary_id_t EncodedVariableInterpreter::add_dict_var(
537549
std::string_view var,
538550
LogTypeDictionaryEntryType& logtype_dict_entry,

components/core/src/clp/GrepCore.hpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
#include "EncodedVariableInterpreter.hpp"
1818
#include "ir/parsing.hpp"
1919
#include "ir/types.hpp"
20+
#include "LogTypeDictionaryReaderReq.hpp"
2021
#include "Query.hpp"
2122
#include "QueryToken.hpp"
23+
#include "VariableDictionaryReaderReq.hpp"
2224

2325
namespace clp {
2426
class GrepCore {
@@ -42,7 +44,9 @@ class GrepCore {
4244
* @param use_heuristic
4345
* @return Query if it may match a message, std::nullopt otherwise
4446
*/
45-
template <typename LogTypeDictionaryReaderType, typename VariableDictionaryReaderType>
47+
template <
48+
LogTypeDictionaryReaderReq LogTypeDictionaryReaderType,
49+
VariableDictionaryReaderReq VariableDictionaryReaderType>
4650
static std::optional<Query> process_raw_query(
4751
LogTypeDictionaryReaderType const& logtype_dict,
4852
VariableDictionaryReaderType const& var_dict,
@@ -107,7 +111,7 @@ class GrepCore {
107111
* @param logtype
108112
* @return true if this token might match a message, false otherwise
109113
*/
110-
template <typename VariableDictionaryReaderType>
114+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
111115
static bool process_var_token(
112116
QueryToken const& query_token,
113117
VariableDictionaryReaderType const& var_dict,
@@ -120,7 +124,6 @@ class GrepCore {
120124
* Generates logtypes and variables for subquery.
121125
* @tparam LogTypeDictionaryReaderType
122126
* @tparam VariableDictionaryReaderType
123-
* @tparam LogTypeDictionaryEntryType
124127
* @param logtype_dict
125128
* @param var_dict
126129
* @param processed_search_string
@@ -132,9 +135,8 @@ class GrepCore {
132135
* @return SubQueryMatchabilityResult::MayMatch
133136
*/
134137
template <
135-
typename LogTypeDictionaryReaderType,
136-
typename VariableDictionaryReaderType,
137-
typename LogTypeDictionaryEntryType = typename LogTypeDictionaryReaderType::entry_t>
138+
LogTypeDictionaryReaderReq LogTypeDictionaryReaderType,
139+
VariableDictionaryReaderReq VariableDictionaryReaderType>
138140
static SubQueryMatchabilityResult generate_logtypes_and_vars_for_subquery(
139141
LogTypeDictionaryReaderType const& logtype_dict,
140142
VariableDictionaryReaderType const& var_dict,
@@ -145,7 +147,9 @@ class GrepCore {
145147
);
146148
};
147149

148-
template <typename LogTypeDictionaryReaderType, typename VariableDictionaryReaderType>
150+
template <
151+
LogTypeDictionaryReaderReq LogTypeDictionaryReaderType,
152+
VariableDictionaryReaderReq VariableDictionaryReaderType>
149153
std::optional<Query> GrepCore::process_raw_query(
150154
LogTypeDictionaryReaderType const& logtype_dict,
151155
VariableDictionaryReaderType const& var_dict,
@@ -266,7 +270,7 @@ std::optional<Query> GrepCore::process_raw_query(
266270
};
267271
}
268272

269-
template <typename VariableDictionaryReaderType>
273+
template <VariableDictionaryReaderReq VariableDictionaryReaderType>
270274
bool GrepCore::process_var_token(
271275
QueryToken const& query_token,
272276
VariableDictionaryReaderType const& var_dict,
@@ -328,9 +332,8 @@ bool GrepCore::process_var_token(
328332
}
329333

330334
template <
331-
typename LogTypeDictionaryReaderType,
332-
typename VariableDictionaryReaderType,
333-
typename LogTypeDictionaryEntryType>
335+
LogTypeDictionaryReaderReq LogTypeDictionaryReaderType,
336+
VariableDictionaryReaderReq VariableDictionaryReaderType>
334337
GrepCore::SubQueryMatchabilityResult GrepCore::generate_logtypes_and_vars_for_subquery(
335338
LogTypeDictionaryReaderType const& logtype_dict,
336339
VariableDictionaryReaderType const& var_dict,
@@ -407,7 +410,7 @@ GrepCore::SubQueryMatchabilityResult GrepCore::generate_logtypes_and_vars_for_su
407410
}
408411

409412
// Find matching logtypes
410-
std::unordered_set<LogTypeDictionaryEntryType const*> possible_logtype_entries;
413+
std::unordered_set<typename LogTypeDictionaryReaderType::Entry const*> possible_logtype_entries;
411414
logtype_dict
412415
.get_entries_matching_wildcard_string(logtype, ignore_case, possible_logtype_entries);
413416
if (possible_logtype_entries.empty()) {

0 commit comments

Comments
 (0)