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::AttachedDatabase Class Reference

The AttachedDatabase represents an attached database instance. More...

#include <duckdb.hpp>

Inheritance diagram for duckdb::AttachedDatabase:
Collaboration diagram for duckdb::AttachedDatabase:

Public Member Functions

 AttachedDatabase (DatabaseInstance &db, AttachedDatabaseType type=AttachedDatabaseType::SYSTEM_DATABASE)
 Create the built-in system database (without storage).
 AttachedDatabase (DatabaseInstance &db, Catalog &catalog, string name, string file_path, AttachOptions &options)
 Create an attached database instance with the specified name and storage.
 AttachedDatabase (DatabaseInstance &db, Catalog &catalog, StorageExtension &ext, ClientContext &context, string name, AttachInfo &info, AttachOptions &options)
 Create an attached database instance with the specified storage extension.
void Initialize (optional_ptr< ClientContext > context=nullptr)
 Initializes the catalog and storage of the attached database.
void FinalizeLoad (optional_ptr< ClientContext > context)
void Close (const DatabaseCloseAction action)
 Close the database before shutting it down.
Catalog & ParentCatalog () override
const Catalog & ParentCatalog () const override
bool HasStorageManager () const
StorageManager & GetStorageManager ()
const StorageManager & GetStorageManager () const
Catalog & GetCatalog ()
TransactionManager & GetTransactionManager ()
DatabaseInstance & GetDatabase ()
optional_ptr< StorageExtension > GetStorageExtension ()
const string & GetName () const
void SetName (const string &new_name)
bool IsSystem () const
bool IsTemporary () const
bool IsReadOnly () const
bool IsInitialDatabase () const
void SetInitialDatabase ()
void SetReadOnlyDatabase ()
void OnDetach (ClientContext &context)
RecoveryMode GetRecoveryMode () const
AttachVisibility GetVisibility () const
const unordered_map< string, Value > & GetAttachOptions () const
string StoredPath () const
Public Member Functions inherited from duckdb::CatalogEntry
 CatalogEntry (CatalogType type, Catalog &catalog, string name)
 CatalogEntry (CatalogType type, string name, idx_t oid)
virtual unique_ptr< CatalogEntry > AlterEntry (ClientContext &context, AlterInfo &info)
virtual unique_ptr< CatalogEntry > AlterEntry (CatalogTransaction transaction, AlterInfo &info)
virtual void UndoAlter (ClientContext &context, AlterInfo &info)
virtual void Rollback (CatalogEntry &prev_entry)
virtual void OnDrop ()
virtual unique_ptr< CatalogEntry > Copy (ClientContext &context) const
virtual unique_ptr< CreateInfo > GetInfo () const
virtual void SetAsRoot ()
 Sets the CatalogEntry as the new root entry (i.e. the newest entry).
virtual string ToSQL () const
 Convert the catalog entry to a SQL string that can be used to re-construct the catalog entry.
virtual SchemaCatalogEntry & ParentSchema ()
virtual const SchemaCatalogEntry & ParentSchema () const
virtual void Verify (Catalog &catalog)
void Serialize (Serializer &serializer) const
void SetChild (unique_ptr< CatalogEntry > child)
unique_ptr< CatalogEntry > TakeChild ()
bool HasChild () const
bool HasParent () const
CatalogEntry & Child ()
CatalogEntry & Parent ()
const CatalogEntry & Parent () const
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
Public Member Functions inherited from duckdb::enable_shared_from_this< AttachedDatabase >
shared_ptr< AttachedDatabase > shared_from_this ()

Static Public Member Functions

static bool NameIsReserved (const string &name)
static string ExtractDatabaseName (const string &dbpath, FileSystem &fs)
static void InvokeCloseIfLastReference (shared_ptr< AttachedDatabase > &attached_database)
Static Public Member Functions inherited from duckdb::CatalogEntry
static unique_ptr< CreateInfo > Deserialize (Deserializer &deserializer)

