| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/main/db_instance_cache.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/main/connection_manager.hpp" |
| 12 | #include "duckdb/main/database.hpp" |
| 13 | #include "duckdb/common/unordered_map.hpp" |
| 14 | #include "duckdb/function/replacement_scan.hpp" |
| 15 | |
| 16 | namespace duckdb { |
| 17 | class DBInstanceCache { |
| 18 | public: |
| 19 | DBInstanceCache() {}; |
| 20 | //! Gets a DB Instance from the cache if already exists (Fails if the configurations do not match) |
| 21 | shared_ptr<DuckDB> GetInstance(const string &database, const DBConfig &config_dict); |
| 22 | |
| 23 | //! Creates and caches a new DB Instance (Fails if a cached instance already exists) |
| 24 | shared_ptr<DuckDB> CreateInstance(const string &database, DBConfig &config_dict, bool cache_instance = true); |
| 25 | |
| 26 | //! Creates and caches a new DB Instance (Fails if a cached instance already exists) |
| 27 | shared_ptr<DuckDB> GetOrCreateInstance(const string &database, DBConfig &config_dict, bool cache_instance); |
| 28 | |
| 29 | private: |
| 30 | //! A map with the cached instances <absolute_path/instance> |
| 31 | unordered_map<string, weak_ptr<DuckDB>> db_instances; |
| 32 | |
| 33 | //! Lock to alter cache |
| 34 | mutex cache_lock; |
| 35 | |
| 36 | private: |
| 37 | shared_ptr<DuckDB> GetInstanceInternal(const string &database, const DBConfig &config_dict); |
| 38 | shared_ptr<DuckDB> CreateInstanceInternal(const string &database, DBConfig &config_dict, bool cache_instance); |
| 39 | }; |
| 40 | } // namespace duckdb |
| 41 |