Skip to content

Commit

Permalink
Fixed QueryLogtype class to use setters/getters, declare functions in…
Browse files Browse the repository at this point in the history
… the correct order, and define longer functions in cpp; Added back in stopwatch test
  • Loading branch information
SharafMohamed committed Jul 8, 2024
1 parent c55a26a commit b84a354
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 100 deletions.
82 changes: 71 additions & 11 deletions components/core/src/clp/Grep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,66 @@ SubQueryMatchabilityResult generate_logtypes_and_vars_for_subquery(
}
} // namespace

bool QueryLogtype::operator<(QueryLogtype const& rhs) const {
if (m_logtype.size() < rhs.m_logtype.size()) {
return true;
} else if (m_logtype.size() > rhs.m_logtype.size()) {
return false;
}
for (uint32_t i = 0; i < m_logtype.size(); i++) {
if (m_logtype[i] < rhs.m_logtype[i]) {
return true;
} else if (m_logtype[i] > rhs.m_logtype[i]) {
return false;
}
}
for (uint32_t i = 0; i < m_query.size(); i++) {
if (m_query[i] < rhs.m_query[i]) {
return true;
} else if (m_query[i] > rhs.m_query[i]) {
return false;
}
}
for (uint32_t i = 0; i < m_is_potentially_in_dict.size(); i++) {
if (m_is_potentially_in_dict[i] < rhs.m_is_potentially_in_dict[i]) {
return true;
} else if (m_is_potentially_in_dict[i] > rhs.m_is_potentially_in_dict[i]) {
return false;
}
}
return false;
}

void QueryLogtype::append_logtype(QueryLogtype& suffix) {
m_logtype.insert(m_logtype.end(), suffix.m_logtype.begin(), suffix.m_logtype.end());
m_query.insert(
m_query.end(),
suffix.m_query.begin(),
suffix.m_query.end()
);
m_is_potentially_in_dict.insert(
m_is_potentially_in_dict.end(),
suffix.m_is_potentially_in_dict.begin(),
suffix.m_is_potentially_in_dict.end()
);
m_has_wildcard.insert(
m_has_wildcard.end(),
suffix.m_has_wildcard.begin(),
suffix.m_has_wildcard.end()
);
}

void QueryLogtype::append_value(
std::variant<char, int> const& val,
std::string const& string,
bool var_contains_wildcard
) {
m_has_wildcard.push_back(var_contains_wildcard);
m_logtype.push_back(val);
m_query.push_back(string);
m_is_potentially_in_dict.push_back(false);
}

