FPruneForest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "FirebaseDatabase/Sources/Persistence/FPruneForest.h"
  17. #import "FirebaseDatabase/Sources/Core/Utilities/FImmutableTree.h"
  18. @interface FPruneForest ()
  19. @property(nonatomic, strong) FImmutableTree *pruneForest;
  20. @end
  21. @implementation FPruneForest
  22. static BOOL (^kFPrunePredicate)(id) = ^BOOL(NSNumber *pruneValue) {
  23. return [pruneValue boolValue];
  24. };
  25. static BOOL (^kFKeepPredicate)(id) = ^BOOL(NSNumber *pruneValue) {
  26. return ![pruneValue boolValue];
  27. };
  28. + (FImmutableTree *)pruneTree {
  29. static dispatch_once_t onceToken;
  30. static FImmutableTree *pruneTree;
  31. dispatch_once(&onceToken, ^{
  32. pruneTree = [[FImmutableTree alloc] initWithValue:@YES];
  33. });
  34. return pruneTree;
  35. }
  36. + (FImmutableTree *)keepTree {
  37. static dispatch_once_t onceToken;
  38. static FImmutableTree *keepTree;
  39. dispatch_once(&onceToken, ^{
  40. keepTree = [[FImmutableTree alloc] initWithValue:@NO];
  41. });
  42. return keepTree;
  43. }
  44. - (id)initWithForest:(FImmutableTree *)tree {
  45. self = [super init];
  46. if (self != nil) {
  47. self->_pruneForest = tree;
  48. }
  49. return self;
  50. }
  51. + (FPruneForest *)empty {
  52. static dispatch_once_t onceToken;
  53. static FPruneForest *forest;
  54. dispatch_once(&onceToken, ^{
  55. forest = [[FPruneForest alloc] initWithForest:[FImmutableTree empty]];
  56. });
  57. return forest;
  58. }
  59. - (BOOL)prunesAnything {
  60. return [self.pruneForest containsValueMatching:kFPrunePredicate];
  61. }
  62. - (BOOL)shouldPruneUnkeptDescendantsAtPath:(FPath *)path {
  63. NSNumber *shouldPrune = [self.pruneForest leafMostValueOnPath:path];
  64. return shouldPrune != nil && [shouldPrune boolValue];
  65. }
  66. - (BOOL)shouldKeepPath:(FPath *)path {
  67. NSNumber *shouldPrune = [self.pruneForest leafMostValueOnPath:path];
  68. return shouldPrune != nil && ![shouldPrune boolValue];
  69. }
  70. - (BOOL)affectsPath:(FPath *)path {
  71. return [self.pruneForest rootMostValueOnPath:path] != nil ||
  72. ![[self.pruneForest subtreeAtPath:path] isEmpty];
  73. }
  74. - (FPruneForest *)child:(NSString *)childKey {
  75. FImmutableTree *childPruneForest = [self.pruneForest.children get:childKey];
  76. if (childPruneForest == nil) {
  77. if (self.pruneForest.value != nil) {
  78. childPruneForest = [self.pruneForest.value boolValue]
  79. ? [FPruneForest pruneTree]
  80. : [FPruneForest keepTree];
  81. } else {
  82. childPruneForest = [FImmutableTree empty];
  83. }
  84. } else {
  85. if (childPruneForest.value == nil && self.pruneForest.value != nil) {
  86. childPruneForest = [childPruneForest setValue:self.pruneForest.value
  87. atPath:[FPath empty]];
  88. }
  89. }
  90. return [[FPruneForest alloc] initWithForest:childPruneForest];
  91. }
  92. - (FPruneForest *)childAtPath:(FPath *)path {
  93. if (path.isEmpty) {
  94. return self;
  95. } else {
  96. return [[self child:path.getFront] childAtPath:[path popFront]];
  97. }
  98. }
  99. - (FPruneForest *)prunePath:(FPath *)path {
  100. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  101. [NSException raise:NSInvalidArgumentException
  102. format:@"Can't prune path that was kept previously!"];
  103. }
  104. if ([self.pruneForest rootMostValueOnPath:path matching:kFPrunePredicate]) {
  105. // This path will already be pruned
  106. return self;
  107. } else {
  108. FImmutableTree *newPruneForest =
  109. [self.pruneForest setTree:[FPruneForest pruneTree] atPath:path];
  110. return [[FPruneForest alloc] initWithForest:newPruneForest];
  111. }
  112. }
  113. - (FPruneForest *)keepPath:(FPath *)path {
  114. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  115. // This path will already be kept
  116. return self;
  117. } else {
  118. FImmutableTree *newPruneForest =
  119. [self.pruneForest setTree:[FPruneForest keepTree] atPath:path];
  120. return [[FPruneForest alloc] initWithForest:newPruneForest];
  121. }
  122. }
  123. - (FPruneForest *)keepAll:(NSSet *)children atPath:(FPath *)path {
  124. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  125. // This path will already be kept
  126. return self;
  127. } else {
  128. return [self setPruneValue:[FPruneForest keepTree]
  129. forAll:children
  130. atPath:path];
  131. }
  132. }
  133. - (FPruneForest *)pruneAll:(NSSet *)children atPath:(FPath *)path {
  134. if ([self.pruneForest rootMostValueOnPath:path matching:kFKeepPredicate]) {
  135. [NSException raise:NSInvalidArgumentException
  136. format:@"Can't prune path that was kept previously!"];
  137. }
  138. if ([self.pruneForest rootMostValueOnPath:path matching:kFPrunePredicate]) {
  139. // This path will already be pruned
  140. return self;
  141. } else {
  142. return [self setPruneValue:[FPruneForest pruneTree]
  143. forAll:children
  144. atPath:path];
  145. }
  146. }
  147. - (FPruneForest *)setPruneValue:(FImmutableTree *)pruneValue
  148. forAll:(NSSet *)children
  149. atPath:(FPath *)path {
  150. FImmutableTree *subtree = [self.pruneForest subtreeAtPath:path];
  151. __block FImmutableSortedDictionary *childrenDictionary = subtree.children;
  152. [children enumerateObjectsUsingBlock:^(NSString *childKey, BOOL *stop) {
  153. childrenDictionary = [childrenDictionary insertKey:childKey
  154. withValue:pruneValue];
  155. }];
  156. FImmutableTree *newSubtree =
  157. [[FImmutableTree alloc] initWithValue:subtree.value
  158. children:childrenDictionary];
  159. return [[FPruneForest alloc]
  160. initWithForest:[self.pruneForest setTree:newSubtree atPath:path]];
  161. }
  162. - (void)enumerateKeptNodesUsingBlock:(void (^)(FPath *))block {
  163. [self.pruneForest forEach:^(FPath *path, id value) {
  164. if (value != nil && ![value boolValue]) {
  165. block(path);
  166. }
  167. }];
  168. }
  169. @end