Private Member Functions

void Cleanup ()
 Clean any (shared) resources held by the database.

Private Attributes

DatabaseInstance & db
unique_ptr< StoredDatabasePath > stored_database_path
unique_ptr< StorageManager > storage
unique_ptr< Catalog > catalog
unique_ptr< TransactionManager > transaction_manager
AttachedDatabaseType type
optional_ptr< Catalog > parent_catalog
optional_ptr< StorageExtension > storage_extension
RecoveryMode recovery_mode = RecoveryMode::DEFAULT
AttachVisibility visibility = AttachVisibility::SHOWN
bool is_initial_database = false
bool is_closed = false
shared_ptr< mutex > close_lock
unordered_map< string, Value > attach_options

Additional Inherited Members

Public Attributes inherited from duckdb::CatalogEntry
idx_t oid
 The oid of the entry.
CatalogType type
 The type of this catalog entry.
optional_ptr< CatalogSet > set
 Reference to the catalog set this entry is stored in.
string name
 The name of the entry.
bool deleted
 Whether or not the object is deleted.
bool temporary
 Whether or not the object is temporary and should not be added to the WAL.
bool internal
 Whether or not the entry is an internal entry (cannot be deleted, not dumped, etc).
atomic< transaction_t > timestamp
 Timestamp at which the catalog entry was created.
Value comment
 (optional) comment on this entry
InsertionOrderPreservingMap< string > tags
 (optional) extra data associated with this entry
Protected Member Functions inherited from duckdb::enable_shared_from_this< AttachedDatabase >
enable_shared_from_this & operator= (enable_shared_from_this const &) noexcept

Detailed Description

The AttachedDatabase represents an attached database instance.

Constructor & Destructor Documentation

◆ AttachedDatabase() [1/3]

duckdb::AttachedDatabase::AttachedDatabase ( DatabaseInstance & db,
AttachedDatabaseType type = AttachedDatabaseType::SYSTEM_DATABASE )
explicit

Create the built-in system database (without storage).

262157 : CatalogEntry(CatalogType::DATABASE_ENTRY,
262158 type == AttachedDatabaseType::SYSTEM_DATABASE ? SYSTEM_CATALOG : TEMP_CATALOG, 0),
262159 db(db), type(type), close_lock(make_shared_ptr<mutex>()) {
262160 // This database does not have storage, or uses temporary_objects for in-memory storage.
262161 D_ASSERT(type == AttachedDatabaseType::TEMP_DATABASE || type == AttachedDatabaseType::SYSTEM_DATABASE);
262162 if (type == AttachedDatabaseType::TEMP_DATABASE) {
262163 unordered_map<string, Value> options;
262164 AttachOptions attach_options(options, AccessMode::READ_WRITE);
262165 storage = make_uniq<SingleFileStorageManager>(*this, string(IN_MEMORY_PATH), attach_options);
262166 }
262167
262168 catalog = make_uniq<DuckCatalog>(*this);
262169 transaction_manager = make_uniq<DuckTransactionManager>(*this);
262170 internal = true;
262171}
CatalogType type
The type of this catalog entry.
Definition duckdb.hpp:6307
bool internal
Whether or not the entry is an internal entry (cannot be deleted, not dumped, etc).
Definition duckdb.hpp:6317

◆ AttachedDatabase() [2/3]

duckdb::AttachedDatabase::AttachedDatabase ( DatabaseInstance & db,
Catalog & catalog,
string name,
string file_path,
AttachOptions & options )

Create an attached database instance with the specified name and storage.

