-
Notifications
You must be signed in to change notification settings - Fork 3
Add output streams and serializers #29
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
Open
Alex-PLACET
wants to merge
13
commits into
QuantStack:main
Choose a base branch
from
Alex-PLACET:add_output_stream_and_serializers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,448
−1,601
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2e32ebb
Add output streams and serializers
Alex-PLACET c4d42eb
fix
Alex-PLACET 4785a7b
fix
Alex-PLACET b3beb9a
wip
Alex-PLACET 08e0183
fix
Alex-PLACET 36fe236
Update include/sparrow_ipc/serializer.hpp
Alex-PLACET b5b3d97
Fix name
Alex-PLACET 458a9cf
wip
Alex-PLACET 14952d0
Merge branch 'add_output_stream_and_serializers' of https://github.co…
Alex-PLACET 75c9827
Update include/sparrow_ipc/serializer.hpp
Alex-PLACET 7d0e68b
Merge branch 'add_output_stream_and_serializers' of https://github.co…
Alex-PLACET 37ec2fe
Try fix
Alex-PLACET c69100f
fix
Alex-PLACET File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <ranges> | ||
|
||
#include "sparrow_ipc/output_stream.hpp" | ||
|
||
namespace sparrow_ipc | ||
{ | ||
template <typename R> | ||
requires std::ranges::random_access_range<R> | ||
&& std::ranges::random_access_range<std::ranges::range_value_t<R>> | ||
&& std::same_as<typename std::ranges::range_value_t<R>::value_type, uint8_t> | ||
class chunked_memory_output_stream final : public output_stream | ||
{ | ||
public: | ||
|
||
explicit chunked_memory_output_stream(R& chunks) | ||
: m_chunks(&chunks) {}; | ||
|
||
std::size_t write(std::span<const std::uint8_t> span) override | ||
{ | ||
m_chunks->emplace_back(span.begin(), span.end()); | ||
return span.size(); | ||
} | ||
|
||
std::size_t write(std::vector<uint8_t>&& buffer) | ||
{ | ||
m_chunks->emplace_back(std::move(buffer)); | ||
return m_chunks->back().size(); | ||
} | ||
|
||
std::size_t write(uint8_t value, std::size_t count) override | ||
{ | ||
m_chunks->emplace_back(count, value); | ||
return count; | ||
} | ||
|
||
void reserve(std::size_t size) override | ||
{ | ||
m_chunks->reserve(size); | ||
} | ||
|
||
void reserve(const std::function<std::size_t()>& calculate_reserve_size) override | ||
{ | ||
m_chunks->reserve(calculate_reserve_size()); | ||
} | ||
|
||
size_t size() const override | ||
{ | ||
return std::accumulate( | ||
m_chunks->begin(), | ||
m_chunks->end(), | ||
0, | ||
[](size_t acc, const auto& chunk) | ||
{ | ||
return acc + chunk.size(); | ||
} | ||
); | ||
} | ||
|
||
void flush() override | ||
{ | ||
// Implementation for flushing memory | ||
} | ||
|
||
void close() override | ||
{ | ||
// Implementation for closing the stream | ||
} | ||
|
||
bool is_open() const override | ||
{ | ||
return true; | ||
} | ||
|
||
private: | ||
|
||
R* m_chunks; | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#pragma once | ||
|
||
#include <sparrow/record_batch.hpp> | ||
|
||
#include "sparrow_ipc/chunk_memory_output_stream.hpp" | ||
#include "sparrow_ipc/memory_output_stream.hpp" | ||
#include "sparrow_ipc/serialize.hpp" | ||
#include "sparrow_ipc/serialize_utils.hpp" | ||
#include "sparrow_ipc/config/config.hpp" | ||
|
||
namespace sparrow_ipc | ||
{ | ||
class SPARROW_IPC_API chunk_serializer | ||
{ | ||
public: | ||
|
||
chunk_serializer( | ||
const sparrow::record_batch& rb, | ||
chunked_memory_output_stream<std::vector<std::vector<uint8_t>>>& stream | ||
); | ||
|
||
template <std::ranges::input_range R> | ||
requires std::same_as<std::ranges::range_value_t<R>, sparrow::record_batch> | ||
chunk_serializer( | ||
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same range constraint issue: constructor uses record_batches[0] and record_batches.size(); tighten constraints to std::ranges::random_access_range + sized_range or adapt to first/advance. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
const R& record_batches, | ||
chunked_memory_output_stream<std::vector<std::vector<uint8_t>>>& stream | ||
) | ||
: m_pstream(&stream) | ||
{ | ||
if (record_batches.empty()) | ||
{ | ||
throw std::invalid_argument("Record batches collection is empty"); | ||
} | ||
m_dtypes = get_column_dtypes(record_batches[0]); | ||
|
||
m_pstream->reserve(record_batches.size() + 1); | ||
std::vector<uint8_t> buffer; | ||
memory_output_stream schema_stream(buffer); | ||
serialize_schema_message(record_batches[0], schema_stream); | ||
m_pstream->write(std::move(buffer)); | ||
append(record_batches); | ||
} | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void append(const sparrow::record_batch& rb); | ||
|
||
template <std::ranges::input_range R> | ||
requires std::same_as<std::ranges::range_value_t<R>, sparrow::record_batch> | ||
void append(const R& record_batches) | ||
{ | ||
if (m_ended) | ||
{ | ||
throw std::runtime_error("Cannot append to a serializer that has been ended"); | ||
} | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
m_pstream->reserve(m_pstream->size() + record_batches.size()); | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (const auto& rb : record_batches) | ||
{ | ||
std::vector<uint8_t> buffer; | ||
memory_output_stream stream(buffer); | ||
serialize_record_batch(rb, stream); | ||
m_pstream->write(std::move(buffer)); | ||
} | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void end(); | ||
|
||
private: | ||
|
||
std::vector<sparrow::data_type> m_dtypes; | ||
chunked_memory_output_stream<std::vector<std::vector<uint8_t>>>* m_pstream; | ||
bool m_ended{false}; | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
#include <cstdint> | ||
#include <variant> | ||
#include <span> | ||
|
||
#include "Message_generated.h" | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <fstream> | ||
#include <functional> | ||
|
||
#include "sparrow_ipc/output_stream.hpp" | ||
|
||
|
||
namespace sparrow_ipc | ||
{ | ||
class SPARROW_IPC_API file_output_stream final : public output_stream | ||
{ | ||
public: | ||
|
||
explicit file_output_stream(std::ofstream& file); | ||
|
||
std::size_t write(std::span<const std::uint8_t> span) override; | ||
|
||
std::size_t write(uint8_t value, std::size_t count = 1) override; | ||
|
||
size_t size() const override; | ||
|
||
void reserve(std::size_t size) override; | ||
|
||
void reserve(const std::function<std::size_t()>& calculate_reserve_size) override; | ||
|
||
void flush() override; | ||
|
||
void close() override; | ||
|
||
bool is_open() const override; | ||
|
||
private: | ||
|
||
std::ofstream& m_file; | ||
size_t m_written_bytes = 0; | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.