Skip to content

Commit 3a52d52

Browse files
committed
auth lmdb: implement schema v5, plus a bunch of cleanup
1 parent 547554a commit 3a52d52

File tree

29 files changed

+1510
-252
lines changed

29 files changed

+1510
-252
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ __pycache__
5656
.circleci/config.yml-local
5757
.env
5858
compile_commands.*
59-
/.cache
59+
.cache
6060
/.clang-tidy
6161
.gdb_history
6262
.venv

ext/lmdb-safe/lmdb-safe.cc

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include <string.h>
77
#include <map>
88

9+
#ifndef DNSDIST
10+
#include <lmdb.h>
11+
#include "../../pdns/gettime.hh"
12+
#endif
13+
914
using std::string;
1015
using std::runtime_error;
1116
using std::tuple;
@@ -16,6 +21,56 @@ static string MDBError(int rc)
1621
return mdb_strerror(rc);
1722
}
1823

24+
#ifndef DNSDIST
25+
26+
namespace LMDBLS {
27+
// this also returns a pointer to the string's data. Do not hold on to it too long!
28+
LSheader* LSassertFixedHeaderSize(std::string_view val) {
29+
// cerr<<"val.size()="<<val.size()<<endl;
30+
if (val.size() < LS_MIN_HEADER_SIZE) {
31+
throw std::runtime_error("LSheader too short");
32+
}
33+
34+
return (LSheader*)val.data();
35+
}
36+
37+
size_t LScheckHeaderAndGetSize(std::string_view val, size_t datasize) {
38+
LSheader* lsh = LSassertFixedHeaderSize(val);
39+
40+
if (lsh->d_version != 0) {
41+
throw std::runtime_error("LSheader has wrong version (not zero)");
42+
}
43+
44+
size_t headersize = LS_MIN_HEADER_SIZE;
45+
46+
headersize += ntohs(lsh->d_numextra) * LS_BLOCK_SIZE;
47+
48+
if (val.size() < headersize) {
49+
throw std::runtime_error("LSheader too short for promised extra data");
50+
}
51+
52+
if (datasize && val.size() < (headersize+datasize)) {
53+
throw std::runtime_error("Trailing data after LSheader has wrong size");
54+
}
55+
56+
return headersize;
57+
}
58+
59+
size_t LScheckHeaderAndGetSize(const MDBOutVal *val, size_t datasize) {
60+
return LScheckHeaderAndGetSize(val->getNoStripHeader<string_view>(), datasize);
61+
}
62+
63+
bool LSisDeleted(std::string_view val) {
64+
LSheader* lsh = LSassertFixedHeaderSize(val);
65+
66+
return (lsh->d_flags & LS_FLAG_DELETED) != 0;
67+
}
68+
69+
bool s_flag_deleted{false};
70+
}
71+
72+
#endif /* #ifndef DNSDIST */
73+
1974
MDBDbi::MDBDbi(MDB_env* env, MDB_txn* txn, const string_view dbname, int flags)
2075
{
2176
// A transaction that uses this function must finish (either commit or abort) before any other transaction in the process may use this function.
@@ -184,6 +239,13 @@ MDB_txn *MDBRWTransactionImpl::openRWTransaction(MDBEnv *env, MDB_txn *parent, i
184239
MDBRWTransactionImpl::MDBRWTransactionImpl(MDBEnv* parent, int flags):
185240
MDBRWTransactionImpl(parent, openRWTransaction(parent, nullptr, flags))
186241
{
242+
#ifndef DNSDIST
243+
struct timespec tp;
244+
245+
gettime(&tp, true);
246+
247+
d_txtime = tp.tv_sec * 1E9 + tp.tv_nsec;
248+
#endif
187249
}
188250

189251
MDBRWTransactionImpl::~MDBRWTransactionImpl()
@@ -301,9 +363,10 @@ MDBRWCursor MDBRWTransactionImpl::getRWCursor(const MDBDbi& dbi)
301363
MDB_cursor *cursor;
302364
int rc= mdb_cursor_open(d_txn, dbi, &cursor);
303365
if(rc) {
304-
throw std::runtime_error("Error creating RO cursor: "+std::string(mdb_strerror(rc)));
366+
throw std::runtime_error("Error creating RW cursor: "+std::string(mdb_strerror(rc)));
305367
}
306-
return MDBRWCursor(d_rw_cursors, cursor);
368+
369+
return MDBRWCursor(d_rw_cursors, cursor, d_txn, d_txtime);
307370
}
308371

309372
MDBRWCursor MDBRWTransactionImpl::getCursor(const MDBDbi &dbi)

0 commit comments

Comments
 (0)