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

Classes

struct  TemporaryFileLock

Public Member Functions

 TemporaryFileHandle (TemporaryFileManager &manager, TemporaryFileIdentifier identifier, idx_t temp_file_count)
TemporaryFileIndex TryGetBlockIndex (idx_t block_header_size)
 Try to get an index of where to write in this file. Returns an invalid index if full.
void EraseBlockIndex (block_id_t block_index)
 Remove block index from this TemporaryFileHandle.
unique_ptr< FileBuffer > ReadTemporaryBuffer (QueryContext context, const TemporaryFileIndex &index_in_file, unique_ptr< FileBuffer > reusable_buffer) const
 Read/Write temporary buffers at given positions in this file (potentially compressed).
void WriteTemporaryBuffer (FileBuffer &buffer, idx_t block_index, AllocatedData &compressed_buffer) const
bool DeleteIfEmpty ()
 Deletes the file if there are no more blocks.
bool IsEncrypted () const
TemporaryFileInformation GetTemporaryFile ()
 Get information about this temporary file.

Private Member Functions

void CreateFileIfNotExists (TemporaryFileLock &)
 Create temporary file if it did not exist yet.
void RemoveTempBlockIndex (TemporaryFileLock &, idx_t index)
 Remove block index from this file.
idx_t GetPositionInFile (idx_t index) const
 Get the position of a block in the file.

Private Attributes

DatabaseInstance & db
 Reference to the DB instance.
const TemporaryFileIdentifier identifier
 The identifier (size/file index) of this TemporaryFileHandle.
const idx_t max_allowed_index
 The maximum allowed index.
const string path
 File path/handle.
unique_ptr< FileHandle > handle
mutex file_lock
 Lock for concurrent access and block index manager.
BlockIndexManager index_manager

Static Private Attributes

static constexpr idx_t MAX_ALLOWED_INDEX_BASE = 4000

Constructor & Destructor Documentation

◆ TemporaryFileHandle()

duckdb::TemporaryFileHandle::TemporaryFileHandle ( TemporaryFileManager & manager,
TemporaryFileIdentifier identifier,
idx_t temp_file_count )
442854 : db(manager.db), identifier(identifier_p), max_allowed_index((1 << temp_file_count) * MAX_ALLOWED_INDEX_BASE),
442855 path(manager.CreateTemporaryFileName(identifier)), index_manager(manager) {
442856}
DatabaseInstance & db
Reference to the DB instance.
Definition duckdb.cpp:61652
const idx_t max_allowed_index
The maximum allowed index.
Definition duckdb.cpp:61656
const TemporaryFileIdentifier identifier
The identifier (size/file index) of this TemporaryFileHandle.
Definition duckdb.cpp:61654
const string path
File path/handle.
Definition duckdb.cpp:61658

◆ ~TemporaryFileHandle()

duckdb::TemporaryFileHandle::~TemporaryFileHandle ( )
442858 {
442859}

Member Function Documentation

◆ TryGetBlockIndex()

TemporaryFileIndex duckdb::TemporaryFileHandle::TryGetBlockIndex ( idx_t block_header_size)

Try to get an index of where to write in this file. Returns an invalid index if full.

442864 {
442866 if (index_manager.GetMaxIndex() >= max_allowed_index && index_manager.HasFreeBlocks()) {
442867 // file is at capacity
442868 return TemporaryFileIndex();
442869 }
442870 // open the file handle if it does not yet exist
442871 CreateFileIfNotExists(lock);
442872 // fetch a new block index to write to
442873 auto block_index = index_manager.GetNewBlockIndex(identifier.size);
442874 return TemporaryFileIndex(identifier, block_index, block_header_size);
442875}
void CreateFileIfNotExists(TemporaryFileLock &)
Create temporary file if it did not exist yet.
Definition duckdb.cpp:442991
mutex file_lock
Lock for concurrent access and block index manager.
Definition duckdb.cpp:61661
Here is the call graph for this function:

◆ EraseBlockIndex()

void duckdb::TemporaryFileHandle::EraseBlockIndex ( block_id_t block_index)

Remove block index from this TemporaryFileHandle.

442959 {
442960 // remove the block (and potentially truncate the temp file)
442962 D_ASSERT(handle);
442963 RemoveTempBlockIndex(lock, NumericCast<idx_t>(block_index));
442964}
void RemoveTempBlockIndex(TemporaryFileLock &, idx_t index)
Remove block index from this file.
Definition duckdb.cpp:443003
Here is the call graph for this function:

