Skip to content

Commit f88e168

Browse files
committed
Add examples
1 parent 5dd96b7 commit f88e168

File tree

6 files changed

+347
-10
lines changed

6 files changed

+347
-10
lines changed

.github/workflows/linux.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ jobs:
5959
working-directory: build
6060
run: cmake --build . --target run_example
6161

62+
- name: Build deserializer example
63+
working-directory: build
64+
run: cmake --build . --target deserializer_example
65+
66+
- name: Run deserializer example
67+
working-directory: build
68+
run: cmake --build . --target run_deserializer_example
69+
6270
- name: Install
6371
working-directory: build
6472
run: cmake --install .
@@ -103,6 +111,14 @@ jobs:
103111
working-directory: build
104112
run: cmake --build . --target run_example
105113

114+
- name: Build deserializer example
115+
working-directory: build
116+
run: cmake --build . --target deserializer_example
117+
118+
- name: Run deserializer example
119+
working-directory: build
120+
run: cmake --build . --target run_deserializer_example
121+
106122
- name: Install
107123
working-directory: build
108124
run: sudo cmake --install .

.github/workflows/osx.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ jobs:
6464
working-directory: build
6565
run: cmake --build . --target run_example
6666

67+
- name: Build deserializer example
68+
working-directory: build
69+
run: cmake --build . --target deserializer_example
70+
71+
- name: Run deserializer example
72+
working-directory: build
73+
run: cmake --build . --target run_deserializer_example
74+
6775
- name: Install
6876
working-directory: build
6977
run: cmake --install .
@@ -113,6 +121,14 @@ jobs:
113121
working-directory: build
114122
run: cmake --build . --target run_example
115123

124+
- name: Build deserializer example
125+
working-directory: build
126+
run: cmake --build . --target deserializer_example
127+
128+
- name: Run deserializer example
129+
working-directory: build
130+
run: cmake --build . --target run_deserializer_example
131+
116132
- name: Install
117133
working-directory: build
118134
run: sudo cmake --install .

.github/workflows/windows.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ jobs:
6464
working-directory: build
6565
run: cmake --build . --config ${{ matrix.build_type }} --target run_example
6666

67+
- name: Build deserializer example
68+
working-directory: build
69+
run: cmake --build . --config ${{ matrix.build_type }} --target deserializer_example
70+
71+
- name: Run deserializer example
72+
working-directory: build
73+
run: cmake --build . --config ${{ matrix.build_type }} --target run_deserializer_example
74+
6775
- name: Install
6876
working-directory: build
6977
run: cmake --install . --config ${{ matrix.build_type }}
@@ -113,6 +121,14 @@ jobs:
113121
working-directory: build
114122
run: cmake --build . --config ${{ matrix.build_type }} --target run_example
115123

124+
- name: Build deserializer example
125+
working-directory: build
126+
run: cmake --build . --config ${{ matrix.build_type }} --target deserializer_example
127+
128+
- name: Run deserializer example
129+
working-directory: build
130+
run: cmake --build . --config ${{ matrix.build_type }} --target run_deserializer_example
131+
116132
- name: Install
117133
working-directory: build
118134
run: cmake --install . --config ${{ matrix.build_type }}

