FRepoManager.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2017 Google
  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. #import "FRepoManager.h"
  17. #import "FAtomicNumber.h"
  18. #import "FIRDatabaseConfig_Private.h"
  19. #import "FIRDatabaseQuery_Private.h"
  20. #import "FIRDatabase_Private.h"
  21. #import "FRepo.h"
  22. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  23. @implementation FRepoManager
  24. typedef NSMutableDictionary<NSString *,
  25. NSMutableDictionary<FRepoInfo *, FRepo *> *>
  26. FRepoDictionary;
  27. + (FRepoDictionary *)configs {
  28. static dispatch_once_t pred = 0;
  29. static FRepoDictionary *configs;
  30. dispatch_once(&pred, ^{
  31. configs = [NSMutableDictionary dictionary];
  32. });
  33. return configs;
  34. }
  35. /**
  36. * Used for legacy unit tests. The public API should go through
  37. * FirebaseDatabase which calls createRepo.
  38. */
  39. + (FRepo *)getRepo:(FRepoInfo *)repoInfo config:(FIRDatabaseConfig *)config {
  40. [config freeze];
  41. FRepoDictionary *configs = [FRepoManager configs];
  42. @synchronized(configs) {
  43. NSMutableDictionary<FRepoInfo *, FRepo *> *repos =
  44. configs[config.sessionIdentifier];
  45. if (!repos || repos[repoInfo] == nil) {
  46. // Calling this should create the repo.
  47. [FIRDatabase createDatabaseForTests:repoInfo config:config];
  48. }
  49. return configs[config.sessionIdentifier][repoInfo];
  50. }
  51. }
  52. + (FRepo *)createRepo:(FRepoInfo *)repoInfo
  53. config:(FIRDatabaseConfig *)config
  54. database:(FIRDatabase *)database {
  55. [config freeze];
  56. FRepoDictionary *configs = [FRepoManager configs];
  57. @synchronized(configs) {
  58. NSMutableDictionary<FRepoInfo *, FRepo *> *repos =
  59. configs[config.sessionIdentifier];
  60. if (!repos) {
  61. repos = [NSMutableDictionary dictionary];
  62. configs[config.sessionIdentifier] = repos;
  63. }
  64. FRepo *repo = repos[repoInfo];
  65. if (repo == nil) {
  66. repo = [[FRepo alloc] initWithRepoInfo:repoInfo
  67. config:config
  68. database:database];
  69. repos[repoInfo] = repo;
  70. return repo;
  71. } else {
  72. [NSException
  73. raise:@"RepoExists"
  74. format:@"createRepo called for Repo that already exists."];
  75. return nil;
  76. }
  77. }
  78. }
  79. + (void)interrupt:(FIRDatabaseConfig *)config {
  80. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  81. FRepoDictionary *configs = [FRepoManager configs];
  82. NSMutableDictionary<FRepoInfo *, FRepo *> *repos =
  83. configs[config.sessionIdentifier];
  84. for (FRepo *repo in [repos allValues]) {
  85. [repo interrupt];
  86. }
  87. });
  88. }
  89. + (void)interruptAll {
  90. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  91. FRepoDictionary *configs = [FRepoManager configs];
  92. for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in
  93. [configs allValues]) {
  94. for (FRepo *repo in [repos allValues]) {
  95. [repo interrupt];
  96. }
  97. }
  98. });
  99. }
  100. + (void)resume:(FIRDatabaseConfig *)config {
  101. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  102. FRepoDictionary *configs = [FRepoManager configs];
  103. NSMutableDictionary<FRepoInfo *, FRepo *> *repos =
  104. configs[config.sessionIdentifier];
  105. for (FRepo *repo in [repos allValues]) {
  106. [repo resume];
  107. }
  108. });
  109. }
  110. + (void)resumeAll {
  111. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  112. FRepoDictionary *configs = [FRepoManager configs];
  113. for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in
  114. [configs allValues]) {
  115. for (FRepo *repo in [repos allValues]) {
  116. [repo resume];
  117. }
  118. }
  119. });
  120. }
  121. + (void)disposeRepos:(FIRDatabaseConfig *)config {
  122. // Do this synchronously to make sure we release our references to LevelDB
  123. // before returning, allowing LevelDB to close and release its exclusive
  124. // locks.
  125. dispatch_sync([FIRDatabaseQuery sharedQueue], ^{
  126. FFLog(@"I-RDB040001", @"Disposing all repos for Config with name %@",
  127. config.sessionIdentifier);
  128. NSMutableDictionary *configs = [FRepoManager configs];
  129. for (FRepo *repo in [configs[config.sessionIdentifier] allValues]) {
  130. [repo dispose];
  131. }
  132. [configs removeObjectForKey:config.sessionIdentifier];
  133. });
  134. }
  135. @end