FRangedFilter.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "FNamedNode.h"
  19. #import "FQueryParams.h"
  20. #import "FIndexedFilter.h"
  21. #import "FQueryParams.h"
  22. #import "FEmptyNode.h"
  23. #import "FChildrenNode.h"
  24. #import "FIndexedNode.h"
  25. @interface FRangedFilter ()
  26. @property (nonatomic, strong, readwrite) id<FNodeFilter> indexedFilter;
  27. @property (nonatomic, strong, readwrite) id<FIndex> index;
  28. @property (nonatomic, strong, readwrite) FNamedNode *startPost;
  29. @property (nonatomic, strong, readwrite) FNamedNode *endPost;
  30. @end
  31. @implementation FRangedFilter
  32. - (id) initWithQueryParams:(FQueryParams *)params {
  33. self = [super init];
  34. if (self) {
  35. self.indexedFilter = [[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 andNode:self.startPost.node toOtherKey:key andNode:node] <= NSOrderedSame &&
  60. [self.index compareKey:key andNode:node toOtherKey:self.endPost.name andNode:self.endPost.node] <= NSOrderedSame);
  61. }
  62. - (FIndexedNode *)updateChildIn:(FIndexedNode *)oldSnap
  63. forChildKey:(NSString *)childKey
  64. newChild:(id<FNode>)newChildSnap
  65. affectedPath:(FPath *)affectedPath
  66. fromSource:(id<FCompleteChildSource>)source
  67. accumulator:(FChildChangeAccumulator *)optChangeAccumulator
  68. {
  69. if (![self matchesKey:childKey andNode:newChildSnap]) {
  70. newChildSnap = [FEmptyNode emptyNode];
  71. }
  72. return [self.indexedFilter updateChildIn:oldSnap
  73. forChildKey:childKey
  74. newChild:newChildSnap
  75. affectedPath:affectedPath
  76. fromSource:source
  77. accumulator:optChangeAccumulator];
  78. }
  79. - (FIndexedNode *) updateFullNode:(FIndexedNode *)oldSnap
  80. withNewNode:(FIndexedNode *)newSnap
  81. accumulator:(FChildChangeAccumulator *)optChangeAccumulator
  82. {
  83. __block FIndexedNode *filtered;
  84. if (newSnap.node.isLeafNode) {
  85. // Make sure we have a children node with the correct index, not a leaf node
  86. filtered = [FIndexedNode indexedNodeWithNode:[FEmptyNode emptyNode] index:self.index];
  87. } else {
  88. // Dont' support priorities on queries
  89. filtered = [newSnap updatePriority:[FEmptyNode emptyNode]];
  90. [newSnap.node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
  91. if (![self matchesKey:key andNode:node]) {
  92. filtered = [filtered updateChild:key withNewChild:[FEmptyNode emptyNode]];
  93. }
  94. }];
  95. }
  96. return [self.indexedFilter updateFullNode:oldSnap withNewNode:filtered accumulator:optChangeAccumulator];
  97. }
  98. - (FIndexedNode *) updatePriority:(id<FNode>)priority forNode:(FIndexedNode *)oldSnap
  99. {
  100. // Don't support priorities on queries
  101. return oldSnap;
  102. }
  103. - (BOOL) filtersNodes {
  104. return YES;
  105. }
  106. @end