| 1 |
|
| 2 |
|
| 3 |
#ifndef LMDBXX_H |
| 4 |
#define LMDBXX_H |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef __cplusplus |
| 14 |
#error "<lmdb++.h> requires a C++ compiler" |
| 15 |
#endif |
| 16 |
|
| 17 |
#if __cplusplus < 201103L |
| 18 |
#if !defined(_MSC_VER) || _MSC_VER < 1900 |
| 19 |
#error "<lmdb++.h> requires a C++11 compiler (CXXFLAGS='-std=c++11')" |
| 20 |
#endif |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <lmdb.h> |
| 26 |
|
| 27 |
#ifdef LMDBXX_DEBUG |
| 28 |
#include <cassert> |
| 29 |
#endif |
| 30 |
#include <cstddef> |
| 31 |
#include <cstdio> |
| 32 |
#include <cstring> |
| 33 |
#include <stdexcept> |
| 34 |
#include <string> |
| 35 |
#include <type_traits> |
| 36 |
|
| 37 |
namespace lmdb { |
| 38 |
using mode = mdb_mode_t; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
namespace lmdb { |
| 45 |
class error; |
| 46 |
class logic_error; |
| 47 |
class fatal_error; |
| 48 |
class runtime_error; |
| 49 |
class key_exist_error; |
| 50 |
class not_found_error; |
| 51 |
class corrupted_error; |
| 52 |
class panic_error; |
| 53 |
class version_mismatch_error; |
| 54 |
class map_full_error; |
| 55 |
class bad_dbi_error; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
class lmdb::error : public std::runtime_error { |
| 64 |
protected: |
| 65 |
const int _code; |
| 66 |
|
| 67 |
public: |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
[[noreturn]] static inline void raise(const char* origin, int rc); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
error(const char* const origin, |
| 77 |
const int rc) noexcept |
| 78 |
: runtime_error{origin}, |
| 79 |
_code{rc} {} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
int code() const noexcept { |
| 85 |
return _code; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
const char* origin() const noexcept { |
| 92 |
return runtime_error::what(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
virtual const char* what() const noexcept { |
| 99 |
static thread_local char buffer[1024]; |
| 100 |
std::snprintf(buffer, sizeof(buffer), |
| 101 |
"%s: %s", origin(), ::mdb_strerror(code())); |
| 102 |
return buffer; |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
class lmdb::logic_error : public lmdb::error { |
| 110 |
public: |
| 111 |
using error::error; |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
class lmdb::fatal_error : public lmdb::error { |
| 118 |
public: |
| 119 |
using error::error; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
class lmdb::runtime_error : public lmdb::error { |
| 126 |
public: |
| 127 |
using error::error; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
class lmdb::key_exist_error final : public lmdb::runtime_error { |
| 136 |
public: |
| 137 |
using runtime_error::runtime_error; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
class lmdb::not_found_error final : public lmdb::runtime_error { |
| 146 |
public: |
| 147 |
using runtime_error::runtime_error; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
class lmdb::corrupted_error final : public lmdb::fatal_error { |
| 156 |
public: |
| 157 |
using fatal_error::fatal_error; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
class lmdb::panic_error final : public lmdb::fatal_error { |
| 166 |
public: |
| 167 |
using fatal_error::fatal_error; |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
class lmdb::version_mismatch_error final : public lmdb::fatal_error { |
| 176 |
public: |
| 177 |
using fatal_error::fatal_error; |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
class lmdb::map_full_error final : public lmdb::runtime_error { |
| 186 |
public: |
| 187 |
using runtime_error::runtime_error; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
class lmdb::bad_dbi_error final : public lmdb::runtime_error { |
| 197 |
public: |
| 198 |
using runtime_error::runtime_error; |
| 199 |
}; |
| 200 |
|
| 201 |
inline void |
| 202 |
lmdb::error::raise(const char* const origin, |
| 203 |
const int rc) { |
| 204 |
switch (rc) { |
| 205 |
case MDB_KEYEXIST: throw key_exist_error{origin, rc}; |
| 206 |
case MDB_NOTFOUND: throw not_found_error{origin, rc}; |
| 207 |
case MDB_CORRUPTED: throw corrupted_error{origin, rc}; |
| 208 |
case MDB_PANIC: throw panic_error{origin, rc}; |
| 209 |
case MDB_VERSION_MISMATCH: throw version_mismatch_error{origin, rc}; |
| 210 |
case MDB_MAP_FULL: throw map_full_error{origin, rc}; |
| 211 |
#ifdef MDB_BAD_DBI |
| 212 |
case MDB_BAD_DBI: throw bad_dbi_error{origin, rc}; |
| 213 |
#endif |
| 214 |
default: throw lmdb::runtime_error{origin, rc}; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
namespace lmdb { |
| 222 |
|
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
namespace lmdb { |
| 230 |
static inline void env_create(MDB_env** env); |
| 231 |
static inline void env_open(MDB_env* env, |
| 232 |
const char* path, unsigned int flags, mode mode); |
| 233 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14) |
| 234 |
static inline void env_copy(MDB_env* env, const char* path, unsigned int flags); |
| 235 |
static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd, unsigned int flags); |
| 236 |
#else |
| 237 |
static inline void env_copy(MDB_env* env, const char* path); |
| 238 |
static inline void env_copy_fd(MDB_env* env, mdb_filehandle_t fd); |
| 239 |
#endif |
| 240 |
static inline void env_stat(MDB_env* env, MDB_stat* stat); |
| 241 |
static inline void env_info(MDB_env* env, MDB_envinfo* stat); |
| 242 |
static inline void env_sync(MDB_env* env, bool force); |
| 243 |
static inline void env_close(MDB_env* env) noexcept; |
| 244 |
static inline void env_set_flags(MDB_env* env, unsigned int flags, bool onoff); |
| 245 |
static inline void env_get_flags(MDB_env* env, unsigned int* flags); |
| 246 |
static inline void env_get_path(MDB_env* env, const char** path); |
| 247 |
static inline void env_get_fd(MDB_env* env, mdb_filehandle_t* fd); |
| 248 |
static inline void env_set_mapsize(MDB_env* env, std::size_t size); |
| 249 |
static inline void env_set_max_readers(MDB_env* env, unsigned int count); |
| 250 |
static inline void env_get_max_readers(MDB_env* env, unsigned int* count); |
| 251 |
static inline void env_set_max_dbs(MDB_env* env, MDB_dbi count); |
| 252 |
static inline unsigned int env_get_max_keysize(MDB_env* env); |
| 253 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11) |
| 254 |
static inline void env_set_userctx(MDB_env* env, void* ctx); |
| 255 |
static inline void* env_get_userctx(MDB_env* env); |
| 256 |
#endif |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static inline void |
| 267 |
lmdb::env_create(MDB_env** env) { |
| 268 |
const int rc = ::mdb_env_create(env); |
| 269 |
if (rc != MDB_SUCCESS) { |
| 270 |
error::raise("mdb_env_create", rc); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static inline void |
| 279 |
lmdb::env_open(MDB_env* const env, |
| 280 |
const char* const path, |
| 281 |
const unsigned int flags, |
| 282 |
const mode mode) { |
| 283 |
const int rc = ::mdb_env_open(env, path, flags, mode); |
| 284 |
if (rc != MDB_SUCCESS) { |
| 285 |
error::raise("mdb_env_open", rc); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static inline void |
| 295 |
lmdb::env_copy(MDB_env* const env, |
| 296 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14) |
| 297 |
const char* const path, |
| 298 |
const unsigned int flags = 0) { |
| 299 |
const int rc = ::mdb_env_copy2(env, path, flags); |
| 300 |
#else |
| 301 |
const char* const path) { |
| 302 |
const int rc = ::mdb_env_copy(env, path); |
| 303 |
#endif |
| 304 |
if (rc != MDB_SUCCESS) { |
| 305 |
error::raise("mdb_env_copy2", rc); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static inline void |
| 315 |
lmdb::env_copy_fd(MDB_env* const env, |
| 316 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 14) |
| 317 |
const mdb_filehandle_t fd, |
| 318 |
const unsigned int flags = 0) { |
| 319 |
const int rc = ::mdb_env_copyfd2(env, fd, flags); |
| 320 |
#else |
| 321 |
const mdb_filehandle_t fd) { |
| 322 |
const int rc = ::mdb_env_copyfd(env, fd); |
| 323 |
#endif |
| 324 |
if (rc != MDB_SUCCESS) { |
| 325 |
error::raise("mdb_env_copyfd2", rc); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static inline void |
| 334 |
lmdb::env_stat(MDB_env* const env, |
| 335 |
MDB_stat* const stat) { |
| 336 |
const int rc = ::mdb_env_stat(env, stat); |
| 337 |
if (rc != MDB_SUCCESS) { |
| 338 |
error::raise("mdb_env_stat", rc); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static inline void |
| 347 |
lmdb::env_info(MDB_env* const env, |
| 348 |
MDB_envinfo* const stat) { |
| 349 |
const int rc = ::mdb_env_info(env, stat); |
| 350 |
if (rc != MDB_SUCCESS) { |
| 351 |
error::raise("mdb_env_info", rc); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
static inline void |
| 360 |
lmdb::env_sync(MDB_env* const env, |
| 361 |
const bool force = true) { |
| 362 |
const int rc = ::mdb_env_sync(env, force); |
| 363 |
if (rc != MDB_SUCCESS) { |
| 364 |
error::raise("mdb_env_sync", rc); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static inline void |
| 372 |
lmdb::env_close(MDB_env* const env) noexcept { |
| 373 |
::mdb_env_close(env); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static inline void |
| 381 |
lmdb::env_set_flags(MDB_env* const env, |
| 382 |
const unsigned int flags, |
| 383 |
const bool onoff = true) { |
| 384 |
const int rc = ::mdb_env_set_flags(env, flags, onoff ? 1 : 0); |
| 385 |
if (rc != MDB_SUCCESS) { |
| 386 |
error::raise("mdb_env_set_flags", rc); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
static inline void |
| 395 |
lmdb::env_get_flags(MDB_env* const env, |
| 396 |
unsigned int* const flags) { |
| 397 |
const int rc = ::mdb_env_get_flags(env, flags); |
| 398 |
if (rc != MDB_SUCCESS) { |
| 399 |
error::raise("mdb_env_get_flags", rc); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static inline void |
| 408 |
lmdb::env_get_path(MDB_env* const env, |
| 409 |
const char** path) { |
| 410 |
const int rc = ::mdb_env_get_path(env, path); |
| 411 |
if (rc != MDB_SUCCESS) { |
| 412 |
error::raise("mdb_env_get_path", rc); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
static inline void |
| 421 |
lmdb::env_get_fd(MDB_env* const env, |
| 422 |
mdb_filehandle_t* const fd) { |
| 423 |
const int rc = ::mdb_env_get_fd(env, fd); |
| 424 |
if (rc != MDB_SUCCESS) { |
| 425 |
error::raise("mdb_env_get_fd", rc); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static inline void |
| 434 |
lmdb::env_set_mapsize(MDB_env* const env, |
| 435 |
const std::size_t size) { |
| 436 |
const int rc = ::mdb_env_set_mapsize(env, size); |
| 437 |
if (rc != MDB_SUCCESS) { |
| 438 |
error::raise("mdb_env_set_mapsize", rc); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static inline void |
| 447 |
lmdb::env_set_max_readers(MDB_env* const env, |
| 448 |
const unsigned int count) { |
| 449 |
const int rc = ::mdb_env_set_maxreaders(env, count); |
| 450 |
if (rc != MDB_SUCCESS) { |
| 451 |
error::raise("mdb_env_set_maxreaders", rc); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
static inline void |
| 460 |
lmdb::env_get_max_readers(MDB_env* const env, |
| 461 |
unsigned int* const count) { |
| 462 |
const int rc = ::mdb_env_get_maxreaders(env, count); |
| 463 |
if (rc != MDB_SUCCESS) { |
| 464 |
error::raise("mdb_env_get_maxreaders", rc); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
static inline void |
| 473 |
lmdb::env_set_max_dbs(MDB_env* const env, |
| 474 |
const MDB_dbi count) { |
| 475 |
const int rc = ::mdb_env_set_maxdbs(env, count); |
| 476 |
if (rc != MDB_SUCCESS) { |
| 477 |
error::raise("mdb_env_set_maxdbs", rc); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
static inline unsigned int |
| 485 |
lmdb::env_get_max_keysize(MDB_env* const env) { |
| 486 |
const int rc = ::mdb_env_get_maxkeysize(env); |
| 487 |
#ifdef LMDBXX_DEBUG |
| 488 |
assert(rc >= 0); |
| 489 |
#endif |
| 490 |
return static_cast<unsigned int>(rc); |
| 491 |
} |
| 492 |
|
| 493 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11) |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
static inline void |
| 500 |
lmdb::env_set_userctx(MDB_env* const env, |
| 501 |
void* const ctx) { |
| 502 |
const int rc = ::mdb_env_set_userctx(env, ctx); |
| 503 |
if (rc != MDB_SUCCESS) { |
| 504 |
error::raise("mdb_env_set_userctx", rc); |
| 505 |
} |
| 506 |
} |
| 507 |
#endif |
| 508 |
|
| 509 |
#if MDB_VERSION_FULL >= MDB_VERINT(0, 9, 11) |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static inline void* |
| 515 |
lmdb::env_get_userctx(MDB_env* const env) { |
| 516 |
return ::mdb_env_get_userctx(env); |
| 517 |
} |
| 518 |
#endif |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
namespace lmdb { |
| 524 |
static inline void txn_begin( |
| 525 |
MDB_env* env, MDB_txn* parent, unsigned int flags, MDB_txn** txn); |
| 526 |
static inline MDB_env* txn_env(MDB_txn* txn) noexcept; |
| 527 |
#ifdef LMDBXX_TXN_ID |
| 528 |
static inline std::size_t txn_id(MDB_txn* txn) noexcept; |
| 529 |
#endif |
| 530 |
static inline void txn_commit(MDB_txn* txn); |
| 531 |
static inline void txn_abort(MDB_txn* txn) noexcept; |
| 532 |
static inline void txn_reset(MDB_txn* txn) noexcept; |
| 533 |
static inline void txn_renew(MDB_txn* txn); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
static inline void |
| 541 |
lmdb::txn_begin(MDB_env* const env, |
| 542 |
MDB_txn* const parent, |
| 543 |
const unsigned int flags, |
| 544 |
MDB_txn** txn) { |
| 545 |
const int rc = ::mdb_txn_begin(env, parent, flags, txn); |
| 546 |
if (rc != MDB_SUCCESS) { |
| 547 |
error::raise("mdb_txn_begin", rc); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static inline MDB_env* |
| 555 |
lmdb::txn_env(MDB_txn* const txn) noexcept { |
| 556 |
return ::mdb_txn_env(txn); |
| 557 |
} |
| 558 |
|
| 559 |
#ifdef LMDBXX_TXN_ID |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
static inline std::size_t |
| 564 |
lmdb::txn_id(MDB_txn* const txn) noexcept { |
| 565 |
return ::mdb_txn_id(txn); |
| 566 |
} |
| 567 |
#endif |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
static inline void |
| 574 |
lmdb::txn_commit(MDB_txn* const txn) { |
| 575 |
const int rc = ::mdb_txn_commit(txn); |
| 576 |
if (rc != MDB_SUCCESS) { |
| 577 |
error::raise("mdb_txn_commit", rc); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
static inline void |
| 585 |
lmdb::txn_abort(MDB_txn* const txn) noexcept { |
| 586 |
::mdb_txn_abort(txn); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static inline void |
| 593 |
lmdb::txn_reset(MDB_txn* const txn) noexcept { |
| 594 |
::mdb_txn_reset(txn); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static inline void |
| 602 |
lmdb::txn_renew(MDB_txn* const txn) { |
| 603 |
const int rc = ::mdb_txn_renew(txn); |
| 604 |
if (rc != MDB_SUCCESS) { |
| 605 |
error::raise("mdb_txn_renew", rc); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
namespace lmdb { |
| 613 |
static inline void dbi_open( |
| 614 |
MDB_txn* txn, const char* name, unsigned int flags, MDB_dbi* dbi); |
| 615 |
static inline void dbi_stat(MDB_txn* txn, MDB_dbi dbi, MDB_stat* stat); |
| 616 |
static inline void dbi_flags(MDB_txn* txn, MDB_dbi dbi, unsigned int* flags); |
| 617 |
static inline void dbi_close(MDB_env* env, MDB_dbi dbi) noexcept; |
| 618 |
static inline void dbi_drop(MDB_txn* txn, MDB_dbi dbi, bool del); |
| 619 |
static inline void dbi_set_compare(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp); |
| 620 |
static inline void dbi_set_dupsort(MDB_txn* txn, MDB_dbi dbi, MDB_cmp_func* cmp); |
| 621 |
static inline void dbi_set_relfunc(MDB_txn* txn, MDB_dbi dbi, MDB_rel_func* rel); |
| 622 |
static inline void dbi_set_relctx(MDB_txn* txn, MDB_dbi dbi, void* ctx); |
| 623 |
static inline bool dbi_get(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data); |
| 624 |
static inline bool dbi_put(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, MDB_val* data, unsigned int flags); |
| 625 |
static inline bool dbi_del(MDB_txn* txn, MDB_dbi dbi, const MDB_val* key, const MDB_val* data); |
| 626 |
|
| 627 |
|
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
static inline void |
| 635 |
lmdb::dbi_open(MDB_txn* const txn, |
| 636 |
const char* const name, |
| 637 |
const unsigned int flags, |
| 638 |
MDB_dbi* const dbi) { |
| 639 |
const int rc = ::mdb_dbi_open(txn, name, flags, dbi); |
| 640 |
if (rc != MDB_SUCCESS) { |
| 641 |
error::raise("mdb_dbi_open", rc); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
static inline void |
| 650 |
lmdb::dbi_stat(MDB_txn* const txn, |
| 651 |
const MDB_dbi dbi, |
| 652 |
MDB_stat* const result) { |
| 653 |
const int rc = ::mdb_stat(txn, dbi, result); |
| 654 |
if (rc != MDB_SUCCESS) { |
| 655 |
error::raise("mdb_stat", rc); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
static inline void |
| 664 |
lmdb::dbi_flags(MDB_txn* const txn, |
| 665 |
const MDB_dbi dbi, |
| 666 |
unsigned int* const flags) { |
| 667 |
const int rc = ::mdb_dbi_flags(txn, dbi, flags); |
| 668 |
if (rc != MDB_SUCCESS) { |
| 669 |
error::raise("mdb_dbi_flags", rc); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
static inline void |
| 677 |
lmdb::dbi_close(MDB_env* const env, |
| 678 |
const MDB_dbi dbi) noexcept { |
| 679 |
::mdb_dbi_close(env, dbi); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
static inline void |
| 686 |
lmdb::dbi_drop(MDB_txn* const txn, |
| 687 |
const MDB_dbi dbi, |
| 688 |
const bool del = false) { |
| 689 |
const int rc = ::mdb_drop(txn, dbi, del ? 1 : 0); |
| 690 |
if (rc != MDB_SUCCESS) { |
| 691 |
error::raise("mdb_drop", rc); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
static inline void |
| 700 |
lmdb::dbi_set_compare(MDB_txn* const txn, |
| 701 |
const MDB_dbi dbi, |
| 702 |
MDB_cmp_func* const cmp = nullptr) { |
| 703 |
const int rc = ::mdb_set_compare(txn, dbi, cmp); |
| 704 |
if (rc != MDB_SUCCESS) { |
| 705 |
error::raise("mdb_set_compare", rc); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static inline void |
| 714 |
lmdb::dbi_set_dupsort(MDB_txn* const txn, |
| 715 |
const MDB_dbi dbi, |
| 716 |
MDB_cmp_func* const cmp = nullptr) { |
| 717 |
const int rc = ::mdb_set_dupsort(txn, dbi, cmp); |
| 718 |
if (rc != MDB_SUCCESS) { |
| 719 |
error::raise("mdb_set_dupsort", rc); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
static inline void |
| 728 |
lmdb::dbi_set_relfunc(MDB_txn* const txn, |
| 729 |
const MDB_dbi dbi, |
| 730 |
MDB_rel_func* const rel) { |
| 731 |
const int rc = ::mdb_set_relfunc(txn, dbi, rel); |
| 732 |
if (rc != MDB_SUCCESS) { |
| 733 |
error::raise("mdb_set_relfunc", rc); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
static inline void |
| 742 |
lmdb::dbi_set_relctx(MDB_txn* const txn, |
| 743 |
const MDB_dbi dbi, |
| 744 |
void* const ctx) { |
| 745 |
const int rc = ::mdb_set_relctx(txn, dbi, ctx); |
| 746 |
if (rc != MDB_SUCCESS) { |
| 747 |
error::raise("mdb_set_relctx", rc); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
static inline bool |
| 757 |
lmdb::dbi_get(MDB_txn* const txn, |
| 758 |
const MDB_dbi dbi, |
| 759 |
const MDB_val* const key, |
| 760 |
MDB_val* const data) { |
| 761 |
const int rc = ::mdb_get(txn, dbi, const_cast<MDB_val*>(key), data); |
| 762 |
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) { |
| 763 |
error::raise("mdb_get", rc); |
| 764 |
} |
| 765 |
return (rc == MDB_SUCCESS); |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
static inline bool |
| 774 |
lmdb::dbi_put(MDB_txn* const txn, |
| 775 |
const MDB_dbi dbi, |
| 776 |
const MDB_val* const key, |
| 777 |
MDB_val* const data, |
| 778 |
const unsigned int flags = 0) { |
| 779 |
const int rc = ::mdb_put(txn, dbi, const_cast<MDB_val*>(key), data, flags); |
| 780 |
if (rc != MDB_SUCCESS && rc != MDB_KEYEXIST) { |
| 781 |
error::raise("mdb_put", rc); |
| 782 |
} |
| 783 |
return (rc == MDB_SUCCESS); |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
static inline bool |
| 792 |
lmdb::dbi_del(MDB_txn* const txn, |
| 793 |
const MDB_dbi dbi, |
| 794 |
const MDB_val* const key, |
| 795 |
const MDB_val* const data = nullptr) { |
| 796 |
const int rc = ::mdb_del(txn, dbi, const_cast<MDB_val*>(key), const_cast<MDB_val*>(data)); |
| 797 |
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) { |
| 798 |
error::raise("mdb_del", rc); |
| 799 |
} |
| 800 |
return (rc == MDB_SUCCESS); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
namespace lmdb { |
| 807 |
static inline void cursor_open(MDB_txn* txn, MDB_dbi dbi, MDB_cursor** cursor); |
| 808 |
static inline void cursor_close(MDB_cursor* cursor) noexcept; |
| 809 |
static inline void cursor_renew(MDB_txn* txn, MDB_cursor* cursor); |
| 810 |
static inline MDB_txn* cursor_txn(MDB_cursor* cursor) noexcept; |
| 811 |
static inline MDB_dbi cursor_dbi(MDB_cursor* cursor) noexcept; |
| 812 |
static inline bool cursor_get(MDB_cursor* cursor, MDB_val* key, MDB_val* data, MDB_cursor_op op); |
| 813 |
static inline void cursor_put(MDB_cursor* cursor, MDB_val* key, MDB_val* data, unsigned int flags); |
| 814 |
static inline void cursor_del(MDB_cursor* cursor, unsigned int flags); |
| 815 |
static inline void cursor_count(MDB_cursor* cursor, std::size_t& count); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
static inline void |
| 823 |
lmdb::cursor_open(MDB_txn* const txn, |
| 824 |
const MDB_dbi dbi, |
| 825 |
MDB_cursor** const cursor) { |
| 826 |
const int rc = ::mdb_cursor_open(txn, dbi, cursor); |
| 827 |
if (rc != MDB_SUCCESS) { |
| 828 |
error::raise("mdb_cursor_open", rc); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
static inline void |
| 836 |
lmdb::cursor_close(MDB_cursor* const cursor) noexcept { |
| 837 |
::mdb_cursor_close(cursor); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
static inline void |
| 845 |
lmdb::cursor_renew(MDB_txn* const txn, |
| 846 |
MDB_cursor* const cursor) { |
| 847 |
const int rc = ::mdb_cursor_renew(txn, cursor); |
| 848 |
if (rc != MDB_SUCCESS) { |
| 849 |
error::raise("mdb_cursor_renew", rc); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
static inline MDB_txn* |
| 857 |
lmdb::cursor_txn(MDB_cursor* const cursor) noexcept { |
| 858 |
return ::mdb_cursor_txn(cursor); |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
static inline MDB_dbi |
| 865 |
lmdb::cursor_dbi(MDB_cursor* const cursor) noexcept { |
| 866 |
return ::mdb_cursor_dbi(cursor); |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static inline bool |
| 874 |
lmdb::cursor_get(MDB_cursor* const cursor, |
| 875 |
MDB_val* const key, |
| 876 |
MDB_val* const data, |
| 877 |
const MDB_cursor_op op) { |
| 878 |
const int rc = ::mdb_cursor_get(cursor, key, data, op); |
| 879 |
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND) { |
| 880 |
error::raise("mdb_cursor_get", rc); |
| 881 |
} |
| 882 |
return (rc == MDB_SUCCESS); |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
static inline void |
| 890 |
lmdb::cursor_put(MDB_cursor* const cursor, |
| 891 |
MDB_val* const key, |
| 892 |
MDB_val* const data, |
| 893 |
const unsigned int flags = 0) { |
| 894 |
const int rc = ::mdb_cursor_put(cursor, key, data, flags); |
| 895 |
if (rc != MDB_SUCCESS) { |
| 896 |
error::raise("mdb_cursor_put", rc); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
static inline void |
| 905 |
lmdb::cursor_del(MDB_cursor* const cursor, |
| 906 |
const unsigned int flags = 0) { |
| 907 |
const int rc = ::mdb_cursor_del(cursor, flags); |
| 908 |
if (rc != MDB_SUCCESS) { |
| 909 |
error::raise("mdb_cursor_del", rc); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
static inline void |
| 918 |
lmdb::cursor_count(MDB_cursor* const cursor, |
| 919 |
std::size_t& count) { |
| 920 |
const int rc = ::mdb_cursor_count(cursor, &count); |
| 921 |
if (rc != MDB_SUCCESS) { |
| 922 |
error::raise("mdb_cursor_count", rc); |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
namespace lmdb { |
| 930 |
class val; |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
class lmdb::val { |
| 940 |
protected: |
| 941 |
MDB_val _val; |
| 942 |
|
| 943 |
public: |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
val() noexcept = default; |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
val(const std::string& data) noexcept |
| 953 |
: val{data.data(), data.size()} {} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
val(const char* const data) noexcept |
| 959 |
: val{data, std::strlen(data)} {} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
val(const void* const data, |
| 965 |
const std::size_t size) noexcept |
| 966 |
: _val{size, const_cast<void*>(data)} {} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
val(val&& other) noexcept = default; |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
val& operator=(val&& other) noexcept = default; |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
~val() noexcept = default; |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
operator MDB_val*() noexcept { |
| 987 |
return &_val; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
operator const MDB_val*() const noexcept { |
| 994 |
return &_val; |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
bool empty() const noexcept { |
| 1001 |
return size() == 0; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
std::size_t size() const noexcept { |
| 1008 |
return _val.mv_size; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
template<typename T> |
| 1015 |
T* data() noexcept { |
| 1016 |
return reinterpret_cast<T*>(_val.mv_data); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
template<typename T> |
| 1023 |
const T* data() const noexcept { |
| 1024 |
return reinterpret_cast<T*>(_val.mv_data); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
char* data() noexcept { |
| 1031 |
return reinterpret_cast<char*>(_val.mv_data); |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
const char* data() const noexcept { |
| 1038 |
return reinterpret_cast<char*>(_val.mv_data); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
template<typename T> |
| 1045 |
val& assign(const T* const data, |
| 1046 |
const std::size_t size) noexcept { |
| 1047 |
_val.mv_size = size; |
| 1048 |
_val.mv_data = const_cast<void*>(reinterpret_cast<const void*>(data)); |
| 1049 |
return *this; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
val& assign(const char* const data) noexcept { |
| 1056 |
return assign(data, std::strlen(data)); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
val& assign(const std::string& data) noexcept { |
| 1063 |
return assign(data.data(), data.size()); |
| 1064 |
} |
| 1065 |
}; |
| 1066 |
|
| 1067 |
#if !(defined(__COVERITY__) || defined(_MSC_VER)) |
| 1068 |
static_assert(std::is_pod<lmdb::val>::value, "lmdb::val must be a POD type"); |
| 1069 |
static_assert(sizeof(lmdb::val) == sizeof(MDB_val), "sizeof(lmdb::val) != sizeof(MDB_val)"); |
| 1070 |
#endif |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
namespace lmdb { |
| 1076 |
class env; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
class lmdb::env { |
| 1086 |
protected: |
| 1087 |
MDB_env* _handle{nullptr}; |
| 1088 |
|
| 1089 |
public: |
| 1090 |
static constexpr unsigned int default_flags = 0; |
| 1091 |
static constexpr mode default_mode = 0644; |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
static env create(const unsigned int flags = default_flags) { |
| 1100 |
MDB_env* handle{nullptr}; |
| 1101 |
lmdb::env_create(&handle); |
| 1102 |
#ifdef LMDBXX_DEBUG |
| 1103 |
assert(handle != nullptr); |
| 1104 |
#endif |
| 1105 |
if (flags) { |
| 1106 |
try { |
| 1107 |
lmdb::env_set_flags(handle, flags); |
| 1108 |
} |
| 1109 |
catch (const lmdb::error&) { |
| 1110 |
lmdb::env_close(handle); |
| 1111 |
throw; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
return env{handle}; |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
env(MDB_env* const handle) noexcept |
| 1123 |
: _handle{handle} {} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
env(env&& other) noexcept { |
| 1129 |
std::swap(_handle, other._handle); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
env& operator=(env&& other) noexcept { |
| 1136 |
if (this != &other) { |
| 1137 |
std::swap(_handle, other._handle); |
| 1138 |
} |
| 1139 |
return *this; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
~env() noexcept { |
| 1146 |
try { close(); } catch (...) {} |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
operator MDB_env*() const noexcept { |
| 1153 |
return _handle; |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
MDB_env* handle() const noexcept { |
| 1160 |
return _handle; |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
void sync(const bool force = true) { |
| 1170 |
lmdb::env_sync(handle(), force); |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
void close() noexcept { |
| 1180 |
if (handle()) { |
| 1181 |
lmdb::env_close(handle()); |
| 1182 |
_handle = nullptr; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
env& open(const char* const path, |
| 1195 |
const unsigned int flags = default_flags, |
| 1196 |
const mode mode = default_mode) { |
| 1197 |
lmdb::env_open(handle(), path, flags, mode); |
| 1198 |
return *this; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
env& set_flags(const unsigned int flags, |
| 1207 |
const bool onoff = true) { |
| 1208 |
lmdb::env_set_flags(handle(), flags, onoff); |
| 1209 |
return *this; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
env& set_mapsize(const std::size_t size) { |
| 1217 |
lmdb::env_set_mapsize(handle(), size); |
| 1218 |
return *this; |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
env& set_max_readers(const unsigned int count) { |
| 1226 |
lmdb::env_set_max_readers(handle(), count); |
| 1227 |
return *this; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
env& set_max_dbs(const MDB_dbi count) { |
| 1235 |
lmdb::env_set_max_dbs(handle(), count); |
| 1236 |
return *this; |
| 1237 |
} |
| 1238 |
}; |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
namespace lmdb { |
| 1244 |
class txn; |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
class lmdb::txn { |
| 1254 |
protected: |
| 1255 |
MDB_txn* _handle{nullptr}; |
| 1256 |
|
| 1257 |
public: |
| 1258 |
static constexpr unsigned int default_flags = 0; |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
static txn begin(MDB_env* const env, |
| 1269 |
MDB_txn* const parent = nullptr, |
| 1270 |
const unsigned int flags = default_flags) { |
| 1271 |
MDB_txn* handle{nullptr}; |
| 1272 |
lmdb::txn_begin(env, parent, flags, &handle); |
| 1273 |
#ifdef LMDBXX_DEBUG |
| 1274 |
assert(handle != nullptr); |
| 1275 |
#endif |
| 1276 |
return txn{handle}; |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
txn(MDB_txn* const handle) noexcept |
| 1285 |
: _handle{handle} {} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
txn(txn&& other) noexcept { |
| 1291 |
std::swap(_handle, other._handle); |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
txn& operator=(txn&& other) noexcept { |
| 1298 |
if (this != &other) { |
| 1299 |
std::swap(_handle, other._handle); |
| 1300 |
} |
| 1301 |
return *this; |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
~txn() noexcept { |
| 1308 |
if (_handle) { |
| 1309 |
try { abort(); } catch (...) {} |
| 1310 |
_handle = nullptr; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
operator MDB_txn*() const noexcept { |
| 1318 |
return _handle; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
MDB_txn* handle() const noexcept { |
| 1325 |
return _handle; |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
MDB_env* env() const noexcept { |
| 1332 |
return lmdb::txn_env(handle()); |
| 1333 |
} |
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
void commit() { |
| 1342 |
lmdb::txn_commit(_handle); |
| 1343 |
_handle = nullptr; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
void abort() noexcept { |
| 1352 |
lmdb::txn_abort(_handle); |
| 1353 |
_handle = nullptr; |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
void reset() noexcept { |
| 1360 |
lmdb::txn_reset(_handle); |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
void renew() { |
| 1369 |
lmdb::txn_renew(_handle); |
| 1370 |
} |
| 1371 |
}; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
namespace lmdb { |
| 1377 |
class dbi; |
| 1378 |
} |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
class lmdb::dbi { |
| 1387 |
protected: |
| 1388 |
MDB_dbi _handle{0}; |
| 1389 |
|
| 1390 |
public: |
| 1391 |
static constexpr unsigned int default_flags = 0; |
| 1392 |
static constexpr unsigned int default_put_flags = 0; |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
static dbi |
| 1403 |
open(MDB_txn* const txn, |
| 1404 |
const char* const name = nullptr, |
| 1405 |
const unsigned int flags = default_flags) { |
| 1406 |
MDB_dbi handle{}; |
| 1407 |
lmdb::dbi_open(txn, name, flags, &handle); |
| 1408 |
return dbi{handle}; |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
dbi(const MDB_dbi handle) noexcept |
| 1417 |
: _handle{handle} {} |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
dbi(dbi&& other) noexcept { |
| 1423 |
std::swap(_handle, other._handle); |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
dbi& operator=(dbi&& other) noexcept { |
| 1430 |
if (this != &other) { |
| 1431 |
std::swap(_handle, other._handle); |
| 1432 |
} |
| 1433 |
return *this; |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
~dbi() noexcept { |
| 1440 |
if (_handle) { |
| 1441 |
|
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
operator MDB_dbi() const noexcept { |
| 1449 |
return _handle; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
MDB_dbi handle() const noexcept { |
| 1456 |
return _handle; |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
MDB_stat stat(MDB_txn* const txn) const { |
| 1466 |
MDB_stat result; |
| 1467 |
lmdb::dbi_stat(txn, handle(), &result); |
| 1468 |
return result; |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
unsigned int flags(MDB_txn* const txn) const { |
| 1478 |
unsigned int result{}; |
| 1479 |
lmdb::dbi_flags(txn, handle(), &result); |
| 1480 |
return result; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
std::size_t size(MDB_txn* const txn) const { |
| 1490 |
return stat(txn).ms_entries; |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
void drop(MDB_txn* const txn, |
| 1499 |
const bool del = false) { |
| 1500 |
lmdb::dbi_drop(txn, handle(), del); |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
dbi& set_compare(MDB_txn* const txn, |
| 1511 |
MDB_cmp_func* const cmp = nullptr) { |
| 1512 |
lmdb::dbi_set_compare(txn, handle(), cmp); |
| 1513 |
return *this; |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
bool get(MDB_txn* const txn, |
| 1525 |
const val& key, |
| 1526 |
val& data) { |
| 1527 |
return lmdb::dbi_get(txn, handle(), key, data); |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
template<typename K> |
| 1538 |
bool get(MDB_txn* const txn, |
| 1539 |
const K& key) const { |
| 1540 |
const lmdb::val k{&key, sizeof(K)}; |
| 1541 |
lmdb::val v{}; |
| 1542 |
return lmdb::dbi_get(txn, handle(), k, v); |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
template<typename K, typename V> |
| 1554 |
bool get(MDB_txn* const txn, |
| 1555 |
const K& key, |
| 1556 |
V& val) const { |
| 1557 |
const lmdb::val k{&key, sizeof(K)}; |
| 1558 |
lmdb::val v{}; |
| 1559 |
const bool result = lmdb::dbi_get(txn, handle(), k, v); |
| 1560 |
if (result) { |
| 1561 |
val = *v.data<const V>(); |
| 1562 |
} |
| 1563 |
return result; |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
template<typename V> |
| 1575 |
bool get(MDB_txn* const txn, |
| 1576 |
const char* const key, |
| 1577 |
V& val) const { |
| 1578 |
const lmdb::val k{key, std::strlen(key)}; |
| 1579 |
lmdb::val v{}; |
| 1580 |
const bool result = lmdb::dbi_get(txn, handle(), k, v); |
| 1581 |
if (result) { |
| 1582 |
val = *v.data<const V>(); |
| 1583 |
} |
| 1584 |
return result; |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
bool put(MDB_txn* const txn, |
| 1597 |
const val& key, |
| 1598 |
val& data, |
| 1599 |
const unsigned int flags = default_put_flags) { |
| 1600 |
return lmdb::dbi_put(txn, handle(), key, data, flags); |
| 1601 |
} |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
template<typename K> |
| 1612 |
bool put(MDB_txn* const txn, |
| 1613 |
const K& key, |
| 1614 |
const unsigned int flags = default_put_flags) { |
| 1615 |
const lmdb::val k{&key, sizeof(K)}; |
| 1616 |
lmdb::val v{}; |
| 1617 |
return lmdb::dbi_put(txn, handle(), k, v, flags); |
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
template<typename K, typename V> |
| 1630 |
bool put(MDB_txn* const txn, |
| 1631 |
const K& key, |
| 1632 |
const V& val, |
| 1633 |
const unsigned int flags = default_put_flags) { |
| 1634 |
const lmdb::val k{&key, sizeof(K)}; |
| 1635 |
lmdb::val v{&val, sizeof(V)}; |
| 1636 |
return lmdb::dbi_put(txn, handle(), k, v, flags); |
| 1637 |
} |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
template<typename V> |
| 1649 |
bool put(MDB_txn* const txn, |
| 1650 |
const char* const key, |
| 1651 |
const V& val, |
| 1652 |
const unsigned int flags = default_put_flags) { |
| 1653 |
const lmdb::val k{key, std::strlen(key)}; |
| 1654 |
lmdb::val v{&val, sizeof(V)}; |
| 1655 |
return lmdb::dbi_put(txn, handle(), k, v, flags); |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
bool put(MDB_txn* const txn, |
| 1668 |
const char* const key, |
| 1669 |
const char* const val, |
| 1670 |
const unsigned int flags = default_put_flags) { |
| 1671 |
const lmdb::val k{key, std::strlen(key)}; |
| 1672 |
lmdb::val v{val, std::strlen(val)}; |
| 1673 |
return lmdb::dbi_put(txn, handle(), k, v, flags); |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
bool del(MDB_txn* const txn, |
| 1684 |
const val& key) { |
| 1685 |
return lmdb::dbi_del(txn, handle(), key); |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
template<typename K> |
| 1696 |
bool del(MDB_txn* const txn, |
| 1697 |
const K& key) { |
| 1698 |
const lmdb::val k{&key, sizeof(K)}; |
| 1699 |
return lmdb::dbi_del(txn, handle(), k); |
| 1700 |
} |
| 1701 |
}; |
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
namespace lmdb { |
| 1707 |
class cursor; |
| 1708 |
} |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
class lmdb::cursor { |
| 1717 |
protected: |
| 1718 |
MDB_cursor* _handle{nullptr}; |
| 1719 |
|
| 1720 |
public: |
| 1721 |
static constexpr unsigned int default_flags = 0; |
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
static cursor |
| 1731 |
open(MDB_txn* const txn, |
| 1732 |
const MDB_dbi dbi) { |
| 1733 |
MDB_cursor* handle{}; |
| 1734 |
lmdb::cursor_open(txn, dbi, &handle); |
| 1735 |
#ifdef LMDBXX_DEBUG |
| 1736 |
assert(handle != nullptr); |
| 1737 |
#endif |
| 1738 |
return cursor{handle}; |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
cursor(MDB_cursor* const handle) noexcept |
| 1747 |
: _handle{handle} {} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
cursor(cursor&& other) noexcept { |
| 1753 |
std::swap(_handle, other._handle); |
| 1754 |
} |
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
cursor& operator=(cursor&& other) noexcept { |
| 1760 |
if (this != &other) { |
| 1761 |
std::swap(_handle, other._handle); |
| 1762 |
} |
| 1763 |
return *this; |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
~cursor() noexcept { |
| 1770 |
try { close(); } catch (...) {} |
| 1771 |
} |
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
operator MDB_cursor*() const noexcept { |
| 1777 |
return _handle; |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
MDB_cursor* handle() const noexcept { |
| 1784 |
return _handle; |
| 1785 |
} |
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
void close() noexcept { |
| 1794 |
if (_handle) { |
| 1795 |
lmdb::cursor_close(_handle); |
| 1796 |
_handle = nullptr; |
| 1797 |
} |
| 1798 |
} |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
void renew(MDB_txn* const txn) { |
| 1807 |
lmdb::cursor_renew(txn, handle()); |
| 1808 |
} |
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
MDB_txn* txn() const noexcept { |
| 1814 |
return lmdb::cursor_txn(handle()); |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
MDB_dbi dbi() const noexcept { |
| 1821 |
return lmdb::cursor_dbi(handle()); |
| 1822 |
} |
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
bool get(MDB_val* const key, |
| 1832 |
const MDB_cursor_op op) { |
| 1833 |
return get(key, nullptr, op); |
| 1834 |
} |
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
bool get(lmdb::val& key, |
| 1844 |
const MDB_cursor_op op) { |
| 1845 |
return get(key, nullptr, op); |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
bool get(MDB_val* const key, |
| 1857 |
MDB_val* const val, |
| 1858 |
const MDB_cursor_op op) { |
| 1859 |
return lmdb::cursor_get(handle(), key, val, op); |
| 1860 |
} |
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
bool get(lmdb::val& key, |
| 1871 |
lmdb::val& val, |
| 1872 |
const MDB_cursor_op op) { |
| 1873 |
return lmdb::cursor_get(handle(), key, val, op); |
| 1874 |
} |
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
bool get(std::string& key, |
| 1885 |
std::string& val, |
| 1886 |
const MDB_cursor_op op) { |
| 1887 |
lmdb::val k{}, v{}; |
| 1888 |
const bool found = get(k, v, op); |
| 1889 |
if (found) { |
| 1890 |
key.assign(k.data(), k.size()); |
| 1891 |
val.assign(v.data(), v.size()); |
| 1892 |
} |
| 1893 |
return found; |
| 1894 |
} |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
template<typename K> |
| 1904 |
bool find(const K& key, |
| 1905 |
const MDB_cursor_op op = MDB_SET) { |
| 1906 |
lmdb::val k{&key, sizeof(K)}; |
| 1907 |
return get(k, nullptr, op); |
| 1908 |
} |
| 1909 |
}; |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
#endif |