Baloo

idfilenamedb.cpp
1/*
2 This file is part of the KDE Baloo project.
3 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "idfilenamedb.h"
9#include "enginedebug.h"
10
11using namespace Baloo;
12
13IdFilenameDB::IdFilenameDB(MDB_dbi dbi, MDB_txn* txn)
14 : m_txn(txn)
15 , m_dbi(dbi)
16{
17 Q_ASSERT(txn != nullptr);
18 Q_ASSERT(dbi != 0);
19}
20
21IdFilenameDB::~IdFilenameDB()
22{
23}
24
25MDB_dbi IdFilenameDB::create(MDB_txn* txn)
26{
27 MDB_dbi dbi = 0;
28 int rc = mdb_dbi_open(txn, "idfilename", MDB_CREATE | MDB_INTEGERKEY, &dbi);
29 if (rc) {
30 qCWarning(ENGINE) << "IdFilenameDB::create" << mdb_strerror(rc);
31 return 0;
32 }
33
34 return dbi;
35}
36
37MDB_dbi IdFilenameDB::open(MDB_txn* txn)
38{
39 MDB_dbi dbi = 0;
40 int rc = mdb_dbi_open(txn, "idfilename", MDB_INTEGERKEY, &dbi);
41 if (rc) {
42 qCWarning(ENGINE) << "IdFilenameDB::open" << mdb_strerror(rc);
43 return 0;
44 }
45
46 return dbi;
47}
48
49void IdFilenameDB::put(quint64 docId, const FilePath& path)
50{
51 Q_ASSERT(docId > 0);
52 Q_ASSERT(!path.name.isEmpty());
53
54 MDB_val key;
55 key.mv_size = sizeof(quint64);
56 key.mv_data = static_cast<void*>(&docId);
57
58 QByteArray data(8 + path.name.size(), Qt::Uninitialized);
59 memcpy(data.data(), &path.parentId, 8);
60 memcpy(data.data() + 8, path.name.data(), path.name.size());
61
62 MDB_val val;
63 val.mv_size = data.size();
64 val.mv_data = static_cast<void*>(data.data());
65
66 int rc = mdb_put(m_txn, m_dbi, &key, &val, 0);
67 if (rc) {
68 qCWarning(ENGINE) << "IdFilenameDB::put" << mdb_strerror(rc);
69 }
70}
71
72IdFilenameDB::FilePath IdFilenameDB::get(quint64 docId)
73{
74 FilePath path;
75 if (get(docId, path)) {
76 return path;
77 }
78 return {};
79}
80
81bool IdFilenameDB::get(quint64 docId, FilePath& path)
82{
83 Q_ASSERT(docId > 0);
84
85 MDB_val key;
86 key.mv_size = sizeof(quint64);
87 key.mv_data = static_cast<void*>(&docId);
88
89 MDB_val val{0, nullptr};
90 int rc = mdb_get(m_txn, m_dbi, &key, &val);
91 if (rc) {
92 if (rc != MDB_NOTFOUND) {
93 qCDebug(ENGINE) << "IdfilenameDB::get" << docId << mdb_strerror(rc);
94 }
95 return false;
96 }
97 if (val.mv_size < 8) {
98 return false;
99 }
100
101 path.name.resize(val.mv_size - 8);
102 memcpy(&path.parentId, val.mv_data, 8);
103 memcpy(path.name.data(), static_cast<const char*>(val.mv_data) + 8, path.name.size());
104
105 return true;
106}
107
108bool IdFilenameDB::contains(quint64 docId)
109{
110 Q_ASSERT(docId > 0);
111
112 MDB_val key;
113 key.mv_size = sizeof(quint64);
114 key.mv_data = static_cast<void*>(&docId);
115
116 MDB_val val{0, nullptr};
117 int rc = mdb_get(m_txn, m_dbi, &key, &val);
118 if (rc) {
119 if (rc != MDB_NOTFOUND) {
120 qCDebug(ENGINE) << "IdfilenameDB::contains" << docId << mdb_strerror(rc);
121 }
122 return false;
123 }
124 return true;
125}
126
127void IdFilenameDB::del(quint64 docId)
128{
129 Q_ASSERT(docId > 0);
130
131 MDB_val key;
132 key.mv_size = sizeof(quint64);
133 key.mv_data = static_cast<void*>(&docId);
134
135 int rc = mdb_del(m_txn, m_dbi, &key, nullptr);
136 if (rc != 0 && rc != MDB_NOTFOUND) {
137 qCDebug(ENGINE) << "IdFilenameDB::del" << mdb_strerror(rc);
138 }
139}
140
141QMap<quint64, IdFilenameDB::FilePath> IdFilenameDB::toTestMap() const
142{
143 MDB_cursor* cursor;
144 mdb_cursor_open(m_txn, m_dbi, &cursor);
145
146 MDB_val key = {0, nullptr};
147 MDB_val val;
148
150 while (1) {
151 int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
152 if (rc) {
153 qCDebug(ENGINE) << "IdFilenameDB::toTestMap" << mdb_strerror(rc);
154 break;
155 }
156
157 const quint64 id = *(static_cast<quint64*>(key.mv_data));
158
159 FilePath path;
160 path.parentId = static_cast<quint64*>(val.mv_data)[0];
161 path.name = QByteArray(static_cast<char*>(val.mv_data) + 8, val.mv_size - 8);
162
163 map.insert(id, path);
164 }
165
166 mdb_cursor_close(cursor);
167 return map;
168}
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
QString path(const QString &relativePath)
QChar * data()
bool isEmpty() const const
void resize(qsizetype newSize, QChar fillChar)
qsizetype size() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 11:58:31 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.