262175 : CatalogEntry(CatalogType::DATABASE_ENTRY, catalog_p, std::move(name_p)), db(db), parent_catalog(&catalog_p),
262176 close_lock(make_shared_ptr<mutex>()) {
262177 if (options.access_mode == AccessMode::READ_ONLY) {
262178 type = AttachedDatabaseType::READ_ONLY_DATABASE;
262179 } else {
262180 type = AttachedDatabaseType::READ_WRITE_DATABASE;
262181 }
262182 recovery_mode = options.recovery_mode;
262183 visibility = options.visibility;
262184
262185 // We create the storage after the catalog to guarantee we allow extensions to instantiate the DuckCatalog.
262186 catalog = make_uniq<DuckCatalog>(*this);
262187 stored_database_path = std::move(options.stored_database_path);
262188 storage = make_uniq<SingleFileStorageManager>(*this, std::move(file_path_p), options);
262189 transaction_manager = make_uniq<DuckTransactionManager>(*this);
262190 attach_options = options.options;
262191 internal = true;
262192}

◆ AttachedDatabase() [3/3]

duckdb::AttachedDatabase::AttachedDatabase ( DatabaseInstance & db,
Catalog & catalog,
StorageExtension & ext,
ClientContext & context,
string name,
AttachInfo & info,
AttachOptions & options )

Create an attached database instance with the specified storage extension.

262196 : CatalogEntry(CatalogType::DATABASE_ENTRY, catalog_p, std::move(name_p)), db(db), parent_catalog(&catalog_p),
262197 storage_extension(&storage_extension_p), close_lock(make_shared_ptr<mutex>()) {
262198 if (options.access_mode == AccessMode::READ_ONLY) {
262199 type = AttachedDatabaseType::READ_ONLY_DATABASE;
262200 } else {
262201 type = AttachedDatabaseType::READ_WRITE_DATABASE;
262202 }
262203 recovery_mode = options.recovery_mode;
262204 visibility = options.visibility;
262205
262206 optional_ptr<StorageExtensionInfo> storage_info = storage_extension->storage_info.get();
262207 catalog = storage_extension->attach(storage_info, context, *this, name, info, options);
262208 stored_database_path = std::move(options.stored_database_path);
262209 if (!catalog) {
262210 throw InternalException("AttachedDatabase - attach function did not return a catalog");
262211 }
262212 if (catalog->IsDuckCatalog()) {
262213 // The attached database uses the DuckCatalog.
262214 storage = make_uniq<SingleFileStorageManager>(*this, info.path, options);
262215 }
262216 transaction_manager = storage_extension->create_transaction_manager(storage_info, *this, *catalog);
262217 if (!transaction_manager) {
262218 throw InternalException(
262219 "AttachedDatabase - create_transaction_manager function did not return a transaction manager");
262220 }
262221 attach_options = options.options;
262222 internal = true;
262223}
string name
The name of the entry.
Definition duckdb.hpp:6311

◆ ~AttachedDatabase()

duckdb::AttachedDatabase::~AttachedDatabase ( )
override
262225 {
262226 // FIXME: Theoretically, we should catch all places higher up the call stack where the
262227 // FIXME: shared pointer goes out of scope and we need to invoke a checkpoint.
262228 // FIXME: However, for now, we keep this to avoid unintentional behavior / oversights.
262229 Close(DatabaseCloseAction::TRY_CHECKPOINT);
262230}
void Close(const DatabaseCloseAction action)
Close the database before shutting it down.
Definition duckdb.cpp:262351

Member Function Documentation

◆ Initialize()

void duckdb::AttachedDatabase::Initialize ( optional_ptr< ClientContext > context = nullptr)

Initializes the catalog and storage of the attached database.

262281 {
262282 if (IsSystem()) {
262283 catalog->Initialize(context, true);
262284 } else {
262285 catalog->Initialize(context, false);
262286 }
262287 if (storage) {
262288 storage->Initialize(context);
262289 }
262290}

◆ FinalizeLoad()

void duckdb::AttachedDatabase::FinalizeLoad ( optional_ptr< ClientContext > context)
262292 {
262293 catalog->FinalizeLoad(context);
262294}

◆ Close()

void duckdb::AttachedDatabase::Close ( const DatabaseCloseAction action)

