Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::BatchedBufferedData Class Reference
Inheritance diagram for duckdb::BatchedBufferedData:
Collaboration diagram for duckdb::BatchedBufferedData:

Public Member Functions

 BatchedBufferedData (ClientContext &context)
void Append (const DataChunk &chunk, idx_t batch)
void BlockSink (const InterruptState &blocked_sink, idx_t batch)
bool ShouldBlockBatch (idx_t batch)
StreamExecutionResult ExecuteTaskInternal (StreamQueryResult &result, ClientContextLock &context_lock) override
unique_ptr< DataChunk > Scan () override
void UpdateMinBatchIndex (idx_t min_batch_index)
bool IsMinimumBatchIndex (lock_guard< mutex > &lock, idx_t batch)
void CompleteBatch (idx_t batch)
bool BufferIsEmpty ()
void UnblockSinks () override
idx_t ReadQueueCapacity () const
idx_t BufferCapacity () const
Public Member Functions inherited from duckdb::BufferedData
 BufferedData (Type type, ClientContext &context)
StreamExecutionResult ReplenishBuffer (StreamQueryResult &result, ClientContextLock &context_lock)
shared_ptr< ClientContext > GetContext ()
bool Closed () const
void Close ()
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Attributes

static constexpr const BufferedData::Type TYPE = BufferedData::Type::BATCHED

Private Member Functions

void ResetReplenishState ()
void MoveCompletedBatches (lock_guard< mutex > &lock)

Private Attributes

map< idx_t, InProgressBatch > buffer
 The buffer where chunks are written before they are ready to be read.
idx_t buffer_capacity
atomic< idx_t > buffer_byte_count
deque< unique_ptr< DataChunk > > read_queue
 The queue containing the chunks that can be read.
idx_t read_queue_capacity
atomic< idx_t > read_queue_byte_count
map< idx_t, InterruptState > blocked_sinks
idx_t min_batch
idx_t lowest_moved_batch = 0
 Debug variable to verify that order is preserved correctly.

Additional Inherited Members

Protected Types inherited from duckdb::BufferedData
enum class  Type { SIMPLE , BATCHED }
Protected Attributes inherited from duckdb::BufferedData
Type type
weak_ptr< ClientContext > context
 This is weak to avoid a cyclical reference.
idx_t total_buffer_size
 The maximum amount of memory we should keep buffered.
mutex glock
 Protect against populate/fetch race condition.

Constructor & Destructor Documentation

◆ BatchedBufferedData()

duckdb::BatchedBufferedData::BatchedBufferedData ( ClientContext & context)
explicit
262416 : BufferedData(BufferedData::Type::BATCHED, context), buffer_byte_count(0), read_queue_byte_count(0), min_batch(0) {
262417 read_queue_capacity = (idx_t)(static_cast<double>(total_buffer_size) * 0.6);
262418 buffer_capacity = (idx_t)(static_cast<double>(total_buffer_size) * 0.4);
262419}
idx_t total_buffer_size
The maximum amount of memory we should keep buffered.
Definition duckdb.hpp:41223
weak_ptr< ClientContext > context
This is weak to avoid a cyclical reference.
Definition duckdb.hpp:41221
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ Append()

void duckdb::BatchedBufferedData::Append ( const DataChunk & chunk,
idx_t batch )
262592 {
262593 // We should never find any chunks with a smaller batch index than the minimum
262594
262595 auto chunk = make_uniq<DataChunk>();
262596 chunk->Initialize(Allocator::DefaultAllocator(), to_append.GetTypes());
262597 to_append.Copy(*chunk, 0);
262598 auto allocation_size = chunk->GetAllocationSize();
262599
262600 lock_guard<mutex> lock(glock);
262601 D_ASSERT(batch >= min_batch);
262602 auto is_minimum = IsMinimumBatchIndex(lock, batch);
262603 if (is_minimum) {
262604 for (auto &it : buffer) {
262605 auto batch_index = it.first;
262606 if (batch_index >= min_batch) {
262607 break;
262608 }
262609 // There should not be any batches in the buffer that are lower or equal to the minimum batch index
262610 throw InternalException("Batches remaining in buffer");
262611 }
262612 read_queue.push_back(std::move(chunk));
262613 read_queue_byte_count += allocation_size;
262614 } else {
262615 auto &in_progress_batch = buffer[batch];
262616 auto &chunks = in_progress_batch.chunks;
262617 in_progress_batch.completed = false;
262618 buffer_byte_count += allocation_size;
262619 chunks.push_back(std::move(chunk));
262620 }
262621}
deque< unique_ptr< DataChunk > > read_queue
The queue containing the chunks that can be read.
Definition duckdb.cpp:177338
map< idx_t, InProgressBatch > buffer
The buffer where chunks are written before they are ready to be read.
Definition duckdb.cpp:177333
mutex glock
Protect against populate/fetch race condition.
Definition duckdb.hpp:41225
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791

