Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Cache Config and LRU GC #1308

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firestore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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
Expand Down
3 changes: 2 additions & 1 deletion firestore/integration_test_internal/src/bundle_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>

#include "firebase/firestore.h"
#include "firebase/firestore/local_cache_settings.h"
#include "firebase_test_framework.h"
#include "firestore_integration_test.h"
#include "gmock/gmock.h"
Expand Down Expand Up @@ -327,7 +328,7 @@ 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_persistence_enabled(false);
new_settings.set_local_cache_settings(MemoryCacheSettings::Create());
db->set_settings(new_settings);

auto bundle = CreateTestBundle(db);
Expand Down
119 changes: 119 additions & 0 deletions firestore/integration_test_internal/src/firestore_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#include <future>
#include <memory>
#include <stdexcept>
#include "firebase/firestore/document_snapshot.h"
#include "firebase/firestore/field_value.h"
#include "firebase/firestore/local_cache_settings.h"
#include "firebase/firestore/map_field_value.h"
#include "firebase/firestore/source.h"

#if defined(__ANDROID__)
#include "android/firestore_integration_test_android.h"
Expand All @@ -42,6 +47,7 @@
#include "util/future_test_util.h"
#if !defined(__ANDROID__)
#include "Firestore/core/src/util/autoid.h"
#include "Firestore/core/src/util/warnings.h"
#include "firestore/src/main/converter_main.h"
#include "firestore/src/main/firestore_main.h"
#else
Expand Down Expand Up @@ -1757,6 +1763,119 @@ TEST_F(FirestoreTest, ClearPersistenceWhileRunningFails) {
EXPECT_EQ(await_clear_persistence.error(), Error::kErrorFailedPrecondition);
}

class FirestoreCacheConfigTest : public FirestoreIntegrationTest {
protected:
void VerifyCachedDocumentDeletedImmediately(Firestore* db) {
Await(db->Document("rooms/eros")
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));

auto get_future = db->Document("rooms/eros").Get(Source::kCache);
const DocumentSnapshot* snapshot = Await(get_future);
ASSERT_NE(snapshot, nullptr);
ASSERT_FALSE(snapshot->is_valid());
}

void VerifyCachedDocumentStaysAround(Firestore* db) {
Await(db->Document("rooms/eros")
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));

auto get_future = db->Document("rooms/eros").Get(Source::kCache);
const DocumentSnapshot* snapshot = Await(get_future);
ASSERT_NE(snapshot, nullptr);
ASSERT_TRUE(snapshot->is_valid());
ASSERT_THAT(
snapshot->GetData(),
ContainerEq(MapFieldValue{{"desc", FieldValue::String("eros")}}));
}
};

TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForMemoryCacheWorks) {
auto* db = TestFirestore("legacy_memory_cache");
auto settings = db->settings();
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN();
settings.set_persistence_enabled(false);
SUPPRESS_END();
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
}

TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForPersistenceCacheWorks) {
auto* db = TestFirestore("legacy_persistent_cache");
auto settings = db->settings();
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN();
settings.set_persistence_enabled(true);
SUPPRESS_END();
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
}

TEST_F(FirestoreCacheConfigTest, NewCacheConfigForMemoryCacheWorks) {
auto* db = TestFirestore("new_memory_cache");
auto settings = db->settings();
settings.set_local_cache_settings(MemoryCacheSettings::Create());
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
}

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));
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
}

TEST_F(FirestoreCacheConfigTest, CannotMixNewAndLegacyCacheConfig) {
{
auto* db = TestFirestore("mixing_1");
auto settings = db->settings();
settings.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024));

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();
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN();
settings.set_persistence_enabled(false);
SUPPRESS_END();
EXPECT_THROW(
settings.set_local_cache_settings(MemoryCacheSettings::Create()),
std::logic_error);
}
}

TEST_F(FirestoreCacheConfigTest, CanGetDocumentFromCacheWithMemoryLruGC) {
auto* db = TestFirestore("new_persistent_cache");
auto settings = db->settings();
settings.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create()));
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
}

