| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // 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
|