◆ BlockSink()

void duckdb::BatchedBufferedData::BlockSink ( const InterruptState & blocked_sink,
idx_t batch )
262409 {
262410 lock_guard<mutex> lock(glock);
262411 D_ASSERT(!blocked_sinks.count(batch));
262412 blocked_sinks.emplace(batch, blocked_sink);
262413}

◆ ShouldBlockBatch()

bool duckdb::BatchedBufferedData::ShouldBlockBatch ( idx_t batch)
262421 {
262422 lock_guard<mutex> lock(glock);
262423 bool is_minimum = IsMinimumBatchIndex(lock, batch);
262424 if (is_minimum) {
262425 // If there is room in the read queue, we want to process the minimum batch
262426 return read_queue_byte_count >= ReadQueueCapacity();
262427 }
262428 return buffer_byte_count >= BufferCapacity();
262429}

◆ ExecuteTaskInternal()

StreamExecutionResult duckdb::BatchedBufferedData::ExecuteTaskInternal ( StreamQueryResult & result,
ClientContextLock & context_lock )
overridevirtual

Implements duckdb::BufferedData.

262526 {
262527 auto cc = context.lock();
262528 if (!cc) {
262529 return StreamExecutionResult::EXECUTION_CANCELLED;
262530 }
262531
262532 if (!BufferIsEmpty()) {
262533 // The buffer isn't empty yet, just return
262534 return StreamExecutionResult::CHUNK_READY;
262535 }
262536 // Unblock any pending sinks if the buffer isnt full
262537 UnblockSinks();
262538 // Let the executor run until the buffer is no longer empty
262539 auto execution_result = cc->ExecuteTaskInternal(context_lock, result);
262540 if (!BufferIsEmpty()) {
262541 return StreamExecutionResult::CHUNK_READY;
262542 }
262543 if (execution_result == PendingExecutionResult::BLOCKED ||
262544 execution_result == PendingExecutionResult::RESULT_READY) {
262545 return StreamExecutionResult::BLOCKED;
262546 }
262547 if (result.HasError()) {
262548 Close();
262549 }
262550 switch (execution_result) {
262551 case PendingExecutionResult::NO_TASKS_AVAILABLE:
262552 case PendingExecutionResult::RESULT_NOT_READY:
262553 return StreamExecutionResult::CHUNK_NOT_READY;
262554 case PendingExecutionResult::EXECUTION_FINISHED:
262555 return StreamExecutionResult::EXECUTION_FINISHED;
262556 case PendingExecutionResult::EXECUTION_ERROR:
262557 return StreamExecutionResult::EXECUTION_ERROR;
262558 default:
262559 throw InternalException("No conversion from PendingExecutionResult (%s) -> StreamExecutionResult",
262560 EnumUtil::ToString(execution_result));
262561 }
262562}

◆ Scan()

unique_ptr< DataChunk > duckdb::BatchedBufferedData::Scan ( )
overridevirtual

Implements duckdb::BufferedData.

262575 {
262576 unique_ptr<DataChunk> chunk;
262577 lock_guard<mutex> lock(glock);
262578 if (!read_queue.empty()) {
262579 chunk = std::move(read_queue.front());
262580 read_queue.pop_front();
262581 auto allocation_size = chunk->GetAllocationSize();
262582 read_queue_byte_count -= allocation_size;
262583 } else {
262584 context.reset();
262585 D_ASSERT(blocked_sinks.empty());
262586 D_ASSERT(buffer.empty());
262587 return nullptr;
262588 }
262589 return chunk;
262590}

◆ UpdateMinBatchIndex()

void duckdb::BatchedBufferedData::UpdateMinBatchIndex ( idx_t min_batch_index)
262512 {
262513 lock_guard<mutex> lock(glock);
262514
262515 auto old_min_batch = min_batch;
262516 auto new_min_batch = MaxValue(old_min_batch, min_batch_index);
262517 if (new_min_batch == min_batch) {
262518 // No change, early out
262519 return;
262520 }
262521 min_batch = new_min_batch;
262522 MoveCompletedBatches(lock);
262523}

