Source browser/Examples
C++ · complete file

02_batch_callback.cpp

Receive grouped data and iterate the records in each source range.

examples/two_buf_queue/02_batch_callback.cpp
34 lines1.7 KiBClick a line number to link to it
02_batch_callback.cpp
1// TwoBufQueueBatch: one callback receives a batch rather than one record at a time.2// Useful when applications split a contiguous batch themselves, possibly processing its parts in parallel.34#include "../example_support.hpp"5using namespace TwoBufQueue;67int main() try {8    //---------------1. Initialize a frameless writer with no length prefix--9    auto writer = make_writer<Writer<1, false>>();1011    std::size_t values = 0;12    // Consumer callback: receive a contiguous batch and split its records; see read_records for a parser.13    auto rcvCallback = [&](const DataBatch& batch) noexcept -> bool {14        for (const auto& frame : batch.frames)15            values += frame.size / sizeof(std::uint64_t); // Each record is one 8-byte value.16        return true; // The whole batch is handled; false rejects the entire batch for a later retry.17    }; // Batch storage may be released as soon as the callback returns. Parallel subtasks must all finish before returning; never retain the batch's pointers.1819    ::TwoBufQueue::TwoBufQueueBatch queue(rcvCallback, writer);2021    //---------------2. Produce 100 uint64_t records-------------------------22    bool written = true;23    for (std::uint64_t i = 0; i < 100; ++i)24        written &= writer.write(i);2526    //---------------3. Finish----------------------------------------------27    writer.finish();28    queue.stop();2930    //---------------4. Verify----------------------------------------------31    example::require(written && values == 100 &&32                     queue.error_code() == ReadError::none, "batch delivery mismatch");33    std::puts("PASS: 100 frameless records delivered in batches");34} catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; }

Continue reading

TwoBufQueue · Full guideTwoBufQueue · Full guide (Chinese)API usage guideAPI usage guide (Chinese)ExamplesExamples (Chinese)