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 "progress_bar.h"
8#include "timer.h"
9#include "value.h"
10#include "scan_replacement.h"
11#include "client_config.h"
12#include "statement.h"
13#include "prepared_statement.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;
27} // namespace common
28
29namespace extension {
30struct ExtensionOptions;
31}
32
33namespace main {
34struct DBConfig;
35class Database;
36class DatabaseManager;
37class AttachedKuzuDatabase;
38
40 explicit ActiveQuery();
41 std::atomic<bool> interrupted;
43
44 void reset();
45};
46
52 friend class Connection;
53 friend class binder::Binder;
54 friend class binder::ExpressionBinder;
55
56public:
57 explicit ClientContext(Database* database);
59
60 // Client config
61 const ClientConfig* getClientConfig() const { return &clientConfig; }
62 ClientConfig* getClientConfigUnsafe() { return &clientConfig; }
63 const DBConfig* getDBConfig() const { return &dbConfig; }
64 DBConfig* getDBConfigUnsafe() { return &dbConfig; }
65 common::Value getCurrentSetting(const std::string& optionName);
66 bool isOptionSet(const std::string& optionName) const;
67 // Timer and timeout
68 void interrupt() { activeQuery.interrupted = true; }
69 bool interrupted() const { return activeQuery.interrupted; }
70 bool hasTimeout() const { return clientConfig.timeoutInMS != 0; }
71 void setQueryTimeOut(uint64_t timeoutInMS);
72 uint64_t getQueryTimeOut() const;
73 void startTimer();
74 uint64_t getTimeoutRemainingInMS() const;
75 void resetActiveQuery() { activeQuery.reset(); }
76
77 // Parallelism
78 void setMaxNumThreadForExec(uint64_t numThreads);
79 uint64_t getMaxNumThreadForExec() const;
80
81 // Transaction.
84
85 // Progress bar
87
88 // Replace function.
90 std::unique_ptr<function::ScanReplacementData> tryReplace(const std::string& objectName) const;
91 // Extension
92 void setExtensionOption(std::string name, common::Value value);
93 extension::ExtensionOptions* getExtensionOptions() const;
94 std::string getExtensionDir() const;
95
96 // Environment.
97 std::string getEnvVariable(const std::string& name);
98
99 // Database component getters.
100 std::string getDatabasePath() const;
101 Database* getDatabase() const { return localDatabase; }
102 common::TaskScheduler* getTaskScheduler() const;
103 DatabaseManager* getDatabaseManager() const;
104 storage::StorageManager* getStorageManager() const;
105 storage::MemoryManager* getMemoryManager();
106 storage::WAL* getWAL() const;
108 transaction::TransactionManager* getTransactionManagerUnsafe() const;
109 common::VirtualFileSystem* getVFSUnsafe() const;
110 common::RandomEngine* getRandomEngine();
111
112 // Query.
113 std::unique_ptr<PreparedStatement> prepare(std::string_view query);
114 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
115 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
116 std::optional<uint64_t> queryID = std::nullopt);
117 std::unique_ptr<QueryResult> query(std::string_view queryStatement,
118 std::optional<uint64_t> queryID = std::nullopt);
119 void runQuery(std::string query);
120
121 // TODO(Jiamin): should remove after supporting ddl in manual tx
122 std::unique_ptr<PreparedStatement> prepareTest(std::string_view query);
123 // only use for test framework
124 std::vector<std::shared_ptr<parser::Statement>> parseQuery(std::string_view query);
125
126 void setDefaultDatabase(AttachedKuzuDatabase* defaultDatabase_);
128
129 void runFuncInTransaction(const std::function<void(void)>& fun);
130 void addScalarFunction(std::string name, function::function_set definitions);
131 void removeScalarFunction(std::string name);
132
133 void cleanUP();
134
135private:
136 std::unique_ptr<QueryResult> query(std::string_view query, std::string_view encodedJoin,
137 bool enumerateAllPlans = true, std::optional<uint64_t> queryID = std::nullopt);
138
139 std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
140
141 std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
142
143 // when we do prepare, we will start a transaction for the query
144 // when we execute after prepare in a same context, we set requireNewTx to false and will not
145 // commit the transaction in prepare when we only prepare a query statement, we set requireNewTx
146 // to true and will commit the transaction in prepare
147 std::unique_ptr<PreparedStatement> prepareNoLock(
148 std::shared_ptr<parser::Statement> parsedStatement, bool enumerateAllPlans = false,
149 std::string_view joinOrder = std::string_view(), bool requireNewTx = true,
150 std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams =
151 std::nullopt);
152
153 template<typename T, typename... Args>
154 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
155 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
156 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
157 auto name = arg.first;
158 auto val = std::make_unique<common::Value>((T)arg.second);
159 params.insert({name, std::move(val)});
160 return executeWithParams(preparedStatement, std::move(params), args...);
161 }
162
163 void bindParametersNoLock(PreparedStatement* preparedStatement,
164 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
165
166 std::unique_ptr<QueryResult> executeAndAutoCommitIfNecessaryNoLock(
167 PreparedStatement* preparedStatement, uint32_t planIdx = 0u,
168 std::optional<uint64_t> queryID = std::nullopt);
169
170 bool canExecuteWriteQuery();
171
172 // Client side configurable settings.
173 ClientConfig clientConfig;
174 // Database configurable settings.
175 DBConfig& dbConfig;
176 // Current query.
177 ActiveQuery activeQuery;
178 // Transaction context.
179 std::unique_ptr<transaction::TransactionContext> transactionContext;
180 // Replace external object as pointer Value;
181 std::vector<function::ScanReplacement> scanReplacements;
182 // Extension configurable settings.
183 std::unordered_map<std::string, common::Value> extensionOptionValues;
184 // Random generator for UUID.
185 std::unique_ptr<common::RandomEngine> randomEngine;
186 // Local database.
187 Database* localDatabase;
188 // Remote database.
189 AttachedKuzuDatabase* remoteDatabase;
190 // Progress bar.
191 std::unique_ptr<common::ProgressBar> progressBar;
192 std::mutex mtx;
193};
194
195} // namespace main
196} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition catalog.h:48
Progress bar for tracking the progress of a pipeline. Prints the progress of each query pipeline and ...
Definition progress_bar.h:18
Definition timer.h:12
Definition value.h:27
Contain client side configuration. We make profiler associated per query, so profiler is not maintain...
Definition client_context.h:51
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:101
ClientConfig * getClientConfigUnsafe()
Definition client_context.h:62
common::RandomEngine * getRandomEngine()
storage::StorageManager * getStorageManager() const
common::ProgressBar * getProgressBar() const
common::Value getCurrentSetting(const std::string &optionName)
void runQuery(std::string query)
std::unique_ptr< PreparedStatement > prepare(std::string_view query)
common::TaskScheduler * getTaskScheduler() const
const ClientConfig * getClientConfig() const
Definition client_context.h:61
uint64_t getMaxNumThreadForExec() const
void setMaxNumThreadForExec(uint64_t numThreads)
uint64_t getTimeoutRemainingInMS() const
std::string getEnvVariable(const std::string &name)
std::unique_ptr< PreparedStatement > prepareTest(std::string_view query)
catalog::Catalog * getCatalog() const
extension::ExtensionOptions * getExtensionOptions() const
uint64_t getQueryTimeOut() const
DBConfig * getDBConfigUnsafe()
Definition client_context.h:64
void addScalarFunction(std::string name, function::function_set definitions)
std::string getDatabasePath() const
void runFuncInTransaction(const std::function< void(void)> &fun)
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:69
void removeScalarFunction(std::string name)
void addScanReplace(function::ScanReplacement scanReplacement)
void interrupt()
Definition client_context.h:68
const DBConfig * getDBConfig() const
Definition client_context.h:63
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)
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:75
ClientContext(Database *database)
void setExtensionOption(std::string name, common::Value value)
bool hasTimeout() const
Definition client_context.h:70
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 transaction_context.h:32
Definition transaction.h:28
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:43
Definition alter_type.h:5
Definition scan_replacement.h:16
Definition client_context.h:39
std::atomic< bool > interrupted
Definition client_context.h:41
common::Timer timer
Definition client_context.h:42
Definition client_config.h:11
Definition db_config.h:53