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, name: "idfilename", MDB_CREATE | MDB_INTEGERKEY, dbi: &dbi);
29 if (rc) {
30 qCWarning(ENGINE) << "IdFilenameDB::create" << mdb_strerror(err: 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, name: "idfilename", MDB_INTEGERKEY, dbi: &dbi);
41 if (rc) {
42 qCWarning(ENGINE) << "IdFilenameDB::open" << mdb_strerror(err: 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(dest: data.data(), src: &path.parentId, n: 8);
60 memcpy(dest: data.data() + 8, src: path.name.data(), n: 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(txn: m_txn, dbi: m_dbi, key: &key, data: &val, flags: 0);
67 if (rc) {
68 qCWarning(ENGINE) << "IdFilenameDB::put" << mdb_strerror(err: 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{.mv_size: 0, .mv_data: nullptr};
90 int rc = mdb_get(txn: m_txn, dbi: m_dbi, key: &key, data: &val);
91 if (rc) {
92 if (rc != MDB_NOTFOUND) {
93 qCDebug(ENGINE) << "IdfilenameDB::get" << docId << mdb_strerror(err: rc);
94 }
95 return false;
96 }
97 if (val.mv_size < 8) {
98 return false;
99 }
100
101 path.name.resize(size: val.mv_size - 8);
102 memcpy(dest: &path.parentId, src: val.mv_data, n: 8);
103 memcpy(dest: path.name.data(), src: static_cast<const char*>(val.mv_data) + 8, n: 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{.mv_size: 0, .mv_data: nullptr};
117 int rc = mdb_get(txn: m_txn, dbi: m_dbi, key: &key, data: &val);
118 if (rc) {
119 if (rc != MDB_NOTFOUND) {
120 qCDebug(ENGINE) << "IdfilenameDB::contains" << docId << mdb_strerror(err: 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(txn: m_txn, dbi: m_dbi, key: &key, data: nullptr);
136 if (rc != 0 && rc != MDB_NOTFOUND) {
137 qCDebug(ENGINE) << "IdFilenameDB::del" << mdb_strerror(err: rc);
138 }
139}
140
141QMap<quint64, IdFilenameDB::FilePath> IdFilenameDB::toTestMap() const
142{
143 MDB_cursor* cursor;
144 mdb_cursor_open(txn: m_txn, dbi: m_dbi, cursor: &cursor);
145
146 MDB_val key = {.mv_size: 0, .mv_data: nullptr};
147 MDB_val val;
148
149 QMap<quint64, FilePath> map;
150 while (1) {
151 int rc = mdb_cursor_get(cursor, key: &key, data: &val, op: MDB_NEXT);
152 if (rc) {
153 qCDebug(ENGINE) << "IdFilenameDB::toTestMap" << mdb_strerror(err: 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(key: id, value: path);
164 }
165
166 mdb_cursor_close(cursor);
167 return map;
168}
169

source code of baloo/src/engine/idfilenamedb.cpp