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

Public Member Functions

 CSVBufferManager (ClientContext &context, const CSVReaderOptions &options, const OpenFileInfo &file, bool per_file_single_threaded=false, unique_ptr< CSVFileHandle > file_handle=nullptr)
shared_ptr< CSVBufferHandle > GetBuffer (const idx_t buffer_idx)
void ResetBuffer (const idx_t buffer_idx)
void Initialize ()
 Initializes the buffer manager, during it's construction/reset.
void UnpinBuffer (const idx_t cache_idx)
idx_t GetBufferSize () const
 Returns the buffer size set for this CSV buffer manager.
idx_t BufferCount () const
 Returns the number of buffers in the cached_buffers cache.
bool Done () const
void ResetBufferManager ()
string GetFilePath () const
bool IsBlockUnloaded (idx_t block_idx)
idx_t GetBytesRead () const

Public Attributes

unique_ptr< CSVFileHandle > file_handle
 unique_ptr to the file handle, gets stolen after sniffing
ClientContext & context
idx_t skip_rows = 0
bool sniffing = false
const bool per_file_single_threaded

Private Member Functions

bool ReadNextAndCacheIt ()
 Reads next buffer in reference to cached_buffers.front().

Private Attributes

const OpenFileInfo file
 The file this Buffer Manager refers to.
vector< shared_ptr< CSVBuffer > > cached_buffers
 The cached buffers.
shared_ptr< CSVBuffer > last_buffer
 The last buffer it was accessed.
idx_t global_csv_pos = 0
idx_t buffer_size
 The size of the buffer, if the csv file has a smaller size than this, we will use that instead to malloc less.
