Skip to content

Commit 2021a27

Browse files
committed
Compiling..pheww
1 parent ef27076 commit 2021a27

File tree

8 files changed

+100
-11
lines changed

8 files changed

+100
-11
lines changed

firestore/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(common_SRCS
4141
src/common/hard_assert_common.h
4242
src/common/listener_registration.cc
4343
src/common/load_bundle_task_progress.cc
44+
src/common/local_cache_settings.cc
4445
src/common/macros.h
4546
src/common/query.cc
4647
src/common/query_snapshot.cc

firestore/src/common/local_cache_settings.cc

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ PersistentCacheSettings PersistentCacheSettings::WithSizeBytes(
4848
settings_internal_->core_settings().WithSizeBytes(size)}};
4949
}
5050

51+
const CoreCacheSettings& PersistentCacheSettings::core_cache_settings() const {
52+
return settings_internal_->core_settings();
53+
}
54+
5155
MemoryCacheSettings MemoryCacheSettings::Create() { return {}; }
5256

5357
MemoryCacheSettings::MemoryCacheSettings() {
@@ -61,5 +65,10 @@ MemoryCacheSettings::MemoryCacheSettings(
6165
const MemoryCacheSettingsInternal& other) {
6266
settings_internal_ = std::make_unique<MemoryCacheSettingsInternal>(other);
6367
}
68+
69+
const CoreCacheSettings& MemoryCacheSettings::core_cache_settings() const {
70+
return settings_internal_->core_settings();
71+
}
72+
6473
} // namespace firestore
6574
} // namespace firebase

firestore/src/common/settings.cc

+38
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
#include "firestore/src/include/firebase/firestore/settings.h"
1818

19+
#include <memory>
1920
#include <ostream>
2021
#include <sstream>
2122

23+
#include "Firestore/core/src/api/settings.h"
24+
#include "Firestore/core/src/util/hard_assert.h"
2225
#include "app/meta/move.h"
26+
#include "firebase/firestore/local_cache_settings.h"
2327

2428
#if !defined(__ANDROID__)
2529
#include "Firestore/core/src/util/executor.h"
@@ -51,12 +55,46 @@ void Settings::set_host(std::string host) { host_ = firebase::Move(host); }
5155

5256
void Settings::set_ssl_enabled(bool enabled) { ssl_enabled_ = enabled; }
5357

58+
std::shared_ptr<LocalCacheSettings> Settings::local_cache_settings() const {
59+
if (used_legacy_cache_settings_) {
60+
if (is_persistence_enabled()) {
61+
return std::make_shared<PersistentCacheSettings>(
62+
*PersistentCacheSettings::Create()
63+
.WithSizeBytes(cache_size_bytes())
64+
.settings_internal_);
65+
} else {
66+
return std::make_shared<MemoryCacheSettings>(
67+
*MemoryCacheSettings::Create().settings_internal_);
68+
}
69+
} else if (local_cache_settings_ != nullptr) {
70+
return local_cache_settings_;
71+
}
72+
73+
return std::make_shared<PersistentCacheSettings>(
74+
*PersistentCacheSettings::Create().settings_internal_);
75+
}
76+
77+
void Settings::set_local_cache_settings(const LocalCacheSettings& cache) {
78+
HARD_ASSERT(!used_legacy_cache_settings_, "");
79+
if (cache.kind() == api::LocalCacheSettings::Kind::kPersistent) {
80+
local_cache_settings_ = std::make_shared<PersistentCacheSettings>(
81+
*static_cast<const PersistentCacheSettings&>(cache).settings_internal_);
82+
} else {
83+
local_cache_settings_ = std::make_shared<MemoryCacheSettings>(
84+
*static_cast<const MemoryCacheSettings&>(cache).settings_internal_);
85+
}
86+
}
87+
5488
void Settings::set_persistence_enabled(bool enabled) {
89+
HARD_ASSERT(local_cache_settings() == nullptr, "");
5590
persistence_enabled_ = enabled;
91+
used_legacy_cache_settings_ = true;
5692
}
5793

5894
void Settings::set_cache_size_bytes(int64_t value) {
95+
HARD_ASSERT(local_cache_settings() == nullptr, "");
5996
cache_size_bytes_ = value;
97+
used_legacy_cache_settings_ = true;
6098
}
6199