◆ IsMinimumBatchIndex()

bool duckdb::BatchedBufferedData::IsMinimumBatchIndex ( lock_guard< mutex > & lock,
idx_t batch )
262436 {
262437 return min_batch == batch;
262438}

◆ CompleteBatch()

void duckdb::BatchedBufferedData::CompleteBatch ( idx_t batch)
262564 {
262565 lock_guard<mutex> lock(glock);
262566 auto it = buffer.find(batch);
262567 if (it == buffer.end()) {
262568 return;
262569 }
262570
262571 auto &in_progress_batch = it->second;
262572 in_progress_batch.completed = true;
262573}

◆ BufferIsEmpty()

bool duckdb::BatchedBufferedData::BufferIsEmpty ( )
262431 {
262432 lock_guard<mutex> lock(glock);
262433 return read_queue.empty();
262434}

◆ UnblockSinks()

void duckdb::BatchedBufferedData::UnblockSinks ( )
overridevirtual

Implements duckdb::BufferedData.

262440 {
262441 lock_guard<mutex> lock(glock);
262442 stack<idx_t> to_remove;
262443 for (auto it = blocked_sinks.begin(); it != blocked_sinks.end(); it++) {
262444 auto batch = it->first;
262445 auto &blocked_sink = it->second;
262446 const bool is_minimum = IsMinimumBatchIndex(lock, batch);
262447 if (is_minimum) {
262448 if (read_queue_byte_count >= ReadQueueCapacity()) {
262449 continue;
262450 }
262451 } else {
262452 if (buffer_byte_count >= BufferCapacity()) {
262453 continue;
262454 }
262455 }
262456 blocked_sink.Callback();
262457 to_remove.push(batch);
262458 }
262459 while (!to_remove.empty()) {
262460 auto batch = to_remove.top();
262461 to_remove.pop();
262462 blocked_sinks.erase(batch);
262463 }
262464}

◆ ReadQueueCapacity()

idx_t duckdb::BatchedBufferedData::ReadQueueCapacity ( ) const
inline
177320 {
177321 return read_queue_capacity;
177322 }

◆ BufferCapacity()

idx_t duckdb::BatchedBufferedData::BufferCapacity ( ) const
inline
177323 {
177324 return buffer_capacity;
177325 }

◆ MoveCompletedBatches()

void duckdb::BatchedBufferedData::MoveCompletedBatches ( lock_guard< mutex > & lock)
private
262466 {
262467 stack<idx_t> to_remove;
262468 for (auto &it : buffer) {
262469 auto batch_index = it.first;
262470 auto &in_progress_batch = it.second;
262471 if (batch_index > min_batch) {
262472 break;
262473 }
262474 D_ASSERT(in_progress_batch.completed || batch_index == min_batch);
262475 // min_batch - took longer than others
262476 // min_batch+1 - completed before min_batch
262477 // min_batch+2 - completed before min_batch
262478 // new min_batch
262479 //
262480 // To preserve the order, the completed batches have to be processed before we can start scanning the "new
262481 // min_batch"
262482 auto &chunks = in_progress_batch.chunks;
262483
262484 idx_t batch_allocation_size = 0;
262485 for (auto it = chunks.begin(); it != chunks.end(); it++) {
262486 auto chunk = std::move(*it);
262487 auto allocation_size = chunk->GetAllocationSize();
262488 batch_allocation_size += allocation_size;
262489 read_queue.push_back(std::move(chunk));
262490 }
262491 // Verification to make sure we're not breaking the order by moving batches before the previous ones have
262492 // finished
262493 if (lowest_moved_batch > batch_index) {
262494 throw InternalException("Lowest moved batch is %d, attempted to move %d afterwards\nAttempted to move %d "
262495 "chunks, of %d bytes in total\nmin_batch is %d",
262496 lowest_moved_batch, batch_index, chunks.size(), batch_allocation_size, min_batch);
262497 }
262498 D_ASSERT(lowest_moved_batch <= batch_index);
262499 lowest_moved_batch = batch_index;
262500
262501 buffer_byte_count -= batch_allocation_size;
262502 read_queue_byte_count += batch_allocation_size;
262503 to_remove.push(batch_index);
262504 }
262505 while (!to_remove.empty()) {
262506 auto batch_index = to_remove.top();
262507 to_remove.pop();
262508 buffer.erase(batch_index);
262509 }
262510}
idx_t lowest_moved_batch
Debug variable to verify that order is preserved correctly.
Definition duckdb.cpp:177346

The documentation for this class was generated from the following file: