userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
options.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/sqlite/options.hpp
4
/// @brief SQLite options
5
6
#
include
<
string
>
7
8
#
include
<
userver
/
components
/
component_config
.
hpp
>
9
10
USERVER_NAMESPACE_BEGIN
11
12
namespace
storages::sqlite::settings {
13
14
/// @brief SQLite transaction options.
15
struct
TransactionOptions
{
16
/// @brief Locking mode for the transaction.
17
///
18
/// Determines how database locks are acquired during the transaction.
19
/// @see https://www.sqlite.org/lang_transaction.html
20
enum
LockingMode
{
21
kDeferred
,
///< Locks are acquired as needed (default behavior).
22
kImmediate
,
///< Acquires a reserved lock immediately.
23
kExclusive
///< Acquires an exclusive lock immediately.
24
};
25
26
/// @brief Isolation level for the transaction.
27
///
28
/// SQLite supports limited isolation levels. Note that full support for
29
/// READ UNCOMMITTED requires shared-cache mode.
30
/// @see https://www.sqlite.org/isolation.html
31
enum
class
IsolationLevel
{
32
kSerializable
,
///< Default isolation level; ensures full isolation.
33
kReadUncommitted
///< Allows reading uncommitted changes; requires shared-cache mode.
34
};
35
36
IsolationLevel
isolation_level =
IsolationLevel
::
kSerializable
;
37
LockingMode
mode =
LockingMode
::
kDeferred
;
38
39
constexpr
TransactionOptions() =
default
;
40
constexpr
explicit
TransactionOptions(
IsolationLevel
lvl)
41
: isolation_level{lvl}
42
{}
43
constexpr
TransactionOptions(
IsolationLevel
lvl,
LockingMode
m)
44
: isolation_level{lvl},
45
mode{m}
46
{}
47
constexpr
explicit
TransactionOptions(
LockingMode
m)
48
: mode{m}
49
{}
50
51
/// @brief Creates a TransactionOptions instance with deferred locking mode.
52
static
constexpr
TransactionOptions
Deferred
() {
return
TransactionOptions
{
kDeferred
}; }
53
};
54
55
constexpr
inline
bool
operator==(
const
TransactionOptions
& lhs,
const
TransactionOptions
& rhs) {
56
return
lhs.mode == rhs.mode;
57
}
58
59
std::string IsolationLevelToString(
const
TransactionOptions
::
IsolationLevel
& lvl);
60
61
/// @brief Default maximum size for the prepared statements cache.
62
inline
constexpr
std::size_t
kDefaultMaxPreparedCacheSize
= 200;
63
64
/// @brief Default setting for caching prepared statements.
65
inline
constexpr
bool
kDefaultPrepareStatement
=
true
;
66
67
/// @brief SQLite connection settings.
68
///
69
/// Configures behavior related to prepared statements and their caching.
70
struct
ConnectionSettings
{
71
/// @brief Options for handling prepared statements.
72
enum
PreparedStatementOptions
{
73
kCachePreparedStatements
,
///< Enable caching of prepared statements.
74
kNoPreparedStatements
,
///< Disable caching of prepared statements.
75
};
76
77
/// Cache prepared statements or not
78
PreparedStatementOptions
79
prepared_statements
=
kDefaultPrepareStatement
?
kCachePreparedStatements
:
kNoPreparedStatements
;
80
81
/// Maximum number of prepared statements to cache
82
std::size_t
max_prepared_cache_size
=
kDefaultMaxPreparedCacheSize
;
83
84
static
ConnectionSettings
Create(
const
components
::ComponentConfig& config);
85
};
86
87
/// @brief Default initial size for the connection pool
88
inline
constexpr
std::size_t
kDefaultInitialPoolSize
= 5;
89
90
/// @brief Default maximum size for the connection pool
91
inline
constexpr
std::size_t
kDefaultMaxPoolSize
= 10;
92
93
/// @brief SQLite connection pool settings.
94
///
95
/// Configures the behavior of the connection pool, including its size.
96
struct
PoolSettings
final
{
97
/// @brief Number of connections created initially.
98
std::size_t
initial_pool_size
{
kDefaultInitialPoolSize
};
99
100
/// @brief Maximum number of connections in the pool.
101
std::size_t
max_pool_size
{
kDefaultMaxPoolSize
};
102
103
static
PoolSettings Create(
const
components
::ComponentConfig& config);
104
};
105
106
inline
constexpr
bool
kDefaultCreateFile =
true
;
107
inline
constexpr
bool
kDefaultIsReadOnly =
false
;
108
inline
constexpr
bool
kDefaultSharedCache =
false
;
109
inline
constexpr
bool
kDefaultReadUncommitted =
false
;
110
inline
constexpr
bool
kDefaultForeignKeys =
true
;
111
inline
constexpr
std::string_view kDefaultJournalMode =
"wal"
;
112
inline
constexpr
std::string_view kDefaultSynchronous =
"normal"
;
113
inline
constexpr
std::string_view kDefaultTempStore =
"memory"
;
114
inline
constexpr
int
kDefaultBusyTimeout = 5000;
115
inline
constexpr
int
kDefaultCacheSize = -2000;
116
inline
constexpr
int
kDefaultJournalSizeLimit = 67108864;
117
inline
constexpr
int
kDefaultMmapSize = 134217728;
118
inline
constexpr
int
kDefaultPageSize = 4096;
119
120
/// @brief Comprehensive SQLite settings.
121
///
122
/// Aggregates various settings for configuring SQLite behavior.
123
struct
SQLiteSettings
{
124
/// @brief Read mode for the database.
125
enum
class
ReadMode
{
126
kReadOnly
,
///< Open the database in read-only mode
127
kReadWrite
///< Open the database in read-write mode
128
};
129
130
/// @brief Journal mode options.
131
enum
class
JournalMode
{
132
kDelete
,
///< Delete the journal file after each transaction.
133
kTruncate
,
///< Truncate the journal file to zero length.
134
kPersist
,
///< Keep the journal file but zero its header.
135
kMemory
,
///< Store the journal in memory.
136
kWal
,
///< Use write-ahead logging.
137
kOff
///< Disable the journal.
138
};
139
140
/// @brief Synchronous setting options.
141
enum
Synchronous
{
142
kExtra
,
///< Extra synchronization.
143
kFull
,
///< Full synchronization.
144
kNormal
,
///< Normal synchronization.
145
kOff
///< No synchronization.
146
};
147
148
/// @brief Temporary store options.
149
enum
TempStore
{
150
kMemory
,
///< Store temporary tables in memory.
151
kFile
///< Store temporary tables in a file.
152
};
153
154
ReadMode
read_mode = !kDefaultIsReadOnly ?
ReadMode
::
kReadWrite
:
ReadMode
::
kReadOnly
;
155
bool
create_file = kDefaultCreateFile;
156
bool
shared_cache = kDefaultSharedCache;
157
bool
read_uncommitted = kDefaultReadUncommitted;
158
bool
foreign_keys = kDefaultForeignKeys;
159
JournalMode
journal_mode =
JournalMode
::
kWal
;
160
int
busy_timeout = kDefaultBusyTimeout;
161
Synchronous
synchronous =
Synchronous
::
kNormal
;
162
int
cache_size = kDefaultCacheSize;
163
TempStore
temp_store =
TempStore
::
kMemory
;
164
int
journal_size_limit = kDefaultJournalSizeLimit;
165
int
mmap_size = kDefaultMmapSize;
166
int
page_size = kDefaultPageSize;
167
std::string db_path;
168
ConnectionSettings
conn_settings;
169
PoolSettings pool_settings;
170
};
171
172
std::string JournalModeToString(
const
SQLiteSettings
::
JournalMode
& mode);
173
174
}
// namespace storages::sqlite::settings
175
176
USERVER_NAMESPACE_END
userver
storages
sqlite
options.hpp
Generated on
for userver by
Doxygen
1.17.0