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::StorageManager Class Referenceabstract

StorageManager is responsible for managing the physical storage of a persistent database. More...

Inheritance diagram for duckdb::StorageManager:
Collaboration diagram for duckdb::StorageManager:

Public Member Functions

 StorageManager (AttachedDatabase &db, string path, AttachOptions &options)
void Initialize (QueryContext context)
DatabaseInstance & GetDatabase ()
AttachedDatabase & GetAttached () const
idx_t GetWALSize ()
 Gets the size of the WAL, or zero, if there is no WAL.
bool HasWAL () const
void AddWALSize (idx_t size)
void SetWALSize (idx_t size)
idx_t GetWALEntriesCount () const
 Gets the number of WAL entries since last checkpoint.
void ResetWALEntriesCount ()
void IncrementWALEntriesCount ()
optional_ptr< WriteAheadLog > GetWAL ()
 Gets the WAL of the StorageManager, or nullptr, if there is no WAL.
bool WALStartCheckpoint (MetaBlockPointer meta_block, CheckpointOptions &options)
 Write that we started a checkpoint to the WAL if there is one - returns whether or not there is a WAL.
void WALFinishCheckpoint (lock_guard< mutex > &wal_lock)
 Finishes a checkpoint.
unique_ptr< lock_guard< mutex > > GetWALLock ()
string GetDBPath () const
 Returns the database file path.
bool IsLoaded () const
string GetWALPath (const string &suffix=".wal")
 The path to the WAL, derived from the database file path.
string GetCheckpointWALPath ()
 The path to the WAL that is used while a checkpoint is running.
string GetRecoveryWALPath ()
 The path to the WAL that is used while recovering from a crash involving the checkpoint WAL.
bool InMemory () const
virtual bool AutomaticCheckpoint (idx_t estimated_wal_bytes)=0
virtual unique_ptr< StorageCommitState > GenStorageCommitState (WriteAheadLog &wal)=0
virtual bool IsCheckpointClean (MetaBlockPointer checkpoint_id)=0
virtual void CreateCheckpoint (QueryContext context, CheckpointOptions options=CheckpointOptions())=0
virtual DatabaseSize GetDatabaseSize ()=0
virtual vector< MetadataBlockInfo > GetMetadataInfo ()=0
virtual shared_ptr< TableIOManager > GetTableIOManager (BoundCreateTableInfo *info)=0
virtual BlockManager & GetBlockManager ()=0
virtual void Destroy ()
void SetStorageVersion (idx_t version)
bool HasStorageVersion () const
idx_t GetStorageVersion () const
bool CompressionIsEnabled () const
EncryptionTypes::CipherType GetCipher () const
void SetCipher (EncryptionTypes::CipherType cipher_p)
void SetEncryptionVersion (EncryptionTypes::EncryptionVersion version)
bool IsEncrypted () const
EncryptionTypes::EncryptionVersion GetEncryptionVersion () const
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Member Functions

static StorageManager & Get (AttachedDatabase &db)
static StorageManager & Get (Catalog &catalog)

Protected Member Functions

virtual void LoadDatabase (QueryContext context)=0

Protected Attributes

AttachedDatabase & db
 The attached database managed by this storage manager.
string path
 The path of the database.
string wal_path
 The WAL path.
unique_ptr< WriteAheadLog > wal
 The WriteAheadLog of the storage manager.
mutex wal_lock
 Mutex used to control writes to the WAL.
bool read_only
 Whether or not the database is opened in read-only mode.
bool load_complete = false
optional_idx storage_version
 The serialization compatibility version when reading and writing from this database.
atomic< idx_t > wal_size
atomic< idx_t > wal_entries_count
StorageOptions storage_options
 Storage options passed in through configuration.

Detailed Description

StorageManager is responsible for managing the physical storage of a persistent database.

Constructor & Destructor Documentation

◆ StorageManager()

duckdb::StorageManager::StorageManager ( AttachedDatabase & db,
string path,
AttachOptions & options )
426320 : db(db), path(std::move(path_p)), read_only(options.access_mode == AccessMode::READ_ONLY), wal_size(0) {
426321 if (path.empty()) {
426322 path = IN_MEMORY_PATH;
426323 return;
426324 }
426325 auto &fs = FileSystem::Get(db);
426326 path = fs.ExpandPath(path);
426327
426328 storage_options.Initialize(options.options);
426329}
AttachedDatabase & db
The attached database managed by this storage manager.
Definition duckdb.cpp:11205
atomic< idx_t > wal_size
Definition duckdb.cpp:11223
string path
The path of the database.
Definition duckdb.cpp:11207
StorageOptions storage_options
Storage options passed in through configuration.
Definition duckdb.cpp:11226
bool read_only
Whether or not the database is opened in read-only mode.
Definition duckdb.cpp:11215

