Skip to content

Commit

Permalink
No more core from common
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed Jul 11, 2023
1 parent b757139 commit ded9c2e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 71 deletions.
67 changes: 27 additions & 40 deletions firestore/src/common/local_cache_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "firestore/src/include/firebase/firestore/local_cache_settings.h"
#include <memory>

#include "Firestore/core/src/api/settings.h"
#include "firestore/src/common/hard_assert_common.h"
#if defined(__ANDROID__)
#else
Expand All @@ -27,55 +26,44 @@
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

PersistentCacheSettings PersistentCacheSettings::Create() { return {}; }

PersistentCacheSettings::PersistentCacheSettings() {
settings_internal_ = std::make_shared<PersistentCacheSettingsInternal>(
CorePersistentSettings{});
settings_internal_ = std::make_shared<PersistentCacheSettingsInternal>();
}

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));
PersistentCacheSettings new_settings;
new_settings.settings_internal_ =
std::make_shared<PersistentCacheSettingsInternal>(
this->settings_internal_->WithSizeBytes(size));
return new_settings;
}

int64_t PersistentCacheSettings::size_bytes() const {
return settings_internal_->core_settings().size_bytes();
return settings_internal_->size_bytes();
}

const CoreCacheSettings& PersistentCacheSettings::core_settings() const {
return settings_internal_->core_settings();
const LocalCacheSettingsInternal& PersistentCacheSettings::internal() const {
return *settings_internal_;
}

MemoryEagerGCSettings MemoryEagerGCSettings::Create() { return {}; }

MemoryEagerGCSettings::MemoryEagerGCSettings() {
settings_internal_ = std::make_shared<MemoryEagerGCSettingsInternal>(
CoreMemoryEagerGcSettings{});
settings_internal_ = std::make_shared<MemoryEagerGCSettingsInternal>();
}

const CoreMemoryGarbageCollectorSettings& MemoryEagerGCSettings::core_settings()
const MemoryGarbageCollectorSettingsInternal& MemoryEagerGCSettings::internal()
const {
return settings_internal_->core_settings();
return *settings_internal_;
}

MemoryLruGCSettings MemoryLruGCSettings::Create() { return {}; }

MemoryLruGCSettings::MemoryLruGCSettings() {
settings_internal_ =
std::make_shared<MemoryLruGCSettingsInternal>(CoreMemoryLruGcSettings{});
settings_internal_ = std::make_shared<MemoryLruGCSettingsInternal>();
}

MemoryLruGCSettings::MemoryLruGCSettings(
Expand All @@ -84,42 +72,41 @@ MemoryLruGCSettings::MemoryLruGCSettings(
}

MemoryLruGCSettings MemoryLruGCSettings::WithSizeBytes(int64_t size) {
return {MemoryLruGCSettingsInternal{
settings_internal_->core_settings().WithSizeBytes(size)}};
MemoryLruGCSettings result;
result.settings_internal_ = std::make_shared<MemoryLruGCSettingsInternal>(
this->settings_internal_->WithSizeBytes(size));
return result;
}

int64_t MemoryLruGCSettings::size_bytes() const {
return settings_internal_->core_settings().size_bytes();
return settings_internal_->size_bytes();
}

const CoreMemoryGarbageCollectorSettings& MemoryLruGCSettings::core_settings()
const MemoryGarbageCollectorSettingsInternal& MemoryLruGCSettings::internal()
const {
return settings_internal_->core_settings();
return *settings_internal_;
}

MemoryCacheSettings MemoryCacheSettings::Create() { return {}; }

MemoryCacheSettings::MemoryCacheSettings() {
settings_internal_ =
std::make_shared<MemoryCacheSettingsInternal>(CoreMemorySettings{});
settings_internal_ = std::make_shared<MemoryCacheSettingsInternal>();
}

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_settings()));
MemoryCacheSettings result;
result.settings_internal_ = std::make_shared<MemoryCacheSettingsInternal>(
this->settings_internal_->WithGarbageCollectorSettings(settings));
return result;
}

