Files
tdesktop/Telegram/lib_storage/storage/storage_databases.cpp
allhaileris afb81b8278
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
init
2026-02-16 15:50:16 +03:00

104 lines
2.3 KiB
C++

// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/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)
: _value(value.get())
, _owner(owner) {
}
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());
kept.destroying = nullptr;
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) {
for (auto &entry : _map) {
const auto &path = entry.first; // Need to capture it in lambda.
auto &kept = entry.second;
if (kept.database.get() == database) {
Assert(!kept.destroying.alive());
database->close();
database->waitForCleaner([
=,
guard = kept.destroying.make_guard()
]() mutable {
crl::on_main(std::move(guard), [=] {
_map.erase(path);
});
});
}
}
}
} // namespace Storage