FPathIndex.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/FPathIndex.h"
  17. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  18. #import "FirebaseDatabase/Sources/FMaxNode.h"
  19. #import "FirebaseDatabase/Sources/FNamedNode.h"
  20. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  21. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  22. #import "FirebaseDatabase/Sources/Utilities/FUtilities.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
  32. format:@"Invalid path for PathIndex: %@", path];
  33. }
  34. _path = path;
  35. }
  36. return self;
  37. }
  38. - (NSComparisonResult)compareKey:(NSString *)key1
  39. andNode:(id<FNode>)node1
  40. toOtherKey:(NSString *)key2
  41. andNode:(id<FNode>)node2 {
  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. if (reverse) {
  57. return [self compareKey:key2
  58. andNode:node2
  59. toOtherKey:key1
  60. andNode:node1];
  61. } else {
  62. return [self compareKey:key1
  63. andNode:node1
  64. toOtherKey:key2
  65. andNode:node2];
  66. }
  67. }
  68. - (NSComparisonResult)compareNamedNode:(FNamedNode *)namedNode1
  69. toNamedNode:(FNamedNode *)namedNode2 {
  70. return [self compareKey:namedNode1.name
  71. andNode:namedNode1.node
  72. toOtherKey:namedNode2.name
  73. andNode:namedNode2.node];
  74. }
  75. - (BOOL)isDefinedOn:(id<FNode>)node {
  76. return ![node getChild:self.path].isEmpty;
  77. }
  78. - (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
  79. id<FNode> oldValue = [oldNode getChild:self.path];
  80. id<FNode> newValue = [newNode getChild:self.path];
  81. return [oldValue compare:newValue] != NSOrderedSame;
  82. }
  83. - (FNamedNode *)minPost {
  84. return FNamedNode.min;
  85. }
  86. - (FNamedNode *)maxPost {
  87. id<FNode> maxNode = [[FEmptyNode emptyNode] updateChild:self.path
  88. withNewChild:[FMaxNode maxNode]];
  89. return [[FNamedNode alloc] initWithName:[FUtilities maxName]
  90. andNode:maxNode];
  91. }
  92. - (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
  93. id<FNode> node = [[FEmptyNode emptyNode] updateChild:self.path
  94. withNewChild:indexValue];
  95. return [[FNamedNode alloc] initWithName:name andNode:node];
  96. }
  97. - (NSString *)queryDefinition {
  98. return [self.path wireFormat];
  99. }
  100. - (NSString *)description {
  101. return [NSString stringWithFormat:@"FPathIndex(%@)", self.path];
  102. }
  103. - (id)copyWithZone:(NSZone *)zone {
  104. // Safe since we're immutable.
  105. return self;
  106. }
  107. - (BOOL)isEqual:(id)other {
  108. if (![other isKindOfClass:[FPathIndex class]]) {
  109. return NO;
  110. }
  111. return ([self.path isEqual:((FPathIndex *)other).path]);
  112. }
  113. - (NSUInteger)hash {
  114. return [self.path hash];
  115. }
  116. @end