◆ ReadTemporaryBuffer()

unique_ptr< FileBuffer > duckdb::TemporaryFileHandle::ReadTemporaryBuffer ( QueryContext context,
const TemporaryFileIndex & index_in_file,
unique_ptr< FileBuffer > reusable_buffer ) const

Read/Write temporary buffers at given positions in this file (potentially compressed).

Read nonce and tag.

Read the encrypted compressed buffer.

Decrypt the compressed buffer.

442879 {
442880 auto &buffer_manager = BufferManager::GetBufferManager(db);
442881 auto block_index = index_in_file.block_index.GetIndex();
442882 auto block_header_size = index_in_file.block_header_size.GetIndex();
442883
442884 auto buffer = buffer_manager.ConstructManagedBuffer(buffer_manager.GetBlockAllocSize() - block_header_size,
442885 block_header_size, std::move(reusable_buffer));
442886 AllocatedData compressed_buffer;
442887 data_ptr_t read_buffer;
442888 idx_t read_size;
442889 bool is_uncompressed = identifier.size == TemporaryBufferSize::DEFAULT;
442890 if (is_uncompressed) {
442891 // not compressed - read directly into buffer
442892 read_buffer = buffer->InternalBuffer();
442893 read_size = buffer->AllocSize();
442894 } else {
442895 // compressed - read into separate compression buffer first
442896 compressed_buffer = Allocator::Get(db).Allocate(TemporaryBufferSizeToSize(identifier.size));
442897 read_buffer = compressed_buffer.get();
442898 read_size = compressed_buffer.GetSize();
442899 }
442900
442901 idx_t read_position = GetPositionInFile(block_index);
442902 if (IsEncrypted()) {
442903 uint8_t encryption_metadata[DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE];
442905 handle->Read(context, encryption_metadata, DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE, read_position);
442907 handle->Read(context, read_buffer, read_size, read_position + DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE);
442909 EncryptionEngine::DecryptTemporaryBuffer(db, read_buffer, read_size, encryption_metadata);
442910 } else {
442911 handle->Read(context, read_buffer, read_size, read_position);
442912 }
442913
442914 if (is_uncompressed) {
442915 return buffer;
442916 }
442917
442918 // Decompress into buffer
442919 const auto compressed_size = Load<idx_t>(compressed_buffer.get());
442920 D_ASSERT(!duckdb_zstd::ZSTD_isError(compressed_size));
442921 const auto decompressed_size = duckdb_zstd::ZSTD_decompress(
442922 buffer->InternalBuffer(), buffer->AllocSize(), compressed_buffer.get() + sizeof(idx_t), compressed_size);
442923 (void)decompressed_size;
442924 D_ASSERT(!duckdb_zstd::ZSTD_isError(decompressed_size));
442925
442926 D_ASSERT(decompressed_size == buffer->AllocSize());
442927 return buffer;
442928}
static void DecryptTemporaryBuffer(DatabaseInstance &db, data_ptr_t buffer, idx_t buffer_size, data_ptr_t metadata)
Definition duckdb.cpp:52069
idx_t GetPositionInFile(idx_t index) const
Get the position of a block in the file.
Definition duckdb.cpp:443016
::uint8_t uint8_t
ZSTDLIB_API unsigned ZSTD_isError(size_t code)
Definition duckdb.cpp:614327
ZSTDLIB_API size_t ZSTD_decompress(void *dst, size_t dstCapacity, const void *src, size_t compressedSize)
Definition duckdb.cpp:640497
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ WriteTemporaryBuffer()

