Baloo

documenttimedb.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 "documenttimedb.h"
9#include "enginedebug.h"
10
11using namespace Baloo;
12
13DocumentTimeDB::DocumentTimeDB(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
21DocumentTimeDB::~DocumentTimeDB()
22{
23}
24
25MDB_dbi DocumentTimeDB::create(MDB_txn* txn)
26{
27 MDB_dbi dbi = 0;
28 int rc = mdb_dbi_open(txn, "documenttimedb", MDB_CREATE | MDB_INTEGERKEY, &dbi);
29 if (rc) {
30 qCWarning(ENGINE) << "DocumentTimeDB::create" << mdb_strerror(rc);
31 return 0;
32 }
33
34 return dbi;
35}
36
37MDB_dbi DocumentTimeDB::open(MDB_txn* txn)
38{
39 MDB_dbi dbi = 0;
40 int rc = mdb_dbi_open(txn, "documenttimedb", MDB_INTEGERKEY, &dbi);
41 if (rc) {
42 qCWarning(ENGINE) << "DocumentTimeDB::open" << mdb_strerror(rc);
43 return 0;
44 }
45
46 return dbi;
47}
48
49void DocumentTimeDB::put(quint64 docId, const TimeInfo& info)
50{
51 Q_ASSERT(docId > 0);
52
53 MDB_val key;
54 key.mv_size = sizeof(quint64);
55 key.mv_data = &docId;
56
57 MDB_val val;
58 val.mv_size = sizeof(TimeInfo);
59 val.mv_data = static_cast<void*>(const_cast<TimeInfo*>(&info));
60
61 int rc = mdb_put(m_txn, m_dbi, &key, &val, 0);
62 if (rc) {
63 qCWarning(ENGINE) << "DocumentTimeDB::put" << docId << mdb_strerror(rc);
64 }
65}
66
67DocumentTimeDB::TimeInfo DocumentTimeDB::get(quint64 docId)
68{
69 Q_ASSERT(docId > 0);
70
71 MDB_val key;
72 key.mv_size = sizeof(quint64);
73 key.mv_data = &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) << "DocumentTimeDB::get" << docId << mdb_strerror(rc);
80 }
81 return TimeInfo();
82 }
83
84 return *(static_cast<TimeInfo*>(val.mv_data));
85}
86
87void DocumentTimeDB::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) << "DocumentTimeDB::del" << docId << mdb_strerror(rc);
98 }
99}
100
101bool DocumentTimeDB::contains(quint64 docId)
102{
103 Q_ASSERT(docId > 0);
104
105 MDB_val key;
106 key.mv_size = sizeof(quint64);
107 key.mv_data = static_cast<void*>(&docId);
108
109 MDB_val val;
110 int rc = mdb_get(m_txn, m_dbi, &key, &val);
111 if (rc) {
112 if (rc != MDB_NOTFOUND) {
113 qCDebug(ENGINE) << "DocumentTimeDB::contains" << docId << mdb_strerror(rc);
114 }
115 return false;
116 }
117
118 return true;
119}
120
121QMap<quint64, DocumentTimeDB::TimeInfo> DocumentTimeDB::toTestMap() const
122{
123 MDB_cursor* cursor;
124 mdb_cursor_open(m_txn, m_dbi, &cursor);
125
126 MDB_val key = {0, nullptr};
127 MDB_val val;
128
130 while (1) {
131 int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
132 if (rc) {
133 qCDebug(ENGINE) << "DocumentTimeDB::toTestMap" << mdb_strerror(rc);
134 break;
135 }
136
137 const quint64 id = *(static_cast<quint64*>(key.mv_data));
138 const TimeInfo ti = *(static_cast<TimeInfo*>(val.mv_data));
139 map.insert(id, ti);
140 }
141
142 mdb_cursor_close(cursor);
143 return map;
144}
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
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 11 2024 12:13:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.