◆ ~StorageManager()

duckdb::StorageManager::~StorageManager ( )
virtual
426331 {
426332}

Member Function Documentation

◆ Get() [1/2]

StorageManager & duckdb::StorageManager::Get ( AttachedDatabase & db)
static
426334 {
426335 return db.GetStorageManager();
426336}

◆ Get() [2/2]

StorageManager & duckdb::StorageManager::Get ( Catalog & catalog)
static
426337 {
426338 return StorageManager::Get(catalog.GetAttached());
426339}

◆ Initialize()

void duckdb::StorageManager::Initialize ( QueryContext context)

Initialize a database or load an existing database from the database file path. The block_alloc_size is either set, or invalid. If invalid, then DuckDB defaults to the default_block_alloc_size (DBConfig), or the file's block allocation size, if it is an existing database.

426498 {
426499 bool in_memory = InMemory();
426500 if (in_memory && read_only) {
426501 throw CatalogException("Cannot launch in-memory database in read-only mode!");
426502 }
426503
426504 // Create or load the database from disk, if not in-memory mode.
426505 LoadDatabase(context);
426506
426507 if (storage_options.encryption) {
426508 ClearUserKey(storage_options.user_key);
426509 }
426510}

◆ GetDatabase()

DatabaseInstance & duckdb::StorageManager::GetDatabase ( )
426341 {
426342 return db.GetDatabase();
426343}

◆ GetAttached()

AttachedDatabase & duckdb::StorageManager::GetAttached ( ) const
inline
11117 {
11118 return db;
11119 }

◆ GetWALSize()

idx_t duckdb::StorageManager::GetWALSize ( )

Gets the size of the WAL, or zero, if there is no WAL.

426349 {
426350 return wal_size;
426351}
Here is the caller graph for this function:

◆ HasWAL()

bool duckdb::StorageManager::HasWAL ( ) const
426380 {
426381 if (InMemory() || read_only || !load_complete) {
426382 return false;
426383 }
426384 return true;
426385}
bool load_complete
Definition duckdb.cpp:11218

◆ AddWALSize()

void duckdb::StorageManager::AddWALSize ( idx_t size)
426353 {
426354 wal_size += size;
426355}
GOpaque< Size > size(const GMat &src)

◆ SetWALSize()

void duckdb::StorageManager::SetWALSize ( idx_t size)
426357 {
426358 wal_size = size;
426359}

◆ GetWALEntriesCount()

idx_t duckdb::StorageManager::GetWALEntriesCount ( ) const

Gets the number of WAL entries since last checkpoint.

426361 {
426362 return wal_entries_count;
426363}

◆ ResetWALEntriesCount()

void duckdb::StorageManager::ResetWALEntriesCount ( )
426365 {
426366 wal_entries_count = 0;
426367}

◆ IncrementWALEntriesCount()

void duckdb::StorageManager::IncrementWALEntriesCount ( )
426369 {
426370 wal_entries_count++;
426371}

◆ GetWAL()

optional_ptr< WriteAheadLog > duckdb::StorageManager::GetWAL ( )

Gets the WAL of the StorageManager, or nullptr, if there is no WAL.

426373 {
426374 if (InMemory() || read_only || !load_complete) {
426375 return nullptr;
426376 }
426377 return wal.get();
426378}
unique_ptr< WriteAheadLog > wal
The WriteAheadLog of the storage manager.
Definition duckdb.cpp:11211

◆ WALStartCheckpoint()

bool duckdb::StorageManager::WALStartCheckpoint ( MetaBlockPointer meta_block,
CheckpointOptions & options )

Write that we started a checkpoint to the WAL if there is one - returns whether or not there is a WAL.

