2018-08-29 13:23:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "storage/storage_databases.h"
|
|
|
|
|
|
|
|
#include "storage/cache/storage_cache_database.h"
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
|
|
|
|
DatabasePointer::DatabasePointer(
|
|
|
|
not_null<Databases*> owner,
|
|
|
|
const std::unique_ptr<Cache::Database> &value)
|
2018-08-29 19:07:47 +00:00
|
|
|
: _value(value.get())
|
|
|
|
, _owner(owner) {
|
2018-08-29 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePointer::DatabasePointer(DatabasePointer &&other)
|
|
|
|
: _value(base::take(other._value))
|
|
|
|
, _owner(other._owner) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePointer &DatabasePointer::operator=(DatabasePointer &&other) {
|
|
|
|
if (this != &other) {
|
|
|
|
destroy();
|
|
|
|
_owner = other._owner;
|
|
|
|
_value = base::take(other._value);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePointer::~DatabasePointer() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::Database *DatabasePointer::get() const {
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::Database &DatabasePointer::operator*() const {
|
|
|
|
Expects(_value != nullptr);
|
|
|
|
|
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::Database *DatabasePointer::operator->() const {
|
|
|
|
Expects(_value != nullptr);
|
|
|
|
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePointer::operator bool() const {
|
|
|
|
return get() != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabasePointer::destroy() {
|
|
|
|
if (const auto value = base::take(_value)) {
|
|
|
|
_owner->destroy(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Databases::Kept::Kept(std::unique_ptr<Cache::Database> &&database)
|
|
|
|
: database(std::move(database)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePointer Databases::get(
|
|
|
|
const QString &path,
|
|
|
|
const Cache::details::Settings &settings) {
|
|
|
|
if (const auto i = _map.find(path); i != end(_map)) {
|
|
|
|
auto &kept = i->second;
|
|
|
|
Assert(kept.destroying.alive());
|
2019-01-17 08:18:23 +00:00
|
|
|
kept.destroying = nullptr;
|
2018-08-29 13:23:16 +00:00
|
|
|
kept.database->reconfigure(settings);
|
|
|
|
return DatabasePointer(this, kept.database);
|
|
|
|
}
|
|
|
|
const auto [i, ok] = _map.emplace(
|
|
|
|
path,
|
|
|
|
std::make_unique<Cache::Database>(path, settings));
|
|
|
|
return DatabasePointer(this, i->second.database);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Databases::destroy(Cache::Database *database) {
|
2018-08-29 19:07:47 +00:00
|
|
|
for (auto &entry : _map) {
|
|
|
|
const auto &path = entry.first; // Need to capture it in lambda.
|
|
|
|
auto &kept = entry.second;
|
2018-08-29 13:23:16 +00:00
|
|
|
if (kept.database.get() == database) {
|
|
|
|
Assert(!kept.destroying.alive());
|
2018-08-29 19:17:06 +00:00
|
|
|
database->close();
|
2019-03-27 12:11:38 +00:00
|
|
|
database->waitForCleaner([
|
|
|
|
=,
|
|
|
|
guard = kept.destroying.make_guard()
|
|
|
|
]() mutable {
|
2019-02-17 11:52:57 +00:00
|
|
|
crl::on_main(std::move(guard), [=] {
|
2018-08-29 13:23:16 +00:00
|
|
|
_map.erase(path);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 19:17:06 +00:00
|
|
|
} // namespace Storage
|