10#include "transaction.h"
12#include "documentdb.h"
13#include "documenturldb.h"
14#include "documentiddb.h"
15#include "positiondb.h"
16#include "documenttimedb.h"
17#include "documentdatadb.h"
20#include "enginequery.h"
22#include "andpostingiterator.h"
23#include "orpostingiterator.h"
24#include "phraseanditerator.h"
26#include "writetransaction.h"
30#include "enginedebug.h"
35#if defined _MSC_VER && !defined NVALGRIND
43#include <QMutexLocker>
47Database::Database(
const QString& path)
62bool Database::open(OpenMode mode)
73 dir.mkpath(QStringLiteral(
"."));
76 QFileInfo indexInfo(dir, QStringLiteral(
"index"));
78 if ((mode != CreateDatabase) && !indexInfo.exists()) {
82 if (mode == CreateDatabase) {
84 qCCritical(ENGINE) << m_path <<
"does not have write permissions. Aborting";
88 if (!indexInfo.exists()) {
89 FSUtils::disableCoW(m_path);
93 int rc = mdb_env_create(&m_env);
103 mdb_env_set_maxdbs(m_env, 12);
111 size_t sizeInGByte = 256;
112 if (
sizeof(
void*) == 4) {
114 qCWarning(ENGINE) <<
"Running on 32 bit arch, limiting DB mmap to" << sizeInGByte <<
"GByte";
115 }
else if (RUNNING_ON_VALGRIND) {
118 qCWarning(ENGINE) <<
"Valgrind detected, limiting DB mmap to" << sizeInGByte <<
"GByte";
120 const size_t maximalSizeInBytes = sizeInGByte * size_t(1024) * size_t(1024) * size_t(1024);
121 mdb_env_set_mapsize(m_env, maximalSizeInBytes);
124 auto mdbEnvFlags = MDB_NOSUBDIR | MDB_NOMEMINIT;
125 if (mode == ReadOnlyDatabase) {
126 mdbEnvFlags |= MDB_RDONLY;
131 rc = mdb_env_open(m_env, arr.
constData(), mdbEnvFlags, 0664);
133 mdb_env_close(m_env);
138 rc = mdb_reader_check(m_env,
nullptr);
141 qCWarning(ENGINE) <<
"Database::open reader_check" << mdb_strerror(rc);
142 mdb_env_close(m_env);
151 if (mode != CreateDatabase) {
152 int rc = mdb_txn_begin(m_env,
nullptr, MDB_RDONLY, &txn);
154 qCWarning(ENGINE) <<
"Database::transaction ro begin" << mdb_strerror(rc);
155 mdb_env_close(m_env);
160 m_dbis.postingDbi = PostingDB::open(txn);
161 m_dbis.positionDBi = PositionDB::open(txn);
163 m_dbis.docTermsDbi = DocumentDB::open(
"docterms", txn);
164 m_dbis.docFilenameTermsDbi = DocumentDB::open(
"docfilenameterms", txn);
165 m_dbis.docXattrTermsDbi = DocumentDB::open(
"docxatrrterms", txn);
167 m_dbis.idTreeDbi = IdTreeDB::open(txn);
168 m_dbis.idFilenameDbi = IdFilenameDB::open(txn);
170 m_dbis.docTimeDbi = DocumentTimeDB::open(txn);
171 m_dbis.docDataDbi = DocumentDataDB::open(txn);
173 m_dbis.contentIndexingDbi = DocumentIdDB::open(
"indexingleveldb", txn);
174 m_dbis.failedIdDbi = DocumentIdDB::open(
"failediddb", txn);
176 m_dbis.mtimeDbi = MTimeDB::open(txn);
178 if (!m_dbis.isValid()) {
179 qCWarning(ENGINE) <<
"dbis is invalid";
181 mdb_env_close(m_env);
186 rc = mdb_txn_commit(txn);
188 qCWarning(ENGINE) <<
"Database::transaction ro commit" << mdb_strerror(rc);
189 mdb_env_close(m_env);
194 int rc = mdb_txn_begin(m_env,
nullptr, 0, &txn);
196 qCWarning(ENGINE) <<
"Database::transaction begin" << mdb_strerror(rc);
197 mdb_env_close(m_env);
202 m_dbis.postingDbi = PostingDB::create(txn);
203 m_dbis.positionDBi = PositionDB::create(txn);
205 m_dbis.docTermsDbi = DocumentDB::create(
"docterms", txn);
206 m_dbis.docFilenameTermsDbi = DocumentDB::create(
"docfilenameterms", txn);
207 m_dbis.docXattrTermsDbi = DocumentDB::create(
"docxatrrterms", txn);
209 m_dbis.idTreeDbi = IdTreeDB::create(txn);
210 m_dbis.idFilenameDbi = IdFilenameDB::create(txn);
212 m_dbis.docTimeDbi = DocumentTimeDB::create(txn);
213 m_dbis.docDataDbi = DocumentDataDB::create(txn);
215 m_dbis.contentIndexingDbi = DocumentIdDB::create(
"indexingleveldb", txn);
216 m_dbis.failedIdDbi = DocumentIdDB::create(
"failediddb", txn);
218 m_dbis.mtimeDbi = MTimeDB::create(txn);
220 if (!m_dbis.isValid()) {
221 qCWarning(ENGINE) <<
"dbis is invalid";
223 mdb_env_close(m_env);
228 rc = mdb_txn_commit(txn);
230 qCWarning(ENGINE) <<
"Database::transaction commit" << mdb_strerror(rc);
231 mdb_env_close(m_env);
241bool Database::isOpen()
const
244 return m_env !=
nullptr;
253bool Database::isAvailable()
const
Implements storage for docIds without any associated data Instantiated for:
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
const char * constData() const const
QByteArray encodeName(const QString &fileName)
bool exists() const const
bool permission(QFile::Permissions permissions) const const