426387 {
426388 unique_ptr<lock_guard<mutex>> guard;
426389 if (!options.wal_lock) {
426390 // not holding the WAL lock yet - grab it
426391 guard = GetWALLock();
426392 }
426393 // while holding the WAL lock - get the last committed transaction from the transaction manager
426394 // this is the commit we will be checkpointing on - everything in this commit will be written to the file
426395 // any new commits made will be written to the next wal
426396 auto &transaction_manager = db.GetTransactionManager().Cast<DuckTransactionManager>();
426397 options.transaction_id = transaction_manager.GetNewCheckpointId();
426398
426399 DUCKDB_LOG(db.GetDatabase(), TransactionLogType, db, "Start Checkpoint", options.transaction_id);
426400 if (!wal) {
426401 return false;
426402 }
426403 // start the checkpoint process around the WAL
426404 if (GetWALSize() == 0) {
426405 // no WAL - we don't need to do anything here
426406 return false;
426407 }
426408 // verify the main WAL is the active WAL currently
426409 if (wal->GetPath() != wal_path) {
426410 throw InternalException("Current WAL path \"%s\" does not match base WAL path \"%s\" in WALStartCheckpoint",
426411 wal->GetPath(), wal_path);
426412 }
426413 // write to the main WAL that we have initiated a checkpoint
426414 wal->WriteCheckpoint(meta_block);
426415 wal->Flush();
426416
426417 // close the main WAL
426418 wal.reset();
426419
426420 // replace the WAL with a new WAL (.checkpoint.wal) that transactions can write to while the checkpoint is happening
426421 // we don't eagerly write to this WAL - we just instantiate it here so it can be written to
426422 // if a checkpoint WAL already exists - delete it before proceeding
426423 auto checkpoint_wal_path = GetCheckpointWALPath();
426424 auto &fs = FileSystem::Get(db);
426425 fs.TryRemoveFile(checkpoint_wal_path);
426426
426427 // the checkpoint WAL belongs to the NEXT checkpoint - when we are done we will overwrite the current WAL with it
426428 // as such override the checkpoint iteration number to the next one
426429 auto &single_file_block_manager = GetBlockManager().Cast<SingleFileBlockManager>();
426430 auto next_checkpoint_iteration = single_file_block_manager.GetCheckpointIteration() + 1;
426431 wal = make_uniq<WriteAheadLog>(*this, checkpoint_wal_path, 0ULL, WALInitState::NO_WAL, next_checkpoint_iteration);
426432 return true;
426433}
string GetCheckpointWALPath()
The path to the WAL that is used while a checkpoint is running.
Definition duckdb.cpp:426474
string wal_path
The WAL path.
Definition duckdb.cpp:11209
idx_t GetWALSize()
Gets the size of the WAL, or zero, if there is no WAL.
Definition duckdb.cpp:426349
Here is the call graph for this function:

◆ WALFinishCheckpoint()

void duckdb::StorageManager::WALFinishCheckpoint ( lock_guard< mutex > & wal_lock)

Finishes a checkpoint.

426435 {
426436 D_ASSERT(wal.get());
426437
426438 // "wal" points to the checkpoint WAL
426439 // first check if the checkpoint WAL has been written to
426440 auto &fs = FileSystem::Get(db);
426441 if (!wal->Initialized()) {
426442 // the checkpoint WAL has not been written to
426443 // this is the common scenario if there are no concurrent writes happening while checkpointing
426444 // in this case we can just remove the main WAL and re-instantiate it to empty
426445 fs.TryRemoveFile(wal_path);
426446 ResetWALEntriesCount();
426447 wal = make_uniq<WriteAheadLog>(*this, wal_path);
426448 return;
426449 }
426450
426451 // we have had writes to the checkpoint WAL - we need to override our WAL with the checkpoint WAL
426452 // first close the WAL writer
426453 auto checkpoint_wal_path = wal->GetPath();
426454 wal.reset();
426455
426456 // move the secondary WAL over the main WAL
426457 fs.MoveFile(checkpoint_wal_path, wal_path);
426458
426459 // open what is now the main WAL again
426460 wal = make_uniq<WriteAheadLog>(*this, wal_path);
426461 wal->Initialize();
426462
426463 DUCKDB_LOG(db.GetDatabase(), TransactionLogType, db, "Finish Checkpoint");
426464}

◆ GetWALLock()

unique_ptr< lock_guard< mutex > > duckdb::StorageManager::GetWALLock ( )
426466 {
426467 return make_uniq<lock_guard<mutex>>(wal_lock);
426468}
mutex wal_lock
Mutex used to control writes to the WAL.
Definition duckdb.cpp:11213

◆ GetDBPath()

string duckdb::StorageManager::GetDBPath ( ) const
inline

Returns the database file path.

11140 {
11141 return path;
11142 }

◆ IsLoaded()

bool duckdb::StorageManager::IsLoaded ( ) const
inline
11143 {
11144 return load_complete;
11145 }

