FPathIndex.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "FPathIndex.h"
  17. #import "FUtilities.h"
  18. #import "FMaxNode.h"
  19. #import "FEmptyNode.h"
  20. #import "FSnapshotUtilities.h"
  21. #import "FNamedNode.h"
  22. #import "FPath.h"
  23. @interface FPathIndex ()
  24. @property (nonatomic, strong) FPath *path;
  25. @end
  26. @implementation FPathIndex
  27. - (id) initWithPath:(FPath *)path {
  28. self = [super init];
  29. if (self) {
  30. if (path.isEmpty || [path.getFront isEqualToString:@".priority"]) {
  31. [NSException raise:NSInvalidArgumentException format:@"Invalid path for PathIndex: %@", path];
  32. }
  33. _path = path;
  34. }
  35. return self;
  36. }
  37. - (NSComparisonResult) compareKey:(NSString *)key1
  38. andNode:(id<FNode>)node1
  39. toOtherKey:(NSString *)key2
  40. andNode:(id<FNode>)node2
  41. {
  42. id<FNode> child1 = [node1 getChild:self.path];
  43. id<FNode> child2 = [node2 getChild:self.path];
  44. NSComparisonResult indexCmp = [child1 compare:child2];
  45. if (indexCmp == NSOrderedSame) {
  46. return [FUtilities compareKey:key1 toKey:key2];
  47. } else {
  48. return indexCmp;
  49. }
  50. }
  51. - (NSComparisonResult) compareKey:(NSString *)key1
  52. andNode:(id<FNode>)node1
  53. toOtherKey:(NSString *)key2
  54. andNode:(id<FNode>)node2
  55. reverse:(BOOL)reverse
  56. {
  57. if (reverse) {
  58. return [self compareKey:key2 andNode:node2 toOtherKey:key1 andNode:node1];
  59. } else {
  60. return [self compareKey:key1 andNode:node1 toOtherKey:key2 andNode:node2];
  61. }
  62. }
  63. - (NSComparisonResult) compareNamedNode:(FNamedNode *)namedNode1 toNamedNode:(FNamedNode *)namedNode2
  64. {
  65. return [self compareKey:namedNode1.name andNode:namedNode1.node toOtherKey:namedNode2.name andNode:namedNode2.node];
  66. }
  67. - (BOOL)isDefinedOn:(id <FNode>)node {
  68. return ![node getChild:self.path].isEmpty;
  69. }
  70. - (BOOL)indexedValueChangedBetween:(id <FNode>)oldNode and:(id <FNode>)newNode {
  71. id<FNode> oldValue = [oldNode getChild:self.path];
  72. id<FNode> newValue = [newNode getChild:self.path];
  73. return [oldValue compare:newValue] != NSOrderedSame;
  74. }
  75. - (FNamedNode *)minPost {
  76. return FNamedNode.min;
  77. }
  78. - (FNamedNode *)maxPost {
  79. id<FNode> maxNode = [[FEmptyNode emptyNode] updateChild:self.path
  80. withNewChild:[FMaxNode maxNode]];
  81. return [[FNamedNode alloc] initWithName:[FUtilities maxName] andNode:maxNode];
  82. }
  83. - (FNamedNode*)makePost:(id<FNode>)indexValue name:(NSString*)name {
  84. id<FNode> node = [[FEmptyNode emptyNode] updateChild:self.path withNewChild:indexValue];
  85. return [[FNamedNode alloc] initWithName:name andNode:node];
  86. }
  87. - (NSString *)queryDefinition {
  88. return [self.path wireFormat];
  89. }
  90. - (NSString *)description {
  91. return [NSString stringWithFormat:@"FPathIndex(%@)", self.path];
  92. }
  93. - (id)copyWithZone:(NSZone *)zone {
  94. // Safe since we're immutable.
  95. return self;
  96. }
  97. - (BOOL) isEqual:(id)other {
  98. if (![other isKindOfClass:[FPathIndex class]]) {
  99. return NO;
  100. }
  101. return ([self.path isEqual:((FPathIndex*)other).path]);
  102. }
  103. - (NSUInteger) hash {
  104. return [self.path hash];
  105. }
  106. @end