FRepoManager.m 4.5 KB

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