1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/main/connection_manager.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/mutex.hpp"
13#include "duckdb/main/client_context.hpp"
14#include "duckdb/common/vector.hpp"
15
16namespace duckdb {
17class ClientContext;
18class DatabaseInstance;
19
20class ConnectionManager {
21public:
22 ConnectionManager() {
23 }
24
25 void AddConnection(ClientContext &context) {
26 lock_guard<mutex> lock(connections_lock);
27 connections.insert(x: make_pair(x: &context, y: weak_ptr<ClientContext>(context.shared_from_this())));
28 }
29
30 void RemoveConnection(ClientContext &context) {
31 lock_guard<mutex> lock(connections_lock);
32 connections.erase(x: &context);
33 }
34
35 vector<shared_ptr<ClientContext>> GetConnectionList() {
36 vector<shared_ptr<ClientContext>> result;
37 for (auto &it : connections) {
38 auto connection = it.second.lock();
39 if (!connection) {
40 connections.erase(x: it.first);
41 continue;
42 } else {
43 result.push_back(x: std::move(connection));
44 }
45 }
46
47 return result;
48 }
49
50 ClientContext *GetConnection(DatabaseInstance *db);
51
52 static ConnectionManager &Get(DatabaseInstance &db);
53 static ConnectionManager &Get(ClientContext &context);
54
55public:
56 mutex connections_lock;
57 unordered_map<ClientContext *, weak_ptr<ClientContext>> connections;
58};
59
60} // namespace duckdb
61