Skip to content

Commit

Permalink
Refactor many clp_s components to use string_view, and remove some du…
Browse files Browse the repository at this point in the history
…plicated code
  • Loading branch information
gibber9809 committed Dec 13, 2024
1 parent 909e19e commit 3d62e5e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion components/core/src/clp_s/ArchiveWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ArchiveWriter {
epochtime_t ingest_timestamp_entry(
std::string const& key,
int32_t node_id,
std::string const& timestamp,
std::string_view const timestamp,
uint64_t& pattern_id
) {
return m_timestamp_dict.ingest_entry(key, node_id, timestamp, pattern_id);
Expand Down
9 changes: 9 additions & 0 deletions components/core/src/clp_s/ParsedMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <variant>

Expand Down Expand Up @@ -34,6 +35,10 @@ class ParsedMessage {
m_message.emplace(node_id, value);
}

inline void add_value(int32_t node_id, std::string_view const value) {
m_message.emplace(node_id, std::string{value});
}

/**
* Adds a timestamp value and its encoding to the message for a given MST node ID.
* @param node_id
Expand All @@ -55,6 +60,10 @@ class ParsedMessage {
m_unordered_message.emplace_back(value);
}

inline void add_unordered_value(std::string_view const value) {
m_unordered_message.emplace_back(std::string{value});
}

/**
* Clears the message
*/
Expand Down
10 changes: 6 additions & 4 deletions components/core/src/clp_s/TimestampDictionaryWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "TimestampDictionaryWriter.hpp"

#include <cstdint>
#include <sstream>
#include <string_view>

#include "Utils.hpp"

Expand Down Expand Up @@ -42,9 +44,9 @@ uint64_t TimestampDictionaryWriter::get_pattern_id(TimestampPattern const* patte
}

epochtime_t TimestampDictionaryWriter::ingest_entry(
std::string const& key,
std::string_view const key,
int32_t node_id,
std::string const& timestamp,
std::string_view const timestamp,
uint64_t& pattern_id
) {
epochtime_t ret;
Expand Down Expand Up @@ -88,7 +90,7 @@ epochtime_t TimestampDictionaryWriter::ingest_entry(
}

void TimestampDictionaryWriter::ingest_entry(
std::string const& key,
std::string_view const key,
int32_t node_id,
double timestamp
) {
Expand All @@ -103,7 +105,7 @@ void TimestampDictionaryWriter::ingest_entry(
}

void TimestampDictionaryWriter::ingest_entry(
std::string const& key,
std::string_view const key,
int32_t node_id,
int64_t timestamp
) {
Expand Down
10 changes: 6 additions & 4 deletions components/core/src/clp_s/TimestampDictionaryWriter.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef CLP_S_TIMESTAMPDICTIONARYWRITER_HPP
#define CLP_S_TIMESTAMPDICTIONARYWRITER_HPP

#include <cstdint>
#include <map>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -47,9 +49,9 @@ class TimestampDictionaryWriter {
* @return the epoch time corresponding to the string timestamp
*/
epochtime_t ingest_entry(
std::string const& key,
std::string_view const key,
int32_t node_id,
std::string const& timestamp,
std::string_view const timestamp,
uint64_t& pattern_id
);

Expand All @@ -59,9 +61,9 @@ class TimestampDictionaryWriter {
* @param node_id
* @param timestamp
*/
void ingest_entry(std::string const& key, int32_t node_id, double timestamp);
void ingest_entry(std::string_view const key, int32_t node_id, double timestamp);

void ingest_entry(std::string const& key, int32_t node_id, int64_t timestamp);
void ingest_entry(std::string_view const key, int32_t node_id, int64_t timestamp);

/**
* TODO: guarantee epoch milliseconds. The current clp-s approach to encoding timestamps and
Expand Down
3 changes: 2 additions & 1 deletion components/core/src/clp_s/TimestampEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <sstream>
#include <string>
#include <string_view>
#include <unordered_set>
#include <variant>

Expand Down Expand Up @@ -43,7 +44,7 @@ class TimestampEntry {
m_epoch_start(cEpochTimeMax),
m_epoch_end(cEpochTimeMin) {}

TimestampEntry(std::string const& key_name)
TimestampEntry(std::string_view const key_name)
: m_encoding(UnkownTimestampEncoding),
m_epoch_start_double(cDoubleEpochTimeMax),
m_epoch_end_double(cDoubleEpochTimeMin),
Expand Down
13 changes: 7 additions & 6 deletions components/core/src/clp_s/TimestampPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <chrono>
#include <cstring>
#include <string_view>
#include <vector>

#include <date/include/date/date.h>
Expand Down Expand Up @@ -71,7 +72,7 @@ append_padded_value_notz(int value, char padding_character, size_t max_length, s
* @return true if conversion succeeds, false otherwise
*/
static bool convert_string_to_number(
string const& str,
std::string_view const str,
size_t begin_ix,
size_t end_ix,
char padding_character,
Expand All @@ -89,7 +90,7 @@ static bool convert_string_to_number(
* @return true if conversion succeeds, false otherwise
*/
static bool convert_string_to_number_notz(
string const& str,
std::string_view const str,
size_t max_digits,
size_t begin_ix,
size_t& end_ix,
Expand Down Expand Up @@ -125,7 +126,7 @@ append_padded_value_notz(int value, char padding_character, size_t max_length, s
}

static bool convert_string_to_number(
string const& str,
std::string_view const str,
size_t begin_ix,
size_t end_ix,
char padding_character,
Expand Down Expand Up @@ -154,7 +155,7 @@ static bool convert_string_to_number(
}

static bool convert_string_to_number_notz(
string const& str,
std::string_view const str,
size_t max_digits,
size_t begin_ix,
size_t& end_ix,
Expand Down Expand Up @@ -306,7 +307,7 @@ void TimestampPattern::init() {
}

TimestampPattern const* TimestampPattern::search_known_ts_patterns(
string const& line,
std::string_view const line,
epochtime_t& timestamp,
size_t& timestamp_begin_pos,
size_t& timestamp_end_pos
Expand Down Expand Up @@ -342,7 +343,7 @@ void TimestampPattern::clear() {
}

bool TimestampPattern::parse_timestamp(
string const& line,
std::string_view const line,
epochtime_t& timestamp,
size_t& timestamp_begin_pos,
size_t& timestamp_end_pos
Expand Down
5 changes: 3 additions & 2 deletions components/core/src/clp_s/TimestampPattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string_view>
#include <utility>

#include "Defs.hpp"
Expand Down Expand Up @@ -83,7 +84,7 @@ class TimestampPattern {
* @return pointer to the timestamp pattern if found, nullptr otherwise
*/
static TimestampPattern const* search_known_ts_patterns(
std::string const& line,
std::string_view const line,
epochtime_t& timestamp,
size_t& timestamp_begin_pos,
size_t& timestamp_end_pos
Expand Down Expand Up @@ -121,7 +122,7 @@ class TimestampPattern {
* @return true if parsed successfully, false otherwise
*/
bool parse_timestamp(
std::string const& line,
std::string_view const line,
epochtime_t& timestamp,
size_t& timestamp_begin_pos,
size_t& timestamp_end_pos
Expand Down
15 changes: 3 additions & 12 deletions components/core/src/clp_s/search/StringLiteral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <memory>
#include <string>

#include "../Utils.hpp"
#include "Literal.hpp"

namespace clp_s::search {
Expand Down Expand Up @@ -69,18 +70,8 @@ class StringLiteral : public Literal {
}

// If '?' and '*' are not escaped, we add LiteralType::ClpStringT to m_string_type
bool escape = false;
for (char const c : m_v) {
if ('\\' == c) {
escape = !escape;
} else if ('?' == c || '*' == c) {
if (false == escape) {
m_string_type |= LiteralType::ClpStringT;
break;
}
} else {
escape = false;
}
if (StringUtils::has_unescaped_wildcards(m_v)) {
m_string_type |= LiteralType::ClpStringT;
}
}
};
Expand Down

0 comments on commit 3d62e5e

Please sign in to comment.