FIRLocalCacheSettings.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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::CacheSizeUnlimited &&
  64. size.longLongValue < Settings::MinimumCacheSizeBytes) {
  65. ThrowInvalidArgument("Cache size must be set to at least %s bytes",
  66. Settings::MinimumCacheSizeBytes);
  67. }
  68. self.internalSettings = PersistentCacheSettings{}.WithSizeBytes(size.longLongValue);
  69. return self;
  70. }
  71. @end
  72. @implementation FIRMemoryEagerGCSettings {
  73. MemoryEagerGcSettings _internalSettings;
  74. }
  75. - (BOOL)isEqual:(id)other {
  76. if (self == other) {
  77. return YES;
  78. } else if (![other isKindOfClass:[FIRMemoryEagerGCSettings class]]) {
  79. return NO;
  80. }
  81. FIRMemoryEagerGCSettings *otherSettings = (FIRMemoryEagerGCSettings *)other;
  82. return _internalSettings == otherSettings.internalSettings;
  83. }
  84. - (NSUInteger)hash {
  85. return _internalSettings.Hash();
  86. }
  87. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  88. FIRMemoryEagerGCSettings *copy = [[FIRMemoryEagerGCSettings alloc] init];
  89. copy.internalSettings = self.internalSettings;
  90. return copy;
  91. }
  92. - (void)setInternalSettings:(const MemoryEagerGcSettings &)settings {
  93. _internalSettings = settings;
  94. }
  95. - (const MemoryEagerGcSettings &)internalSettings {
  96. return _internalSettings;
  97. }
  98. - (instancetype)init {
  99. if (self = [super init]) {
  100. self.internalSettings = MemoryEagerGcSettings{};
  101. }
  102. return self;
  103. }
  104. @end
  105. @implementation FIRMemoryLRUGCSettings {
  106. MemoryLruGcSettings _internalSettings;
  107. }
  108. - (BOOL)isEqual:(id)other {
  109. if (self == other) {
  110. return YES;
  111. } else if (![other isKindOfClass:[FIRMemoryLRUGCSettings class]]) {
  112. return NO;
  113. }
  114. FIRMemoryLRUGCSettings *otherSettings = (FIRMemoryLRUGCSettings *)other;
  115. return _internalSettings == otherSettings.internalSettings;
  116. }
  117. - (NSUInteger)hash {
  118. return _internalSettings.Hash();
  119. }
  120. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  121. FIRMemoryLRUGCSettings *copy = [[FIRMemoryLRUGCSettings alloc] init];
  122. copy.internalSettings = self.internalSettings;
  123. return copy;
  124. }
  125. - (void)setInternalSettings:(const MemoryLruGcSettings &)settings {
  126. _internalSettings = settings;
  127. }
  128. - (const MemoryLruGcSettings &)internalSettings {
  129. return _internalSettings;
  130. }
  131. - (instancetype)init {
  132. if (self = [super init]) {
  133. self.internalSettings = MemoryLruGcSettings{};
  134. }
  135. return self;
  136. }
  137. - (instancetype)initWithSizeBytes:(NSNumber *)size {
  138. if (self = [super init]) {
  139. self.internalSettings = MemoryLruGcSettings{}.WithSizeBytes(size.longLongValue);
  140. }
  141. return self;
  142. }
  143. @end
  144. @implementation FIRMemoryCacheSettings {
  145. MemoryCacheSettings _internalSettings;
  146. }
  147. - (BOOL)isEqual:(id)other {
  148. if (self == other) {
  149. return YES;
  150. } else if (![other isKindOfClass:[FIRMemoryCacheSettings class]]) {
  151. return NO;
  152. }
  153. FIRMemoryCacheSettings *otherSettings = (FIRMemoryCacheSettings *)other;
  154. return _internalSettings == otherSettings.internalSettings;
  155. }
  156. - (NSUInteger)hash {
  157. return _internalSettings.Hash();
  158. }
  159. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  160. FIRMemoryCacheSettings *copy = [[FIRMemoryCacheSettings alloc] init];
  161. copy.internalSettings = self.internalSettings;
  162. return copy;
  163. }
  164. - (void)setInternalSettings:(const MemoryCacheSettings &)settings {
  165. _internalSettings = settings;
  166. }
  167. - (const MemoryCacheSettings &)internalSettings {
  168. return _internalSettings;
  169. }
  170. - (instancetype)init {
  171. if (self = [super init]) {
  172. self.internalSettings = MemoryCacheSettings{};
  173. }
  174. return self;
  175. }
  176. - (instancetype)initWithGarbageCollectorSettings:
  177. (id<FIRMemoryGarbageCollectorSettings, NSObject>)settings {
  178. if (self = [super init]) {
  179. if ([settings isKindOfClass:[FIRMemoryEagerGCSettings class]]) {
  180. FIRMemoryEagerGCSettings *casted = (FIRMemoryEagerGCSettings *)settings;
  181. self.internalSettings =
  182. MemoryCacheSettings{}.WithMemoryGarbageCollectorSettings(casted.internalSettings);
  183. } else if ([settings isKindOfClass:[FIRMemoryLRUGCSettings class]]) {
  184. FIRMemoryLRUGCSettings *casted = (FIRMemoryLRUGCSettings *)settings;
  185. self.internalSettings =
  186. MemoryCacheSettings{}.WithMemoryGarbageCollectorSettings(casted.internalSettings);
  187. }
  188. }
  189. return self;
  190. }
  191. @end
  192. NS_ASSUME_NONNULL_END