FRepoManager.m 4.6 KB

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