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 main {
39struct DBConfig;
40class Database;
41class DatabaseManager;
42class AttachedKuzuDatabase;
43struct SpillToDiskSetting;
44
46 explicit ActiveQuery();
47 std::atomic<bool> interrupted;
49
50 void reset();
51};
52
58 friend class Connection;
59 friend class binder::Binder;
60 friend class binder::ExpressionBinder;
61 friend class processor::ImportDB;
62 friend struct main::SpillToDiskSetting;
63
64public:
65 explicit ClientContext(Database* database);
67
68 // Client config
69 const ClientConfig* getClientConfig() const { return &clientConfig; }
70 ClientConfig* getClientConfigUnsafe() { return &clientConfig; }
71 const DBConfig* getDBConfig() const { return &dbConfig; }
72 DBConfig* getDBConfigUnsafe() { return &dbConfig; }
73 common::Value getCurrentSetting(const std::string& optionName);
74 bool isOptionSet(const std::string& optionName) const;
75 // Timer and timeout
76 void interrupt() { activeQuery.interrupted = true; }
77 bool interrupted() const { return activeQuery.interrupted; }
78 bool hasTimeout() const { return clientConfig.timeoutInMS != 0; }
79 void setQueryTimeOut(uint64_t timeoutInMS);
80 uint64_t getQueryTimeOut() const;
81 void startTimer();
82 uint64_t getTimeoutRemainingInMS() const;
83 void resetActiveQuery() { activeQuery.reset(); }
84
85 // Parallelism
86 void setMaxNumThreadForExec(uint64_t numThreads);
87 uint64_t getMaxNumThreadForExec() const;
88
89 // Transaction.
92
93 // Progress bar
94 common::ProgressBar* getProgressBar() const;
95
96 // Replace function.
98 std::unique_ptr<function::ScanReplacementData> tryReplace(const std::string& objectName) const;
99 // Extension
100 void setExtensionOption(std::string name, common::Value value);
101 extension::ExtensionOptions* getExtensionOptions() const;
102 std::string getExtensionDir() const;
103
104 // Environment.
105 std::string getEnvVariable(const std::string& name);
106
107 // Database component getters.
108 std::string getDatabasePath() const;
109 Database* getDatabase() const { return localDatabase; }
110 common::TaskScheduler* getTaskScheduler() const;
111 DatabaseManager* getDatabaseManager() const;
112 storage::StorageManager* getStorageManager() const;
113 storage::MemoryManager* getMemoryManager();
114 storage::WAL* getWAL() const;
115 catalog::Catalog* getCatalog() const;
116 transaction::TransactionManager* getTransactionManagerUnsafe() const;
117 common::VirtualFileSystem* getVFSUnsafe() const;
118 common::RandomEngine* getRandomEngine();
119
120 // Query.
121 std::unique_ptr<PreparedStatement> prepare(std::string_view query);
122 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
123 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
124 std::optional<uint64_t> queryID = std::nullopt);
125 std::unique_ptr<QueryResult> query(std::string_view queryStatement,
126 std::optional<uint64_t> queryID = std::nullopt);
127
128 // only use for test framework
129 std::vector<std::shared_ptr<parser::Statement>> parseQuery(std::string_view query);
130
131 void setDefaultDatabase(AttachedKuzuDatabase* defaultDatabase_);
133
134 void addScalarFunction(std::string name, function::function_set definitions);
135 void removeScalarFunction(std::string name);
136
139
140 void cleanUP();
141
142private:
143 std::unique_ptr<QueryResult> queryInternal(std::string_view query, std::string_view encodedJoin,
144 bool enumerateAllPlans = true, std::optional<uint64_t> queryID = std::nullopt);
145
146 std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
147
148 std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
149
150 // when we do prepare, we will start a transaction for the query
151 // when we execute after prepare in a same context, we set requireNewTx to false and will not
152 // commit the transaction in prepare when we only prepare a query statement, we set requireNewTx
153 // to true and will commit the transaction in prepare
154 std::unique_ptr<PreparedStatement> prepareNoLock(
155 std::shared_ptr<parser::Statement> parsedStatement, bool enumerateAllPlans = false,
156 std::string_view joinOrder = std::string_view(), bool requireNewTx = true,
157 std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams =
158 std::nullopt);
159
160 template<typename T, typename... Args>
161 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
162 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
163 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
164 auto name = arg.first;
165 auto val = std::make_unique<common::Value>((T)arg.second);
166 params.insert({name, std::move(val)});
167 return executeWithParams(preparedStatement, std::move(params), args...);
168 }
169
170 void bindParametersNoLock(PreparedStatement* preparedStatement,
171 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
172
173 std::unique_ptr<QueryResult> executeNoLock(PreparedStatement* preparedStatement,
174 uint32_t planIdx = 0u, std::optional<uint64_t> queryID = std::nullopt);
175
176 bool canExecuteWriteQuery();
177
178 void runFuncInTransaction(const std::function<void(void)>& fun);
179
180 // Client side configurable settings.
181 ClientConfig clientConfig;
182 // Database configurable settings.
183 DBConfig& dbConfig;
184 // Current query.
185 ActiveQuery activeQuery;
186 // Transaction context.
187 std::unique_ptr<transaction::TransactionContext> transactionContext;
188 // Replace external object as pointer Value;
189 std::vector<function::ScanReplacement> scanReplacements;
190 // Extension configurable settings.
191 std::unordered_map<std::string, common::Value> extensionOptionValues;
192 // Random generator for UUID.
193 std::unique_ptr<common::RandomEngine> randomEngine;
194 // Local database.
195 Database* localDatabase;
196 // Remote database.
197 AttachedKuzuDatabase* remoteDatabase;
198 // Progress bar.
199 std::unique_ptr<common::ProgressBar> progressBar;
200 // Warning information
201 processor::WarningContext warningContext;
202 std::mutex mtx;
203};
204
205} // namespace main
206} // 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:57
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:109
ClientConfig * getClientConfigUnsafe()
Definition client_context.h:70
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:69
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:72
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:77
void removeScalarFunction(std::string name)
void addScanReplace(function::ScanReplacement scanReplacement)
void interrupt()
Definition client_context.h:76
const DBConfig * getDBConfig() const
Definition client_context.h:71
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)
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:83
ClientContext(Database *database)
void setExtensionOption(std::string name, common::Value value)
bool hasTimeout() const
Definition client_context.h:78
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:30
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:45
std::atomic< bool > interrupted
Definition client_context.h:47
common::Timer timer
Definition client_context.h:48
Definition client_config.h:26
Definition db_config.h:53