Close the database before shutting it down.

262351 {
262352 if (is_closed) {
262353 return;
262354 }
262355 D_ASSERT(catalog);
262356 is_closed = true;
262357
262358 try {
262359 auto create_checkpoint = true;
262360 if (action == DatabaseCloseAction::TRY_CHECKPOINT && Exception::UncaughtException()) {
262361 create_checkpoint = false;
262362 } else if (!storage || storage->InMemory() || ValidChecker::IsInvalidated(db)) {
262363 create_checkpoint = false;
262364 }
262365
262366 if (create_checkpoint) {
262367 auto &config = DBConfig::GetConfig(db);
262368 if (config.options.checkpoint_on_shutdown) {
262369 CheckpointOptions options;
262370 options.wal_action = CheckpointWALAction::DELETE_WAL;
262371 storage->CreateCheckpoint(QueryContext(), options);
262372 }
262373 }
262374 } catch (std::exception &ex) {
262375 ErrorData data(ex);
262376 if (action == DatabaseCloseAction::TRY_CHECKPOINT) {
262377 try {
262378 DUCKDB_LOG_ERROR(db, "Silent exception in AttachedDatabase::Close():\t" + data.Message());
262379 } catch (...) { // NOLINT
262380 }
262381 } else {
262382 Cleanup();
262383 data.Throw("Detached database '" + name + "', but CHECKPOINT during DETACH failed. \n");
262384 }
262385 } catch (...) { // NOLINT
262386 }
262387
262388 Cleanup();
262389}
void Cleanup()
Clean any (shared) resources held by the database.
Definition duckdb.cpp:262391
@ DELETE_WAL
Delete the WAL file after the checkpoint completes - generally done on shutdown.
Definition duckdb.hpp:23117
Here is the call graph for this function:

◆ ParentCatalog() [1/2]

Catalog & duckdb::AttachedDatabase::ParentCatalog ( )
overridevirtual

Reimplemented from duckdb::CatalogEntry.

262322 {
262323 return *parent_catalog;
262324}

◆ ParentCatalog() [2/2]

const Catalog & duckdb::AttachedDatabase::ParentCatalog ( ) const
overridevirtual

Reimplemented from duckdb::CatalogEntry.

262326 {
262327 return *parent_catalog;
262328}

◆ HasStorageManager()

bool duckdb::AttachedDatabase::HasStorageManager ( ) const
262296 {
262297 return storage.get();
262298}

◆ GetStorageManager() [1/2]

StorageManager & duckdb::AttachedDatabase::GetStorageManager ( )
262300 {
262301 if (!storage) {
262302 throw InternalException("Internal system catalog does not have storage");
262303 }
262304 return *storage;
262305}

◆ GetStorageManager() [2/2]

const StorageManager & duckdb::AttachedDatabase::GetStorageManager ( ) const
262307 {
262308 if (!storage) {
262309 throw InternalException("Internal system catalog does not have storage");
262310 }
262311 return *storage;
262312}

◆ GetCatalog()

Catalog & duckdb::AttachedDatabase::GetCatalog ( )
262314 {
262315 return *catalog;
262316}

◆ GetTransactionManager()

TransactionManager & duckdb::AttachedDatabase::GetTransactionManager ( )
262318 {
262319 return *transaction_manager;
262320}

◆ GetDatabase()

DatabaseInstance & duckdb::AttachedDatabase::GetDatabase ( )
inline
40407 {
40408 return db;
40409 }

◆ GetStorageExtension()

optional_ptr< StorageExtension > duckdb::AttachedDatabase::GetStorageExtension ( )
inline
40411 {
40412 return storage_extension;
40413 }

◆ GetName()

const string & duckdb::AttachedDatabase::GetName ( ) const
inline
40415 {
40416 return name;
40417 }

◆ SetName()

void duckdb::AttachedDatabase::SetName ( const string & new_name)
inline
40418 {
40419 name = new_name;
40420 }