void duckdb::TemporaryFileHandle::WriteTemporaryBuffer ( FileBuffer & buffer,
idx_t block_index,
AllocatedData & compressed_buffer ) const
442931 {
442932 // We group DEFAULT_BLOCK_ALLOC_SIZE blocks into the same file.
442933 D_ASSERT(buffer.AllocSize() == BufferManager::GetBufferManager(db).GetBlockAllocSize());
442934 bool is_uncompressed = identifier.size == TemporaryBufferSize::DEFAULT;
442935 data_ptr_t write_buffer;
442936 idx_t write_size;
442937 if (is_uncompressed) {
442938 // write the uncompressed buffer to the file directly
442939 write_buffer = buffer.InternalBuffer();
442940 write_size = buffer.AllocSize();
442941 } else {
442942 // write the compressed buffer to the file
442943 write_buffer = compressed_buffer.get();
442944 write_size = TemporaryBufferSizeToSize(identifier.size);
442945 }
442946 idx_t write_position = GetPositionInFile(block_index);
442947 if (IsEncrypted()) {
442948 uint8_t encryption_metadata[DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE];
442949 EncryptionEngine::EncryptTemporaryBuffer(db, write_buffer, write_size, encryption_metadata);
442950
442951 handle->Write(QueryContext(), encryption_metadata, DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE, write_position);
442952 handle->Write(QueryContext(), write_buffer, write_size, write_position + DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE);
442953 } else {
442954 // write file directly
442955 handle->Write(QueryContext(), write_buffer, write_size, write_position);
442956 }
442957}
static void EncryptTemporaryBuffer(DatabaseInstance &db, data_ptr_t buffer, idx_t buffer_size, data_ptr_t metadata)
Definition duckdb.cpp:52005

◆ DeleteIfEmpty()

bool duckdb::TemporaryFileHandle::DeleteIfEmpty ( )

Deletes the file if there are no more blocks.

442966 {
442968 if (index_manager.GetMaxIndex() > 0) {
442969 // there are still blocks in this file
442970 return false;
442971 }
442972 // the file is empty: delete it
442973 handle.reset();
442974 auto &fs = FileSystem::GetFileSystem(db);
442975 fs.RemoveFile(path);
442976 return true;
442977}

◆ IsEncrypted()

bool duckdb::TemporaryFileHandle::IsEncrypted ( ) const
442979 {
442980 return identifier.encrypted;
442981}

◆ GetTemporaryFile()

TemporaryFileInformation duckdb::TemporaryFileHandle::GetTemporaryFile ( )

Get information about this temporary file.

442983 {
442985 TemporaryFileInformation info;
442986 info.path = path;
442987 info.size = GetPositionInFile(index_manager.GetMaxIndex());
442988 return info;
442989}
Here is the call graph for this function:

◆ CreateFileIfNotExists()

void duckdb::TemporaryFileHandle::CreateFileIfNotExists ( TemporaryFileLock & )
private

Create temporary file if it did not exist yet.

442991 {
442992 if (handle) {
442993 return;
442994 }
442995 auto &fs = FileSystem::GetFileSystem(db);
442997 if (db.config.options.use_direct_io) {
442998 open_flags |= FileFlags::FILE_FLAGS_DIRECT_IO;
442999 }
443000 handle = fs.OpenFile(path, open_flags);
443001}
static constexpr FileOpenFlags FILE_FLAGS_FILE_CREATE
Create file if not exists, can only be used together with WRITE.
Definition duckdb.hpp:7856
static constexpr FileOpenFlags FILE_FLAGS_READ
Open file with read access.
Definition duckdb.hpp:7850
static constexpr FileOpenFlags FILE_FLAGS_DIRECT_IO
Use direct IO when reading/writing to the file.
Definition duckdb.hpp:7854
static constexpr FileOpenFlags FILE_FLAGS_WRITE
Open file with write access.
Definition duckdb.hpp:7852
Here is the caller graph for this function:

◆ RemoveTempBlockIndex()

void duckdb::TemporaryFileHandle::RemoveTempBlockIndex ( TemporaryFileLock & ,
idx_t index )
private

Remove block index from this file.

443003 {
443004 // remove the block index from the index manager
443005 if (index_manager.RemoveIndex(index, identifier.size)) {
443006 // the max_index that is currently in use has decreased
443007 // as a result we can truncate the file
443008#ifndef WIN32 // this ended up causing issues when sorting
443009 auto max_index = index_manager.GetMaxIndex();
443010 auto &fs = FileSystem::GetFileSystem(db);
443011 fs.Truncate(*handle, NumericCast<int64_t>(GetPositionInFile(max_index + 1)));
443012#endif
443013 }
443014}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPositionInFile()

idx_t duckdb::TemporaryFileHandle::GetPositionInFile ( idx_t index) const
private

Get the position of a block in the file.

443016 {
443017 idx_t metadata_size = 0;
443018 if (IsEncrypted()) {
443019 // if the file is encrypted, we need to account for the header size
443020 metadata_size = DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE;
443021 }
443022 return index * (static_cast<idx_t>(identifier.size) + metadata_size);
443023}
index
Here is the caller graph for this function:

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