Jazz 1.25.+
Loading...
Searching...
No Matches
persisted.h
1/* Jazz (c) 2018-2026 kaalam.ai (The Authors of Jazz), using (under the same license):
2
3 1. Biomodelling - The AATBlockQueue class (c) Jacques BasaldĂșa, 2009-2012 licensed
4 exclusively for the use in the Jazz server software.
5
6 Copyright 2009-2012 Jacques BasaldĂșa
7
8 2. BBVA - Jazz: A lightweight analytical web server for data-driven applications.
9
10 Copyright 2016-2017 Banco Bilbao Vizcaya Argentaria, S.A.
11
12 This product includes software developed at
13
14 BBVA (https://www.bbva.com/)
15
16 3. LMDB, Copyright 2011-2017 Howard Chu, Symas Corp. All rights reserved.
17
18 Licensed under http://www.OpenLDAP.org/license.html
19
20
21 Licensed under the Apache License, Version 2.0 (the "License");
22 you may not use this file except in compliance with the License.
23 You may obtain a copy of the License at
24
25 http://www.apache.org/licenses/LICENSE-2.0
26
27 Unless required by applicable law or agreed to in writing, software
28 distributed under the License is distributed on an "AS IS" BASIS,
29 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 See the License for the specific language governing permissions and
31 limitations under the License.
32*/
33
34
35#include "src/jazz_elements/volatile.h"
36
37#ifdef CATCH_TEST
38#ifndef INCLUDED_JAZZ_CATCH2
39#define INCLUDED_JAZZ_CATCH2
40
41#include "src/catch2/catch.hpp"
42
43#endif
44#endif
45
46
47#ifndef INCLUDED_JAZZ_ELEMENTS_PERSISTED
48#define INCLUDED_JAZZ_ELEMENTS_PERSISTED
49
50#include "src/lmdb/lmdb.h"
51
52
53namespace jazz_elements
54{
55
56#define MAX_POSSIBLE_SOURCES 32
57#define MAX_LMDB_HOME_LEN 128
58#define LMDB_UNIX_FILE_PERMISSIONS 0664
59#define INVALID_MDB_DBI 0xefefEFEF
60
61
62// Bit masks to trigger LMDB failures in Persisted wrappers during tests.
63#define TRIGGER_FAIL_MDB_ENV_CREATE (1u << 0)
64#define TRIGGER_FAIL_MDB_ENV_SET_MAXREADERS (1u << 1)
65#define TRIGGER_FAIL_MDB_ENV_SET_MAXDBS (1u << 2)
66#define TRIGGER_FAIL_MDB_ENV_SET_MAPSIZE (1u << 3)
67#define TRIGGER_FAIL_MDB_ENV_OPEN (1u << 4)
68#define TRIGGER_FAIL_MDB_ENV_SYNC (1u << 5)
69#define TRIGGER_FAIL_MDB_TXN_BEGIN (1u << 6)
70#define TRIGGER_FAIL_MDB_DBI_OPEN (1u << 7)
71#define TRIGGER_FAIL_MDB_PUT (1u << 8)
72#define TRIGGER_FAIL_MDB_TXN_COMMIT (1u << 9)
73#define TRIGGER_FAIL_MDB_DEL (1u << 10)
74#define TRIGGER_FAIL_MDB_GET (1u << 11)
75#define TRIGGER_FAIL_MDB_CURSOR_OPEN (1u << 12)
76#define TRIGGER_FAIL_MDB_CURSOR_GET (1u << 13)
77#define TRIGGER_FAIL_MDB_DROP (1u << 14)
78
79
83 char path[MAX_LMDB_HOME_LEN];
84
88 int flags;
89};
90
91
92typedef std::map <String, MDB_dbi> DBImap;
93typedef MDB_txn *pMDB_txn;
94
95
111class Persisted : public Container {
112
113 public:
114
115 Persisted(pLogger a_logger, pConfigFile a_config);
116 ~Persisted();
117
118 virtual pChar const id();
119
120 StatusCode start ();
122
123 // The easy interface (Requires explicit pulling because of the native interface using the same names.)
124
125 using Container::get;
126 using Container::header;
127 using Container::put;
129 using Container::remove;
130 using Container::copy;
131
132 // The "native" interface
133
134 virtual StatusCode get (pTransaction &p_txn,
135 Locator &what);
136 virtual StatusCode get (pTransaction &p_txn,
137 Locator &what,
138 pBlock p_row_filter);
139 virtual StatusCode get (pTransaction &p_txn,
140 Locator &what,
141 pChar name);
142 virtual StatusCode header (StaticBlockHeader &hea,
143 Locator &what);
144 virtual StatusCode header (pTransaction &p_txn,
145 Locator &what);
146 virtual StatusCode put (Locator &where,
147 pBlock p_block,
148 int mode = WRITE_AS_FULL_BLOCK);
149 virtual StatusCode new_entity(Locator &where);
150 virtual StatusCode remove (Locator &where);
151 virtual StatusCode copy (Locator &where,
152 Locator &what);
153
154 // Support for container names in the BaseAPI .base_names()
155
157 bool dbi_exists(Name dbi_name);
158
163 inline bool is_running() {
164 return lmdb_env != nullptr;
165 }
166
167#ifndef CATCH_TEST
168 private:
169#endif
170
171 // Hot LMDB get
172
174 void done_pointer_to_block(pMDB_txn &p_txn);
175
176 // Internal dbi management
177
178 bool open_all_databases ();
179 void close_all_databases();
182
183 // Logger with full messages for lmdb errors.
184
185 void log_lmdb_err(int loglevel, int lmdb_err, const char *msg);
186
187#ifdef CATCH_TEST
188 // LMDB wrappers for tests, allowing deterministic failure injection.
189 int mdb_env_create (MDB_env **env);
190 int mdb_env_set_maxreaders (MDB_env *env,
191 unsigned int readers);
192 int mdb_env_set_maxdbs (MDB_env *env,
193 MDB_dbi dbs);
194 int mdb_env_set_mapsize (MDB_env *env,
195 mdb_size_t size);
196 int mdb_env_open (MDB_env *env,
197 const char *path,
198 unsigned int flags,
199 mdb_mode_t mode);
200 int mdb_env_sync (MDB_env *env,
201 int force);
202 int mdb_txn_begin (MDB_env *env,
203 MDB_txn *parent,
204 unsigned int flags,
205 MDB_txn **txn);
206 int mdb_dbi_open (MDB_txn *txn,
207 const char *name,
208 unsigned int flags,
209 MDB_dbi *dbi);
210 int mdb_put (MDB_txn *txn,
211 MDB_dbi dbi,
212 MDB_val *key,
213 MDB_val *data,
214 unsigned int flags);
215 int mdb_txn_commit (MDB_txn *txn);
216 int mdb_del (MDB_txn *txn,
217 MDB_dbi dbi,
218 MDB_val *key,
219 MDB_val *data);
220 int mdb_get (MDB_txn *txn,
221 MDB_dbi dbi,
222 MDB_val *key,
223 MDB_val *data);
224 int mdb_cursor_open (MDB_txn *txn,
225 MDB_dbi dbi,
226 MDB_cursor **cursor);
227 int mdb_cursor_get (MDB_cursor *cursor,
228 MDB_val *key,
229 MDB_val *data,
230 MDB_cursor_op op);
231 int mdb_drop (MDB_txn *txn,
232 MDB_dbi dbi,
233 int del);
234
235 uint32_t debug_trigger_failure = 0;
236#endif
237
240 MDB_env *lmdb_env = nullptr;
241};
243
244// Instancing Persisted
245// --------------------
246
247#ifdef CATCH_TEST
248
249extern Persisted PER;
250
251#endif
252
253} // namespace jazz_elements
254
255#endif // ifndef INCLUDED_JAZZ_ELEMENTS_PERSISTED
A configuration file as a key/value store.
Definition utils.h:218
Container: A Service to manage Jazz blocks. All Jazz blocks are managed by this or a descendant of th...
Definition container.h:287
virtual StatusCode put(pChar p_where, pBlock p_block, int mode=WRITE_AS_BASE_DEFAULT)
Definition container.cpp:1927
virtual StatusCode remove(pChar p_where)
Definition container.cpp:1965
virtual StatusCode new_entity(pChar p_where)
Definition container.cpp:1946
virtual StatusCode copy(pChar p_where, pChar p_what)
Definition container.cpp:1987
virtual StatusCode get(pTransaction &p_txn, pChar p_what)
Definition container.cpp:1784
virtual StatusCode header(StaticBlockHeader &hea, pChar p_what)
Definition container.cpp:1878
A simple logger.
Definition utils.h:248
Persisted: A Service to manage data objects in LMDB.
Definition persisted.h:111
void base_names(BaseNames &base_names)
Definition persisted.cpp:646
void done_pointer_to_block(pMDB_txn &p_txn)
Completes the transaction started by lock_pointer_to_block() doing a mdb_txn_commit() which invalidat...
Definition persisted.cpp:729
StatusCode shut_down()
Shuts down the Persisted Service.
Definition persisted.cpp:201
StatusCode remove_database(pChar name)
Definition persisted.cpp:922
virtual StatusCode copy(Locator &where, Locator &what)
Definition persisted.cpp:618
bool dbi_exists(Name dbi_name)
Check the internal std::map to see if a (dbi) database name exists.
Definition persisted.cpp:658
virtual StatusCode remove(Locator &where)
Definition persisted.cpp:549
DBImap source_dbi
The lmdb MDB_dbi handles for each source.
Definition persisted.h:238
bool open_all_databases()
Definition persisted.cpp:745
void close_all_databases()
Definition persisted.cpp:810
virtual pChar const id()
Definition persisted.cpp:61
StatusCode new_database(pChar name)
Definition persisted.cpp:832
MDB_env * lmdb_env
The LMDB environment.
Definition persisted.h:240
virtual StatusCode new_entity(Locator &where)
Definition persisted.cpp:534
virtual StatusCode put(Locator &where, pBlock p_block, int mode=WRITE_AS_FULL_BLOCK)
Definition persisted.cpp:447
virtual StatusCode get(pTransaction &p_txn, Locator &what)
Definition persisted.cpp:234
bool is_running()
Check if the service is running.
Definition persisted.h:163
StatusCode start()
Starts the service, checking the environment and building the databases.
Definition persisted.cpp:73
void log_lmdb_err(int loglevel, int lmdb_err, const char *msg)
A nicer presentation for LMDB error messages.
Definition persisted.cpp:988
JazzLmdbOptions lmdb_opt
The LMDB options.
Definition persisted.h:239
virtual StatusCode header(StaticBlockHeader &hea, Locator &what)
Definition persisted.cpp:355
pBlock lock_pointer_to_block(Locator &what, pMDB_txn &p_txn)
Locates a block doing an mdb_get() leaving the transaction open.
Definition persisted.cpp:674
The namespace for Jazz Utils, Blocks, Kinds, Tuples, Containers, etc.
Definition block.cpp:39
std::map< String, pContainer > BaseNames
A map of names for the containers (or structure engines like "map" or "tree" inside Volatile).
Definition container.h:157
MDB_txn * pMDB_txn
A pointer to a MDB_txn structure which is what mdb_txn_begin() returns.
Definition persisted.h:93
char * pChar
A pointer to a char buffer.
Definition types.h:189
std::map< String, MDB_dbi > DBImap
The lmdb MDB_dbi handles for each source.
Definition persisted.h:92
class Block * pBlock
A (forward defined) pointer to a Block.
Definition block.h:66
Persisted * pPersisted
A pointer to a Persisted object.
Definition persisted.h:242
char Name[NAME_SIZE]
A short identifier used in Blocks, Containers and API.
Definition types.h:187
int StatusCode
Type returned by the Service API.
Definition utils.h:142
All the necessary LMDB options (a binary representation of the values in the config file)
Definition persisted.h:82
char path[MAX_LMDB_HOME_LEN]
The path to the LMDB home directory.
Definition persisted.h:83
int env_set_maxdbs
The maximum number of databases as defined in configuration key MDB_ENV_SET_MAXDBS.
Definition persisted.h:87
int flags
The flags as defined in many configuration keys MDB_FIXEDMAP, .. MDB_NOMEMINIT.
Definition persisted.h:88
int env_set_mapsize
The size of the memory map as defined in configuration key MDB_ENV_SET_MAPSIZE.
Definition persisted.h:85
int env_set_maxreaders
The maximum number of reader slots as defined in configuration key MDB_ENV_SET_MAXREADERS.
Definition persisted.h:86
Locator: A minimal structure to define the location of resources inside a Container.
Definition container.h:189
A Binary Compatible BlockHeader without Index (and therefore constructors/destructors)
Definition types.h:270
Transaction: A wrapper over a Block that defines the communication of a block with a Container.
Definition container.h:167