◆ IsSystem()

bool duckdb::AttachedDatabase::IsSystem ( ) const
262232 {
262233 D_ASSERT(!storage || type != AttachedDatabaseType::SYSTEM_DATABASE);
262234 return type == AttachedDatabaseType::SYSTEM_DATABASE;
262235}

◆ IsTemporary()

bool duckdb::AttachedDatabase::IsTemporary ( ) const
262237 {
262238 return type == AttachedDatabaseType::TEMP_DATABASE;
262239}

◆ IsReadOnly()

bool duckdb::AttachedDatabase::IsReadOnly ( ) const
262240 {
262241 return type == AttachedDatabaseType::READ_ONLY_DATABASE;
262242}

◆ IsInitialDatabase()

bool duckdb::AttachedDatabase::IsInitialDatabase ( ) const
262330 {
262331 return is_initial_database;
262332}

◆ SetInitialDatabase()

void duckdb::AttachedDatabase::SetInitialDatabase ( )
262334 {
262335 is_initial_database = true;
262336}

◆ SetReadOnlyDatabase()

void duckdb::AttachedDatabase::SetReadOnlyDatabase ( )
262338 {
262339 type = AttachedDatabaseType::READ_ONLY_DATABASE;
262340}

◆ OnDetach()

void duckdb::AttachedDatabase::OnDetach ( ClientContext & context)
262342 {
262343 if (catalog) {
262344 catalog->OnDetach(context);
262345 }
262346 if (stored_database_path && visibility != AttachVisibility::HIDDEN) {
262347 stored_database_path->OnDetach();
262348 }
262349}

◆ GetRecoveryMode()

RecoveryMode duckdb::AttachedDatabase::GetRecoveryMode ( ) const
inline
40428 {
40429 return recovery_mode;
40430 }

◆ GetVisibility()

AttachVisibility duckdb::AttachedDatabase::GetVisibility ( ) const
inline
40431 {
40432 return visibility;
40433 }

◆ GetAttachOptions()

const unordered_map< string, Value > & duckdb::AttachedDatabase::GetAttachOptions ( ) const
inline
40434 {
40435 return attach_options;
40436 }

◆ StoredPath()

string duckdb::AttachedDatabase::StoredPath ( ) const
262248 {
262249 if (stored_database_path) {
262250 return stored_database_path->path;
262251 }
262252 return string();
262253}

◆ NameIsReserved()

bool duckdb::AttachedDatabase::NameIsReserved ( const string & name)
static
262244 {
262245 return name == DEFAULT_SCHEMA || name == TEMP_CATALOG || name == SYSTEM_CATALOG;
262246}

◆ ExtractDatabaseName()

string duckdb::AttachedDatabase::ExtractDatabaseName ( const string & dbpath,
FileSystem & fs )
static
262261 {
262262 if (dbpath.empty() || dbpath == IN_MEMORY_PATH) {
262263 return "memory";
262264 }
262265 auto name = RemoveQueryParams(fs.ExtractBaseName(dbpath));
262266 if (NameIsReserved(name)) {
262267 name += "_db";
262268 }
262269 return name;
262270}

◆ InvokeCloseIfLastReference()

void duckdb::AttachedDatabase::InvokeCloseIfLastReference ( shared_ptr< AttachedDatabase > & attached_database)
static
262272 {
262273 auto close_lock = attached_db->close_lock;
262274 lock_guard<mutex> guard(*close_lock);
262275 if (attached_db.use_count() == 1) {
262276 attached_db->Close(DatabaseCloseAction::CHECKPOINT);
262277 }
262278 attached_db.reset();
262279}

◆ Cleanup()

void duckdb::AttachedDatabase::Cleanup ( )
private

Clean any (shared) resources held by the database.

262391 {
262392 transaction_manager.reset();
262393 catalog.reset();
262394 storage.reset();
262395 stored_database_path.reset();
262396}
Here is the caller graph for this function:

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