Skip to content

Commit

Permalink
LRU tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed May 8, 2023
1 parent 26e9896 commit e7a0f70
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
36 changes: 36 additions & 0 deletions firestore/integration_test_internal/src/firestore_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,42 @@ TEST_F(FirestoreIntegrationTest, CannotMixNewAndLegacyCacheConfig) {
}
}

TEST_F(FirestoreIntegrationTest, 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));

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);
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
EXPECT_TRUE(snapshot->is_valid());
EXPECT_THAT(snapshot->GetData(),
ContainerEq(MapFieldValue{{"desc", FieldValue::String("eros")}}));
}

TEST_F(FirestoreIntegrationTest, 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));

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);
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
EXPECT_FALSE(snapshot->is_valid());
}

// Note: this test only exists in C++.
TEST_F(FirestoreIntegrationTest, DomainObjectsReferToSameFirestoreInstance) {
EXPECT_EQ(TestFirestore(), TestFirestore()->Document("foo/bar").firestore());
Expand Down
10 changes: 10 additions & 0 deletions firestore/src/common/local_cache_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ MemoryCacheSettings::MemoryCacheSettings(const MemoryCacheSettings& other) {

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();
}
Expand Down
68 changes: 42 additions & 26 deletions firestore/src/include/firebase/firestore/local_cache_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace firebase {
namespace firestore {

using CoreCacheSettings = api::LocalCacheSettings;
using CoreMemoryGarbageCollectorSettings = api::MemoryGargabeCollectorSettings;

class PersistentCacheSettingsInternal;
class MemoryCacheSettingsInternal;
Expand Down Expand Up @@ -57,7 +58,6 @@ class PersistentCacheSettings final : public LocalCacheSettings {
friend class Settings;

PersistentCacheSettings();
PersistentCacheSettings(const PersistentCacheSettingsInternal& other);

api::LocalCacheSettings::Kind kind() const override {
return api::LocalCacheSettings::Kind::kPersistent;
Expand All @@ -68,57 +68,73 @@ class PersistentCacheSettings final : public LocalCacheSettings {
std::unique_ptr<PersistentCacheSettingsInternal> settings_internal_;
};

class MemoryGarbageCollectorSettings;

class MemoryCacheSettings final : public LocalCacheSettings {
public:
static MemoryCacheSettings Create();
MemoryCacheSettings(const MemoryCacheSettings& other);
~MemoryCacheSettings();

MemoryCacheSettings WithGarbageCollectorSettings(
const MemoryGarbageCollectorSettings& settings) const;

private:
friend class Settings;

MemoryCacheSettings();

api::LocalCacheSettings::Kind kind() const override {
return api::LocalCacheSettings::Kind::kMemory;
}

const CoreCacheSettings& core_cache_settings() const override;

std::unique_ptr<MemoryCacheSettingsInternal> settings_internal_;
};

class MemoryGarbageCollectorSettings {
public:
virtual ~MemoryGarbageCollectorSettings() = default;

private:
friend class MemoryCacheSettings;
virtual const CoreMemoryGarbageCollectorSettings& core_gc_settings()
const = 0;
};

class MemoryEagerGCSettings final : MemoryGarbageCollectorSettings {
class MemoryEagerGCSettings final : public MemoryGarbageCollectorSettings {
public:
static MemoryEagerGCSettings Create();
~MemoryEagerGCSettings();

private:
friend class MemoryCacheSettings;
MemoryEagerGCSettings();

const CoreMemoryGarbageCollectorSettings& core_gc_settings() const override {
return settings_internal_->core_settings();
}

std::unique_ptr<MemoryEagerGCSettingsInternal> settings_internal_;
};

class MemoryLruGCSettings final : MemoryGarbageCollectorSettings {
class MemoryLruGCSettings final : public MemoryGarbageCollectorSettings {
public:
static MemoryLruGCSettings Create();
~MemoryLruGCSettings();
MemoryLruGCSettings WithSizeBytes(int64_t size);

private:
friend class MemoryCacheSettings;
MemoryLruGCSettings();
MemoryLruGCSettings(const MemoryLruGCSettingsInternal& other);

std::unique_ptr<MemoryLruGCSettingsInternal> settings_internal_;
};

class MemoryCacheSettings final : public LocalCacheSettings {
public:
static MemoryCacheSettings Create();
MemoryCacheSettings(const MemoryCacheSettings& other);
~MemoryCacheSettings();

MemoryCacheSettings WithGarbageCollectorSettings(
const MemoryGarbageCollectorSettings& settings);

private:
friend class Settings;

MemoryCacheSettings();
MemoryCacheSettings(const MemoryCacheSettingsInternal& other);

api::LocalCacheSettings::Kind kind() const override {
return api::LocalCacheSettings::Kind::kMemory;
const CoreMemoryGarbageCollectorSettings& core_gc_settings() const override {
return settings_internal_->core_settings();
}

const CoreCacheSettings& core_cache_settings() const override;

std::unique_ptr<MemoryCacheSettingsInternal> settings_internal_;
std::unique_ptr<MemoryLruGCSettingsInternal> settings_internal_;
};

} // namespace firestore
Expand Down

0 comments on commit e7a0f70

Please sign in to comment.