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