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 | |
11 | using namespace Baloo; |
12 | |
13 | DocumentTimeDB::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 | |
21 | DocumentTimeDB::~DocumentTimeDB() |
22 | { |
23 | } |
24 | |
25 | MDB_dbi DocumentTimeDB::create(MDB_txn* txn) |
26 | { |
27 | MDB_dbi dbi = 0; |
28 | int rc = mdb_dbi_open(txn, name: "documenttimedb" , MDB_CREATE | MDB_INTEGERKEY, dbi: &dbi); |
29 | if (rc) { |
30 | qCWarning(ENGINE) << "DocumentTimeDB::create" << mdb_strerror(err: rc); |
31 | return 0; |
32 | } |
33 | |
34 | return dbi; |
35 | } |
36 | |
37 | MDB_dbi DocumentTimeDB::open(MDB_txn* txn) |
38 | { |
39 | MDB_dbi dbi = 0; |
40 | int rc = mdb_dbi_open(txn, name: "documenttimedb" , MDB_INTEGERKEY, dbi: &dbi); |
41 | if (rc) { |
42 | qCWarning(ENGINE) << "DocumentTimeDB::open" << mdb_strerror(err: rc); |
43 | return 0; |
44 | } |
45 | |
46 | return dbi; |
47 | } |
48 | |
49 | void 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(txn: m_txn, dbi: m_dbi, key: &key, data: &val, flags: 0); |
62 | if (rc) { |
63 | qCWarning(ENGINE) << "DocumentTimeDB::put" << docId << mdb_strerror(err: rc); |
64 | } |
65 | } |
66 | |
67 | DocumentTimeDB::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{.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) << "DocumentTimeDB::get" << docId << mdb_strerror(err: rc); |
80 | } |
81 | return TimeInfo(); |
82 | } |
83 | |
84 | return *(static_cast<TimeInfo*>(val.mv_data)); |
85 | } |
86 | |
87 | void 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(txn: m_txn, dbi: m_dbi, key: &key, data: nullptr); |
96 | if (rc != 0 && rc != MDB_NOTFOUND) { |
97 | qCDebug(ENGINE) << "DocumentTimeDB::del" << docId << mdb_strerror(err: rc); |
98 | } |
99 | } |
100 | |
101 | bool 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(txn: m_txn, dbi: m_dbi, key: &key, data: &val); |
111 | if (rc) { |
112 | if (rc != MDB_NOTFOUND) { |
113 | qCDebug(ENGINE) << "DocumentTimeDB::contains" << docId << mdb_strerror(err: rc); |
114 | } |
115 | return false; |
116 | } |
117 | |
118 | return true; |
119 | } |
120 | |
121 | QMap<quint64, DocumentTimeDB::TimeInfo> DocumentTimeDB::toTestMap() const |
122 | { |
123 | MDB_cursor* cursor; |
124 | mdb_cursor_open(txn: m_txn, dbi: m_dbi, cursor: &cursor); |
125 | |
126 | MDB_val key = {.mv_size: 0, .mv_data: nullptr}; |
127 | MDB_val val; |
128 | |
129 | QMap<quint64, TimeInfo> map; |
130 | while (1) { |
131 | int rc = mdb_cursor_get(cursor, key: &key, data: &val, op: MDB_NEXT); |
132 | if (rc) { |
133 | qCDebug(ENGINE) << "DocumentTimeDB::toTestMap" << mdb_strerror(err: 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(key: id, value: ti); |
140 | } |
141 | |
142 | mdb_cursor_close(cursor); |
143 | return map; |
144 | } |
145 | |