FRangeMerge.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Core/FRangeMerge.h"
  17. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  18. @interface FRangeMerge ()
  19. @property(nonatomic, strong) FPath *optExclusiveStart;
  20. @property(nonatomic, strong) FPath *optInclusiveEnd;
  21. @property(nonatomic, strong) id<FNode> updates;
  22. @end
  23. @implementation FRangeMerge
  24. - (instancetype)initWithStart:(FPath *)start
  25. end:(FPath *)end
  26. updates:(id<FNode>)updates {
  27. self = [super init];
  28. if (self != nil) {
  29. self->_optExclusiveStart = start;
  30. self->_optInclusiveEnd = end;
  31. self->_updates = updates;
  32. }
  33. return self;
  34. }
  35. - (id<FNode>)applyToNode:(id<FNode>)node {
  36. return [self updateRangeInNode:[FPath empty]
  37. node:node
  38. updates:self.updates];
  39. }
  40. - (id<FNode>)updateRangeInNode:(FPath *)currentPath
  41. node:(id<FNode>)node
  42. updates:(id<FNode>)updates {
  43. NSComparisonResult startComparison =
  44. (self.optExclusiveStart == nil)
  45. ? NSOrderedDescending
  46. : [currentPath compare:self.optExclusiveStart];
  47. NSComparisonResult endComparison =
  48. (self.optInclusiveEnd == nil)
  49. ? NSOrderedAscending
  50. : [currentPath compare:self.optInclusiveEnd];
  51. BOOL startInNode = self.optExclusiveStart != nil &&
  52. [currentPath contains:self.optExclusiveStart];
  53. BOOL endInNode = self.optInclusiveEnd != nil &&
  54. [currentPath contains:self.optInclusiveEnd];
  55. if (startComparison == NSOrderedDescending &&
  56. endComparison == NSOrderedAscending && !endInNode) {
  57. // child is completely contained
  58. return updates;
  59. } else if (startComparison == NSOrderedDescending && endInNode &&
  60. [updates isLeafNode]) {
  61. return updates;
  62. } else if (startComparison == NSOrderedDescending &&
  63. endComparison == NSOrderedSame) {
  64. NSAssert(endInNode, @"End not in node");
  65. NSAssert(![updates isLeafNode], @"Found leaf node update, this case "
  66. @"should have been handled above.");
  67. if ([node isLeafNode]) {
  68. // Update node was not a leaf node, so we can delete it
  69. return [FEmptyNode emptyNode];
  70. } else {
  71. // Unaffected by range, ignore
  72. return node;
  73. }
  74. } else if (startInNode || endInNode) {
  75. // There is a partial update we need to do, so collect all relevant
  76. // children
  77. NSMutableSet *allChildren = [NSMutableSet set];
  78. [node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node,
  79. BOOL *stop) {
  80. [allChildren addObject:key];
  81. }];
  82. [updates enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node,
  83. BOOL *stop) {
  84. [allChildren addObject:key];
  85. }];
  86. __block id<FNode> newNode = node;
  87. void (^action)(id, BOOL *) = ^void(NSString *key, BOOL *stop) {
  88. id<FNode> currentChild = [node getImmediateChild:key];
  89. id<FNode> updatedChild =
  90. [self updateRangeInNode:[currentPath childFromString:key]
  91. node:currentChild
  92. updates:[updates getImmediateChild:key]];
  93. // Only need to update if the node changed
  94. if (updatedChild != currentChild) {
  95. newNode = [newNode updateImmediateChild:key
  96. withNewChild:updatedChild];
  97. }
  98. };
  99. [allChildren enumerateObjectsUsingBlock:action];
  100. // Add priority last, so the node is not empty when applying
  101. if (!updates.getPriority.isEmpty || !node.getPriority.isEmpty) {
  102. BOOL stop = NO;
  103. action(@".priority", &stop);
  104. }
  105. return newNode;
  106. } else {
  107. // Unaffected by this range
  108. NSAssert(endComparison == NSOrderedDescending ||
  109. startComparison <= NSOrderedSame,
  110. @"Invalid range for update");
  111. return node;
  112. }
  113. }
  114. - (NSString *)description {
  115. return [NSString stringWithFormat:@"RangeMerge (optExclusiveStart = %@, "
  116. @"optExclusiveEng = %@, updates = %@)",
  117. self.optExclusiveStart,
  118. self.optInclusiveEnd, self.updates];
  119. }
  120. @end