TEST_F(FirestoreCacheConfigTest, CannotGetDocumentFromCacheFromMemoryEagerGC) {
auto* db = TestFirestore("new_persistent_cache");
auto settings = db->settings();
settings.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryEagerGCSettings::Create()));
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
}

// Note: this test only exists in C++.
TEST_F(FirestoreTest, DomainObjectsReferToSameFirestoreInstance) {
EXPECT_EQ(TestFirestore(), TestFirestore()->Document("foo/bar").firestore());
Expand Down
112 changes: 112 additions & 0 deletions firestore/integration_test_internal/src/settings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

#include <stdint.h>

#include "Firestore/core/src/util/warnings.h"
#include "firebase/firestore.h"
#include "firebase/firestore/local_cache_settings.h"
#include "firebase_test_framework.h"

#include "gtest/gtest.h"

Expand All @@ -31,39 +34,51 @@ TEST(SettingsTest, Equality) {
Settings settings1;
settings1.set_host("foo");
settings1.set_ssl_enabled(true);
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);
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);
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);
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);
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);
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);
Expand Down Expand Up @@ -94,6 +109,103 @@ TEST(SettingsTest, Equality) {
EXPECT_TRUE(settings4 != settings5);
}

TEST(SettingsTest, EqualityWithLocalCacheSettings) {
constexpr int64_t kFiveMb = 5 * 1024 * 1024;
constexpr int64_t kSixMb = 6 * 1024 * 1024;

Settings settings1;
settings1.set_host("foo");
settings1.set_ssl_enabled(true);
settings1.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));

Settings settings2;
settings2.set_host("bar");
settings2.set_ssl_enabled(true);
settings2.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));

Settings settings3;
settings3.set_host("foo");
settings3.set_ssl_enabled(false);
settings3.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));

Settings settings4;
settings4.set_host("foo");
settings4.set_ssl_enabled(true);
settings4.set_local_cache_settings(MemoryCacheSettings::Create());

Settings settings5;
settings5.set_host("foo");
settings5.set_ssl_enabled(true);
settings5.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kSixMb));

Settings settings6;
settings6.set_host("foo");
settings6.set_ssl_enabled(true);
settings6.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryEagerGCSettings::Create()));

Settings settings7;
settings7.set_host("foo");
settings7.set_ssl_enabled(true);
settings7.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create().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)));

// 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)));

EXPECT_TRUE(settings1 == settings1);
EXPECT_TRUE(settings6 == settings4);
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TRUE(settings7 == settings9);

EXPECT_FALSE(settings1 == settings2);
EXPECT_FALSE(settings1 == settings3);
EXPECT_FALSE(settings1 == settings4);
EXPECT_FALSE(settings1 == settings5);
EXPECT_FALSE(settings2 == settings3);
EXPECT_FALSE(settings2 == settings4);
EXPECT_FALSE(settings2 == settings5);
EXPECT_FALSE(settings3 == settings4);
EXPECT_FALSE(settings3 == settings5);
EXPECT_FALSE(settings4 == settings5);
EXPECT_FALSE(settings6 == settings7);
EXPECT_FALSE(settings7 == settings8);

EXPECT_FALSE(settings1 != settings1);
EXPECT_FALSE(settings6 != settings4);
EXPECT_FALSE(settings7 != settings9);

EXPECT_TRUE(settings1 != settings2);
EXPECT_TRUE(settings1 != settings3);
EXPECT_TRUE(settings1 != settings4);
EXPECT_TRUE(settings1 != settings5);
EXPECT_TRUE(settings2 != settings3);
EXPECT_TRUE(settings2 != settings4);
EXPECT_TRUE(settings2 != settings5);
EXPECT_TRUE(settings3 != settings4);
EXPECT_TRUE(settings3 != settings5);
EXPECT_TRUE(settings4 != settings5);
EXPECT_TRUE(settings6 != settings7);
EXPECT_TRUE(settings7 != settings8);
}

} // namespace
} // namespace firestore
} // namespace firebase
Loading