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 "documentiddb.h"
9#include "enginedebug.h"
10
11using namespace Baloo;
12
13DocumentIdDB::DocumentIdDB(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
21DocumentIdDB::~DocumentIdDB()
22{
23}
24
25MDB_dbi DocumentIdDB::create(const char* name, MDB_txn* txn)
26{
27 MDB_dbi dbi = 0;
28 int rc = mdb_dbi_open(txn, name, MDB_CREATE | MDB_INTEGERKEY, dbi: &dbi);
29 if (rc) {
30 qCWarning(ENGINE) << "DocumentIdDB::create" << name << mdb_strerror(err: rc);
31 return 0;
32 }
33
34 return dbi;
35}
36
37MDB_dbi DocumentIdDB::open(const char* name, MDB_txn* txn)
38{
39 MDB_dbi dbi = 0;
40 int rc = mdb_dbi_open(txn, name, MDB_INTEGERKEY, dbi: &dbi);
41 if (rc) {
42 qCWarning(ENGINE) << "DocumentIdDB::open" << name << mdb_strerror(err: rc);
43 return 0;
44 }
45
46 return dbi;
47}
48
49void DocumentIdDB::put(quint64 docId)
50{
51 Q_ASSERT(docId > 0);
52
53 MDB_val key;
54 key.mv_size = sizeof(quint64);
55 key.mv_data = static_cast<void*>(&docId);
56
57 MDB_val val;
58 val.mv_size = 0;
59 val.mv_data = nullptr;
60
61 int rc = mdb_put(txn: m_txn, dbi: m_dbi, key: &key, data: &val, flags: 0);
62 if (rc) {
63 qCWarning(ENGINE) << "DocumentIdDB::put" << mdb_strerror(err: rc);
64 }
65}
66
67bool DocumentIdDB::contains(quint64 docId)
68{
69 Q_ASSERT(docId > 0);
70
71 MDB_val key;
72 key.mv_size = sizeof(quint64);
73 key.mv_data = static_cast<void*>(&docId);
74
75 MDB_val val{.mv_size: 0, .mv_data: nullptr};
76 int rc = mdb_get(txn: m_txn, dbi: m_dbi, key: &key, data: &val);
77 if (rc) {
78 if (rc != MDB_NOTFOUND) {
79 qCDebug(ENGINE) << "DocumentIdDB::contains" << docId << mdb_strerror(err: rc);
80 }
81 return false;
82 }
83
84 return true;
85}
86
87void DocumentIdDB::del(quint64 docId)
88{
89 Q_ASSERT(docId > 0);
90
91 MDB_val key;
92 key.mv_size = sizeof(quint64);
93 key.mv_data = static_cast<void*>(&docId);
94
95 int rc = mdb_del(txn: m_txn, dbi: m_dbi, key: &key, data: nullptr);
96 if (rc != 0 && rc != MDB_NOTFOUND) {
97 qCDebug(ENGINE) << "DocumentIdDB::del" << docId << mdb_strerror(err: rc);
98 }
99}
100
101QVector<quint64> DocumentIdDB::fetchItems(int size)
102{
103 Q_ASSERT(size > 0);
104
105 MDB_cursor* cursor;
106 mdb_cursor_open(txn: m_txn, dbi: m_dbi, cursor: &cursor);
107
108 QVector<quint64> vec;
109 vec.reserve(asize: size);
110
111 for (int i = 0; i < size; i++) {
112 MDB_val key{.mv_size: 0, .mv_data: nullptr};
113 int rc = mdb_cursor_get(cursor, key: &key, data: nullptr, op: MDB_NEXT);
114 if (rc) {
115 if (rc != MDB_NOTFOUND) {
116 qCWarning(ENGINE) << "DocumentIdDB::fetchItems" << size << mdb_strerror(err: rc);
117 }
118 break;
119 }
120
121 quint64 id = *(static_cast<quint64*>(key.mv_data));
122 vec << id;
123 }
124 mdb_cursor_close(cursor);
125
126 return vec;
127}
128
129uint DocumentIdDB::size()
130{
131 MDB_stat stat;
132 int rc = mdb_stat(txn: m_txn, dbi: m_dbi, stat: &stat);
133 if (rc) {
134 qCDebug(ENGINE) << "DocumentIdDB::size" << mdb_strerror(err: rc);
135 return 0;
136 }
137
138 return stat.ms_entries;
139}
140
141QVector<quint64> DocumentIdDB::toTestVector() 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{.mv_size: 0, .mv_data: nullptr};
148
149 QVector<quint64> vec;
150 while (1) {
151 int rc = mdb_cursor_get(cursor, key: &key, data: &val, op: MDB_NEXT);
152 if (rc) {
153 qCDebug(ENGINE) << "DocumentTimeDB::toTestMap" << mdb_strerror(err: rc);
154 break;
155 }
156
157 const quint64 id = *(static_cast<quint64*>(key.mv_data));
158 vec << id;
159 }
160
161 mdb_cursor_close(cursor);
162 return vec;
163}
164

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