README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,20 @@ void stream_record_batches(std::ostream& os, record_batch_source& source)
117117
namespace sp = sparrow;
118118
namespace sp_ipc = sparrow_ipc;
119119
120-
std::vector<sp::record_batch> deserialize_stream_to_batches(const std::vector<uint8_t>& stream_data)
120+
std::vector<sp::record_batch> deserialize_stream_example(const std::vector<uint8_t>& stream_data)
121121
{
122+
// Deserialize the entire stream at once
122123
auto batches = sp_ipc::deserialize_stream(stream_data);
123124
return batches;
124125
}
125126
```
126127

127128
#### Using the deserializer class
128129

130+
The deserializer class allows you to accumulate record batches into an existing container as you deserialize data:
131+
129132
```cpp
133+
#include <iostream>
130134
#include <span>
131135
#include <vector>
132136
#include <sparrow_ipc/deserializer.hpp>
@@ -135,22 +139,56 @@ std::vector<sp::record_batch> deserialize_stream_to_batches(const std::vector<ui
135139
namespace sp = sparrow;
136140
namespace sp_ipc = sparrow_ipc;
137141

138-
void deserialize_incremental_stream(const std::vector<std::vector<uint8_t>>& stream_chunks)
142+
void deserializer_basic_example(const std::vector<uint8_t>& stream_data)
139143
{
144+
// Create a container to hold the deserialized batches
140145
std::vector<sp::record_batch> batches;
146+
147+
// Create a deserializer that will append to our container
141148
sp_ipc::deserializer deser(batches);
142149

143-
// Deserialize chunks incrementally as they arrive
144-
for (const auto& chunk : stream_chunks)
150+
// Deserialize the stream data
151+
deser.deserialize(std::span<const uint8_t>(stream_data));
152+
153+
// Process the accumulated batches
154+
for (const auto& batch : batches)
145155
{
146-
deser << std::span<const uint8_t>(chunk);
156+
std::cout << "Batch with " << batch.nb_rows() << " rows and " << batch.nb_columns() << " columns\n";
147157
}
158+
}
159+
```
148160

149-
// Process accumulated batches
150-
for (const auto& batch : batches)
161+
#### Incremental deserialization
162+
163+
The deserializer class is particularly useful for streaming scenarios where data arrives in chunks:
164+
165+
```cpp
166+
#include <iostream>
167+
#include <span>
168+
#include <vector>
169+
#include <sparrow_ipc/deserializer.hpp>
170+
#include <sparrow/record_batch.hpp>
171+
172+
namespace sp = sparrow;
173+
namespace sp_ipc = sparrow_ipc;
174+
175+
void deserializer_incremental_example(const std::vector<std::vector<uint8_t>>& stream_chunks)
176+
{
177+
// Container to accumulate all deserialized batches
178+
std::vector<sp::record_batch> batches;
179+
180+
// Create a deserializer
181+
sp_ipc::deserializer deser(batches);
182+
183+
// Deserialize chunks as they arrive using the streaming operator
184+
for (const auto& chunk : stream_chunks)
151185
{
152-
// Process each batch...
186+
deser << std::span<const uint8_t>(chunk);
187+
std::cout << "After chunk: " << batches.size() << " batches accumulated\n";
153188
}
189+
190+
// All batches are now available in the container
191+
std::cout << "Total batches deserialized: " << batches.size() << "\n";
154192
}
155193
```
156194

examples/CMakeLists.txt

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ target_link_libraries(write_and_read_streams
1111
arrow-testing-data
1212
)
1313

14+
# Create executable for the deserializer_example
15+
add_executable(deserializer_example deserializer_example.cpp)
16+
17+
# Link against sparrow-ipc and its dependencies
18+
target_link_libraries(deserializer_example
19+
PRIVATE
20+
sparrow-ipc
21+
sparrow::sparrow
22+
)
23+
1424
# Set C++ standard to match the main project
15-
set_target_properties(write_and_read_streams
25+
set_target_properties(write_and_read_streams deserializer_example
1626
PROPERTIES
1727
CXX_STANDARD 20
1828
CXX_STANDARD_REQUIRED ON
@@ -26,8 +36,15 @@ target_include_directories(write_and_read_streams
2636
${CMAKE_BINARY_DIR}/generated
2737
)
2838

39+
target_include_directories(deserializer_example
40+
PRIVATE
41+
${CMAKE_SOURCE_DIR}/include
42+
${CMAKE_BINARY_DIR}/generated
43+
)
44+
2945
# Ensure generated flatbuffer headers are available
3046
add_dependencies(write_and_read_streams generate_flatbuffers_headers)
47+
add_dependencies(deserializer_example generate_flatbuffers_headers)
3148

3249
# Optional: Copy to build directory for easy execution
3350
if(WIN32)
@@ -38,7 +55,7 @@ if(WIN32)
3855
set(ZSTD_DLL_TARGET libzstd_static)
3956
endif()
4057

41-
# On Windows, copy required DLLs
58+
# On Windows, copy required DLLs for write_and_read_streams
4259
set(DLL_COPY_COMMANDS "") # Initialize a list to hold all copy commands
4360
# Add unconditional copy commands
4461
list(APPEND DLL_COPY_COMMANDS
@@ -66,6 +83,31 @@ if(WIN32)
6683
${DLL_COPY_COMMANDS}
6784
COMMENT "Copying required DLLs to example executable directory"
6885
)
86+
87+
# On Windows, copy required DLLs for deserializer_example
88+
set(DLL_COPY_COMMANDS_DESER "") # Initialize a list to hold all copy commands
89+
list(APPEND DLL_COPY_COMMANDS_DESER
90+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
91+
"$<TARGET_FILE:sparrow::sparrow>"
92+
"$<TARGET_FILE_DIR:deserializer_example>"
93+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
94+
"$<TARGET_FILE:sparrow-ipc>"
95+
"$<TARGET_FILE_DIR:deserializer_example>"
96+
)
97+
98+
if(ZSTD_DLL_TARGET)
99+
list(APPEND DLL_COPY_COMMANDS_DESER
100+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
101+
"$<TARGET_FILE:${ZSTD_DLL_TARGET}>"
102+
"$<TARGET_FILE_DIR:deserializer_example>"
103+
)
104+
endif()
105+
106+
add_custom_command(
107+
TARGET deserializer_example POST_BUILD
108+
${DLL_COPY_COMMANDS_DESER}
109+
COMMENT "Copying required DLLs to deserializer_example executable directory"
110+
)
69111
endif()
70112

71113
# Create a custom target to easily run the example
@@ -77,3 +119,13 @@ add_custom_target(run_example
77119
)
78120

79121
set_target_properties(run_example PROPERTIES FOLDER "Examples")
122+
123+
# Create a custom target to run the deserializer example
124+
add_custom_target(run_deserializer_example
125+
COMMAND deserializer_example
126+
DEPENDS deserializer_example
127+
COMMENT "Running deserializer_example"
128+
USES_TERMINAL
129+
)
130+
131+
set_target_properties(run_deserializer_example PROPERTIES FOLDER "Examples")

0 commit comments

Comments
 (0)