FRangedFilter.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "FRangedFilter.h"
  17. #import "FChildChangeAccumulator.h"
  18. #import "FChildrenNode.h"
  19. #import "FEmptyNode.h"
  20. #import "FIndexedFilter.h"
  21. #import "FIndexedNode.h"
  22. #import "FNamedNode.h"
  23. #import "FQueryParams.h"
  24. @interface FRangedFilter ()
  25. @property(nonatomic, strong, readwrite) id<FNodeFilter> indexedFilter;
  26. @property(nonatomic, strong, readwrite) id<FIndex> index;
  27. @property(nonatomic, strong, readwrite) FNamedNode *startPost;
  28. @property(nonatomic, strong, readwrite) FNamedNode *endPost;
  29. @end
  30. @implementation FRangedFilter
  31. - (id)initWithQueryParams:(FQueryParams *)params {
  32. self = [super init];
  33. if (self) {
  34. self.indexedFilter =
  35. [[FIndexedFilter alloc] initWithIndex:params.index];
  36. self.index = params.index;
  37. self.startPost = [FRangedFilter startPostFromQueryParams:params];
  38. self.endPost = [FRangedFilter endPostFromQueryParams:params];
  39. }
  40. return self;
  41. }
  42. + (FNamedNode *)startPostFromQueryParams:(FQueryParams *)params {
  43. if ([params hasStart]) {
  44. NSString *startKey = params.indexStartKey;
  45. return [params.index makePost:params.indexStartValue name:startKey];
  46. } else {
  47. return params.index.minPost;
  48. }
  49. }
  50. + (FNamedNode *)endPostFromQueryParams:(FQueryParams *)params {
  51. if ([params hasEnd]) {
  52. NSString *endKey = params.indexEndKey;
  53. return [params.index makePost:params.indexEndValue name:endKey];
  54. } else {
  55. return params.index.maxPost;
  56. }
  57. }
  58. - (BOOL)matchesKey:(NSString *)key andNode:(id<FNode>)node {
  59. return ([self.index compareKey:self.startPost.name
  60. andNode:self.startPost.node
  61. toOtherKey:key
  62. andNode:node] <= NSOrderedSame &&
  63. [self.index compareKey:key
  64. andNode:node
  65. toOtherKey:self.endPost.name
  66. andNode:self.endPost.node] <= NSOrderedSame);
  67. }
  68. - (FIndexedNode *)updateChildIn:(FIndexedNode *)oldSnap
  69. forChildKey:(NSString *)childKey
  70. newChild:(id<FNode>)newChildSnap
  71. affectedPath:(FPath *)affectedPath
  72. fromSource:(id<FCompleteChildSource>)source
  73. accumulator:
  74. (FChildChangeAccumulator *)optChangeAccumulator {
  75. if (![self matchesKey:childKey andNode:newChildSnap]) {
  76. newChildSnap = [FEmptyNode emptyNode];
  77. }
  78. return [self.indexedFilter updateChildIn:oldSnap
  79. forChildKey:childKey
  80. newChild:newChildSnap
  81. affectedPath:affectedPath
  82. fromSource:source
  83. accumulator:optChangeAccumulator];
  84. }
  85. - (FIndexedNode *)updateFullNode:(FIndexedNode *)oldSnap
  86. withNewNode:(FIndexedNode *)newSnap
  87. accumulator:
  88. (FChildChangeAccumulator *)optChangeAccumulator {
  89. __block FIndexedNode *filtered;
  90. if (newSnap.node.isLeafNode) {
  91. // Make sure we have a children node with the correct index, not a leaf
  92. // node
  93. filtered = [FIndexedNode indexedNodeWithNode:[FEmptyNode emptyNode]
  94. index:self.index];
  95. } else {
  96. // Dont' support priorities on queries
  97. filtered = [newSnap updatePriority:[FEmptyNode emptyNode]];
  98. [newSnap.node enumerateChildrenUsingBlock:^(
  99. NSString *key, id<FNode> node, BOOL *stop) {
  100. if (![self matchesKey:key andNode:node]) {
  101. filtered = [filtered updateChild:key
  102. withNewChild:[FEmptyNode emptyNode]];
  103. }
  104. }];
  105. }
  106. return [self.indexedFilter updateFullNode:oldSnap
  107. withNewNode:filtered
  108. accumulator:optChangeAccumulator];
  109. }
  110. - (FIndexedNode *)updatePriority:(id<FNode>)priority
  111. forNode:(FIndexedNode *)oldSnap {
  112. // Don't support priorities on queries
  113. return oldSnap;
  114. }
  115. - (BOOL)filtersNodes {
  116. return YES;
  117. }
  118. @end