std::optional<Query> Grep::process_raw_query(
Archive const& archive,
string const& search_string,
Expand Down Expand Up @@ -1133,11 +1193,11 @@ void Grep::generate_sub_queries(
// comparing against the dictionary than they do when comparing against the segment.
std::string logtype_string;
bool has_vars = true;
for (uint32_t i = 0; i < query_logtype.m_logtype.size(); i++) {
auto const& logtype_value = query_logtype.m_logtype[i];
auto const& raw_string = query_logtype.m_search_query[i];
auto const& is_dict_var = query_logtype.m_is_potentially_in_dict[i];
auto const& var_has_wildcard = query_logtype.m_var_has_wildcard[i];
for (uint32_t i = 0; i < query_logtype.get_logtype_size(); i++) {
auto const& logtype_value = query_logtype.get_logtype_value(i);
auto const& raw_string = query_logtype.get_query_string(i);
auto const& is_dict_var = query_logtype.get_is_potentially_in_dict(i);
auto const& var_has_wildcard = query_logtype.get_has_wildcard(i);
if (std::holds_alternative<char>(logtype_value)) {
logtype_string.push_back(std::get<char>(logtype_value));
} else {
Expand All @@ -1151,7 +1211,7 @@ void Grep::generate_sub_queries(
&& (schema_type == "int" || schema_type == "float"))
{
QueryLogtype new_query_logtype = query_logtype;
new_query_logtype.m_is_potentially_in_dict[i] = true;
new_query_logtype.set_var_is_potentially_in_dict(i, true);
// TODO: sketchy, but works cause < operator inserts it after current iterator
query_logtypes.insert(new_query_logtype);
}
Expand Down Expand Up @@ -1199,11 +1259,11 @@ void Grep::generate_sub_queries(
// encoded in the segment, we just assume it exists in the segment, as we estimate that
// checking is slower than decompressing.
SubQuery sub_query;
for (uint32_t i = 0; i < query_logtype.m_logtype.size(); i++) {
auto const& logtype_value = query_logtype.m_logtype[i];
auto const& raw_string = query_logtype.m_search_query[i];
auto const& is_dict_var = query_logtype.m_is_potentially_in_dict[i];
auto const& var_has_wildcard = query_logtype.m_var_has_wildcard[i];
for (uint32_t i = 0; i < query_logtype.get_logtype_size(); i++) {
auto const& logtype_value = query_logtype.get_logtype_value(i);
auto const& raw_string = query_logtype.get_query_string(i);
auto const& is_dict_var = query_logtype.get_is_potentially_in_dict(i);
auto const& var_has_wildcard = query_logtype.get_has_wildcard(i);
if (std::holds_alternative<int>(logtype_value)) {
auto& schema_type = lexer.m_id_symbol[std::get<int>(logtype_value)];
encoded_variable_t encoded_var;
Expand Down
129 changes: 49 additions & 80 deletions components/core/src/clp/Grep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,99 +24,68 @@ namespace clp {
*/
class QueryLogtype {
public:
std::vector<std::variant<char, int>> m_logtype;
std::vector<std::string> m_search_query;
std::vector<bool> m_is_potentially_in_dict;
std::vector<bool> m_var_has_wildcard;
QueryLogtype() = default;

QueryLogtype(
std::variant<char, int> const& val,
std::string const& string,
bool var_contains_wildcard
) {
append_value(val, string, var_contains_wildcard);
}

/**
* @param rhs
* @return true if the current logtype is shorter than rhs, false if the current logtype
* is longer. If equally long, true if the current logtype is lexicographically smaller than
* rhs, false if bigger. If the logtypes are identical, true if the current search query is
* lexicographically smaller than rhs, false if bigger. If the search queries are identical,
* true if the first mismatch in special character locations is a non-special character for the
* current logtype, false otherwise.
*/
bool operator<(QueryLogtype const& rhs) const;

/**
* Append a logtype to the current logtype.
* @param suffix
*/
auto append_logtype(QueryLogtype& suffix) -> void {
m_logtype.insert(m_logtype.end(), suffix.m_logtype.begin(), suffix.m_logtype.end());
m_search_query.insert(
m_search_query.end(),
suffix.m_search_query.begin(),
suffix.m_search_query.end()
);
m_is_potentially_in_dict.insert(
m_is_potentially_in_dict.end(),
suffix.m_is_potentially_in_dict.begin(),
suffix.m_is_potentially_in_dict.end()
);
m_var_has_wildcard.insert(
m_var_has_wildcard.end(),
suffix.m_var_has_wildcard.begin(),
suffix.m_var_has_wildcard.end()
);
}
void append_logtype(QueryLogtype& suffix);

/**
* Append a single value to the current logtype.
* @param val
* @param string
* @param var_contains_wildcard
*/
auto append_value(
void append_value(
std::variant<char, int> const& val,
std::string const& string,
bool var_contains_wildcard
) -> void {
m_var_has_wildcard.push_back(var_contains_wildcard);
m_logtype.push_back(val);
m_search_query.push_back(string);
m_is_potentially_in_dict.push_back(false);
);

void set_var_is_potentially_in_dict(uint32_t i, bool value) {
m_is_potentially_in_dict[i] = value;
}

QueryLogtype(
std::variant<char, int> const& val,
std::string const& string,
bool var_contains_wildcard
) {
append_value(val, string, var_contains_wildcard);
[[nodiscard]] uint32_t get_logtype_size() const { return m_logtype.size(); }

[[nodiscard]] std::variant<char, int> get_logtype_value(uint32_t i) const {
return m_logtype[i];
}

QueryLogtype() = default;
[[nodiscard]] std::string const& get_query_string(uint32_t i) const { return m_query[i]; }

/**
* @param rhs
* @return true if the current logtype is shorter than rhs, false if the current logtype
* is longer. If equally long, true if the current logtype is lexicographically smaller than
* rhs, false if bigger. If the logtypes are identical, true if the current search query is
* lexicographically smaller than rhs, false if bigger. If the search queries are identical,
* true if the first mismatch in special character locations is a non-special character for the
* current logtype, false otherwise.
*/
bool operator<(QueryLogtype const& rhs) const {
if (m_logtype.size() < rhs.m_logtype.size()) {
return true;
} else if (m_logtype.size() > rhs.m_logtype.size()) {
return false;
}
for (uint32_t i = 0; i < m_logtype.size(); i++) {
if (m_logtype[i] < rhs.m_logtype[i]) {
return true;
} else if (m_logtype[i] > rhs.m_logtype[i]) {
return false;
}
}
for (uint32_t i = 0; i < m_search_query.size(); i++) {
if (m_search_query[i] < rhs.m_search_query[i]) {
return true;
} else if (m_search_query[i] > rhs.m_search_query[i]) {
return false;
}
}
for (uint32_t i = 0; i < m_is_potentially_in_dict.size(); i++) {
if (m_is_potentially_in_dict[i] < rhs.m_is_potentially_in_dict[i]) {
return true;
} else if (m_is_potentially_in_dict[i] > rhs.m_is_potentially_in_dict[i]) {
return false;
}
}
return false;
[[nodiscard]] bool get_is_potentially_in_dict(uint32_t i) const {
return m_is_potentially_in_dict[i];
}

[[nodiscard]] bool get_has_wildcard(uint32_t i) const { return m_has_wildcard[i]; }

private:
std::vector<std::variant<char, int>> m_logtype;
std::vector<std::string> m_query;
std::vector<bool> m_is_potentially_in_dict;
std::vector<bool> m_has_wildcard;
};

class Grep {
Expand All @@ -135,7 +104,7 @@ class Grep {
std::string const& decompressed_msg,
void* custom_arg
);

// Methods
/**
* Processes a raw user query into a Query
Expand Down Expand Up @@ -173,7 +142,7 @@ class Grep {
size_t& end_pos,
bool& is_var
);

/**
* Marks which sub-queries in each query are relevant to the given file
* @param compressed_file
Expand Down Expand Up @@ -257,12 +226,12 @@ class Grep {

/**
* Compare all possible query logtypes against the archive to determine all possible sub queries
* that can match against messages in the archive.
* @param query_logtypes
* @param archive
* @param lexer
* @param ignore_case
* @param sub_queries
* that can match against messages in the archive.
* @param query_logtypes
* @param archive
* @param lexer
* @param ignore_case
* @param sub_queries
*/
static void generate_sub_queries(
std::set<QueryLogtype>& query_logtypes,
Expand Down
1 change: 0 additions & 1 deletion components/core/src/clp/StringReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ using std::string;
namespace clp {
StringReader::~StringReader() {
close();
free(m_getdelim_buf);
}

ErrorCode StringReader::try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) {
Expand Down
8 changes: 0 additions & 8 deletions components/core/src/clp/StringReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ class StringReader : public ReaderInterface {
* Closes the file if it's open
*/
void close();
/**
* Tries to stat the current file
* @param stat_buffer
* @return ErrorCode_errno on error
* @return ErrorCode_Success on success
*/
private:
size_t m_getdelim_buf_len{0};
char* m_getdelim_buf{nullptr};
std::string m_input_string;
uint32_t m_pos{0};
bool m_string_is_set{false};
Expand Down
19 changes: 19 additions & 0 deletions components/core/tests/test-Stopwatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ TEST_CASE("Stopwatch", "[Stopwatch]") {
REQUIRE(time_taken < 1.1);
}
}

SECTION("Test multiple measurements") {
// Measure some work
stopwatch.start();
sleep(1);
stopwatch.stop();

// Do some other work
sleep(1);

// Measure some work again
stopwatch.start();
sleep(2);
stopwatch.stop();

double time_taken = stopwatch.get_time_taken_in_seconds();
REQUIRE(time_taken >= 3.0);
REQUIRE(time_taken < 3.1);
}

0 comments on commit b84a354

Please sign in to comment.