bool done = false
 If this buffer manager is done (i.e., no more buffers to read beyond the ones that were cached.
idx_t bytes_read = 0
mutex main_mutex
 Because the buffer manager can be accessed in Parallel we need a mutex.
bool has_seeked = false
 If the file_handle used seek.
unordered_set< idx_t > reset_when_possible
bool is_pipe

Detailed Description

This class is used to manage the CSV buffers. Buffers are cached when used for auto detection. When parsing, buffer are not cached and just returned. A CSV Buffer Manager is created for each separate CSV File.

Constructor & Destructor Documentation

◆ CSVBufferManager()

duckdb::CSVBufferManager::CSVBufferManager ( ClientContext & context,
const CSVReaderOptions & options,
const OpenFileInfo & file,
bool per_file_single_threaded = false,
unique_ptr< CSVFileHandle > file_handle = nullptr )
166699 : context(context_p), per_file_single_threaded(per_file_single_threaded_p), file(file_p),
166700 buffer_size(options.buffer_size_option.GetValue()) {
166701 D_ASSERT(!file.path.empty());
166702 if (file_handle_p) {
166703 file_handle = std::move(file_handle_p);
166704 } else {
166705 file_handle = ReadCSV::OpenCSV(file, options, context);
166706 }
166707 is_pipe = file_handle->IsPipe();
166708 skip_rows = options.dialect_options.skip_rows.GetValue();
166709 Initialize();
166710}
idx_t buffer_size
The size of the buffer, if the csv file has a smaller size than this, we will use that instead to mal...
Definition duckdb.cpp:165728
void Initialize()
Initializes the buffer manager, during it's construction/reset.
Definition duckdb.cpp:166718
unique_ptr< CSVFileHandle > file_handle
unique_ptr to the file handle, gets stolen after sniffing
Definition duckdb.cpp:165692
const OpenFileInfo file
The file this Buffer Manager refers to.
Definition duckdb.cpp:165721

Member Function Documentation

◆ GetBuffer()

shared_ptr< CSVBufferHandle > duckdb::CSVBufferManager::GetBuffer ( const idx_t buffer_idx)

Returns a buffer from a buffer id (starting from 0). If it's in the auto-detection then we cache new buffers Otherwise we remove them from the cache if they are already there, or just return them bypassing the cache.

166743 {
166744 lock_guard<mutex> parallel_lock(main_mutex);
166745 if (pos == 0 && done && cached_buffers.empty()) {
166746 if (is_pipe) {
166747 throw InvalidInputException("Recursive CTEs are not allowed when using piped csv files");
166748 }
166749 // This is a recursive CTE, we have to reset out whole buffer
166750 done = false;
166751 file_handle->Reset();
166752 Initialize();
166753 }
166754 while (pos >= cached_buffers.size()) {
166755 if (done) {
166756 return nullptr;
166757 }
166758 if (!ReadNextAndCacheIt()) {
166759 done = true;
166760 }
166761 }
166762 if (pos != 0 && (sniffing || file_handle->CanSeek() || per_file_single_threaded)) {
166763 // We don't need to unpin the buffers here if we are not sniffing since we
166764 // control it per-thread on the scan
166765 if (cached_buffers[pos - 1]) {
166766 cached_buffers[pos - 1]->Unpin();
166767 }
166768 }
166769 return cached_buffers[pos]->Pin(*file_handle, has_seeked);
166770}
bool ReadNextAndCacheIt()
Reads next buffer in reference to cached_buffers.front().
Definition duckdb.cpp:166725
vector< shared_ptr< CSVBuffer > > cached_buffers
The cached buffers.
Definition duckdb.cpp:165723
bool has_seeked
If the file_handle used seek.
Definition duckdb.cpp:165735
bool done
If this buffer manager is done (i.e., no more buffers to read beyond the ones that were cached.
Definition duckdb.cpp:165730
mutex main_mutex
Because the buffer manager can be accessed in Parallel we need a mutex.
Definition duckdb.cpp:165733
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ResetBuffer()

void duckdb::CSVBufferManager::ResetBuffer ( const idx_t buffer_idx)
166772 {
166773 lock_guard<mutex> parallel_lock(main_mutex);
166774 if (buffer_idx >= cached_buffers.size()) {
166775 // Nothing to reset
166776 return;
166777 }
166778 D_ASSERT(cached_buffers[buffer_idx]);
166779 if (buffer_idx == 0 && cached_buffers.size() > 1) {
166780 cached_buffers[buffer_idx].reset();
166781 idx_t cur_buffer = buffer_idx + 1;
166782 while (reset_when_possible.find(cur_buffer) != reset_when_possible.end()) {
166783 cached_buffers[cur_buffer].reset();
166784 reset_when_possible.erase(cur_buffer);
166785 cur_buffer++;
166786 }
166787 return;
166788 }
166789 // We only reset if previous one was also already reset
166790 if (buffer_idx > 0 && !cached_buffers[buffer_idx - 1]) {
166791 if (cached_buffers[buffer_idx]->last_buffer) {
166792 // We clear the whole shebang
166793 cached_buffers.clear();
166794 reset_when_possible.clear();
166795 return;
166796 }
166797 cached_buffers[buffer_idx].reset();
166798 idx_t cur_buffer = buffer_idx + 1;
166799 while (reset_when_possible.find(cur_buffer) != reset_when_possible.end()) {
166800 cached_buffers[cur_buffer].reset();
166801 reset_when_possible.erase(cur_buffer);
166802 cur_buffer++;
166803 }
166804 } else {
166805 reset_when_possible.insert(buffer_idx);
166806 }
166807}
shared_ptr< CSVBuffer > last_buffer
The last buffer it was accessed.
Definition duckdb.cpp:165725
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Initialize()

void duckdb::CSVBufferManager::Initialize ( )

Initializes the buffer manager, during it's construction/reset.

166718 {
166719 if (cached_buffers.empty()) {
166720 cached_buffers.emplace_back(make_shared_ptr<CSVBuffer>(context, buffer_size, *file_handle, global_csv_pos));
166721 last_buffer = cached_buffers.front();
166722 }
166723}
Here is the caller graph for this function:

◆ UnpinBuffer()

void duckdb::CSVBufferManager::UnpinBuffer ( const idx_t cache_idx)
166712 {
166713 if (cache_idx < cached_buffers.size()) {
166714 cached_buffers[cache_idx]->Unpin();
166715 }
166716}

◆ GetBufferSize()

idx_t duckdb::CSVBufferManager::GetBufferSize ( ) const

Returns the buffer size set for this CSV buffer manager.

166809 {
166810 return buffer_size;
166811}

◆ BufferCount()

idx_t duckdb::CSVBufferManager::BufferCount ( ) const

Returns the number of buffers in the cached_buffers cache.

166813 {
166814 return cached_buffers.size();
166815}

◆ Done()

bool duckdb::CSVBufferManager::Done ( ) const

If this buffer manager is done. In the context of a buffer manager it means that it read all buffers at least once.

166817 {
166818 return done;
166819}

◆ ResetBufferManager()

void duckdb::CSVBufferManager::ResetBufferManager ( )
166821 {
166822 if (!file_handle->IsPipe()) {
166823 // If this is not a pipe we reset the buffer manager and restart it when doing the actual scan
166824 cached_buffers.clear();
166825 reset_when_possible.clear();
166826 file_handle->Reset();
166827 last_buffer = nullptr;
166828 done = false;
166829 global_csv_pos = 0;
166830 Initialize();
166831 }
166832}

◆ GetFilePath()

string duckdb::CSVBufferManager::GetFilePath ( ) const
166834 {
166835 return file.path;
166836}

◆ IsBlockUnloaded()

bool duckdb::CSVBufferManager::IsBlockUnloaded ( idx_t block_idx)
166838 {
166839 if (block_idx < cached_buffers.size()) {
166840 return cached_buffers[block_idx]->IsUnloaded();
166841 }
166842 return false;
166843}

◆ GetBytesRead()

idx_t duckdb::CSVBufferManager::GetBytesRead ( ) const
166845 {
166846 return bytes_read;
166847}

◆ ReadNextAndCacheIt()

bool duckdb::CSVBufferManager::ReadNextAndCacheIt ( )
private

Reads next buffer in reference to cached_buffers.front().

166725 {
166726 D_ASSERT(last_buffer);
166727 for (idx_t i = 0; i < 2; i++) {
166728 if (!last_buffer->IsCSVFileLastBuffer()) {
166729 auto maybe_last_buffer = last_buffer->Next(*file_handle, buffer_size, has_seeked);
166730 if (!maybe_last_buffer) {
166731 last_buffer->last_buffer = true;
166732 return false;
166733 }
166734 last_buffer = std::move(maybe_last_buffer);
166735 bytes_read += last_buffer->GetBufferSize();
166736 cached_buffers.emplace_back(last_buffer);
166737 return true;
166738 }
166739 }
166740 return false;
166741}
Here is the caller graph for this function:

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