FRepoManager.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <FirebaseCore/FIRLogger.h>
  17. #import "FRepoManager.h"
  18. #import "FRepo.h"
  19. #import "FIRDatabaseQuery_Private.h"
  20. #import "FAtomicNumber.h"
  21. #import "FIRDatabaseConfig_Private.h"
  22. #import "FIRDatabase_Private.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 FirebaseDatabase which
  37. * 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 = configs[config.sessionIdentifier];
  44. if (!repos || repos[repoInfo] == nil) {
  45. // Calling this should create the repo.
  46. [FIRDatabase createDatabaseForTests:repoInfo config:config];
  47. }
  48. return configs[config.sessionIdentifier][repoInfo];
  49. }
  50. }
  51. + (FRepo *) createRepo:(FRepoInfo *)repoInfo config:(FIRDatabaseConfig *)config database:(FIRDatabase *)database {
  52. [config freeze];
  53. FRepoDictionary *configs = [FRepoManager configs];
  54. @synchronized(configs) {
  55. NSMutableDictionary<FRepoInfo *, FRepo *> *repos =
  56. configs[config.sessionIdentifier];
  57. if (!repos) {
  58. repos = [NSMutableDictionary dictionary];
  59. configs[config.sessionIdentifier] = repos;
  60. }
  61. FRepo *repo = repos[repoInfo];
  62. if (repo == nil) {
  63. repo = [[FRepo alloc] initWithRepoInfo:repoInfo config:config database:database];
  64. repos[repoInfo] = repo;
  65. return repo;
  66. } else {
  67. [NSException raise:@"RepoExists" format:@"createRepo called for Repo that already exists."];
  68. return nil;
  69. }
  70. }
  71. }
  72. + (void) interrupt:(FIRDatabaseConfig *)config {
  73. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  74. FRepoDictionary *configs = [FRepoManager configs];
  75. NSMutableDictionary<FRepoInfo *, FRepo *> *repos = configs[config.sessionIdentifier];
  76. for (FRepo *repo in [repos allValues]) {
  77. [repo interrupt];
  78. }
  79. });
  80. }
  81. + (void) interruptAll {
  82. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  83. FRepoDictionary *configs = [FRepoManager configs];
  84. for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in [configs allValues]) {
  85. for (FRepo *repo in [repos allValues]) {
  86. [repo interrupt];
  87. }
  88. }
  89. });
  90. }
  91. + (void) resume:(FIRDatabaseConfig *)config {
  92. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  93. FRepoDictionary *configs = [FRepoManager configs];
  94. NSMutableDictionary<FRepoInfo *, FRepo *> *repos = configs[config.sessionIdentifier];
  95. for (FRepo *repo in [repos allValues]) {
  96. [repo resume];
  97. }
  98. });
  99. }
  100. + (void) resumeAll {
  101. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  102. FRepoDictionary *configs = [FRepoManager configs];
  103. for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in [configs allValues]) {
  104. for (FRepo *repo in [repos allValues]) {
  105. [repo resume];
  106. }
  107. }
  108. });
  109. }
  110. + (void)disposeRepos:(FIRDatabaseConfig *)config {
  111. // Do this synchronously to make sure we release our references to LevelDB before returning, allowing LevelDB
  112. // to close and release its exclusive locks.
  113. dispatch_sync([FIRDatabaseQuery sharedQueue], ^{
  114. FFLog(@"I-RDB040001", @"Disposing all repos for Config with name %@", config.sessionIdentifier);
  115. NSMutableDictionary *configs = [FRepoManager configs];
  116. for (FRepo* repo in [configs[config.sessionIdentifier] allValues]) {
  117. [repo dispose];
  118. }
  119. [configs removeObjectForKey:config.sessionIdentifier];
  120. });
  121. }
  122. @end