1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #include "storage/cache/storage_cache_database.h"
8 
9 #include "storage/cache/storage_cache_database_object.h"
10 
11 namespace Storage {
12 namespace Cache {
13 
Database(const QString & path,const Settings & settings)14 Database::Database(const QString &path, const Settings &settings)
15 : _wrapped(path, settings) {
16 }
17 
reconfigure(const Settings & settings)18 void Database::reconfigure(const Settings &settings) {
19 	_wrapped.with([settings](Implementation &unwrapped) mutable {
20 		unwrapped.reconfigure(settings);
21 	});
22 }
23 
updateSettings(const SettingsUpdate & update)24 void Database::updateSettings(const SettingsUpdate &update) {
25 	_wrapped.with([update](Implementation &unwrapped) mutable {
26 		unwrapped.updateSettings(update);
27 	});
28 }
29 
open(EncryptionKey && key,FnMut<void (Error)> && done)30 void Database::open(EncryptionKey &&key, FnMut<void(Error)> &&done) {
31 	_wrapped.with([
32 		key = std::move(key),
33 		done = std::move(done)
34 	](Implementation &unwrapped) mutable {
35 		unwrapped.open(std::move(key), std::move(done));
36 	});
37 }
38 
close(FnMut<void ()> && done)39 void Database::close(FnMut<void()> &&done) {
40 	_wrapped.with([
41 		done = std::move(done)
42 	](Implementation &unwrapped) mutable {
43 		unwrapped.close(std::move(done));
44 	});
45 }
46 
waitForCleaner(FnMut<void ()> && done)47 void Database::waitForCleaner(FnMut<void()> &&done) {
48 	_wrapped.with([
49 		done = std::move(done)
50 	](Implementation &unwrapped) mutable {
51 		unwrapped.waitForCleaner(std::move(done));
52 	});
53 }
54 
put(const Key & key,QByteArray && value,FnMut<void (Error)> && done)55 void Database::put(
56 		const Key &key,
57 		QByteArray &&value,
58 		FnMut<void(Error)> &&done) {
59 	return put(key, TaggedValue(std::move(value), 0), std::move(done));
60 }
61 
get(const Key & key,FnMut<void (QByteArray &&)> && done)62 void Database::get(const Key &key, FnMut<void(QByteArray&&)> &&done) {
63 	if (done) {
64 		auto untag = [done = std::move(done)](TaggedValue &&value) mutable {
65 			done(std::move(value.bytes));
66 		};
67 		getWithTag(key, std::move(untag));
68 	} else {
69 		getWithTag(key, nullptr);
70 	}
71 }
72 
remove(const Key & key,FnMut<void (Error)> && done)73 void Database::remove(const Key &key, FnMut<void(Error)> &&done) {
74 	_wrapped.with([
75 		key,
76 		done = std::move(done)
77 	](Implementation &unwrapped) mutable {
78 		unwrapped.remove(key, std::move(done));
79 	});
80 }
81 
putIfEmpty(const Key & key,QByteArray && value,FnMut<void (Error)> && done)82 void Database::putIfEmpty(
83 		const Key &key,
84 		QByteArray &&value,
85 		FnMut<void(Error)> &&done) {
86 	return putIfEmpty(
87 		key,
88 		TaggedValue(std::move(value), 0),
89 		std::move(done));
90 }
91 
copyIfEmpty(const Key & from,const Key & to,FnMut<void (Error)> && done)92 void Database::copyIfEmpty(
93 		const Key &from,
94 		const Key &to,
95 		FnMut<void(Error)> &&done) {
96 	_wrapped.with([
97 		from,
98 		to,
99 		done = std::move(done)
100 	](Implementation &unwrapped) mutable {
101 		unwrapped.copyIfEmpty(from, to, std::move(done));
102 	});
103 }
104 
moveIfEmpty(const Key & from,const Key & to,FnMut<void (Error)> && done)105 void Database::moveIfEmpty(
106 		const Key &from,
107 		const Key &to,
108 		FnMut<void(Error)> &&done) {
109 	_wrapped.with([
110 		from,
111 		to,
112 		done = std::move(done)
113 	](Implementation &unwrapped) mutable {
114 		unwrapped.moveIfEmpty(from, to, std::move(done));
115 	});
116 }
117 
put(const Key & key,TaggedValue && value,FnMut<void (Error)> && done)118 void Database::put(
119 		const Key &key,
120 		TaggedValue &&value,
121 		FnMut<void(Error)> &&done) {
122 	_wrapped.with([
123 		key,
124 		value = std::move(value),
125 		done = std::move(done)
126 	](Implementation &unwrapped) mutable {
127 		unwrapped.put(key, std::move(value), std::move(done));
128 	});
129 }
130 
putIfEmpty(const Key & key,TaggedValue && value,FnMut<void (Error)> && done)131 void Database::putIfEmpty(
132 		const Key &key,
133 		TaggedValue &&value,
134 		FnMut<void(Error)> &&done) {
135 	_wrapped.with([
136 		key,
137 		value = std::move(value),
138 		done = std::move(done)
139 	](Implementation &unwrapped) mutable {
140 		unwrapped.putIfEmpty(key, std::move(value), std::move(done));
141 	});
142 }
143 
getWithTag(const Key & key,FnMut<void (TaggedValue &&)> && done)144 void Database::getWithTag(
145 		const Key &key,
146 		FnMut<void(TaggedValue&&)> &&done) {
147 	_wrapped.with([
148 		key,
149 		done = std::move(done)
150 	](Implementation &unwrapped) mutable {
151 		unwrapped.get(key, std::move(done));
152 	});
153 }
154 
getWithSizes(const Key & key,std::vector<Key> && keys,FnMut<void (QByteArray &&,std::vector<int> &&)> && done)155 void Database::getWithSizes(
156 		const Key &key,
157 		std::vector<Key> &&keys,
158 		FnMut<void(QByteArray&&, std::vector<int>&&)> &&done) {
159 	_wrapped.with([
160 		key,
161 		keys = std::move(keys),
162 		done = std::move(done)
163 	](Implementation &unwrapped) mutable {
164 		unwrapped.getWithSizes(key, std::move(keys), std::move(done));
165 	});
166 }
167 
statsOnMain() const168 auto Database::statsOnMain() const -> rpl::producer<Stats> {
169 	return _wrapped.producer_on_main([](const Implementation &unwrapped) {
170 		return unwrapped.stats();
171 	});
172 }
173 
clear(FnMut<void (Error)> && done)174 void Database::clear(FnMut<void(Error)> &&done) {
175 	_wrapped.with([
176 		done = std::move(done)
177 	](Implementation &unwrapped) mutable {
178 		unwrapped.clear(std::move(done));
179 	});
180 }
181 
clearByTag(uint8 tag,FnMut<void (Error)> && done)182 void Database::clearByTag(uint8 tag, FnMut<void(Error)> &&done) {
183 	_wrapped.with([
184 		tag,
185 		done = std::move(done)
186 	](Implementation &unwrapped) mutable {
187 		unwrapped.clearByTag(tag, std::move(done));
188 	});
189 }
190 
sync()191 void Database::sync() {
192 	auto semaphore = crl::semaphore();
193 	_wrapped.with([&](Implementation &) {
194 		semaphore.release();
195 	});
196 	semaphore.acquire();
197 }
198 
199 Database::~Database() = default;
200 
201 } // namespace Cache
202 } // namespace Storage
203