Skip to content

Commit

Permalink
shift lmdb initialization to provider constructor which is called onl…
Browse files Browse the repository at this point in the history
…y once
  • Loading branch information
ziollek committed Apr 29, 2022
1 parent 00483e4 commit 82326ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
38 changes: 11 additions & 27 deletions src/collection/backend/lmdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,43 +501,27 @@ void LMDB::resolveRegularExpression(const std::string& var,
}


MDBEnvProvider::MDBEnvProvider() :
m_env(NULL), initialized(false) {
pthread_mutex_init(&m_lock, NULL);
MDBEnvProvider::MDBEnvProvider() : m_env(NULL) {
MDB_txn *txn;
mdb_env_create(&m_env);
mdb_env_open(m_env, "./modsec-shared-collections",
MDB_WRITEMAP | MDB_NOSUBDIR, 0664);
mdb_txn_begin(m_env, NULL, 0, &txn);
mdb_dbi_open(txn, NULL, MDB_CREATE | MDB_DUPSORT, &m_dbi);
mdb_txn_commit(txn);
}

MDB_env* MDBEnvProvider::GetEnv() {
init();
return m_env;
}

MDB_dbi* MDBEnvProvider::GetDBI() {
init();
return &m_dbi;
}

void MDBEnvProvider::init() {
pthread_mutex_lock(&m_lock);
if (!initialized) {
MDB_txn *txn;
initialized = true;
mdb_env_create(&m_env);
mdb_env_open(m_env, "./modsec-shared-collections",
MDB_WRITEMAP | MDB_NOSUBDIR, 0664);
mdb_txn_begin(m_env, NULL, 0, &txn);
mdb_dbi_open(txn, NULL, MDB_CREATE | MDB_DUPSORT, &m_dbi);
mdb_txn_commit(txn);
}
pthread_mutex_unlock(&m_lock);
}

void MDBEnvProvider::close() {
pthread_mutex_lock(&m_lock);
if (initialized) {
mdb_dbi_close(m_env, m_dbi);
mdb_env_close(m_env);
}
pthread_mutex_unlock(&m_lock);
MDBEnvProvider::~MDBEnvProvider() {
mdb_dbi_close(m_env, m_dbi);
mdb_env_close(m_env);
}

#endif
Expand Down
6 changes: 1 addition & 5 deletions src/collection/backend/lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,12 @@ class MDBEnvProvider {
}
MDB_env* GetEnv();
MDB_dbi* GetDBI();

~MDBEnvProvider();
private:
MDB_env *m_env;
MDB_dbi m_dbi;
pthread_mutex_t m_lock;

MDBEnvProvider();
bool initialized;
void init();
void close();
};

class LMDB :
Expand Down

0 comments on commit 82326ff

Please sign in to comment.