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

Public Member Functions

shared_ptr< DuckDB > GetInstance (const string &database, const DBConfig &config_dict)
 Gets a DB Instance from the cache if already exists (Fails if the configurations do not match).
shared_ptr< DuckDB > CreateInstance (const string &database, DBConfig &config_dict, bool cache_instance=true, const std::function< void(DuckDB &)> &on_create=nullptr)
 Creates and caches a new DB Instance (Fails if a cached instance already exists).
shared_ptr< DuckDB > GetOrCreateInstance (const string &database, DBConfig &config_dict, bool cache_instance, const std::function< void(DuckDB &)> &on_create=nullptr)
 Either returns an existing entry, or creates and caches a new DB Instance.
shared_ptr< DuckDB > GetOrCreateInstance (const string &database, DBConfig &config_dict, CacheBehavior cache_behavior=CacheBehavior::AUTOMATIC, const std::function< void(DuckDB &)> &on_create=nullptr)

Private Member Functions

shared_ptr< DuckDB > GetInstanceInternal (const string &database, const DBConfig &config, std::unique_lock< std::mutex > &db_instances_lock)
shared_ptr< DuckDB > CreateInstanceInternal (const string &database, DBConfig &config_dict, bool cache_instance, std::unique_lock< std::mutex > db_instances_lock, const std::function< void(DuckDB &)> &on_create)

Private Attributes

shared_ptr< DatabaseFilePathManager > path_manager
unordered_map< string, weak_ptr< DatabaseCacheEntry > > db_instances
 A map with the cached instances <absolute_path/instance>.
mutex cache_lock
 Lock to alter cache.

Constructor & Destructor Documentation

◆ DBInstanceCache()

duckdb::DBInstanceCache::DBInstanceCache ( )
277988 {
277989 path_manager = make_shared_ptr<DatabaseFilePathManager>();
277990}

◆ ~DBInstanceCache()

duckdb::DBInstanceCache::~DBInstanceCache ( )
277992 {
277993}

Member Function Documentation

◆ GetInstance()

shared_ptr< DuckDB > duckdb::DBInstanceCache::GetInstance ( const string & database,
const DBConfig & config_dict )

Gets a DB Instance from the cache if already exists (Fails if the configurations do not match).

278040 {
278041 unique_lock<mutex> lock {cache_lock};
278042 return GetInstanceInternal(database, config, lock);
278043}
mutex cache_lock
Lock to alter cache.
Definition duckdb.hpp:56916

◆ CreateInstance()

shared_ptr< DuckDB > duckdb::DBInstanceCache::CreateInstance ( const string & database,
DBConfig & config_dict,
bool cache_instance = true,
const std::function< void(DuckDB &)> & on_create = nullptr )

Creates and caches a new DB Instance (Fails if a cached instance already exists).

278086 {
278087 return CreateInstanceInternal(database, config, cache_instance, unique_lock<mutex>(cache_lock), on_create);
278088}

◆ GetOrCreateInstance() [1/2]

shared_ptr< DuckDB > duckdb::DBInstanceCache::GetOrCreateInstance ( const string & database,
DBConfig & config_dict,
bool cache_instance,
const std::function< void(DuckDB &)> & on_create = nullptr )

Either returns an existing entry, or creates and caches a new DB Instance.

278092 {
278093 auto cache_behavior = cache_instance ? CacheBehavior::ALWAYS_CACHE : CacheBehavior::NEVER_CACHE;
278094 return GetOrCreateInstance(database, config_dict, cache_behavior, on_create);
278095}
shared_ptr< DuckDB > GetOrCreateInstance(const string &database, DBConfig &config_dict, bool cache_instance, const std::function< void(DuckDB &)> &on_create=nullptr)
Either returns an existing entry, or creates and caches a new DB Instance.
Definition duckdb.cpp:278090
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetOrCreateInstance() [2/2]

shared_ptr< DuckDB > duckdb::DBInstanceCache::GetOrCreateInstance ( const string & database,
DBConfig & config_dict,
CacheBehavior cache_behavior = CacheBehavior::AUTOMATIC,
const std::function< void(DuckDB &)> & on_create = nullptr )
278099 {
278100 unique_lock<mutex> lock(cache_lock, std::defer_lock);
278101 bool cache_instance = cache_behavior == CacheBehavior::ALWAYS_CACHE;
278102 if (cache_behavior == CacheBehavior::AUTOMATIC) {
278103 // cache all unnamed in-memory connections
278104 cache_instance = true;
278105 if (database == IN_MEMORY_PATH || database.empty()) {
278106 cache_instance = false;
278107 }
278108 }
278109 if (cache_instance) {
278110 // While we do not own the lock, we cannot definitively say that the database instance does not exist.
278111 while (!lock.owns_lock()) {
278112 // The problem is, that we have to unlock the mutex in GetInstanceInternal, so we can non-blockingly wait
278113 // for the database creation within the cache entry.
278114 // Now even after unlocking and waiting for the DB creation we cannot guarantee that the database exists (it
278115 // could have gone out of scope in the meantime). If that happened we have unlocked the global lock to wait,
278116 // however we still return a nullptr. In this case we have to re-lock and try again until we either do not
278117 // find a cache entry (can be done without unlocking) or we find a cache entry and the database is valid as
278118 // well (in this case we can return that database)
278119 lock.lock();
278120 auto instance = GetInstanceInternal(database, config_dict, lock);
278121 if (instance) {
278122 return instance;
278123 }
278124 }
278125 } else {
278126 lock.lock();
278127 }
278128 return CreateInstanceInternal(database, config_dict, cache_instance, std::move(lock), on_create);
278129}