◆ GetWALPath()

string duckdb::StorageManager::GetWALPath ( const string & suffix = ".wal")

The path to the WAL, derived from the database file path.

426470 {
426471 return Path::AddSuffixToPath(path, suffix);
426472}
Here is the caller graph for this function:

◆ GetCheckpointWALPath()

string duckdb::StorageManager::GetCheckpointWALPath ( )

The path to the WAL that is used while a checkpoint is running.

426474 {
426475 return GetWALPath(".wal.checkpoint");
426476}
string GetWALPath(const string &suffix=".wal")
The path to the WAL, derived from the database file path.
Definition duckdb.cpp:426470
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRecoveryWALPath()

string duckdb::StorageManager::GetRecoveryWALPath ( )

The path to the WAL that is used while recovering from a crash involving the checkpoint WAL.

426478 {
426479 return GetWALPath(".wal.recovery");
426480}
Here is the call graph for this function:

◆ InMemory()

bool duckdb::StorageManager::InMemory ( ) const
426482 {
426483 D_ASSERT(!path.empty());
426484 return path == IN_MEMORY_PATH;
426485}

◆ Destroy()

void duckdb::StorageManager::Destroy ( )
virtual
426487 {
426488}

◆ SetStorageVersion()

void duckdb::StorageManager::SetStorageVersion ( idx_t version)
inline
11164 {
11165 storage_version = version;
11166 }
optional_idx storage_version
The serialization compatibility version when reading and writing from this database.
Definition duckdb.cpp:11220

◆ HasStorageVersion()

bool duckdb::StorageManager::HasStorageVersion ( ) const
inline
11167 {
11168 return storage_version.IsValid();
11169 }

◆ GetStorageVersion()

idx_t duckdb::StorageManager::GetStorageVersion ( ) const
inline
11170 {
11171 D_ASSERT(HasStorageVersion());
11172 return storage_version.GetIndex();
11173 }

◆ CompressionIsEnabled()

bool duckdb::StorageManager::CompressionIsEnabled ( ) const
inline
11174 {
11175 return storage_options.compress_in_memory == CompressInMemory::COMPRESS;
11176 }

◆ GetCipher()

EncryptionTypes::CipherType duckdb::StorageManager::GetCipher ( ) const
inline
11177 {
11178 return storage_options.encryption_cipher;
11179 }

◆ SetCipher()

void duckdb::StorageManager::SetCipher ( EncryptionTypes::CipherType cipher_p)
inline
11180 {
11181 D_ASSERT(cipher_p != EncryptionTypes::INVALID);
11182 if (cipher_p == EncryptionTypes::CBC) {
11183 throw InvalidInputException("CBC cipher is disabled");
11184 }
11185 storage_options.encryption_cipher = cipher_p;
11186 }

◆ SetEncryptionVersion()

void duckdb::StorageManager::SetEncryptionVersion ( EncryptionTypes::EncryptionVersion version)
inline
11188 {
11189 storage_options.encryption_version = version;
11190 }

◆ IsEncrypted()

bool duckdb::StorageManager::IsEncrypted ( ) const
inline
11192 {
11193 return storage_options.encryption;
11194 }

◆ GetEncryptionVersion()

EncryptionTypes::EncryptionVersion duckdb::StorageManager::GetEncryptionVersion ( ) const
inline
11196 {
11197 return storage_options.encryption_version;
11198 }

◆ LoadDatabase()

virtual void duckdb::StorageManager::LoadDatabase ( QueryContext context)
protectedpure virtual

◆ Cast() [1/2]

template<class TARGET>
TARGET & duckdb::StorageManager::Cast ( )
inline
11230 {
11231 DynamicCastCheck<TARGET>(this);
11232 return reinterpret_cast<TARGET &>(*this);
11233 }

◆ Cast() [2/2]

template<class TARGET>
const TARGET & duckdb::StorageManager::Cast ( ) const
inline
11235 {
11236 DynamicCastCheck<TARGET>(this);
11237 return reinterpret_cast<const TARGET &>(*this);
11238 }

Member Data Documentation

◆ load_complete

bool duckdb::StorageManager::load_complete = false
protected

When loading a database, we do not yet set the wal-field. Therefore, GetWriteAheadLog must return nullptr when loading a database

◆ wal_size

atomic<idx_t> duckdb::StorageManager::wal_size
protected

Estimated size of changes for determining automatic checkpointing on in-memory databases and databases without a WAL.


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