Kùzu C++ API
Loading...
Searching...
No Matches
client_context.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <memory>
5#include <mutex>
6
7#include "timer.h"
8#include "value.h"
9#include "scan_replacement.h"
10#include "client_config.h"
11#include "statement.h"
12#include "prepared_statement.h"
13#include "warning_context.h"
14#include "query_result.h"
15#include "transaction_context.h"
16
17namespace kuzu {
18
19namespace binder {
20class Binder;
21class ExpressionBinder;
22} // namespace binder
23
24namespace common {
25class RandomEngine;
26class TaskScheduler;
27class ProgressBar;
28} // namespace common
29
30namespace extension {
31struct ExtensionOptions;
32}
33
34namespace processor {
35class ImportDB;
36}
37
38namespace graph {
39class GraphEntrySet;
40}
41
42namespace main {
43struct DBConfig;
44class Database;
45class DatabaseManager;
46class AttachedKuzuDatabase;
47struct SpillToDiskSetting;
48
50 explicit ActiveQuery();
51 std::atomic<bool> interrupted;
53
54 void reset();
55};
56
62 friend class Connection;
63 friend class binder::Binder;
64 friend class binder::ExpressionBinder;
65 friend class processor::ImportDB;
66 friend struct main::SpillToDiskSetting;
67
68public:
69 explicit ClientContext(Database* database);
71
72 // Client config
73 const ClientConfig* getClientConfig() const { return &clientConfig; }
74 ClientConfig* getClientConfigUnsafe() { return &clientConfig; }
75 const DBConfig* getDBConfig() const { return &dbConfig; }
76 DBConfig* getDBConfigUnsafe() { return &dbConfig; }
77 common::Value getCurrentSetting(const std::string& optionName);
78 bool isOptionSet(const std::string& optionName) const;
79 // Timer and timeout
80 void interrupt() { activeQuery.interrupted = true; }
81 bool interrupted() const { return activeQuery.interrupted; }
82 bool hasTimeout() const { return clientConfig.timeoutInMS != 0; }
83 void setQueryTimeOut(uint64_t timeoutInMS);
84 uint64_t getQueryTimeOut() const;
85 void startTimer();
86 uint64_t getTimeoutRemainingInMS() const;
87 void resetActiveQuery() { activeQuery.reset(); }
88
89 // Parallelism
90 void setMaxNumThreadForExec(uint64_t numThreads);
91 uint64_t getMaxNumThreadForExec() const;
92
93 // Transaction.
96
97 // Progress bar
98 common::ProgressBar* getProgressBar() const;
99
100 // Replace function.
102 std::unique_ptr<function::ScanReplacementData> tryReplace(const std::string& objectName) const;
103 // Extension
104 void setExtensionOption(std::string name, common::Value value);
105 extension::ExtensionOptions* getExtensionOptions() const;
106 std::string getExtensionDir() const;
107
108 // Environment.
109 std::string getEnvVariable(const std::string& name);
110
111 // Database component getters.
112 std::string getDatabasePath() const;
113 Database* getDatabase() const { return localDatabase; }
114 common::TaskScheduler* getTaskScheduler() const;
115 DatabaseManager* getDatabaseManager() const;
116 storage::StorageManager* getStorageManager() const;
117 storage::MemoryManager* getMemoryManager();
118 storage::WAL* getWAL() const;
119 catalog::Catalog* getCatalog() const;
120 transaction::TransactionManager* getTransactionManagerUnsafe() const;
121 common::VirtualFileSystem* getVFSUnsafe() const;
122 common::RandomEngine* getRandomEngine();
123
124 // Query.
125 std::unique_ptr<PreparedStatement> prepare(std::string_view query);
126 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
127 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
128 std::optional<uint64_t> queryID = std::nullopt);
129 std::unique_ptr<QueryResult> query(std::string_view queryStatement,
130 std::optional<uint64_t> queryID = std::nullopt);
131
132 // only use for test framework
133 std::vector<std::shared_ptr<parser::Statement>> parseQuery(std::string_view query);
134
135 void setDefaultDatabase(AttachedKuzuDatabase* defaultDatabase_);
137
138 void addScalarFunction(std::string name, function::function_set definitions);
139 void removeScalarFunction(std::string name);
140
143
144 graph::GraphEntrySet& getGraphEntrySetUnsafe();
145
146 void cleanUP();
147
148 std::unique_ptr<QueryResult> queryInternal(std::string_view query, std::string_view encodedJoin,
149 bool enumerateAllPlans = true, std::optional<uint64_t> queryID = std::nullopt);
150
151private:
152 std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
153
154 std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
155
156 // when we do prepare, we will start a transaction for the query
157 // when we execute after prepare in a same context, we set requireNewTx to false and will not
158 // commit the transaction in prepare when we only prepare a query statement, we set requireNewTx
159 // to true and will commit the transaction in prepare
160 std::unique_ptr<PreparedStatement> prepareNoLock(
161 std::shared_ptr<parser::Statement> parsedStatement, bool enumerateAllPlans = false,
162 std::string_view joinOrder = std::string_view(), bool requireNewTx = true,
163 std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams =
164 std::nullopt);
165
166 template<typename T, typename... Args>
167 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
168 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
169 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
170 auto name = arg.first;
171 auto val = std::make_unique<common::Value>((T)arg.second);
172 params.insert({name, std::move(val)});
173 return executeWithParams(preparedStatement, std::move(params), args...);
174 }
175
176 void bindParametersNoLock(PreparedStatement* preparedStatement,
177 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
178
179 std::unique_ptr<QueryResult> executeNoLock(PreparedStatement* preparedStatement,
180 uint32_t planIdx = 0u, std::optional<uint64_t> queryID = std::nullopt);
181
182 bool canExecuteWriteQuery();
183
184 void runFuncInTransaction(const std::function<void(void)>& fun);
185
186 // Client side configurable settings.
187 ClientConfig clientConfig;
188 // Database configurable settings.
189 DBConfig& dbConfig;
190 // Current query.
191 ActiveQuery activeQuery;
192 // Transaction context.
193 std::unique_ptr<transaction::TransactionContext> transactionContext;
194 // Replace external object as pointer Value;
195 std::vector<function::ScanReplacement> scanReplacements;
196 // Extension configurable settings.
197 std::unordered_map<std::string, common::Value> extensionOptionValues;
198 // Random generator for UUID.
199 std::unique_ptr<common::RandomEngine> randomEngine;
200 // Local database.
201 Database* localDatabase;
202 // Remote database.
203 AttachedKuzuDatabase* remoteDatabase;
204 // Progress bar.
205 std::unique_ptr<common::ProgressBar> progressBar;
206 // Warning information
207 processor::WarningContext warningContext;
208 // Graph entries
209 std::unique_ptr<graph::GraphEntrySet> graphEntrySet;
210 std::mutex mtx;
211};
212
213} // namespace main
214} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition timer.h:12
Definition value.h:26
Contain client side configuration. We make profiler associated per query, so profiler is not maintain...
Definition client_context.h:61
std::unique_ptr< QueryResult > query(std::string_view queryStatement, std::optional< uint64_t > queryID=std::nullopt)
common::VirtualFileSystem * getVFSUnsafe() const
Database * getDatabase() const
Definition client_context.h:113
ClientConfig * getClientConfigUnsafe()
Definition client_context.h:74
processor::WarningContext & getWarningContextUnsafe()
common::RandomEngine * getRandomEngine()
storage::StorageManager * getStorageManager() const
common::ProgressBar * getProgressBar() const
common::Value getCurrentSetting(const std::string &optionName)
std::unique_ptr< PreparedStatement > prepare(std::string_view query)
common::TaskScheduler * getTaskScheduler() const
const ClientConfig * getClientConfig() const
Definition client_context.h:73
uint64_t getMaxNumThreadForExec() const
void setMaxNumThreadForExec(uint64_t numThreads)
uint64_t getTimeoutRemainingInMS() const
std::string getEnvVariable(const std::string &name)
catalog::Catalog * getCatalog() const
extension::ExtensionOptions * getExtensionOptions() const
uint64_t getQueryTimeOut() const
DBConfig * getDBConfigUnsafe()
Definition client_context.h:76
void addScalarFunction(std::string name, function::function_set definitions)
std::string getDatabasePath() const
std::vector< std::shared_ptr< parser::Statement > > parseQuery(std::string_view query)
bool isOptionSet(const std::string &optionName) const
bool interrupted() const
Definition client_context.h:81
void removeScalarFunction(std::string name)
void addScanReplace(function::ScanReplacement scanReplacement)
void interrupt()
Definition client_context.h:80
const DBConfig * getDBConfig() const
Definition client_context.h:75
std::unique_ptr< function::ScanReplacementData > tryReplace(const std::string &objectName) const
std::unique_ptr< QueryResult > executeWithParams(PreparedStatement *preparedStatement, std::unordered_map< std::string, std::unique_ptr< common::Value > > inputParams, std::optional< uint64_t > queryID=std::nullopt)
std::unique_ptr< QueryResult > queryInternal(std::string_view query, std::string_view encodedJoin, bool enumerateAllPlans=true, std::optional< uint64_t > queryID=std::nullopt)
const processor::WarningContext & getWarningContext() const
void setDefaultDatabase(AttachedKuzuDatabase *defaultDatabase_)
void setQueryTimeOut(uint64_t timeoutInMS)
transaction::TransactionContext * getTransactionContext() const
storage::MemoryManager * getMemoryManager()
DatabaseManager * getDatabaseManager() const
transaction::Transaction * getTx() const
storage::WAL * getWAL() const
std::string getExtensionDir() const
transaction::TransactionManager * getTransactionManagerUnsafe() const
void resetActiveQuery()
Definition client_context.h:87
ClientContext(Database *database)
graph::GraphEntrySet & getGraphEntrySetUnsafe()
void setExtensionOption(std::string name, common::Value value)
bool hasTimeout() const
Definition client_context.h:82
Connection is used to interact with a Database instance. Each Connection is thread-safe....
Definition connection.h:14
Database class is the main class of KùzuDB. It manages all database components.
Definition database.h:81
A prepared statement is a parameterized query which can avoid planning the same query for repeated ex...
Definition prepared_statement.h:20
Definition warning_context.h:35
Definition transaction_context.h:32
Definition transaction.h:31
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:43
Definition array_utils.h:7
Definition scan_replacement.h:16
Definition client_context.h:49
std::atomic< bool > interrupted
Definition client_context.h:51
common::Timer timer
Definition client_context.h:52
Definition client_config.h:26
Definition db_config.h:53