Kuzu 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#include <optional>
7
8#include "timer.h"
9#include "value.h"
10#include "scan_replacement.h"
11#include "client_config.h"
13#include "query_result.h"
14#include "statement.h"
15#include "prepared_statement.h"
16#include "warning_context.h"
17#include "transaction_context.h"
18
19namespace kuzu {
20namespace parser {
21class StandaloneCallRewriter;
22} // namespace parser
23
24namespace binder {
25class Binder;
26class ExpressionBinder;
27} // namespace binder
28
29namespace common {
30class RandomEngine;
31class TaskScheduler;
32class ProgressBar;
33} // namespace common
34
35namespace extension {
36class ExtensionManager;
37} // namespace extension
38
39namespace processor {
40class ImportDB;
41class TableFunctionCall;
42} // namespace processor
43
44namespace graph {
45class GraphEntrySet;
46}
47
48namespace main {
49struct DBConfig;
50class Database;
51class DatabaseManager;
52class AttachedKuzuDatabase;
53struct SpillToDiskSetting;
54struct ExtensionOption;
55class EmbeddedShell;
56
58 explicit ActiveQuery();
59 std::atomic<bool> interrupted;
61
62 void reset();
63};
64
70 friend class Connection;
71 friend class binder::Binder;
72 friend class binder::ExpressionBinder;
73 friend class processor::ImportDB;
74 friend class processor::TableFunctionCall;
75 friend class parser::StandaloneCallRewriter;
76 friend struct SpillToDiskSetting;
77 friend class EmbeddedShell;
78 friend class extension::ExtensionManager;
79
80public:
81 explicit ClientContext(Database* database);
83
84 // Client config
85 const ClientConfig* getClientConfig() const { return &clientConfig; }
86 ClientConfig* getClientConfigUnsafe() { return &clientConfig; }
87
88 // Database config
89 const DBConfig* getDBConfig() const;
91 common::Value getCurrentSetting(const std::string& optionName) const;
92
93 // Timer and timeout
94 void interrupt() { activeQuery.interrupted = true; }
95 bool interrupted() const { return activeQuery.interrupted; }
96 bool hasTimeout() const { return clientConfig.timeoutInMS != 0; }
97 void setQueryTimeOut(uint64_t timeoutInMS);
98 uint64_t getQueryTimeOut() const;
99 void startTimer();
100 uint64_t getTimeoutRemainingInMS() const;
101 void resetActiveQuery() { activeQuery.reset(); }
102
103 // Parallelism
104 void setMaxNumThreadForExec(uint64_t numThreads);
105 uint64_t getMaxNumThreadForExec() const;
106
107 // Transaction.
110
111 // Progress bar
112 common::ProgressBar* getProgressBar() const;
113
114 // Replace function.
116 std::unique_ptr<function::ScanReplacementData> tryReplaceByName(
117 const std::string& objectName) const;
118 std::unique_ptr<function::ScanReplacementData> tryReplaceByHandle(
120
121 // Extension
122 void setExtensionOption(std::string name, common::Value value);
123 const ExtensionOption* getExtensionOption(std::string optionName) const;
124 std::string getExtensionDir() const;
125
126 // Getters.
127 std::string getDatabasePath() const;
128 Database* getDatabase() const { return localDatabase; }
129 common::TaskScheduler* getTaskScheduler() const;
130 DatabaseManager* getDatabaseManager() const;
131 storage::StorageManager* getStorageManager() const;
132 storage::MemoryManager* getMemoryManager() const;
133 extension::ExtensionManager* getExtensionManager() const;
134 storage::WAL* getWAL() const;
135 catalog::Catalog* getCatalog() const;
136 transaction::TransactionManager* getTransactionManagerUnsafe() const;
137 common::VirtualFileSystem* getVFSUnsafe() const;
138 common::RandomEngine* getRandomEngine() const;
140 return cachedPreparedStatementManager;
141 }
142
143 bool isInMemory() const;
144
145 static std::string getEnvVariable(const std::string& name);
146 static std::string getUserHomeDir();
147
148 void setDefaultDatabase(AttachedKuzuDatabase* defaultDatabase_);
149 bool hasDefaultDatabase() const;
151 this->useInternalCatalogEntry_ = useInternalCatalogEntry;
152 }
154 return clientConfig.enableInternalCatalog ? true : useInternalCatalogEntry_;
155 }
156
157 void addScalarFunction(std::string name, function::function_set definitions);
158 void removeScalarFunction(const std::string& name);
159
162
163 graph::GraphEntrySet& getGraphEntrySetUnsafe();
164
165 const graph::GraphEntrySet& getGraphEntrySet() const;
166
167 void cleanUp();
168
169 // Query.
170 std::unique_ptr<PreparedStatement> prepareWithParams(std::string_view query,
171 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams = {});
172 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
173 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
174 std::optional<uint64_t> queryID = std::nullopt);
175 std::unique_ptr<QueryResult> query(std::string_view queryStatement,
176 std::optional<uint64_t> queryID = std::nullopt);
177
178private:
179 struct TransactionHelper {
180 enum class TransactionCommitAction : uint8_t {
181 COMMIT_IF_NEW,
182 COMMIT_IF_AUTO,
183 COMMIT_NEW_OR_AUTO,
184 NOT_COMMIT
185 };
186 static bool commitIfNew(TransactionCommitAction action) {
187 return action == TransactionCommitAction::COMMIT_IF_NEW ||
188 action == TransactionCommitAction::COMMIT_NEW_OR_AUTO;
189 }
190 static bool commitIfAuto(TransactionCommitAction action) {
191 return action == TransactionCommitAction::COMMIT_IF_AUTO ||
192 action == TransactionCommitAction::COMMIT_NEW_OR_AUTO;
193 }
194 static TransactionCommitAction getAction(bool commitIfNew, bool commitIfAuto);
195 static void runFuncInTransaction(transaction::TransactionContext& context,
196 const std::function<void()>& fun, bool readOnlyStatement, bool isTransactionStatement,
197 TransactionCommitAction action);
198 };
199 void validateTransaction(bool readOnly, bool requireTransaction) const;
200
201 std::vector<std::shared_ptr<parser::Statement>> parseQuery(std::string_view query);
202
203 struct PrepareResult {
204 std::unique_ptr<PreparedStatement> preparedStatement;
205 std::unique_ptr<CachedPreparedStatement> cachedPreparedStatement;
206 };
207
208 PrepareResult prepareNoLock(std::shared_ptr<parser::Statement> parsedStatement,
209 bool shouldCommitNewTransaction,
210 std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams =
211 std::nullopt);
212
213 template<typename T, typename... Args>
214 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
215 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
216 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
217 auto name = arg.first;
218 auto val = std::make_unique<common::Value>((T)arg.second);
219 params.insert({name, std::move(val)});
220 return executeWithParams(preparedStatement, std::move(params), args...);
221 }
222
223 std::unique_ptr<QueryResult> executeNoLock(PreparedStatement* preparedStatement,
224 CachedPreparedStatement* cachedPreparedStatement,
225 std::optional<uint64_t> queryID = std::nullopt);
226
227 std::unique_ptr<QueryResult> queryNoLock(std::string_view query,
228 std::optional<uint64_t> queryID = std::nullopt);
229
230 bool canExecuteWriteQuery() const;
231
232 std::unique_ptr<QueryResult> handleFailedExecution(std::optional<uint64_t> queryID,
233 const std::exception& e) const;
234
235 std::mutex mtx;
236 // Client side configurable settings.
237 ClientConfig clientConfig;
238 // Current query.
239 ActiveQuery activeQuery;
240 // Cache prepare statement.
241 CachedPreparedStatementManager cachedPreparedStatementManager;
242 // Transaction context.
243 std::unique_ptr<transaction::TransactionContext> transactionContext;
244 // Replace external object as pointer Value;
245 std::vector<function::ScanReplacement> scanReplacements;
246 // Extension configurable settings.
247 std::unordered_map<std::string, common::Value> extensionOptionValues;
248 // Random generator for UUID.
249 std::unique_ptr<common::RandomEngine> randomEngine;
250 // Local database.
251 Database* localDatabase;
252 // Remote database.
253 AttachedKuzuDatabase* remoteDatabase;
254 // Progress bar.
255 std::unique_ptr<common::ProgressBar> progressBar;
256 // Warning information
257 processor::WarningContext warningContext;
258 // Graph entries
259 std::unique_ptr<graph::GraphEntrySet> graphEntrySet;
260 // Whether the query can access internal tables/sequences or not.
261 bool useInternalCatalogEntry_ = false;
262 // Whether the transaction should be rolled back on destruction. If the parent database is
263 // closed, the rollback should be prevented or it will SEGFAULT.
264 bool preventTransactionRollbackOnDestruction = false;
265};
266
267} // namespace main
268} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition timer.h:12
Definition value.h:26
Definition prepared_statement_manager.h:12
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:128
ClientConfig * getClientConfigUnsafe()
Definition client_context.h:86
static std::string getUserHomeDir()
extension::ExtensionManager * getExtensionManager() const
common::Value getCurrentSetting(const std::string &optionName) const
processor::WarningContext & getWarningContextUnsafe()
common::RandomEngine * getRandomEngine() const
void removeScalarFunction(const std::string &name)
storage::StorageManager * getStorageManager() const
common::ProgressBar * getProgressBar() const
std::unique_ptr< function::ScanReplacementData > tryReplaceByName(const std::string &objectName) const
void setUseInternalCatalogEntry(bool useInternalCatalogEntry)
Definition client_context.h:150
common::TaskScheduler * getTaskScheduler() const
const CachedPreparedStatementManager & getCachedPreparedStatementManager() const
Definition client_context.h:139
std::unique_ptr< PreparedStatement > prepareWithParams(std::string_view query, std::unordered_map< std::string, std::unique_ptr< common::Value > > inputParams={})
const ClientConfig * getClientConfig() const
Definition client_context.h:85
friend class Connection
Definition client_context.h:70
uint64_t getMaxNumThreadForExec() const
void setMaxNumThreadForExec(uint64_t numThreads)
uint64_t getTimeoutRemainingInMS() const
catalog::Catalog * getCatalog() const
uint64_t getQueryTimeOut() const
transaction::Transaction * getTransaction() const
bool useInternalCatalogEntry() const
Definition client_context.h:153
void addScalarFunction(std::string name, function::function_set definitions)
std::string getDatabasePath() const
bool interrupted() const
Definition client_context.h:95
bool hasDefaultDatabase() const
friend class EmbeddedShell
Definition client_context.h:77
void addScanReplace(function::ScanReplacement scanReplacement)
DBConfig * getDBConfigUnsafe() const
void interrupt()
Definition client_context.h:94
const DBConfig * getDBConfig() const
friend struct SpillToDiskSetting
Definition client_context.h:76
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 graph::GraphEntrySet & getGraphEntrySet() const
const processor::WarningContext & getWarningContext() const
void setDefaultDatabase(AttachedKuzuDatabase *defaultDatabase_)
void setQueryTimeOut(uint64_t timeoutInMS)
static std::string getEnvVariable(const std::string &name)
transaction::TransactionContext * getTransactionContext() const
storage::MemoryManager * getMemoryManager() const
DatabaseManager * getDatabaseManager() const
const ExtensionOption * getExtensionOption(std::string optionName) const
std::unique_ptr< function::ScanReplacementData > tryReplaceByHandle(function::scan_replace_handle_t handle) const
storage::WAL * getWAL() const
std::string getExtensionDir() const
transaction::TransactionManager * getTransactionManagerUnsafe() const
void resetActiveQuery()
Definition client_context.h:101
ClientContext(Database *database)
graph::GraphEntrySet & getGraphEntrySetUnsafe()
void setExtensionOption(std::string name, common::Value value)
bool hasTimeout() const
Definition client_context.h:96
Database class is the main class of Kuzu. It manages all database components.
Definition database.h:98
A prepared statement is a parameterized query which can avoid planning the same query for repeated ex...
Definition prepared_statement.h:39
Definition warning_context.h:35
Definition transaction_context.h:33
Definition transaction.h:77
Definition bind_input.h:12
Definition client_context.h:35
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:44
uint8_t * scan_replace_handle_t
Definition scan_replacement.h:14
Definition client_context.h:44
Definition bind_input.h:16
Definition client_context.h:20
Definition client_context.h:39
Definition array_utils.h:7
Definition scan_replacement.h:19
std::atomic< bool > interrupted
Definition client_context.h:59
common::Timer timer
Definition client_context.h:60
Definition client_config.h:28
Definition db_config.h:57
Definition db_config.h:48