diff --git a/app/src/include/firebase/internal/common.h b/app/src/include/firebase/internal/common.h index 2e2878a0bd..5f47de6172 100644 --- a/app/src/include/firebase/internal/common.h +++ b/app/src/include/firebase/internal/common.h @@ -108,15 +108,10 @@ struct AlignedStorage { #define FIREBASE_DEPRECATED #endif // defined(SWIG) || defined(DOXYGEN) +// TODO: Replace all usages of the FIREBASE_DEPRECATED macro with the C++14 +// [[deprecated]] attribute. #ifndef FIREBASE_DEPRECATED -#ifdef __GNUC__ -#define FIREBASE_DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define FIREBASE_DEPRECATED __declspec(deprecated) -#else -// We don't know how to mark functions as "deprecated" with this compiler. -#define FIREBASE_DEPRECATED -#endif +#define FIREBASE_DEPRECATED [[deprecated]] #endif // FIREBASE_DEPRECATED // Calculates the number of elements in an array. diff --git a/firestore/CMakeLists.txt b/firestore/CMakeLists.txt index cf5c239f88..6a8f8711ef 100644 --- a/firestore/CMakeLists.txt +++ b/firestore/CMakeLists.txt @@ -41,7 +41,6 @@ set(common_SRCS src/common/hard_assert_common.h src/common/listener_registration.cc src/common/load_bundle_task_progress.cc - src/common/local_cache_settings.cc src/common/macros.h src/common/query.cc src/common/query_snapshot.cc @@ -214,6 +213,8 @@ set(main_SRCS src/main/listener_main.h src/main/listener_registration_main.cc src/main/listener_registration_main.h + src/main/local_cache_settings_main.cc + src/main/local_cache_settings_main.h src/main/promise_factory_main.h src/main/promise_main.h src/main/query_main.cc diff --git a/firestore/integration_test_internal/src/bundle_test.cc b/firestore/integration_test_internal/src/bundle_test.cc index 486a5e3058..6f0caf4add 100644 --- a/firestore/integration_test_internal/src/bundle_test.cc +++ b/firestore/integration_test_internal/src/bundle_test.cc @@ -328,7 +328,8 @@ TEST_F(BundleTest, LoadedDocumentsShouldNotBeGarbageCollectedRightAway) { // This test really only makes sense with memory persistence, as disk // persistence only ever lazily deletes data. auto new_settings = db->settings(); - new_settings.set_local_cache_settings(MemoryCacheSettings::Create()); + new_settings.set_local_cache_settings( + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings())); db->set_settings(new_settings); auto bundle = CreateTestBundle(db); diff --git a/firestore/integration_test_internal/src/firestore_test.cc b/firestore/integration_test_internal/src/firestore_test.cc index be0d2df0a2..e57e9ab805 100644 --- a/firestore/integration_test_internal/src/firestore_test.cc +++ b/firestore/integration_test_internal/src/firestore_test.cc @@ -45,6 +45,7 @@ #include "util/future_test_util.h" #if !defined(__ANDROID__) #include "Firestore/core/src/util/autoid.h" +#include "Firestore/core/src/util/warnings.h" #else #include "android/util_autoid.h" #endif // !defined(__ANDROID__) @@ -1533,7 +1534,9 @@ class FirestoreCacheConfigTest : public FirestoreIntegrationTest { TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForMemoryCacheWorks) { auto* db = TestFirestore("legacy_memory_cache"); auto settings = db->settings(); - WITH_DEPRECATED_API(settings.set_persistence_enabled(false)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings.set_persistence_enabled(false); + SUPPRESS_END() db->set_settings(std::move(settings)); VerifyCachedDocumentDeletedImmediately(db); @@ -1542,7 +1545,9 @@ TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForMemoryCacheWorks) { TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForPersistenceCacheWorks) { auto* db = TestFirestore("legacy_persistent_cache"); auto settings = db->settings(); - WITH_DEPRECATED_API(settings.set_persistence_enabled(true)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings.set_persistence_enabled(true); + SUPPRESS_END() db->set_settings(std::move(settings)); VerifyCachedDocumentStaysAround(db); @@ -1551,7 +1556,8 @@ TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForPersistenceCacheWorks) { TEST_F(FirestoreCacheConfigTest, NewCacheConfigForMemoryCacheWorks) { auto* db = TestFirestore("new_memory_cache"); auto settings = db->settings(); - settings.set_local_cache_settings(MemoryCacheSettings::Create()); + settings.set_local_cache_settings( + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings())); db->set_settings(std::move(settings)); VerifyCachedDocumentDeletedImmediately(db); @@ -1560,8 +1566,9 @@ TEST_F(FirestoreCacheConfigTest, NewCacheConfigForMemoryCacheWorks) { TEST_F(FirestoreCacheConfigTest, NewCacheConfigForPersistenceCacheWorks) { auto* db = TestFirestore("new_persistent_cache"); auto settings = db->settings(); - settings.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024)); + settings.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(50 * 1024 * + 1024))); db->set_settings(std::move(settings)); VerifyCachedDocumentStaysAround(db); @@ -1571,20 +1578,24 @@ TEST_F(FirestoreCacheConfigTest, CannotMixNewAndLegacyCacheConfig) { { auto* db = TestFirestore("mixing_1"); auto settings = db->settings(); - settings.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024)); + settings.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(50 * 1024 * + 1024))); - WITH_DEPRECATED_API( - EXPECT_THROW(settings.set_cache_size_bytes(0), std::logic_error)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + EXPECT_THROW(settings.set_cache_size_bytes(0), std::logic_error); + SUPPRESS_END() } { auto* db = TestFirestore("mixing_2"); auto settings = db->settings(); - WITH_DEPRECATED_API(settings.set_persistence_enabled(false)); - EXPECT_THROW( - settings.set_local_cache_settings(MemoryCacheSettings::Create()), - std::logic_error); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings.set_persistence_enabled(false); + SUPPRESS_END() + EXPECT_THROW(settings.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::MemoryCacheSettings())), + std::logic_error); } } @@ -1592,8 +1603,8 @@ TEST_F(FirestoreCacheConfigTest, CanGetDocumentFromCacheWithMemoryLruGC) { auto* db = TestFirestore("new_persistent_cache"); auto settings = db->settings(); settings.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryLruGCSettings::Create())); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::LruGCSettings()))); db->set_settings(std::move(settings)); VerifyCachedDocumentStaysAround(db); @@ -1603,8 +1614,8 @@ TEST_F(FirestoreCacheConfigTest, CannotGetDocumentFromCacheFromMemoryEagerGC) { auto* db = TestFirestore("new_persistent_cache"); auto settings = db->settings(); settings.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryEagerGCSettings::Create())); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::EagerGCSettings()))); db->set_settings(std::move(settings)); VerifyCachedDocumentDeletedImmediately(db); diff --git a/firestore/integration_test_internal/src/settings_test.cc b/firestore/integration_test_internal/src/settings_test.cc index a22e922f19..8a7b380072 100644 --- a/firestore/integration_test_internal/src/settings_test.cc +++ b/firestore/integration_test_internal/src/settings_test.cc @@ -15,13 +15,23 @@ */ #include +#include +#include +#include +#include "absl/types/optional.h" +#include "absl/types/variant.h" #include "firebase/firestore.h" #include "firebase/firestore/local_cache_settings.h" #include "firebase_test_framework.h" #include "gtest/gtest.h" +#if !defined(__ANDROID__) +#include "Firestore/core/src/util/warnings.h" +#else +#endif // !defined(__ANDROID__) + namespace firebase { namespace firestore { namespace { @@ -33,39 +43,51 @@ TEST(SettingsTest, Equality) { Settings settings1; settings1.set_host("foo"); settings1.set_ssl_enabled(true); - WITH_DEPRECATED_API(settings1.set_persistence_enabled(true)); - WITH_DEPRECATED_API(settings1.set_cache_size_bytes(kFiveMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings1.set_persistence_enabled(true); + settings1.set_cache_size_bytes(kFiveMb); + SUPPRESS_END() Settings settings2; settings2.set_host("bar"); settings2.set_ssl_enabled(true); - WITH_DEPRECATED_API(settings2.set_persistence_enabled(true)); - WITH_DEPRECATED_API(settings2.set_cache_size_bytes(kFiveMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings2.set_persistence_enabled(true); + settings2.set_cache_size_bytes(kFiveMb); + SUPPRESS_END() Settings settings3; settings3.set_host("foo"); settings3.set_ssl_enabled(false); - WITH_DEPRECATED_API(settings3.set_persistence_enabled(true)); - WITH_DEPRECATED_API(settings3.set_cache_size_bytes(kFiveMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings3.set_persistence_enabled(true); + settings3.set_cache_size_bytes(kFiveMb); + SUPPRESS_END() Settings settings4; settings4.set_host("foo"); settings4.set_ssl_enabled(true); - WITH_DEPRECATED_API(settings4.set_persistence_enabled(false)); - WITH_DEPRECATED_API(settings4.set_cache_size_bytes(kFiveMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings4.set_persistence_enabled(false); + settings4.set_cache_size_bytes(kFiveMb); + SUPPRESS_END() Settings settings5; settings5.set_host("foo"); settings5.set_ssl_enabled(true); - WITH_DEPRECATED_API(settings5.set_persistence_enabled(true)); - WITH_DEPRECATED_API(settings5.set_cache_size_bytes(kSixMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings5.set_persistence_enabled(true); + settings5.set_cache_size_bytes(kSixMb); + SUPPRESS_END() // This is the same as settings4. Settings settings6; settings6.set_host("foo"); settings6.set_ssl_enabled(true); - WITH_DEPRECATED_API(settings6.set_persistence_enabled(false)); - WITH_DEPRECATED_API(settings6.set_cache_size_bytes(kFiveMb)); + SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() + settings6.set_persistence_enabled(false); + settings6.set_cache_size_bytes(kFiveMb); + SUPPRESS_END() EXPECT_TRUE(settings1 == settings1); EXPECT_TRUE(settings6 == settings4); @@ -103,60 +125,64 @@ TEST(SettingsTest, EqualityWithLocalCacheSettings) { Settings settings1; settings1.set_host("foo"); settings1.set_ssl_enabled(true); - settings1.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(kFiveMb)); + settings1.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb))); Settings settings2; settings2.set_host("bar"); settings2.set_ssl_enabled(true); - settings2.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(kFiveMb)); + settings2.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb))); Settings settings3; settings3.set_host("foo"); settings3.set_ssl_enabled(false); - settings3.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(kFiveMb)); + settings3.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb))); Settings settings4; settings4.set_host("foo"); settings4.set_ssl_enabled(true); - settings4.set_local_cache_settings(MemoryCacheSettings::Create()); + settings4.set_local_cache_settings( + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings())); Settings settings5; settings5.set_host("foo"); settings5.set_ssl_enabled(true); - settings5.set_local_cache_settings( - PersistentCacheSettings::Create().WithSizeBytes(kSixMb)); + settings5.set_local_cache_settings(LocalCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kSixMb))); Settings settings6; settings6.set_host("foo"); settings6.set_ssl_enabled(true); settings6.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryEagerGCSettings::Create())); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::EagerGCSettings()))); Settings settings7; settings7.set_host("foo"); settings7.set_ssl_enabled(true); settings7.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryLruGCSettings::Create().WithSizeBytes(kFiveMb))); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::LruGCSettings() + .WithSizeBytes(kFiveMb)))); Settings settings8; settings8.set_host("foo"); settings8.set_ssl_enabled(true); settings8.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryLruGCSettings::Create().WithSizeBytes(kSixMb))); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::LruGCSettings() + .WithSizeBytes(kSixMb)))); // Same as settings7 Settings settings9; settings9.set_host("foo"); settings9.set_ssl_enabled(true); settings9.set_local_cache_settings( - MemoryCacheSettings::Create().WithGarbageCollectorSettings( - MemoryLruGCSettings::Create().WithSizeBytes(kFiveMb))); + LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::LruGCSettings() + .WithSizeBytes(kFiveMb)))); EXPECT_TRUE(settings1 == settings1); EXPECT_TRUE(settings6 == settings4); @@ -193,6 +219,70 @@ TEST(SettingsTest, EqualityWithLocalCacheSettings) { EXPECT_TRUE(settings7 != settings8); } +TEST(SettingsTest, EqualityAssumptionsAboutSharedPtrAreCorrect) { + const std::shared_ptr shared_ptr_empty1; + const std::shared_ptr shared_ptr_empty2; + const auto shared_ptr1 = std::make_shared("Test String"); + const auto shared_ptr1a = shared_ptr1; + const auto shared_ptr1b = shared_ptr1a; + const auto shared_ptr2 = std::make_shared("Test String"); + + EXPECT_TRUE(shared_ptr_empty1 == shared_ptr_empty1); + EXPECT_TRUE(shared_ptr_empty1 == shared_ptr_empty2); + EXPECT_TRUE(shared_ptr1 == shared_ptr1); + EXPECT_TRUE(shared_ptr1 == shared_ptr1a); + EXPECT_TRUE(shared_ptr1 == shared_ptr1b); + + EXPECT_FALSE(shared_ptr_empty1 == shared_ptr1); + EXPECT_FALSE(shared_ptr1 == shared_ptr_empty1); + EXPECT_FALSE(shared_ptr1 == shared_ptr2); +} + +TEST(SettingsTest, EqualityAssumptionsAboutOptionalAreCorrect) { + absl::optional invalid_optional1; + absl::optional invalid_optional2; + absl::optional optional1("Test String"); + absl::optional optional2("Test String"); + absl::optional optional3("A different Test String"); + + EXPECT_TRUE(invalid_optional1 == invalid_optional1); + EXPECT_TRUE(invalid_optional1 == invalid_optional2); + EXPECT_TRUE(optional1 == optional1); + EXPECT_TRUE(optional1 == optional2); + + EXPECT_FALSE(invalid_optional1 == optional1); + EXPECT_FALSE(optional1 == optional3); +} + +TEST(SettingsTest, EqualityAssumptionsAboutVariantAreCorrect) { + absl::variant> invalid_variant1; + absl::variant> invalid_variant2; + absl::variant> variant_with_string1("zzyzx"); + absl::variant> variant_with_string2("zzyzx"); + absl::variant> variant_with_string3("abcde"); + absl::variant> variant_with_vector1( + std::vector{1, 2, 3}); + absl::variant> variant_with_vector2( + std::vector{1, 2, 3}); + absl::variant> variant_with_vector3( + std::vector{9, 8, 7}); + + EXPECT_TRUE(invalid_variant1 == invalid_variant1); + EXPECT_TRUE(invalid_variant1 == invalid_variant2); + EXPECT_TRUE(variant_with_string1 == variant_with_string1); + EXPECT_TRUE(variant_with_string1 == variant_with_string2); + EXPECT_TRUE(variant_with_vector1 == variant_with_vector1); + EXPECT_TRUE(variant_with_vector1 == variant_with_vector2); + + EXPECT_FALSE(invalid_variant1 == variant_with_string1); + EXPECT_FALSE(variant_with_string1 == invalid_variant1); + EXPECT_FALSE(invalid_variant1 == variant_with_vector1); + EXPECT_FALSE(variant_with_vector1 == invalid_variant1); + EXPECT_FALSE(variant_with_string1 == variant_with_string3); + EXPECT_FALSE(variant_with_string1 == variant_with_vector1); + EXPECT_FALSE(variant_with_vector1 == variant_with_vector3); +} + } // namespace } // namespace firestore } // namespace firebase diff --git a/firestore/src/common/local_cache_settings.cc b/firestore/src/common/local_cache_settings.cc deleted file mode 100644 index 736144b4ca..0000000000 --- a/firestore/src/common/local_cache_settings.cc +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright 2023 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "firestore/src/include/firebase/firestore/local_cache_settings.h" -#include - -#include "Firestore/core/src/api/settings.h" -#include "firestore/src/common/hard_assert_common.h" -#if defined(__ANDROID__) -#else -#include "firestore/src/main/local_cache_settings_main.h" -#endif // defined(__ANDROID__) - -namespace firebase { -namespace firestore { - -namespace { -using CoreCacheSettings = api::LocalCacheSettings; -using CorePersistentSettings = api::PersistentCacheSettings; -using CoreMemorySettings = api::MemoryCacheSettings; -using CoreMemoryGarbageCollectorSettings = api::MemoryGargabeCollectorSettings; -using CoreMemoryEagerGcSettings = api::MemoryEagerGcSettings; -using CoreMemoryLruGcSettings = api::MemoryLruGcSettings; -} // namespace - -bool operator==(const LocalCacheSettings& lhs, const LocalCacheSettings& rhs) { - return lhs.kind() == rhs.kind() && - lhs.core_cache_settings() == rhs.core_cache_settings(); -} - -PersistentCacheSettings PersistentCacheSettings::Create() { return {}; } - -PersistentCacheSettings PersistentCacheSettings::CreateFromCoreSettings( - const CorePersistentSettings& core_settings) { - auto result = PersistentCacheSettings{}; - result.settings_internal_ = - std::make_unique(core_settings); - return result; -} - -PersistentCacheSettings::PersistentCacheSettings() { - settings_internal_ = std::make_unique( - CorePersistentSettings{}); -} - -PersistentCacheSettings::~PersistentCacheSettings() { - settings_internal_.reset(); -} - -PersistentCacheSettings::PersistentCacheSettings( - const PersistentCacheSettings& other) { - settings_internal_ = std::make_unique( - *other.settings_internal_); -} - -PersistentCacheSettings& PersistentCacheSettings::operator=( - const PersistentCacheSettings& other) { - if (this == &other) { - return *this; - } - settings_internal_ = std::make_unique( - *other.settings_internal_); - return *this; -} - -PersistentCacheSettings PersistentCacheSettings::WithSizeBytes( - int64_t size) const { - PersistentCacheSettings new_settings{*this}; - new_settings.settings_internal_->set_core_settings( - new_settings.settings_internal_->core_settings().WithSizeBytes(size)); - return new_settings; -} - -const CoreCacheSettings& PersistentCacheSettings::core_cache_settings() const { - return settings_internal_->core_settings(); -} - -MemoryEagerGCSettings MemoryEagerGCSettings::Create() { return {}; } - -MemoryEagerGCSettings::MemoryEagerGCSettings() { - settings_internal_ = std::make_unique( - CoreMemoryEagerGcSettings{}); -} - -MemoryEagerGCSettings::~MemoryEagerGCSettings() { settings_internal_.reset(); } - -MemoryLruGCSettings MemoryLruGCSettings::Create() { return {}; } - -MemoryLruGCSettings::MemoryLruGCSettings() { - settings_internal_ = - std::make_unique(CoreMemoryLruGcSettings{}); -} - -MemoryLruGCSettings::MemoryLruGCSettings( - const MemoryLruGCSettingsInternal& other) { - settings_internal_ = std::make_unique(other); -} - -MemoryLruGCSettings::~MemoryLruGCSettings() { settings_internal_.reset(); } - -MemoryLruGCSettings MemoryLruGCSettings::WithSizeBytes(int64_t size) { - return {MemoryLruGCSettingsInternal{ - settings_internal_->core_settings().WithSizeBytes(size)}}; -} - -MemoryCacheSettings MemoryCacheSettings::Create() { return {}; } - -MemoryCacheSettings MemoryCacheSettings::CreateFromCoreSettings( - const CoreMemorySettings& core_settings) { - auto result = MemoryCacheSettings{}; - result.settings_internal_ = - std::make_unique(core_settings); - return result; -} - -MemoryCacheSettings::MemoryCacheSettings() { - settings_internal_ = - std::make_unique(CoreMemorySettings{}); -} - -MemoryCacheSettings::MemoryCacheSettings(const MemoryCacheSettings& other) { - settings_internal_ = - std::make_unique(*other.settings_internal_); -} - -MemoryCacheSettings& MemoryCacheSettings::operator=( - const MemoryCacheSettings& other) { - if (this == &other) { - return *this; - } - - settings_internal_ = - std::make_unique(*other.settings_internal_); - return *this; -} - -MemoryCacheSettings::~MemoryCacheSettings() { settings_internal_.reset(); } - -MemoryCacheSettings MemoryCacheSettings::WithGarbageCollectorSettings( - const MemoryGarbageCollectorSettings& settings) const { - MemoryCacheSettings result{*this}; - CoreMemorySettings core_settings = result.settings_internal_->core_settings(); - result.settings_internal_->set_core_settings( - core_settings.WithMemoryGarbageCollectorSettings( - settings.core_gc_settings())); - return result; -} - -const CoreCacheSettings& MemoryCacheSettings::core_cache_settings() const { - return settings_internal_->core_settings(); -} - -bool operator==(const MemoryCacheSettings& lhs, - const MemoryCacheSettings& rhs) { - return &lhs == &rhs || (*lhs.settings_internal_ == *rhs.settings_internal_); -} - -bool operator==(const PersistentCacheSettings& lhs, - const PersistentCacheSettings& rhs) { - return &lhs == &rhs || (*lhs.settings_internal_ == *rhs.settings_internal_); -} - -bool operator==(const MemoryEagerGCSettings& lhs, - const MemoryEagerGCSettings& rhs) { - return &lhs == &rhs || (*lhs.settings_internal_ == *rhs.settings_internal_); -} - -bool operator==(const MemoryLruGCSettings& lhs, - const MemoryLruGCSettings& rhs) { - return &lhs == &rhs || (*lhs.settings_internal_ == *rhs.settings_internal_); -} - -} // namespace firestore -} // namespace firebase diff --git a/firestore/src/common/settings.cc b/firestore/src/common/settings.cc index 3cd671f6b2..45ee385aff 100644 --- a/firestore/src/common/settings.cc +++ b/firestore/src/common/settings.cc @@ -56,59 +56,49 @@ void Settings::set_host(std::string host) { host_ = firebase::Move(host); } void Settings::set_ssl_enabled(bool enabled) { ssl_enabled_ = enabled; } -std::shared_ptr Settings::local_cache_settings() { - if (used_legacy_cache_settings_) { - if (is_persistence_enabled()) { - local_cache_settings_ = std::make_shared( - PersistentCacheSettings::Create().WithSizeBytes(cache_size_bytes())); - } else { - local_cache_settings_ = - std::make_shared(MemoryCacheSettings::Create()); - } - } else if (local_cache_settings_ == nullptr) { - local_cache_settings_ = std::make_shared( - PersistentCacheSettings::Create()); +LocalCacheSettings Settings::local_cache_settings() const { + if (cache_settings_source_ == CacheSettingsSource::kNew) { + return local_cache_settings_; + } else if (is_persistence_enabled()) { + return LocalCacheSettings().WithCacheSettings( + LocalCacheSettings::PersistentCacheSettings().WithSizeBytes( + cache_size_bytes())); + } else { + return LocalCacheSettings().WithCacheSettings( + LocalCacheSettings::MemoryCacheSettings()); } - - return local_cache_settings_; } -void Settings::set_local_cache_settings(const LocalCacheSettings& cache) { - if (used_legacy_cache_settings_) { +void Settings::set_local_cache_settings(LocalCacheSettings cache) { + if (cache_settings_source_ == CacheSettingsSource::kOld) { SimpleThrowIllegalState( "Cannot mix set_local_cache_settings() with legacy cache api like " "set_persistence_enabled() or set_cache_size_bytes()"); } - - if (cache.kind() == LocalCacheSettings::Kind::kPersistent) { - local_cache_settings_ = std::make_shared( - static_cast(cache)); - } else { - local_cache_settings_ = std::make_shared( - static_cast(cache)); - } + cache_settings_source_ = CacheSettingsSource::kNew; + local_cache_settings_ = firebase::Move(cache); } void Settings::set_persistence_enabled(bool enabled) { - if (local_cache_settings_ != nullptr) { + if (cache_settings_source_ == CacheSettingsSource::kNew) { SimpleThrowIllegalState( "Cannot mix legacy cache api set_persistence_enabled() with new cache " "api set_local_cache_settings()"); } + cache_settings_source_ = CacheSettingsSource::kOld; persistence_enabled_ = enabled; - used_legacy_cache_settings_ = true; } void Settings::set_cache_size_bytes(int64_t value) { - if (local_cache_settings_ != nullptr) { + if (cache_settings_source_ == CacheSettingsSource::kNew) { SimpleThrowIllegalState( "Cannot mix legacy cache api set_cache_size_bytes() with new cache api " "set_local_cache_settings()"); } + cache_settings_source_ = CacheSettingsSource::kOld; cache_size_bytes_ = value; - used_legacy_cache_settings_ = true; } std::string Settings::ToString() const { @@ -123,22 +113,21 @@ std::ostream& operator<<(std::ostream& out, const Settings& settings) { } bool operator==(const Settings& lhs, const Settings& rhs) { - bool eq = lhs.host() == rhs.host() && - lhs.is_ssl_enabled() == rhs.is_ssl_enabled() && - lhs.is_persistence_enabled() == rhs.is_persistence_enabled() && - lhs.cache_size_bytes() == rhs.cache_size_bytes(); - - if (eq) { - if (lhs.local_cache_settings_ != rhs.local_cache_settings_) { - if (lhs.local_cache_settings_ != nullptr && - rhs.local_cache_settings_ != nullptr) { - eq = (*lhs.local_cache_settings_ == *rhs.local_cache_settings_); - } else { - eq = false; - } - } + if (lhs.host() != rhs.host()) { + return false; + } + if (lhs.is_ssl_enabled() != rhs.is_ssl_enabled()) { + return false; + } + if (lhs.local_cache_settings() != rhs.local_cache_settings()) { + return false; + } +#if defined(__OBJC__) + if (lhs.dispatch_queue() != rhs.dispatch_queue()) { + return false; } - return eq; +#endif // defined(__OBJC__) + return true; } // Apple uses a different mechanism, defined in `settings_apple.mm`. diff --git a/firestore/src/include/firebase/firestore/local_cache_settings.h b/firestore/src/include/firebase/firestore/local_cache_settings.h index 9b78c0e153..f730d23729 100644 --- a/firestore/src/include/firebase/firestore/local_cache_settings.h +++ b/firestore/src/include/firebase/firestore/local_cache_settings.h @@ -18,41 +18,53 @@ #define FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_LOCAL_CACHE_SETTINGS_H_ #include +#include #include - -#include "firestore/src/include/firebase/firestore/settings.h" -#include "firestore/src/main/firestore_main.h" -#include "firestore/src/main/local_cache_settings_main.h" +#include namespace firebase { namespace firestore { -class PersistentCacheSettingsInternal; -class MemoryCacheSettingsInternal; - -/** - * Abstract class implemented by all supported cache settings. - * - * `PersistentCacheSettings` and `MemoryCacheSettings` are the only cache types - * supported by the SDK. Custom implementation is not supported. - */ -class LocalCacheSettings { +class LocalCacheSettings final { public: - virtual ~LocalCacheSettings() = default; + class PersistentCacheSettings; + class MemoryCacheSettings; - /** Equality function. */ - friend bool operator==(const LocalCacheSettings& lhs, - const LocalCacheSettings& rhs); + LocalCacheSettings(); + explicit LocalCacheSettings(PersistentCacheSettings); + explicit LocalCacheSettings(MemoryCacheSettings); + + LocalCacheSettings(const LocalCacheSettings&) = default; + LocalCacheSettings& operator=(const LocalCacheSettings&) = default; + LocalCacheSettings(LocalCacheSettings&&) = default; + LocalCacheSettings& operator=(LocalCacheSettings&&) = default; + + std::unique_ptr persistent_cache_settings() const; + LocalCacheSettings WithCacheSettings(PersistentCacheSettings) const; + + std::unique_ptr memory_cache_settings() const; + LocalCacheSettings WithCacheSettings(MemoryCacheSettings) const; - protected: - enum class Kind { kMemory, kPersistent }; + friend bool operator==(const LocalCacheSettings&, const LocalCacheSettings&); + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, + const LocalCacheSettings& self) { + self.PrintTo(out); + return out; + } + + std::string ToString() const { return (std::ostringstream() << *this).str(); } private: friend class FirestoreInternal; - friend class Settings; - virtual Kind kind() const = 0; - virtual const api::LocalCacheSettings& core_cache_settings() const = 0; + class Impl; + + explicit LocalCacheSettings(Impl); + + std::shared_ptr impl_; }; /** @@ -65,28 +77,28 @@ class LocalCacheSettings { * pass it to an instance of `Settings` via `set_local_cache_settings()`, and * use the `Settings` instance to configure the Firestore SDK. */ -class PersistentCacheSettings final : public LocalCacheSettings { +class LocalCacheSettings::PersistentCacheSettings final { public: - /** Create a default instance `PersistenceCacheSettings`. */ - static PersistentCacheSettings Create(); - - /** Copy constructor. */ - PersistentCacheSettings(const PersistentCacheSettings& other); + PersistentCacheSettings(); - /** Copy assignment. */ - PersistentCacheSettings& operator=(const PersistentCacheSettings& other); + PersistentCacheSettings(const PersistentCacheSettings&) = default; + PersistentCacheSettings& operator=(const PersistentCacheSettings&) = default; + PersistentCacheSettings(PersistentCacheSettings&&) = default; + PersistentCacheSettings& operator=(PersistentCacheSettings&&) = default; - /** Move constructor. */ - PersistentCacheSettings(PersistentCacheSettings&& other) = default; + /** Equality function. */ + friend bool operator==(const PersistentCacheSettings&, + const PersistentCacheSettings&); - /** Move assignment. */ - PersistentCacheSettings& operator=(PersistentCacheSettings&& other) = default; + void PrintTo(std::ostream& out) const; - ~PersistentCacheSettings() override; + friend std::ostream& operator<<(std::ostream& out, + const PersistentCacheSettings& self) { + self.PrintTo(out); + return out; + } - /** Equality function. */ - friend bool operator==(const PersistentCacheSettings& lhs, - const PersistentCacheSettings& rhs); + std::string ToString() const { return (std::ostringstream() << *this).str(); } /** * Copies this settings instance, with the approximate cache size threshold @@ -101,37 +113,24 @@ class PersistentCacheSettings final : public LocalCacheSettings { * By default, persistence cache is enabled with a cache size of 100 MB. The * minimum value is 1 MB. */ - PersistentCacheSettings WithSizeBytes(int64_t size) const; + PersistentCacheSettings WithSizeBytes(int64_t size_bytes) const; /** * Returns the approximate cache size threshold configured. Garbage collection * kicks in once the cache size exceeds this threshold. */ - int64_t size_bytes() const { - return settings_internal_->core_settings().size_bytes(); - } + int64_t size_bytes() const; private: - friend class Settings; - friend class FirestoreInternal; - - static PersistentCacheSettings CreateFromCoreSettings( - const api::PersistentCacheSettings& core_settings); - - PersistentCacheSettings(); + friend class LocalCacheSettings; - LocalCacheSettings::Kind kind() const override { - return LocalCacheSettings::Kind::kPersistent; - } + class Impl; - // Get the corresponding settings object from the core sdk. - const api::LocalCacheSettings& core_cache_settings() const override; + explicit PersistentCacheSettings(Impl); - std::unique_ptr settings_internal_; + std::shared_ptr impl_; }; -class MemoryGarbageCollectorSettings; - /** * Configures the SDK to use a memory cache. Firestore documents and mutations * are NOT persisted across App restart. @@ -140,63 +139,54 @@ class MemoryGarbageCollectorSettings; * pass it to an instance of `Settings` via `set_local_cache_settings()`, and * use the `Settings` instance to configure the Firestore SDK. */ -class MemoryCacheSettings final : public LocalCacheSettings { +class LocalCacheSettings::MemoryCacheSettings final { public: - /** Create a default instance `MemoryCacheSettings`. */ - static MemoryCacheSettings Create(); + class LruGCSettings; + class EagerGCSettings; - /** Copy constructor. */ - MemoryCacheSettings(const MemoryCacheSettings& other); - - /** Copy assignment. */ - MemoryCacheSettings& operator=(const MemoryCacheSettings& other); + MemoryCacheSettings(); + explicit MemoryCacheSettings(LruGCSettings); + explicit MemoryCacheSettings(EagerGCSettings); - ~MemoryCacheSettings() override; + MemoryCacheSettings(const MemoryCacheSettings&) = default; + MemoryCacheSettings& operator=(const MemoryCacheSettings&) = default; + MemoryCacheSettings(MemoryCacheSettings&&) = default; + MemoryCacheSettings& operator=(MemoryCacheSettings&&) = default; /** Equality function. */ - friend bool operator==(const MemoryCacheSettings& lhs, - const MemoryCacheSettings& rhs); - - /** - * Copies this settings instance, with its `MemoryGarbageCollectorSettins` set - * the the given parameter, and returns the new settings instance. - */ - MemoryCacheSettings WithGarbageCollectorSettings( - const MemoryGarbageCollectorSettings& settings) const; - - private: - friend class Settings; - friend class FirestoreInternal; + friend bool operator==(const MemoryCacheSettings&, + const MemoryCacheSettings&); - static MemoryCacheSettings CreateFromCoreSettings( - const api::MemoryCacheSettings& core_settings); - MemoryCacheSettings(); + void PrintTo(std::ostream& out) const; - LocalCacheSettings::Kind kind() const override { - return LocalCacheSettings::Kind::kMemory; + friend std::ostream& operator<<(std::ostream& out, + const MemoryCacheSettings& self) { + self.PrintTo(out); + return out; } - // Get the corresponding settings object from the core sdk. - const api::LocalCacheSettings& core_cache_settings() const override; + std::string ToString() const { return (std::ostringstream() << *this).str(); } - std::unique_ptr settings_internal_; -}; + /** + * Copies this settings instance, with its `MemoryGarbageCollectorSettings` + * set the the given parameter, and returns the new settings instance. + */ + MemoryCacheSettings WithGarbageCollectorSettings(LruGCSettings) const; -/** - * Abstract class implemented by all supported memory garbage collector. - * - * `MemoryEagerGCSettings` and `MemoryLruGCSettings` are the only memory - * garbage collectors supported by the SDK. Custom implementation is not - * supported. - */ -class MemoryGarbageCollectorSettings { - public: - virtual ~MemoryGarbageCollectorSettings() = default; + /** + * Copies this settings instance, with its `MemoryGarbageCollectorSettings` + * set the the given parameter, and returns the new settings instance. + */ + MemoryCacheSettings WithGarbageCollectorSettings(EagerGCSettings) const; private: - friend class MemoryCacheSettings; - virtual const api::MemoryGargabeCollectorSettings& core_gc_settings() - const = 0; + friend class LocalCacheSettings; + + class Impl; + + explicit MemoryCacheSettings(Impl); + + std::shared_ptr impl_; }; /** @@ -210,30 +200,40 @@ class MemoryGarbageCollectorSettings { * at the risk of documents not being cached for offline queries or for * direct queries to the cache. * - * To use, pass an instance of `MemoryEagerGCSettings` to + * To use, pass an instance of `EagerGCSettings` to * `MemoryCacheSettings::WithGarbageCollectorSettings()` to get a new instance * of `MemoryCacheSettings`, which can be used to configure the SDK. */ -class MemoryEagerGCSettings final : public MemoryGarbageCollectorSettings { +class LocalCacheSettings::MemoryCacheSettings::EagerGCSettings final { public: - /** Create a default instance `MemoryEagerGCSettings`. */ - static MemoryEagerGCSettings Create(); + EagerGCSettings(); - ~MemoryEagerGCSettings() override; + EagerGCSettings(const EagerGCSettings&) = default; + EagerGCSettings& operator=(const EagerGCSettings&) = default; + EagerGCSettings(EagerGCSettings&&) = default; + EagerGCSettings& operator=(EagerGCSettings&&) = default; /** Equality function. */ - friend bool operator==(const MemoryEagerGCSettings& lhs, - const MemoryEagerGCSettings& rhs); + friend bool operator==(const EagerGCSettings&, const EagerGCSettings&); - private: - friend class MemoryCacheSettings; - MemoryEagerGCSettings(); + void PrintTo(std::ostream& out) const; - const api::MemoryGargabeCollectorSettings& core_gc_settings() const override { - return settings_internal_->core_settings(); + friend std::ostream& operator<<(std::ostream& out, + const EagerGCSettings& self) { + self.PrintTo(out); + return out; } - std::unique_ptr settings_internal_; + std::string ToString() const { return (std::ostringstream() << *this).str(); } + + private: + friend class LocalCacheSettings::MemoryCacheSettings; + + class Impl; + + explicit EagerGCSettings(Impl); + + std::shared_ptr impl_; }; /** @@ -252,15 +252,27 @@ class MemoryEagerGCSettings final : public MemoryGarbageCollectorSettings { * `MemoryCacheSettings::WithGarbageCollectorSettings()` to get a new instance * of `MemoryCacheSettings`, which can be used to configure the SDK. */ -class MemoryLruGCSettings final : public MemoryGarbageCollectorSettings { +class LocalCacheSettings::MemoryCacheSettings::LruGCSettings final { public: - /** Create a default instance `MemoryLruGCSettings`. */ - static MemoryLruGCSettings Create(); - ~MemoryLruGCSettings() override; + LruGCSettings(); + + LruGCSettings(const LruGCSettings&) = default; + LruGCSettings& operator=(const LruGCSettings&) = default; + LruGCSettings(LruGCSettings&&) = default; + LruGCSettings& operator=(LruGCSettings&&) = default; /** Equality function. */ - friend bool operator==(const MemoryLruGCSettings& lhs, - const MemoryLruGCSettings& rhs); + friend bool operator==(const LruGCSettings&, const LruGCSettings&); + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, + const LruGCSettings& self) { + self.PrintTo(out); + return out; + } + + std::string ToString() const { return (std::ostringstream() << *this).str(); } /** * Copies this settings instance, with the approximate cache size threshold @@ -275,49 +287,53 @@ class MemoryLruGCSettings final : public MemoryGarbageCollectorSettings { * By default, memory LRU cache is enabled with a cache size of 100 MB. The * minimum value is 1 MB. */ - MemoryLruGCSettings WithSizeBytes(int64_t size); + LruGCSettings WithSizeBytes(int64_t size_bytes) const; /** * Returns the approximate cache size threshold configured. Garbage collection * kicks in once the cache size exceeds this threshold. */ - int64_t size_bytes() const { - return settings_internal_->core_settings().size_bytes(); - } + int64_t size_bytes() const; private: - friend class MemoryCacheSettings; - MemoryLruGCSettings(); - MemoryLruGCSettings(const MemoryLruGCSettingsInternal& other); + friend class LocalCacheSettings::MemoryCacheSettings; - const api::MemoryGargabeCollectorSettings& core_gc_settings() const override { - return settings_internal_->core_settings(); - } + class Impl; + + explicit LruGCSettings(Impl); - std::unique_ptr settings_internal_; + std::shared_ptr impl_; }; /** Inequality function. */ -inline bool operator!=(const MemoryCacheSettings& lhs, - const MemoryCacheSettings& rhs) { +inline bool operator!=(const LocalCacheSettings& lhs, + const LocalCacheSettings& rhs) { + return !(lhs == rhs); +} + +/** Inequality function. */ +inline bool operator!=(const LocalCacheSettings::MemoryCacheSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings& rhs) { return !(lhs == rhs); } /** Inequality function. */ -inline bool operator!=(const PersistentCacheSettings& lhs, - const PersistentCacheSettings& rhs) { +inline bool operator!=(const LocalCacheSettings::PersistentCacheSettings& lhs, + const LocalCacheSettings::PersistentCacheSettings& rhs) { return !(lhs == rhs); } /** Inequality function. */ -inline bool operator!=(const MemoryEagerGCSettings& lhs, - const MemoryEagerGCSettings& rhs) { +inline bool operator!=( + const LocalCacheSettings::MemoryCacheSettings::EagerGCSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings::EagerGCSettings& rhs) { return !(lhs == rhs); } /** Inequality function. */ -inline bool operator!=(const MemoryLruGCSettings& lhs, - const MemoryLruGCSettings& rhs) { +inline bool operator!=( + const LocalCacheSettings::MemoryCacheSettings::LruGCSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings::LruGCSettings& rhs) { return !(lhs == rhs); } diff --git a/firestore/src/include/firebase/firestore/settings.h b/firestore/src/include/firebase/firestore/settings.h index ce1b0348bf..adfe18b6a6 100644 --- a/firestore/src/include/firebase/firestore/settings.h +++ b/firestore/src/include/firebase/firestore/settings.h @@ -17,6 +17,7 @@ #ifndef FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_SETTINGS_H_ #define FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_SETTINGS_H_ +#include "firebase/firestore/local_cache_settings.h" #include "firebase/internal/common.h" #if defined(__OBJC__) #include @@ -48,7 +49,6 @@ class Executor; #endif class FirestoreInternal; -class LocalCacheSettings; /** Settings used to configure a Firestore instance. */ class Settings final { @@ -142,7 +142,7 @@ class Settings final { * Returns a shared pointer to the `LocalCacheSettings` instance * used to configure this SDK. */ - std::shared_ptr local_cache_settings(); + LocalCacheSettings local_cache_settings() const; /** * Configures the SDK with the given `LocalCacheSettings` instance. @@ -154,7 +154,7 @@ class Settings final { * * @param cache_settings Settings object to configue this SDK. */ - void set_local_cache_settings(const LocalCacheSettings& cache_settings); + void set_local_cache_settings(LocalCacheSettings); /** * NOTE: This method is deprecated in favor of `set_local_cache_settings()`. @@ -164,7 +164,8 @@ class Settings final { * * @param enabled Set true to enable local persistent storage. */ - FIREBASE_DEPRECATED void set_persistence_enabled(bool enabled); + [[deprecated("Use set_local_cache_settings() instead")]] void + set_persistence_enabled(bool enabled); /** * NOTE: This method is deprecated in favor of `set_local_cache_settings()`. @@ -179,7 +180,8 @@ class Settings final { * By default, collection is enabled with a cache size of 100 MB. The minimum * value is 1 MB. */ - FIREBASE_DEPRECATED void set_cache_size_bytes(int64_t value); + [[deprecated("Use set_local_cache_settings() instead")]] void + set_cache_size_bytes(int64_t value); #if defined(__OBJC__) || defined(DOXYGEN) /** @@ -239,13 +241,13 @@ class Settings final { private: static constexpr int64_t kDefaultCacheSizeBytes = 100 * 1024 * 1024; + enum class CacheSettingsSource { kNone, kNew, kOld }; std::string host_; bool ssl_enabled_ = true; + CacheSettingsSource cache_settings_source_{CacheSettingsSource::kNone}; + LocalCacheSettings local_cache_settings_; - std::shared_ptr local_cache_settings_ = nullptr; - - bool used_legacy_cache_settings_ = false; bool persistence_enabled_ = true; int64_t cache_size_bytes_ = kDefaultCacheSizeBytes; diff --git a/firestore/src/main/firestore_main.cc b/firestore/src/main/firestore_main.cc index 8d5d2fbdce..44388d38ca 100644 --- a/firestore/src/main/firestore_main.cc +++ b/firestore/src/main/firestore_main.cc @@ -194,14 +194,14 @@ Settings FirestoreInternal::settings() const { result.set_ssl_enabled(from.ssl_enabled()); // TODO(wuandy): We use the deprecated API for default settings, but mark - // `used_legacy_cache_settings_` as false such that new settings API is not + // `cache_settings_source_` as kNone such that new settings API is not // rejected by runtime checks. This should be removed when legacy API is // removed. SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN() result.set_persistence_enabled(from.persistence_enabled()); result.set_cache_size_bytes(from.cache_size_bytes()); SUPPRESS_END() - result.used_legacy_cache_settings_ = false; + result.cache_settings_source_ = Settings::CacheSettingsSource::kNone; return result; } @@ -210,16 +210,24 @@ void FirestoreInternal::set_settings(Settings from) { api::Settings settings; settings.set_host(std::move(from.host())); settings.set_ssl_enabled(from.is_ssl_enabled()); + // TODO(wuandy): Checking `from.local_cache_settings_` is required, because // FirestoreInternal::settings() overrides used_legacy_cache_settings_. All // this special logic should go away when legacy cache config is removed. - if (!from.used_legacy_cache_settings_ && - from.local_cache_settings_ != nullptr) { - settings.set_local_cache_settings( - from.local_cache_settings()->core_cache_settings()); - } else { - settings.set_persistence_enabled(from.is_persistence_enabled()); - settings.set_cache_size_bytes(from.cache_size_bytes()); + switch (from.cache_settings_source_) { + case Settings::CacheSettingsSource::kNone: + case Settings::CacheSettingsSource::kOld: + settings.set_persistence_enabled(from.is_persistence_enabled()); + settings.set_cache_size_bytes(from.cache_size_bytes()); + break; + case Settings::CacheSettingsSource::kNew: { + std::unique_ptr local_cache_settings = + from.local_cache_settings().impl_->ToCoreSettings(); + settings.set_local_cache_settings(*local_cache_settings); + break; + } + default: + FIRESTORE_UNREACHABLE(); } firestore_core_->set_settings(settings); diff --git a/firestore/src/main/local_cache_settings_main.cc b/firestore/src/main/local_cache_settings_main.cc new file mode 100644 index 0000000000..54ca728824 --- /dev/null +++ b/firestore/src/main/local_cache_settings_main.cc @@ -0,0 +1,282 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "firestore/src/main/local_cache_settings_main.h" +#include "firestore/src/include/firebase/firestore/local_cache_settings.h" + +namespace firebase { +namespace firestore { + +//////////////////////////////////////////////////////////////////////////////// +// class LocalCacheSettings +//////////////////////////////////////////////////////////////////////////////// + +LocalCacheSettings::LocalCacheSettings() : impl_(std::make_shared()) {} + +LocalCacheSettings::LocalCacheSettings( + PersistentCacheSettings persistent_cache_settings) + : impl_( + std::make_shared(*std::move(persistent_cache_settings.impl_))) { +} + +LocalCacheSettings::LocalCacheSettings( + MemoryCacheSettings memory_cache_settings) + : impl_(std::make_shared(std::move(*memory_cache_settings.impl_))) {} + +LocalCacheSettings::LocalCacheSettings(Impl impl) + : impl_(std::make_shared(std::move(impl))) {} + +std::unique_ptr +LocalCacheSettings::persistent_cache_settings() const { + if (!impl_->settings().has_value() || + !absl::holds_alternative( + impl_->settings().value())) { + return {}; + } + return std::make_unique( + LocalCacheSettings::PersistentCacheSettings( + absl::get(impl_->settings().value()))); +} + +LocalCacheSettings LocalCacheSettings::WithCacheSettings( + PersistentCacheSettings settings) const { + return LocalCacheSettings( + impl_->WithCacheSettings(std::move(*settings.impl_))); +} + +std::unique_ptr +LocalCacheSettings::memory_cache_settings() const { + if (!impl_->settings().has_value() || + !absl::holds_alternative( + impl_->settings().value())) { + return {}; + } + return std::make_unique( + LocalCacheSettings::MemoryCacheSettings( + absl::get(impl_->settings().value()))); +} + +LocalCacheSettings LocalCacheSettings::WithCacheSettings( + LocalCacheSettings::MemoryCacheSettings settings) const { + return LocalCacheSettings( + impl_->WithCacheSettings(std::move(*settings.impl_))); +} + +void LocalCacheSettings::PrintTo(std::ostream& out) const { out << *impl_; } + +bool operator==(const LocalCacheSettings& lhs, const LocalCacheSettings& rhs) { + return *lhs.impl_ == *rhs.impl_; +} + +bool LocalCacheSettings::Impl::operator==(const Impl& other) const { + return settings_ == other.settings_; +} + +void LocalCacheSettings::Impl::PrintTo(std::ostream& out) const { + out << "LocalCacheSettings{"; + if (!settings_.has_value()) { + out << ""; + } else if (absl::holds_alternative(*settings_)) { + out << absl::get(*settings_); + } else if (absl::holds_alternative( + *settings_)) { + out << absl::get(*settings_); + } else { + FIRESTORE_UNREACHABLE(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// class LocalCacheSettings::PersistentCacheSettings +//////////////////////////////////////////////////////////////////////////////// + +LocalCacheSettings::PersistentCacheSettings::PersistentCacheSettings() + : impl_(std::make_shared()) {} + +LocalCacheSettings::PersistentCacheSettings::PersistentCacheSettings(Impl impl) + : impl_(std::make_shared(std::move(impl))) {} + +LocalCacheSettings::PersistentCacheSettings +LocalCacheSettings::PersistentCacheSettings::WithSizeBytes( + int64_t size_bytes) const { + return PersistentCacheSettings(impl_->WithSizeBytes(size_bytes)); +} + +int64_t LocalCacheSettings::PersistentCacheSettings::size_bytes() const { + return impl_->size_bytes(); +} + +void LocalCacheSettings::PersistentCacheSettings::PrintTo( + std::ostream& out) const { + out << *impl_; +} + +bool operator==(const LocalCacheSettings::PersistentCacheSettings& lhs, + const LocalCacheSettings::PersistentCacheSettings& rhs) { + return *lhs.impl_ == *rhs.impl_; +} + +bool LocalCacheSettings::PersistentCacheSettings::Impl::operator==( + const Impl& other) const { + return settings_ == other.settings_; +} + +void LocalCacheSettings::PersistentCacheSettings::Impl::PrintTo( + std::ostream& out) const { + out << "PersistentCacheSettings{"; + out << "size_bytes=" << settings_.size_bytes(); + out << "}"; +} + +//////////////////////////////////////////////////////////////////////////////// +// class LocalCacheSettings::MemoryCacheSettings +//////////////////////////////////////////////////////////////////////////////// + +LocalCacheSettings::MemoryCacheSettings::MemoryCacheSettings() + : impl_(std::make_shared()) {} + +LocalCacheSettings::MemoryCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::LruGCSettings lru_gc_settings) + : impl_(std::make_shared(std::move(*lru_gc_settings.impl_))) {} + +LocalCacheSettings::MemoryCacheSettings::MemoryCacheSettings( + LocalCacheSettings::MemoryCacheSettings::EagerGCSettings eager_gc_settings) + : impl_(std::make_shared(std::move(*eager_gc_settings.impl_))) {} + +LocalCacheSettings::MemoryCacheSettings::MemoryCacheSettings(Impl impl) + : impl_(std::make_shared(std::move(impl))) {} + +void LocalCacheSettings::MemoryCacheSettings::PrintTo(std::ostream& out) const { + out << *impl_; +} + +bool operator==(const LocalCacheSettings::MemoryCacheSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings& rhs) { + return *lhs.impl_ == *rhs.impl_; +} + +bool LocalCacheSettings::MemoryCacheSettings::Impl::operator==( + const Impl& other) const { + return settings_ == other.settings_; +} + +void LocalCacheSettings::MemoryCacheSettings::Impl::PrintTo( + std::ostream& out) const { + out << "MemoryCacheSettings{"; + if (!settings_.has_value()) { + out << ""; + } else if (absl::holds_alternative(*settings_)) { + out << absl::get(*settings_); + } else if (absl::holds_alternative(*settings_)) { + out << absl::get(*settings_); + } else { + FIRESTORE_UNREACHABLE(); + } + out << "}"; +} + +//////////////////////////////////////////////////////////////////////////////// +// class LocalCacheSettings::MemoryCacheSettings::EagerGCSettings +//////////////////////////////////////////////////////////////////////////////// + +LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::EagerGCSettings() + : impl_(std::make_shared()) {} + +LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::EagerGCSettings( + Impl impl) + : impl_(std::make_shared(std::move(impl))) {} + +LocalCacheSettings::MemoryCacheSettings +LocalCacheSettings::MemoryCacheSettings::WithGarbageCollectorSettings( + LruGCSettings settings) const { + return MemoryCacheSettings( + impl_->WithGarbageCollectorSettings(std::move(*settings.impl_))); +} + +LocalCacheSettings::MemoryCacheSettings +LocalCacheSettings::MemoryCacheSettings::WithGarbageCollectorSettings( + EagerGCSettings settings) const { + return MemoryCacheSettings( + impl_->WithGarbageCollectorSettings(std::move(*settings.impl_))); +} + +void LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::PrintTo( + std::ostream& out) const { + out << *impl_; +} + +bool operator==( + const LocalCacheSettings::MemoryCacheSettings::EagerGCSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings::EagerGCSettings& rhs) { + return *lhs.impl_ == *rhs.impl_; +} + +bool LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::Impl::operator==( + const Impl& other) const { + return settings_ == other.settings_; +} + +void LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::Impl::PrintTo( + std::ostream& out) const { + out << "EagerGCSettings{}"; +} + +//////////////////////////////////////////////////////////////////////////////// +// class LocalCacheSettings::MemoryCacheSettings::LruGCSettings +//////////////////////////////////////////////////////////////////////////////// + +LocalCacheSettings::MemoryCacheSettings::LruGCSettings::LruGCSettings() + : impl_(std::make_shared()) {} + +LocalCacheSettings::MemoryCacheSettings::LruGCSettings::LruGCSettings(Impl impl) + : impl_(std::make_shared(std::move(impl))) {} + +LocalCacheSettings::MemoryCacheSettings::LruGCSettings +LocalCacheSettings::MemoryCacheSettings::LruGCSettings::WithSizeBytes( + int64_t size_bytes) const { + return LruGCSettings(impl_->WithSizeBytes(size_bytes)); +} + +int64_t LocalCacheSettings::MemoryCacheSettings::LruGCSettings::size_bytes() + const { + return impl_->size_bytes(); +} + +void LocalCacheSettings::MemoryCacheSettings::LruGCSettings::PrintTo( + std::ostream& out) const { + out << *impl_; +} + +bool operator==( + const LocalCacheSettings::MemoryCacheSettings::LruGCSettings& lhs, + const LocalCacheSettings::MemoryCacheSettings::LruGCSettings& rhs) { + return *lhs.impl_ == *rhs.impl_; +} + +bool LocalCacheSettings::MemoryCacheSettings::LruGCSettings::Impl::operator==( + const Impl& other) const { + return settings_ == other.settings_; +} + +void LocalCacheSettings::MemoryCacheSettings::LruGCSettings::Impl::PrintTo( + std::ostream& out) const { + out << "LruGCSettings{"; + out << "size_bytes=" << this->size_bytes(); + out << "}"; +} + +} // namespace firestore +} // namespace firebase diff --git a/firestore/src/main/local_cache_settings_main.h b/firestore/src/main/local_cache_settings_main.h index 1e5fc85a3b..8eddfc8884 100644 --- a/firestore/src/main/local_cache_settings_main.h +++ b/firestore/src/main/local_cache_settings_main.h @@ -17,120 +17,210 @@ #ifndef FIREBASE_FIRESTORE_SRC_MAIN_LOCAL_CACHE_SETTINGS_MAIN_H_ #define FIREBASE_FIRESTORE_SRC_MAIN_LOCAL_CACHE_SETTINGS_MAIN_H_ -#include -#include +#include +#include #include "Firestore/core/src/api/settings.h" +#include "absl/types/optional.h" +#include "absl/types/variant.h" +#include "firestore/src/common/macros.h" +#include "firestore/src/include/firebase/firestore/local_cache_settings.h" namespace firebase { namespace firestore { -class LocalCacheSettingsInternal {}; - -class PersistentCacheSettingsInternal final - : public LocalCacheSettingsInternal { +class LocalCacheSettings::MemoryCacheSettings::LruGCSettings::Impl final { public: - explicit PersistentCacheSettingsInternal( - const api::PersistentCacheSettings& core_settings) - : settings_(std::move(core_settings)) {} + Impl() = default; + + explicit Impl(api::MemoryLruGcSettings settings) + : settings_(std::move(settings)) {} + + const api::MemoryLruGcSettings& core_settings() const { return settings_; } - friend bool operator==(const PersistentCacheSettingsInternal& lhs, - const PersistentCacheSettingsInternal& rhs) { - return &lhs == &rhs || lhs.settings_ == rhs.settings_; + int64_t size_bytes() const { return settings_.size_bytes(); } + + Impl WithSizeBytes(int64_t size_bytes) { + return Impl(settings_.WithSizeBytes(size_bytes)); } - const api::PersistentCacheSettings& core_settings() { return settings_; } - void set_core_settings(const api::PersistentCacheSettings& settings) { - settings_ = settings; + bool operator==(const Impl&) const; + bool operator!=(const Impl& rhs) const { return !(*this == rhs); } + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, const Impl& self) { + self.PrintTo(out); + return out; } + std::string ToString() const { return (std::ostringstream() << *this).str(); } + private: - api::PersistentCacheSettings settings_; + api::MemoryLruGcSettings settings_; }; -class MemoryGarbageCollectorSettingsInternal {}; - -class MemoryEagerGCSettingsInternal final - : public MemoryGarbageCollectorSettingsInternal { +class LocalCacheSettings::MemoryCacheSettings::EagerGCSettings::Impl final { public: - explicit MemoryEagerGCSettingsInternal( - const api::MemoryEagerGcSettings& core_settings) - : settings_(std::move(core_settings)) {} + Impl() = default; - friend bool operator==(const MemoryEagerGCSettingsInternal& lhs, - const MemoryEagerGCSettingsInternal& rhs) { - return &lhs == &rhs || lhs.settings_ == rhs.settings_; - } + explicit Impl(api::MemoryEagerGcSettings settings) + : settings_(std::move(settings)) {} + + const api::MemoryEagerGcSettings& core_settings() const { return settings_; } - const api::MemoryEagerGcSettings& core_settings() { return settings_; } - void set_core_settings(const api::MemoryEagerGcSettings& settings) { - settings_ = settings; + bool operator==(const Impl&) const; + bool operator!=(const Impl& rhs) const { return !(*this == rhs); } + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, const Impl& self) { + self.PrintTo(out); + return out; } + std::string ToString() const { return (std::ostringstream() << *this).str(); } + private: api::MemoryEagerGcSettings settings_; }; -class MemoryLruGCSettingsInternal final - : public MemoryGarbageCollectorSettingsInternal { +class LocalCacheSettings::MemoryCacheSettings::Impl final { public: - explicit MemoryLruGCSettingsInternal( - const api::MemoryLruGcSettings& core_settings) - : settings_(std::move(core_settings)) {} + Impl() = default; + + explicit Impl(LruGCSettings::Impl impl) : settings_(std::move(impl)) {} + + explicit Impl(EagerGCSettings::Impl impl) : settings_(std::move(impl)) {} + + std::unique_ptr ToCoreSettings() const { + if (!settings_.has_value()) { + return std::make_unique(); + } + if (absl::holds_alternative(*settings_)) { + return std::make_unique( + api::MemoryCacheSettings().WithMemoryGarbageCollectorSettings( + absl::get(*settings_).core_settings())); + } + if (absl::holds_alternative(*settings_)) { + return std::make_unique( + api::MemoryCacheSettings().WithMemoryGarbageCollectorSettings( + absl::get(*settings_).core_settings())); + } + FIRESTORE_UNREACHABLE(); + } - friend bool operator==(const MemoryLruGCSettingsInternal& lhs, - const MemoryLruGCSettingsInternal& rhs) { - return &lhs == &rhs || lhs.settings_ == rhs.settings_; + Impl WithGarbageCollectorSettings(LruGCSettings::Impl settings) const { + return Impl(std::move(settings)); } - const api::MemoryLruGcSettings& core_settings() { return settings_; } - void set_core_settings(const api::MemoryLruGcSettings& settings) { - settings_ = settings; + Impl WithGarbageCollectorSettings(EagerGCSettings::Impl settings) const { + return Impl(std::move(settings)); } + bool operator==(const Impl&) const; + bool operator!=(const Impl& rhs) const { return !(*this == rhs); } + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, const Impl& self) { + self.PrintTo(out); + return out; + } + + std::string ToString() const { return (std::ostringstream() << *this).str(); } + private: - api::MemoryLruGcSettings settings_; + absl::optional> + settings_; }; -class MemoryCacheSettingsInternal final : public LocalCacheSettingsInternal { +class LocalCacheSettings::PersistentCacheSettings::Impl final { public: - explicit MemoryCacheSettingsInternal( - const api::MemoryCacheSettings& core_settings) - : settings_(std::move(core_settings)) {} + Impl() = default; + + explicit Impl(api::PersistentCacheSettings settings) + : settings_(std::move(settings)) {} - friend bool operator==(const MemoryCacheSettingsInternal& lhs, - const MemoryCacheSettingsInternal& rhs) { - return &lhs == &rhs || lhs.settings_ == rhs.settings_; + std::unique_ptr ToCoreSettings() const { + return std::make_unique(settings_); } - const api::MemoryCacheSettings& core_settings() { return settings_; } - void set_core_settings(const api::MemoryCacheSettings& settings) { - settings_ = settings; + int64_t size_bytes() const { return settings_.size_bytes(); } + + Impl WithSizeBytes(int64_t size_bytes) { + return Impl(settings_.WithSizeBytes(size_bytes)); + } + + bool operator==(const Impl&) const; + bool operator!=(const Impl& rhs) const { return !(*this == rhs); } + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, const Impl& self) { + self.PrintTo(out); + return out; } + std::string ToString() const { return (std::ostringstream() << *this).str(); } + private: - api::MemoryCacheSettings settings_; + api::PersistentCacheSettings settings_; }; -inline bool operator!=(const MemoryCacheSettingsInternal& lhs, - const MemoryCacheSettingsInternal& rhs) { - return !(lhs == rhs); -} - -inline bool operator!=(const MemoryLruGCSettingsInternal& lhs, - const MemoryLruGCSettingsInternal& rhs) { - return !(lhs == rhs); -} - -inline bool operator!=(const MemoryEagerGCSettingsInternal& lhs, - const MemoryEagerGCSettingsInternal& rhs) { - return !(lhs == rhs); -} - -inline bool operator!=(const PersistentCacheSettingsInternal& lhs, - const PersistentCacheSettingsInternal& rhs) { - return !(lhs == rhs); -} +class LocalCacheSettings::Impl final { + public: + Impl() = default; + + explicit Impl(MemoryCacheSettings::Impl impl) : settings_(std::move(impl)) {} + explicit Impl(PersistentCacheSettings::Impl impl) + : settings_(std::move(impl)) {} + + std::unique_ptr ToCoreSettings() const { + if (!settings_.has_value()) { + return std::make_unique(); + } + if (absl::holds_alternative(*settings_)) { + return absl::get(*settings_).ToCoreSettings(); + } + if (absl::holds_alternative(*settings_)) { + return absl::get(*settings_) + .ToCoreSettings(); + } + FIRESTORE_UNREACHABLE(); + } + + Impl WithCacheSettings(MemoryCacheSettings::Impl settings) const { + return Impl(std::move(settings)); + } + + Impl WithCacheSettings(PersistentCacheSettings::Impl settings) const { + return Impl(std::move(settings)); + } + + const absl::optional< + absl::variant>& + settings() const { + return settings_; + } + + bool operator==(const Impl&) const; + bool operator!=(const Impl& rhs) const { return !(*this == rhs); } + + void PrintTo(std::ostream& out) const; + + friend std::ostream& operator<<(std::ostream& out, const Impl& self) { + self.PrintTo(out); + return out; + } + + std::string ToString() const { return (std::ostringstream() << *this).str(); } + + private: + absl::optional< + absl::variant> + settings_; +}; } // namespace firestore } // namespace firebase