◆ GetInstanceInternal()

shared_ptr< DuckDB > duckdb::DBInstanceCache::GetInstanceInternal ( const string & database,
const DBConfig & config,
std::unique_lock< std::mutex > & db_instances_lock )
private
277996 {
277997 D_ASSERT(db_instances_lock.owns_lock());
277998 auto cache_key = GetCacheKey(database, config);
277999 auto entry = db_instances.find(cache_key);
278000 if (entry == db_instances.end()) {
278001 // path does not exist in the list yet - no cache entry
278002 return nullptr;
278003 }
278004 auto weak_cache_entry = entry->second;
278005 auto cache_entry = weak_cache_entry.lock();
278006 if (!cache_entry) {
278007 // cache entry does not exist anymore - clean it up
278008 db_instances.erase(entry);
278009 return nullptr;
278010 }
278011 shared_ptr<DuckDB> db_instance;
278012 {
278013 db_instances_lock.unlock();
278014 std::lock_guard<mutex> create_db_lock(cache_entry->update_database_mutex);
278015 db_instance = cache_entry->database.lock();
278016 }
278017 // cache entry exists - check if the actual database still exists
278018 if (!db_instance) {
278019 // if the database does not exist, but the cache entry still exists, the database is being shut down
278020 // we need to wait until the database is fully shut down to safely proceed
278021 // we do this here using a busy spin
278022 cache_entry.reset();
278023 while (!weak_cache_entry.expired()) {
278024 }
278025 D_ASSERT(!cache_entry);
278026 // the cache entry has now been deleted - clear it from the set of database instances and return
278027 db_instances_lock.lock();
278028 db_instances.erase(cache_key);
278029 db_instances_lock.unlock();
278030 return nullptr;
278031 }
278032 // the database instance exists - check that the config matches
278033 if (db_instance->instance->config != config) {
278034 throw ConnectionException("Can't open a connection to same database file with a different configuration "
278035 "than existing connections");
278036 }
278037 return db_instance;
278038}
unordered_map< string, weak_ptr< DatabaseCacheEntry > > db_instances
A map with the cached instances <absolute_path/instance>.
Definition duckdb.hpp:56913

◆ CreateInstanceInternal()

shared_ptr< DuckDB > duckdb::DBInstanceCache::CreateInstanceInternal ( const string & database,
DBConfig & config_dict,
bool cache_instance,
std::unique_lock< std::mutex > db_instances_lock,
const std::function< void(DuckDB &)> & on_create )
private
278048 {
278049 D_ASSERT(db_instances_lock.owns_lock());
278050 string abs_database_path;
278051 if (config.file_system) {
278052 abs_database_path = GetDBAbsolutePath(database, *config.file_system);
278053 } else {
278054 auto tmp_fs = FileSystem::CreateLocal();
278055 abs_database_path = GetDBAbsolutePath(database, *tmp_fs);
278056 }
278057 // Creates new instance
278058 string instance_path = abs_database_path;
278059 if (abs_database_path.rfind(IN_MEMORY_PATH, 0) == 0) {
278060 instance_path = IN_MEMORY_PATH;
278061 }
278062 shared_ptr<DuckDB> db_instance;
278063 config.path_manager = path_manager;
278064 if (cache_instance) {
278065 string cache_key = GetCacheKey(database, config);
278066 D_ASSERT(db_instances.find(cache_key) == db_instances.end());
278067 shared_ptr<DatabaseCacheEntry> cache_entry = make_shared_ptr<DatabaseCacheEntry>();
278068 config.db_cache_entry = cache_entry;
278069 // Create the new instance after unlocking to avoid new ddb creation requests to be blocked
278070 lock_guard<mutex> create_db_lock(cache_entry->update_database_mutex);
278071 db_instances[cache_key] = cache_entry;
278072 db_instances_lock.unlock();
278073 db_instance = make_shared_ptr<DuckDB>(instance_path, &config);
278074 cache_entry->database = db_instance;
278075 } else {
278076 db_instances_lock.unlock();
278077 db_instance = make_shared_ptr<DuckDB>(instance_path, &config);
278078 }
278079 if (on_create) {
278080 on_create(*db_instance);
278081 }
278082 return db_instance;
278083}
static DUCKDB_API unique_ptr< FileSystem > CreateLocal()
Create a LocalFileSystem.
Definition duckdb.cpp:80214

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