Baloo

documentiddb.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 "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);
29 if (rc) {
30 qCWarning(ENGINE) << "DocumentIdDB::create" << name << mdb_strerror(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);
41 if (rc) {
42 qCWarning(ENGINE) << "DocumentIdDB::open" << name << mdb_strerror(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(m_txn, m_dbi, &key, &val, 0);
62 if (rc) {
63 qCWarning(ENGINE) << "DocumentIdDB::put" << mdb_strerror(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{0, nullptr};
76 int rc = mdb_get(m_txn, m_dbi, &key, &val);
77 if (rc) {
78 if (rc != MDB_NOTFOUND) {
79 qCDebug(ENGINE) << "DocumentIdDB::contains" << docId << mdb_strerror(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(m_txn, m_dbi, &key, nullptr);
96 if (rc != 0 && rc != MDB_NOTFOUND) {
97 qCDebug(ENGINE) << "DocumentIdDB::del" << docId << mdb_strerror(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(m_txn, m_dbi, &cursor);
107
109 vec.reserve(size);
110
111 for (int i = 0; i < size; i++) {
112 MDB_val key{0, nullptr};
113 int rc = mdb_cursor_get(cursor, &key, nullptr, MDB_NEXT);
114 if (rc) {
115 if (rc != MDB_NOTFOUND) {
116 qCWarning(ENGINE) << "DocumentIdDB::fetchItems" << size << mdb_strerror(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(m_txn, m_dbi, &stat);
133 if (rc) {
134 qCDebug(ENGINE) << "DocumentIdDB::size" << mdb_strerror(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(m_txn, m_dbi, &cursor);
145
146 MDB_val key{0, nullptr};
147 MDB_val val{0, nullptr};
148
150 while (1) {
151 int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
152 if (rc) {
153 qCDebug(ENGINE) << "DocumentTimeDB::toTestMap" << mdb_strerror(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}
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
KIOCORE_EXPORT StatJob * stat(const QUrl &url, JobFlags flags=DefaultFlags)
QString name(StandardAction id)
void reserve(qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.