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::BatchInsertGlobalState Class Reference
Inheritance diagram for duckdb::BatchInsertGlobalState:
Collaboration diagram for duckdb::BatchInsertGlobalState:

Public Member Functions

 BatchInsertGlobalState (ClientContext &context, DuckTableEntry &table, const idx_t minimum_memory_per_thread)
bool ReadyToMerge (const idx_t count) const
void ScheduleMergeTasks (ClientContext &context, const idx_t min_batch_index)
PhysicalIndex MergeCollections (ClientContext &context, const vector< RowGroupBatchEntry > &merge_collections, OptimisticDataWriter &writer)
void AddCollection (ClientContext &context, const idx_t batch_index, const idx_t min_batch_index, const PhysicalIndex collection_index, optional_ptr< OptimisticDataWriter > writer=nullptr)
idx_t MaxThreads (const idx_t source_max_threads) override
Public Member Functions inherited from duckdb::GlobalSinkState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
Public Member Functions inherited from duckdb::StateWithBlockableTasks
unique_lock< mutex > Lock ()
void PreventBlocking (const unique_lock< mutex > &guard)
bool BlockTask (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
 Add a task to 'blocked_tasks' before returning SourceResultType::BLOCKED (must hold the lock).
bool CanBlock (const unique_lock< mutex > &guard) const
bool UnblockTasks (const unique_lock< mutex > &guard)
 Unblock all tasks (must hold the lock).
SinkResultType BlockSink (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
SourceResultType BlockSource (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
void VerifyLock (const unique_lock< mutex > &guard) const

Public Attributes

BatchMemoryManager memory_manager
BatchTaskManager< BatchInsertTask > task_manager
mutex lock
DuckTableEntry & table
idx_t row_group_size
idx_t insert_count
vector< RowGroupBatchEntry > collections
idx_t next_start = 0
atomic< bool > optimistically_written
idx_t minimum_memory_per_thread
Public Attributes inherited from duckdb::GlobalSinkState
SinkFinalizeType state

Constructor & Destructor Documentation

◆ BatchInsertGlobalState()

duckdb::BatchInsertGlobalState::BatchInsertGlobalState ( ClientContext & context,
DuckTableEntry & table,
const idx_t minimum_memory_per_thread )
inline
193090 : memory_manager(context, minimum_memory_per_thread), table(table), insert_count(0),
193091 optimistically_written(false), minimum_memory_per_thread(minimum_memory_per_thread) {
193092 row_group_size = table.GetStorage().GetRowGroupSize();
193093 table.GetStorage().BindIndexes(context);
193094 }

Member Function Documentation

◆ ReadyToMerge()

bool duckdb::BatchInsertGlobalState::ReadyToMerge ( const idx_t count) const
193198 {
193199 // we try to merge so the count fits nicely into row groups
193200 if (count >= row_group_size / 10 * 9 && count <= row_group_size) {
193201 // 90%-100% of row group size
193202 return true;
193203 }
193204 if (count >= row_group_size / 10 * 18 && count <= row_group_size * 2) {
193205 // 180%-200% of row group size
193206 return true;
193207 }
193208 if (count >= row_group_size / 10 * 27 && count <= row_group_size * 3) {
193209 // 270%-300% of row group size
193210 return true;
193211 }
193212 if (count >= row_group_size / 10 * 36) {
193213 // >360% of row group size
193214 return true;
193215 }
193216 return false;
193217}

◆ ScheduleMergeTasks()

void duckdb::BatchInsertGlobalState::ScheduleMergeTasks ( ClientContext & context,
const idx_t min_batch_index )
193219 {
193220 idx_t current_idx;
193221 vector<BatchMergeTask> to_be_scheduled_tasks;
193222
193223 BatchMergeTask current_task(next_start);
193224 for (current_idx = current_task.start_index; current_idx < collections.size(); current_idx++) {
193225 auto &entry = collections[current_idx];
193226 if (entry.batch_idx > min_batch_index) {
193227 // this entry is AFTER the min_batch_index
193228 // finished
193229 if (ReadyToMerge(current_task.total_count)) {
193230 current_task.end_index = current_idx;
193231 to_be_scheduled_tasks.push_back(current_task);
193232 }
193233 break;
193234 }
193235 if (entry.type == RowGroupBatchType::FLUSHED) {
193236 // already flushed: cannot flush anything here
193237 if (current_task.total_count > 0) {
193238 current_task.end_index = current_idx;
193239 to_be_scheduled_tasks.push_back(current_task);
193240 }
193241 current_task.start_index = current_idx + 1;
193242 if (current_task.start_index > next_start) {
193243 // avoid checking this segment again in the future
193244 next_start = current_task.start_index;
193245 }
193246 current_task.total_count = 0;
193247 continue;
193248 }
193249 // not flushed - add to set of indexes to flush
193250 current_task.total_count += entry.total_rows;
193251 if (ReadyToMerge(current_task.total_count)) {
193252 // create a task to merge these collections
193253 current_task.end_index = current_idx + 1;
193254 to_be_scheduled_tasks.push_back(current_task);
193255 current_task.start_index = current_idx + 1;
193256 current_task.total_count = 0;
193257 }
193258 }
193259
193260 if (to_be_scheduled_tasks.empty()) {
193261 return;
193262 }
193263 for (auto &scheduled_task : to_be_scheduled_tasks) {
193264 D_ASSERT(scheduled_task.total_count > 0);
193265 D_ASSERT(current_idx > scheduled_task.start_index);
193266 auto merged_batch_index = collections[scheduled_task.start_index].batch_idx;
193267 vector<RowGroupBatchEntry> merge_collections;
193268 for (idx_t idx = scheduled_task.start_index; idx < scheduled_task.end_index; idx++) {
193269 auto &entry = collections[idx];
193270 if (!entry.collection_index.IsValid() || entry.type == RowGroupBatchType::FLUSHED) {
193271 throw InternalException("Adding a row group collection that should not be flushed");
193272 }
193273 auto &collection = table.GetStorage().GetOptimisticCollection(context, entry.collection_index);
193274 RowGroupBatchEntry added_entry(collection, collections[scheduled_task.start_index].batch_idx,
193275 entry.collection_index, RowGroupBatchType::FLUSHED);
193276 added_entry.unflushed_memory = entry.unflushed_memory;
193277 merge_collections.push_back(added_entry);
193278 entry.total_rows = scheduled_task.total_count;
193279 entry.type = RowGroupBatchType::FLUSHED;
193280 entry.collection_index = PhysicalIndex(DConstants::INVALID_INDEX);
193281 }
193282 task_manager.AddTask(make_uniq<MergeCollectionTask>(std::move(merge_collections), merged_batch_index));
193283 }
193284 // erase in reverse order
193285 for (idx_t i = to_be_scheduled_tasks.size(); i > 0; i--) {
193286 auto &scheduled_task = to_be_scheduled_tasks[i - 1];
193287 if (scheduled_task.start_index + 1 < scheduled_task.end_index) {
193288 // erase all entries except the first one
193289 collections.erase(collections.begin() + NumericCast<int64_t>(scheduled_task.start_index) + 1,
193290 collections.begin() + NumericCast<int64_t>(scheduled_task.end_index));
193291 }
193292 }
193293}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

◆ MergeCollections()

PhysicalIndex duckdb::BatchInsertGlobalState::MergeCollections ( ClientContext & context,
const vector< RowGroupBatchEntry > & merge_collections,
OptimisticDataWriter & writer )
193297 {
193298 D_ASSERT(!merge_collections.empty());
193299 CollectionMerger merger(context, table.GetStorage());
193300 idx_t written_data = 0;
193301 for (auto &entry : merge_collections) {
193302 merger.AddCollection(entry.collection_index, RowGroupBatchType::NOT_FLUSHED);
193303 written_data += entry.unflushed_memory;
193304 }
193305 optimistically_written = true;
193306 memory_manager.ReduceUnflushedMemory(written_data);
193307 return merger.Flush(writer);
193308}

◆ AddCollection()

void duckdb::BatchInsertGlobalState::AddCollection ( ClientContext & context,
const idx_t batch_index,
const idx_t min_batch_index,
const PhysicalIndex collection_index,
optional_ptr< OptimisticDataWriter > writer = nullptr )
193312 {
193313 if (batch_index < min_batch_index) {
193314 throw InternalException("Batch index of the added collection (%llu) is smaller than the min batch index (%llu)",
193315 batch_index, min_batch_index);
193316 }
193317 auto &optimistic_collection = table.GetStorage().GetOptimisticCollection(context, collection_index);
193318 auto &collection = *optimistic_collection.collection;
193319 auto new_count = collection.GetTotalRows();
193320 auto batch_type = new_count < row_group_size ? RowGroupBatchType::NOT_FLUSHED : RowGroupBatchType::FLUSHED;
193321 if (batch_type == RowGroupBatchType::FLUSHED && writer) {
193322 writer->WriteUnflushedRowGroups(optimistic_collection);
193323 }
193324 lock_guard<mutex> l(lock);
193325 insert_count += new_count;
193326 // add the collection to the batch index
193327 RowGroupBatchEntry new_entry(optimistic_collection, batch_index, collection_index, batch_type);
193328 if (batch_type == RowGroupBatchType::NOT_FLUSHED) {
193329 memory_manager.IncreaseUnflushedMemory(new_entry.unflushed_memory);
193330 }
193331
193332 auto it = std::lower_bound(
193333 collections.begin(), collections.end(), new_entry,
193334 [&](const RowGroupBatchEntry &a, const RowGroupBatchEntry &b) { return a.batch_idx < b.batch_idx; });
193335 if (it != collections.end() && it->batch_idx == new_entry.batch_idx) {
193336 throw InternalException("PhysicalBatchInsert::AddCollection error: batch index %d is present in multiple "
193337 "collections. This occurs when "
193338 "batch indexes are not uniquely distributed over threads",
193339 batch_index);
193340 }
193341 collections.insert(it, new_entry);
193342 if (writer) {
193343 ScheduleMergeTasks(context, min_batch_index);
193344 }
193345}

◆ MaxThreads()

idx_t duckdb::BatchInsertGlobalState::MaxThreads ( const idx_t source_max_threads)
inlineoverridevirtual

Reimplemented from duckdb::GlobalSinkState.

193114 {
193115 // try to request 4MB per column per thread
193116 memory_manager.SetMemorySize(source_max_threads * minimum_memory_per_thread);
193117 // cap the concurrent threads working on this task based on the amount of available memory
193118 return MinValue<idx_t>(source_max_threads, memory_manager.AvailableMemory() / minimum_memory_per_thread + 1);
193119 }

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