storage_databases.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/storage_databases.h"
  8. #include "storage/cache/storage_cache_database.h"
  9. namespace Storage {
  10. DatabasePointer::DatabasePointer(
  11. not_null<Databases*> owner,
  12. const std::unique_ptr<Cache::Database> &value)
  13. : _value(value.get())
  14. , _owner(owner) {
  15. }
  16. DatabasePointer::DatabasePointer(DatabasePointer &&other)
  17. : _value(base::take(other._value))
  18. , _owner(other._owner) {
  19. }
  20. DatabasePointer &DatabasePointer::operator=(DatabasePointer &&other) {
  21. if (this != &other) {
  22. destroy();
  23. _owner = other._owner;
  24. _value = base::take(other._value);
  25. }
  26. return *this;
  27. }
  28. DatabasePointer::~DatabasePointer() {
  29. destroy();
  30. }
  31. Cache::Database *DatabasePointer::get() const {
  32. return _value;
  33. }
  34. Cache::Database &DatabasePointer::operator*() const {
  35. Expects(_value != nullptr);
  36. return *get();
  37. }
  38. Cache::Database *DatabasePointer::operator->() const {
  39. Expects(_value != nullptr);
  40. return get();
  41. }
  42. DatabasePointer::operator bool() const {
  43. return get() != nullptr;
  44. }
  45. void DatabasePointer::destroy() {
  46. if (const auto value = base::take(_value)) {
  47. _owner->destroy(value);
  48. }
  49. }
  50. Databases::Kept::Kept(std::unique_ptr<Cache::Database> &&database)
  51. : database(std::move(database)) {
  52. }
  53. DatabasePointer Databases::get(
  54. const QString &path,
  55. const Cache::details::Settings &settings) {
  56. if (const auto i = _map.find(path); i != end(_map)) {
  57. auto &kept = i->second;
  58. Assert(kept.destroying.alive());
  59. kept.destroying = nullptr;
  60. kept.database->reconfigure(settings);
  61. return DatabasePointer(this, kept.database);
  62. }
  63. const auto [i, ok] = _map.emplace(
  64. path,
  65. std::make_unique<Cache::Database>(path, settings));
  66. return DatabasePointer(this, i->second.database);
  67. }
  68. void Databases::destroy(Cache::Database *database) {
  69. for (auto &entry : _map) {
  70. const auto &path = entry.first; // Need to capture it in lambda.
  71. auto &kept = entry.second;
  72. if (kept.database.get() == database) {
  73. Assert(!kept.destroying.alive());
  74. database->close();
  75. database->waitForCleaner([
  76. =,
  77. guard = kept.destroying.make_guard()
  78. ]() mutable {
  79. crl::on_main(std::move(guard), [=] {
  80. _map.erase(path);
  81. });
  82. });
  83. }
  84. }
  85. }
  86. } // namespace Storage