FIRLocalCacheSettings.mm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2023 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "FIRLocalCacheSettings.h"
  17. #include <memory>
  18. #import "FIRLocalCacheSettings+Internal.h"
  19. #include "absl/memory/memory.h"
  20. #include "Firestore/core/src/api/settings.h"
  21. #include "Firestore/core/src/util/exception.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. namespace api = firebase::firestore::api;
  24. using api::MemoryCacheSettings;
  25. using api::MemoryEagerGcSettings;
  26. using api::MemoryLruGcSettings;
  27. using api::PersistentCacheSettings;
  28. using api::Settings;
  29. using firebase::firestore::util::ThrowInvalidArgument;
  30. @implementation FIRPersistentCacheSettings {
  31. PersistentCacheSettings _internalSettings;
  32. }
  33. - (BOOL)isEqual:(id)other {
  34. if (self == other) {
  35. return YES;
  36. } else if (![other isKindOfClass:[FIRPersistentCacheSettings class]]) {
  37. return NO;
  38. }
  39. FIRPersistentCacheSettings *otherSettings = (FIRPersistentCacheSettings *)other;
  40. return _internalSettings == otherSettings.internalSettings;
  41. }
  42. - (NSUInteger)hash {
  43. return _internalSettings.Hash();
  44. }
  45. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  46. FIRPersistentCacheSettings *copy = [[FIRPersistentCacheSettings alloc] init];
  47. copy.internalSettings = self.internalSettings;
  48. return copy;
  49. }
  50. - (void)setInternalSettings:(const PersistentCacheSettings &)settings {
  51. _internalSettings = settings;
  52. }
  53. - (const PersistentCacheSettings &)internalSettings {
  54. return _internalSettings;
  55. }
  56. - (instancetype)init {
  57. self = [super init];
  58. self.internalSettings = PersistentCacheSettings{};
  59. return self;
  60. }
  61. - (instancetype)initWithSizeBytes:(NSNumber *)size {
  62. self = [super init];
  63. if (size.longLongValue < Settings::MinimumCacheSizeBytes) {
  64. ThrowInvalidArgument("Cache size must be set to at least %s bytes",
  65. Settings::MinimumCacheSizeBytes);
  66. }
  67. self.internalSettings = PersistentCacheSettings{}.WithSizeBytes(size.longLongValue);
  68. return self;
  69. }
  70. @end
  71. @implementation FIRMemoryEagerGCSettings {
  72. MemoryEagerGcSettings _internalSettings;
  73. }
  74. - (BOOL)isEqual:(id)other {
  75. if (self == other) {
  76. return YES;
  77. } else if (![other isKindOfClass:[FIRMemoryEagerGCSettings class]]) {
  78. return NO;
  79. }
  80. FIRMemoryEagerGCSettings *otherSettings = (FIRMemoryEagerGCSettings *)other;
  81. return _internalSettings == otherSettings.internalSettings;
  82. }
  83. - (NSUInteger)hash {
  84. return _internalSettings.Hash();
  85. }
  86. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  87. FIRMemoryEagerGCSettings *copy = [[FIRMemoryEagerGCSettings alloc] init];
  88. copy.internalSettings = self.internalSettings;
  89. return copy;
  90. }
  91. - (void)setInternalSettings:(const MemoryEagerGcSettings &)settings {
  92. _internalSettings = settings;
  93. }
  94. - (const MemoryEagerGcSettings &)internalSettings {
  95. return _internalSettings;
  96. }
  97. - (instancetype)init {
  98. if (self = [super init]) {
  99. self.internalSettings = MemoryEagerGcSettings{};
  100. }
  101. return self;
  102. }
  103. @end
  104. @implementation FIRMemoryLRUGCSettings {
  105. MemoryLruGcSettings _internalSettings;
  106. }
  107. - (BOOL)isEqual:(id)other {
  108. if (self == other) {
  109. return YES;
  110. } else if (![other isKindOfClass:[FIRMemoryLRUGCSettings class]]) {
  111. return NO;
  112. }
  113. FIRMemoryLRUGCSettings *otherSettings = (FIRMemoryLRUGCSettings *)other;
  114. return _internalSettings == otherSettings.internalSettings;
  115. }
  116. - (NSUInteger)hash {
  117. return _internalSettings.Hash();
  118. }
  119. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  120. FIRMemoryLRUGCSettings *copy = [[FIRMemoryLRUGCSettings alloc] init];
  121. copy.internalSettings = self.internalSettings;
  122. return copy;
  123. }
  124. - (void)setInternalSettings:(const MemoryLruGcSettings &)settings {
  125. _internalSettings = settings;
  126. }
  127. - (const MemoryLruGcSettings &)internalSettings {
  128. return _internalSettings;
  129. }
  130. - (instancetype)init {
  131. if (self = [super init]) {
  132. self.internalSettings = MemoryLruGcSettings{};
  133. }
  134. return self;
  135. }
  136. - (instancetype)initWithSizeBytes:(NSNumber *)size {
  137. if (self = [super init]) {
  138. self.internalSettings = MemoryLruGcSettings{}.WithSizeBytes(size.longLongValue);
  139. }
  140. return self;
  141. }
  142. @end
  143. @implementation FIRMemoryCacheSettings {
  144. MemoryCacheSettings _internalSettings;
  145. }
  146. - (BOOL)isEqual:(id)other {
  147. if (self == other) {
  148. return YES;
  149. } else if (![other isKindOfClass:[FIRMemoryCacheSettings class]]) {
  150. return NO;
  151. }
  152. FIRMemoryCacheSettings *otherSettings = (FIRMemoryCacheSettings *)other;
  153. return _internalSettings == otherSettings.internalSettings;
  154. }
  155. - (NSUInteger)hash {
  156. return _internalSettings.Hash();
  157. }
  158. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  159. FIRMemoryCacheSettings *copy = [[FIRMemoryCacheSettings alloc] init];
  160. copy.internalSettings = self.internalSettings;
  161. return copy;
  162. }
  163. - (void)setInternalSettings:(const MemoryCacheSettings &)settings {
  164. _internalSettings = settings;
  165. }
  166. - (const MemoryCacheSettings &)internalSettings {
  167. return _internalSettings;
  168. }
  169. - (instancetype)init {
  170. if (self = [super init]) {
  171. self.internalSettings = MemoryCacheSettings{};
  172. }
  173. return self;
  174. }
  175. - (instancetype)initWithGarbageCollectorSettings:
  176. (id<FIRMemoryGarbageCollectorSettings, NSObject>)settings {
  177. if (self = [super init]) {
  178. if ([settings isKindOfClass:[FIRMemoryEagerGCSettings class]]) {
  179. FIRMemoryEagerGCSettings *casted = (FIRMemoryEagerGCSettings *)settings;
  180. self.internalSettings =
  181. MemoryCacheSettings{}.WithMemoryGarbageCollectorSettings(casted.internalSettings);
  182. } else if ([settings isKindOfClass:[FIRMemoryLRUGCSettings class]]) {
  183. FIRMemoryLRUGCSettings *casted = (FIRMemoryLRUGCSettings *)settings;
  184. self.internalSettings =
  185. MemoryCacheSettings{}.WithMemoryGarbageCollectorSettings(casted.internalSettings);
  186. }
  187. }
  188. return self;
  189. }
  190. @end
  191. NS_ASSUME_NONNULL_END