11#include < algorithm>
22#include < cstdlib>
3+ #include < filesystem>
4+ #include < fstream>
35#include < iostream>
46#include < random>
57#include < vector>
1012
1113#include < sparrow/record_batch.hpp>
1214
15+ const std::filesystem::path arrow_testing_data_dir = ARROW_TESTING_DATA_DIR;
16+ const std::filesystem::path tests_resources_files_path = arrow_testing_data_dir / " data" / " arrow-ipc-stream"
17+ / " integration" / " cpp-21.0.0" ;
18+
19+
1320namespace sp = sparrow;
1421
1522// Random number generator
@@ -23,34 +30,26 @@ std::mt19937 gen(rd());
2330sp::record_batch create_random_record_batch (size_t num_rows)
2431{
2532 std::uniform_int_distribution<int32_t > int_dist (0 , 1000 );
26- std::uniform_real_distribution<float > float_dist (-100 .0f , 100 .0f );
27- std::uniform_int_distribution<int > bool_dist (0 , 1 );
33+
2834
2935 // Create integer column with random values
3036 std::vector<int32_t > int_values;
3137 int_values.reserve (num_rows);
32- for (size_t i = 0 ; i < num_rows; ++i)
33- {
34- int_values.push_back (int_dist (gen));
35- }
38+ std::generate_n (std::back_inserter (int_values), num_rows, [&]() { return int_dist (gen); });
3639 auto int_array = sp::primitive_array<int32_t >(std::move (int_values));
3740
3841 // Create float column with random values
42+ std::uniform_real_distribution<float > float_dist (-100 .0f , 100 .0f );
3943 std::vector<float > float_values;
4044 float_values.reserve (num_rows);
41- for (size_t i = 0 ; i < num_rows; ++i)
42- {
43- float_values.push_back (float_dist (gen));
44- }
45+ std::generate_n (std::back_inserter (float_values), num_rows, [&]() { return float_dist (gen); });
4546 auto float_array = sp::primitive_array<float >(std::move (float_values));
4647
4748 // Create boolean column with random values
49+ std::uniform_int_distribution<int > bool_dist (0 , 1 );
4850 std::vector<bool > bool_values;
4951 bool_values.reserve (num_rows);
50- for (size_t i = 0 ; i < num_rows; ++i)
51- {
52- bool_values.push_back (static_cast <bool >(bool_dist (gen)));
53- }
52+ std::generate_n (std::back_inserter (bool_values), num_rows, [&]() { return static_cast <bool >(bool_dist (gen)); });
5453 auto bool_array = sp::primitive_array<bool >(std::move (bool_values));
5554
5655 // Create string column with random values
@@ -59,11 +58,7 @@ sp::record_batch create_random_record_batch(size_t num_rows)
5958 const std::vector<std::string> sample_strings =
6059 {" alpha" , " beta" , " gamma" , " delta" , " epsilon" , " zeta" , " eta" , " theta" , " iota" , " kappa" };
6160 std::uniform_int_distribution<size_t > str_dist (0 , sample_strings.size () - 1 );
62-
63- for (size_t i = 0 ; i < num_rows; ++i)
64- {
65- string_values.push_back (sample_strings[str_dist (gen)] + " _" + std::to_string (i));
66- }
61+ std::generate_n (std::back_inserter (string_values), num_rows, [&]() { return sample_strings[str_dist (gen)] + " _" + std::to_string (string_values.size ()); });
6762 auto string_array = sp::string_array (std::move (string_values));
6863
6964 // Create record batch with named columns (same schema for all batches)
@@ -277,6 +272,50 @@ int main()
277272 std::cerr << " ✗ Schema inconsistency detected!\n " ;
278273 }
279274
275+ // Step 8: Read and display a primitive stream file from test resources
276+ std::cout << " \n 8. Reading a primitive stream file from test resources...\n " ;
277+
278+ const std::filesystem::path primitive_stream_file = tests_resources_files_path / " generated_primitive.stream" ;
279+
280+ if (std::filesystem::exists (primitive_stream_file))
281+ {
282+ std::cout << " Reading file: " << primitive_stream_file << " \n " ;
283+
284+ // Read the stream file
285+ std::ifstream stream_file (primitive_stream_file, std::ios::in | std::ios::binary);
286+ if (!stream_file.is_open ())
287+ {
288+ std::cerr << " ERROR: Could not open stream file!\n " ;
289+ }
290+ else
291+ {
292+ const std::vector<uint8_t > file_stream_data (
293+ (std::istreambuf_iterator<char >(stream_file)),
294+ (std::istreambuf_iterator<char >())
295+ );
296+ stream_file.close ();
297+
298+ std::cout << " File size: " << file_stream_data.size () << " bytes\n " ;
299+
300+ // Deserialize the stream
301+ auto file_batches = sparrow_ipc::deserialize_stream (file_stream_data);
302+
303+ std::cout << " Deserialized " << file_batches.size () << " record batch(es) from file\n " ;
304+
305+ // Display the first batch
306+ if (!file_batches.empty ())
307+ {
308+ std::cout << " First batch from file:\n " ;
309+ std::cout << std::format (" {}\n " , file_batches[0 ]);
310+ }
311+ }
312+ }
313+ else
314+ {
315+ std::cout << " Note: Test resource file not found at " << primitive_stream_file << " \n " ;
316+ std::cout << " This is expected if test data is not available.\n " ;
317+ }
318+
280319 std::cout << " \n === Example completed successfully! ===\n " ;
281320 }
282321 catch (const std::exception& e)
0 commit comments