const CoreCacheSettings& MemoryCacheSettings::core_settings() const {
return settings_internal_->core_settings();
const LocalCacheSettingsInternal& MemoryCacheSettings::internal() const {
return *settings_internal_;
}

bool operator==(const LocalCacheSettings& lhs, const LocalCacheSettings& rhs) {
return lhs.kind() == rhs.kind() && lhs.core_settings() == rhs.core_settings();
return lhs.kind() == rhs.kind() && lhs.internal() == rhs.internal();
}

bool operator==(const MemoryCacheSettings& lhs,
Expand All @@ -134,7 +121,7 @@ bool operator==(const PersistentCacheSettings& lhs,

bool operator==(const MemoryGarbageCollectorSettings& lhs,
const MemoryGarbageCollectorSettings& rhs) {
return lhs.core_settings() == rhs.core_settings();
return lhs.internal() == rhs.internal();
}

bool operator==(const MemoryEagerGCSettings& lhs,
Expand Down
1 change: 0 additions & 1 deletion firestore/src/common/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <ostream>
#include <sstream>

#include "Firestore/core/src/api/settings.h"
#include "Firestore/core/src/util/hard_assert.h"
#include "app/meta/move.h"
#include "firebase/firestore/local_cache_settings.h"
Expand Down
27 changes: 13 additions & 14 deletions firestore/src/include/firebase/firestore/local_cache_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@

namespace firebase {
namespace firestore {
namespace api {
class LocalCacheSettings;
class MemoryGargabeCollectorSettings;
} // namespace api

class LocalCacheSettingsInternal;
class PersistentCacheSettingsInternal;
class MemoryCacheSettingsInternal;
class MemoryLruGCSettingsInternal;
class MemoryEagerGCSettingsInternal;
class MemoryGarbageCollectorSettingsInternal;

/**
* Abstract class implemented by all supported cache settings.
Expand All @@ -42,15 +40,16 @@ class MemoryEagerGCSettingsInternal;
*/
class LocalCacheSettings {
public:
enum class Kind { kMemory, kPersistent };

virtual ~LocalCacheSettings() = default;

virtual Kind kind() const = 0;

/** Equality function. */
friend bool operator==(const LocalCacheSettings& lhs,
const LocalCacheSettings& rhs);

protected:
enum class Kind { kMemory, kPersistent };

private:
friend class FirestoreInternal;
friend class Settings;
Expand All @@ -59,8 +58,7 @@ class LocalCacheSettings {

LocalCacheSettings() = default;

virtual Kind kind() const = 0;
virtual const api::LocalCacheSettings& core_settings() const = 0;
virtual const LocalCacheSettingsInternal& internal() const = 0;
};

/**
Expand Down Expand Up @@ -114,7 +112,7 @@ class PersistentCacheSettings final : public LocalCacheSettings {
}

// Get the corresponding settings object from the core sdk.
const api::LocalCacheSettings& core_settings() const override;
const LocalCacheSettingsInternal& internal() const override;

std::shared_ptr<PersistentCacheSettingsInternal> settings_internal_;
};
Expand Down Expand Up @@ -155,7 +153,7 @@ class MemoryCacheSettings final : public LocalCacheSettings {
return LocalCacheSettings::Kind::kMemory;
}

const api::LocalCacheSettings& core_settings() const override;
const LocalCacheSettingsInternal& internal() const override;

std::shared_ptr<MemoryCacheSettingsInternal> settings_internal_;
};
Expand All @@ -178,10 +176,11 @@ class MemoryGarbageCollectorSettings {
friend class MemoryCacheSettings;
friend class MemoryEagerGCSettings;
friend class MemoryLruGCSettings;
friend class MemoryCacheSettingsInternal;

MemoryGarbageCollectorSettings() = default;

virtual const api::MemoryGargabeCollectorSettings& core_settings() const = 0;
virtual const MemoryGarbageCollectorSettingsInternal& internal() const = 0;
};

/**
Expand Down Expand Up @@ -211,7 +210,7 @@ class MemoryEagerGCSettings final : public MemoryGarbageCollectorSettings {
private:
friend class MemoryCacheSettings;
MemoryEagerGCSettings();
const api::MemoryGargabeCollectorSettings& core_settings() const override;
const MemoryGarbageCollectorSettingsInternal& internal() const override;

std::shared_ptr<MemoryEagerGCSettingsInternal> settings_internal_;
};
Expand Down Expand Up @@ -267,7 +266,7 @@ class MemoryLruGCSettings final : public MemoryGarbageCollectorSettings {
MemoryLruGCSettings();
MemoryLruGCSettings(const MemoryLruGCSettingsInternal& other);

const api::MemoryGargabeCollectorSettings& core_settings() const override;
const MemoryGarbageCollectorSettingsInternal& internal() const override;

std::shared_ptr<MemoryLruGCSettingsInternal> settings_internal_;
};
Expand Down
2 changes: 1 addition & 1 deletion firestore/src/main/firestore_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void FirestoreInternal::set_settings(Settings from) {
// this special logic should go away when legacy cache config is removed.
if (from.cache_settings_source_ == Settings::CacheSettingsSource::kNew) {
settings.set_local_cache_settings(
from.local_cache_settings().core_settings());
from.local_cache_settings().internal().core_settings());
} else {
settings.set_persistence_enabled(from.is_persistence_enabled());
settings.set_cache_size_bytes(from.cache_size_bytes());
Expand Down
55 changes: 40 additions & 15 deletions firestore/src/main/local_cache_settings_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@
#include <utility>

#include "Firestore/core/src/api/settings.h"
#include "firebase/firestore/local_cache_settings.h"

namespace firebase {
namespace firestore {

class LocalCacheSettingsInternal {
public:
friend bool operator==(const LocalCacheSettingsInternal& lhs,
const LocalCacheSettingsInternal& rhs) {
return &lhs == &rhs || lhs.core_settings() == rhs.core_settings();
}

virtual const api::LocalCacheSettings& core_settings() const = 0;
};

class PersistentCacheSettingsInternal final
: public LocalCacheSettingsInternal {
public:
explicit PersistentCacheSettingsInternal(
const api::PersistentCacheSettings& core_settings)
: settings_(std::move(core_settings)) {}
explicit PersistentCacheSettingsInternal() = default;

friend bool operator==(const PersistentCacheSettingsInternal& lhs,
const PersistentCacheSettingsInternal& rhs) {
return &lhs == &rhs || lhs.settings_ == rhs.settings_;
}

PersistentCacheSettingsInternal WithSizeBytes(int64_t size) {
PersistentCacheSettingsInternal result;
result.set_core_settings(this->core_settings().WithSizeBytes(size));
return result;
}

int64_t size_bytes() const { return settings_.size_bytes(); }

const api::PersistentCacheSettings& core_settings() const override {
return settings_;
}
Expand All @@ -55,15 +67,18 @@ class PersistentCacheSettingsInternal final

class MemoryGarbageCollectorSettingsInternal {
public:
friend bool operator==(const MemoryGarbageCollectorSettingsInternal& lhs,
const MemoryGarbageCollectorSettingsInternal& rhs) {
return &lhs == &rhs || lhs.core_settings() == rhs.core_settings();
}

virtual const api::MemoryGargabeCollectorSettings& core_settings() const = 0;
};

class MemoryEagerGCSettingsInternal final
: public MemoryGarbageCollectorSettingsInternal {
public:
explicit MemoryEagerGCSettingsInternal(
const api::MemoryEagerGcSettings& core_settings)
: settings_(std::move(core_settings)) {}
explicit MemoryEagerGCSettingsInternal() = default;

friend bool operator==(const MemoryEagerGCSettingsInternal& lhs,
const MemoryEagerGCSettingsInternal& rhs) {
Expand All @@ -84,15 +99,21 @@ class MemoryEagerGCSettingsInternal final
class MemoryLruGCSettingsInternal final
: public MemoryGarbageCollectorSettingsInternal {
public:
explicit MemoryLruGCSettingsInternal(
const api::MemoryLruGcSettings& core_settings)
: settings_(std::move(core_settings)) {}
explicit MemoryLruGCSettingsInternal() = default;

friend bool operator==(const MemoryLruGCSettingsInternal& lhs,
const MemoryLruGCSettingsInternal& rhs) {
return &lhs == &rhs || lhs.settings_ == rhs.settings_;
}

MemoryLruGCSettingsInternal WithSizeBytes(int64_t size) {
MemoryLruGCSettingsInternal result;
result.set_core_settings(this->core_settings().WithSizeBytes(size));
return result;
}

int64_t size_bytes() const { return settings_.size_bytes(); }

const api::MemoryLruGcSettings& core_settings() const override {
return settings_;
}
Expand All @@ -106,23 +127,27 @@ class MemoryLruGCSettingsInternal final

class MemoryCacheSettingsInternal final : public LocalCacheSettingsInternal {
public:
explicit MemoryCacheSettingsInternal(
const api::MemoryCacheSettings& core_settings)
: settings_(std::move(core_settings)) {}
explicit MemoryCacheSettingsInternal() = default;

friend bool operator==(const MemoryCacheSettingsInternal& lhs,
const MemoryCacheSettingsInternal& rhs) {
return &lhs == &rhs || lhs.settings_ == rhs.settings_;
}

MemoryCacheSettingsInternal WithGarbageCollectorSettings(
const MemoryGarbageCollectorSettings& gc_settings) {
return MemoryCacheSettingsInternal(
api::MemoryCacheSettings().WithMemoryGarbageCollectorSettings(
gc_settings.internal().core_settings()));
}

const api::MemoryCacheSettings& core_settings() const override {
return settings_;
}
void set_core_settings(const api::MemoryCacheSettings& settings) {
settings_ = settings;
}

private:
explicit MemoryCacheSettingsInternal(const api::MemoryCacheSettings& settings)
: settings_(settings) {}
api::MemoryCacheSettings settings_;
};

Expand Down

0 comments on commit ded9c2e

Please sign in to comment.