62100
std::string Settings::ToString() const {

firestore/src/include/firebase/firestore/local_cache_settings.h

+31-7
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,66 @@
2121
#include <memory>
2222

2323
#include "Firestore/core/src/api/settings.h"
24+
#include "firestore/src/main/firestore_main.h"
2425

2526
namespace firebase {
2627
namespace firestore {
2728

29+
using CoreCacheSettings = api::LocalCacheSettings;
2830
using CoreMemorySettings = api::MemoryCacheSettings;
2931
using CorePersistentSettings = api::PersistentCacheSettings;
3032

33+
class Settings;
3134
class PersistentCacheSettingsInternal;
3235
class MemoryCacheSettingsInternal;
3336

3437
class LocalCacheSettings {
35-
virtual ~LocalCacheSettings() = 0;
38+
public:
39+
virtual api::LocalCacheSettings::Kind kind() const = 0;
40+
virtual ~LocalCacheSettings() = default;
41+
42+
private:
43+
friend class FirestoreInternal;
44+
45+
virtual const CoreCacheSettings& core_cache_settings() const = 0;
3646
};
3747

38-
class PersistentCacheSettings : public LocalCacheSettings {
48+
class PersistentCacheSettings final : public LocalCacheSettings {
3949
public:
4050
static PersistentCacheSettings Create();
51+
~PersistentCacheSettings();
52+
PersistentCacheSettings(const PersistentCacheSettingsInternal& other);
4153

4254
PersistentCacheSettings WithSizeBytes(int64_t size) const;
55+
const CoreCacheSettings& core_cache_settings() const override;
56+
api::LocalCacheSettings::Kind kind() const override {
57+
return api::LocalCacheSettings::Kind::kPersistent;
58+
}
4359

4460
private:
61+
friend class Settings;
62+
4563
PersistentCacheSettings();
46-
PersistentCacheSettings(const PersistentCacheSettingsInternal& other);
47-
~PersistentCacheSettings();
4864

4965
std::unique_ptr<PersistentCacheSettingsInternal> settings_internal_;
5066
};
5167

52-
class MemoryCacheSettings : public LocalCacheSettings {
68+
class MemoryCacheSettings final : public LocalCacheSettings {
5369
public:
5470
static MemoryCacheSettings Create();
71+
~MemoryCacheSettings();
72+
73+
const CoreCacheSettings& core_cache_settings() const override;
74+
api::LocalCacheSettings::Kind kind() const override {
75+
return api::LocalCacheSettings::Kind::kMemory;
76+
}
77+
78+
MemoryCacheSettings(const MemoryCacheSettingsInternal& other);
5579

5680
private:
81+
friend class Settings;
82+
5783
MemoryCacheSettings();
58-
MemoryCacheSettings(const MemoryCacheSettingsInternal& other);
59-
~MemoryCacheSettings();
6084

6185
std::unique_ptr<MemoryCacheSettingsInternal> settings_internal_;
6286
};

firestore/src/include/firebase/firestore/settings.h

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_SETTINGS_H_
1818
#define FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_SETTINGS_H_
1919

20+
#include "firebase/firestore/local_cache_settings.h"
2021
#if defined(__OBJC__)
2122
#include <dispatch/dispatch.h>
2223
#endif
@@ -47,6 +48,7 @@ class Executor;
4748
#endif
4849

4950
class FirestoreInternal;
51+
class LocalCacheSettings;
5052

5153
/** Settings used to configure a Firestore instance. */
5254
class Settings final {
@@ -136,6 +138,9 @@ class Settings final {
136138
*/
137139
void set_ssl_enabled(bool enabled);
138140

141+
std::shared_ptr<LocalCacheSettings> local_cache_settings() const;
142+
void set_local_cache_settings(const LocalCacheSettings& cache);
143+
139144
/**
140145
* Enables or disables local persistent storage.
141146
*
@@ -213,6 +218,10 @@ class Settings final {
213218

214219
std::string host_;
215220
bool ssl_enabled_ = true;
221+
222+
std::shared_ptr<LocalCacheSettings> local_cache_settings_ = nullptr;
223+
224+
bool used_legacy_cache_settings_ = false;
216225
bool persistence_enabled_ = true;
217226
int64_t cache_size_bytes_ = kDefaultCacheSizeBytes;
218227

firestore/src/main/firestore_main.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "Firestore/core/src/api/document_reference.h"
2525
#include "Firestore/core/src/api/query_core.h"
26+
#include "Firestore/core/src/api/settings.h"
2627
#include "Firestore/core/src/credentials/empty_credentials_provider.h"
2728
#include "Firestore/core/src/model/database_id.h"
2829
#include "Firestore/core/src/model/resource_path.h"
@@ -198,8 +199,13 @@ void FirestoreInternal::set_settings(Settings from) {
198199
api::Settings settings;
199200
settings.set_host(std::move(from.host()));
200201
settings.set_ssl_enabled(from.is_ssl_enabled());
201-
settings.set_persistence_enabled(from.is_persistence_enabled());
202-
settings.set_cache_size_bytes(from.cache_size_bytes());
202+
if (!from.used_legacy_cache_settings_) {
203+
settings.set_local_cache_settings(
204+
from.local_cache_settings()->core_cache_settings());
205+
} else {
206+
settings.set_persistence_enabled(from.is_persistence_enabled());
207+
settings.set_cache_size_bytes(from.cache_size_bytes());
208+
}
203209
firestore_core_->set_settings(settings);
204210

205211
std::unique_ptr<Executor> user_executor = from.CreateExecutor();

firestore/src/main/firestore_main.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Firestore;
4747
class ListenerRegistrationInternal;
4848
class Transaction;
4949
class WriteBatch;
50+
class Settings;
5051

5152
namespace util {
5253
class Executor;

firestore/src/main/local_cache_settings_main.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ using CorePersistentSettings = api::PersistentCacheSettings;
2929

3030
class LocalCacheSettingsInternal {};
3131

32-
class PersistentCacheSettingsInternal : public LocalCacheSettingsInternal {
32+
class PersistentCacheSettingsInternal final
33+
: public LocalCacheSettingsInternal {
3334
public:
3435
explicit PersistentCacheSettingsInternal(
3536
const CorePersistentSettings& core_settings)
@@ -43,7 +44,7 @@ class PersistentCacheSettingsInternal : public LocalCacheSettingsInternal {
4344
CorePersistentSettings settings_;
4445
};
4546

46-
class MemoryCacheSettingsInternal : public LocalCacheSettingsInternal {
47+
class MemoryCacheSettingsInternal final : public LocalCacheSettingsInternal {
4748
public:
4849
explicit MemoryCacheSettingsInternal(const CoreMemorySettings& core_settings)
4950
: settings_(std::move(core_settings)) {}

0 commit comments

Comments
 (0)