Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StreamReaderDataContext to encapsulate readers instead of zstd decompressors; Change its template type to deserializer. #24

17 changes: 10 additions & 7 deletions src/clp_ffi_js/ir/StreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

using namespace std::literals::string_literals;
using clp::ir::four_byte_encoded_variable_t;
using clp::ir::LogEventDeserializer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this into namespace per kirk suggestion in last pr.


namespace clp_ffi_js::ir {
auto StreamReader::create(DataArrayTsType const& data_array) -> StreamReader {
Expand Down Expand Up @@ -89,11 +90,12 @@ auto StreamReader::create(DataArrayTsType const& data_array) -> StreamReader {
};
}

StreamReaderDataContext<four_byte_encoded_variable_t> stream_reader_data_context{
std::move(data_buffer),
std::move(zstd_decompressor),
std::move(result.value())
};
StreamReaderDataContext<LogEventDeserializer<four_byte_encoded_variable_t>>
stream_reader_data_context{
std::move(data_buffer),
std::move(zstd_decompressor),
std::move(result.value())
};
return StreamReader{std::move(stream_reader_data_context)};
}

Expand Down Expand Up @@ -244,10 +246,11 @@ auto StreamReader::decode_range(size_t begin_idx, size_t end_idx, bool use_filte
}

StreamReader::StreamReader(
StreamReaderDataContext<four_byte_encoded_variable_t>&& stream_reader_data_context
StreamReaderDataContext<LogEventDeserializer<four_byte_encoded_variable_t>>&&
stream_reader_data_context
)
: m_stream_reader_data_context{std::make_unique<
StreamReaderDataContext<four_byte_encoded_variable_t>>(
StreamReaderDataContext<LogEventDeserializer<four_byte_encoded_variable_t>>>(
std::move(stream_reader_data_context)
)},
m_ts_pattern{m_stream_reader_data_context->get_deserializer().get_timestamp_pattern()} {}
Expand Down
13 changes: 9 additions & 4 deletions src/clp_ffi_js/ir/StreamReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
#include <optional>
#include <vector>

#include <clp/ir/LogEventDeserializer.hpp>
#include <clp/ir/types.hpp>
#include <clp/TimestampPattern.hpp>
#include <emscripten/bind.h>
#include <emscripten/val.h>

#include <clp_ffi_js/ir/LogEventWithLevel.hpp>
#include <clp_ffi_js/ir/StreamReaderDataContext.hpp>
using clp::ir::four_byte_encoded_variable_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

using clp::ir::LogEventDeserializer;

namespace clp_ffi_js::ir {
EMSCRIPTEN_DECLARE_VAL_TYPE(DataArrayTsType);
Expand Down Expand Up @@ -97,12 +100,14 @@ class StreamReader {

private:
// Constructor
explicit StreamReader(StreamReaderDataContext<clp::ir::four_byte_encoded_variable_t>&&
stream_reader_data_context);
explicit StreamReader(
StreamReaderDataContext<LogEventDeserializer<four_byte_encoded_variable_t>>&&
stream_reader_data_context
);

// Variables
std::vector<LogEventWithLevel<clp::ir::four_byte_encoded_variable_t>> m_encoded_log_events;
std::unique_ptr<StreamReaderDataContext<clp::ir::four_byte_encoded_variable_t>>
std::vector<LogEventWithLevel<four_byte_encoded_variable_t>> m_encoded_log_events;
std::unique_ptr<StreamReaderDataContext<LogEventDeserializer<four_byte_encoded_variable_t>>>
m_stream_reader_data_context;
FilteredLogEventsMap m_filtered_log_event_map;
clp::TimestampPattern m_ts_pattern;
Expand Down
31 changes: 16 additions & 15 deletions src/clp_ffi_js/ir/StreamReaderDataContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@
#include <utility>

#include <clp/Array.hpp>
#include <clp/ir/LogEventDeserializer.hpp>
#include <clp/ir/types.hpp>
#include <clp/streaming_compression/zstd/Decompressor.hpp>
#include <clp/ReaderInterface.hpp>

namespace clp_ffi_js::ir {
/**
* The data context for a `StreamReader`. It encapsulates a chain of the following resources:
* A `clp::ir::LogEventDeserializer` that reads from a
* `clp::streaming_compression::zstd::Decompressor`, which in turn reads from a `clp::Array`.
* @tparam encoded_variable_t Type of encoded variables encoded in the stream.
* A CLP deserializer class that reads from a `clp::ReaderInterface`, which in turn reads from a
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
* `clp::Array`.
* @tparam deserializer_t Type of deserializer.
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
*/
template <typename encoded_variable_t>
template <typename deserializer_t>
class StreamReaderDataContext {
public:
// Constructors
StreamReaderDataContext(
clp::Array<char>&& data_buffer,
std::unique_ptr<clp::streaming_compression::zstd::Decompressor>&& zstd_decompressor,
clp::ir::LogEventDeserializer<clp::ir::four_byte_encoded_variable_t> deserializer
std::unique_ptr<clp::ReaderInterface>&& reader,
deserializer_t deserializer
)
: m_data_buffer{std::move(data_buffer)},
m_zstd_decompressor{std::move(zstd_decompressor)},
m_reader{std::move(reader)},
m_deserializer{std::move(deserializer)} {}

// Disable copy constructor and assignment operator
Expand All @@ -41,17 +39,20 @@ class StreamReaderDataContext {
~StreamReaderDataContext() = default;

// Methods
/**
* @return A reference to the reader.
*/
[[nodiscard]] auto get_reader() -> clp::ReaderInterface& { return *m_reader; }

/**
* @return A reference to the deserializer.
*/
[[nodiscard]] auto get_deserializer() -> clp::ir::LogEventDeserializer<encoded_variable_t>& {
return m_deserializer;
}
[[nodiscard]] auto get_deserializer() -> deserializer_t& { return m_deserializer; }

private:
clp::Array<char> m_data_buffer;
std::unique_ptr<clp::streaming_compression::zstd::Decompressor> m_zstd_decompressor;
clp::ir::LogEventDeserializer<encoded_variable_t> m_deserializer;
std::unique_ptr<clp::ReaderInterface> m_reader;
deserializer_t m_deserializer;
};
} // namespace clp_ffi_js::ir

Expand Down
Loading