FKeyIndex.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/FKeyIndex.h"
  17. #import "FirebaseDatabase/Sources/FNamedNode.h"
  18. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  19. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  20. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  21. @interface FKeyIndex ()
  22. @property(nonatomic, strong) FNamedNode *maxPost;
  23. @end
  24. @implementation FKeyIndex
  25. - (id)init {
  26. self = [super init];
  27. if (self) {
  28. self.maxPost = [[FNamedNode alloc] initWithName:[FUtilities maxName]
  29. andNode:[FEmptyNode emptyNode]];
  30. }
  31. return self;
  32. }
  33. - (NSComparisonResult)compareKey:(NSString *)key1
  34. andNode:(id<FNode>)node1
  35. toOtherKey:(NSString *)key2
  36. andNode:(id<FNode>)node2 {
  37. return [FUtilities compareKey:key1 toKey:key2];
  38. }
  39. - (NSComparisonResult)compareKey:(NSString *)key1
  40. andNode:(id<FNode>)node1
  41. toOtherKey:(NSString *)key2
  42. andNode:(id<FNode>)node2
  43. reverse:(BOOL)reverse {
  44. if (reverse) {
  45. return [self compareKey:key2
  46. andNode:node2
  47. toOtherKey:key1
  48. andNode:node1];
  49. } else {
  50. return [self compareKey:key1
  51. andNode:node1
  52. toOtherKey:key2
  53. andNode:node2];
  54. }
  55. }
  56. - (NSComparisonResult)compareNamedNode:(FNamedNode *)namedNode1
  57. toNamedNode:(FNamedNode *)namedNode2 {
  58. return [self compareKey:namedNode1.name
  59. andNode:namedNode1.node
  60. toOtherKey:namedNode2.name
  61. andNode:namedNode2.node];
  62. }
  63. - (BOOL)isDefinedOn:(id<FNode>)node {
  64. return YES;
  65. }
  66. - (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
  67. return NO; // The key for a node never changes.
  68. }
  69. - (FNamedNode *)minPost {
  70. return [FNamedNode min];
  71. }
  72. - (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
  73. NSString *key = indexValue.val;
  74. NSAssert([key isKindOfClass:[NSString class]],
  75. @"KeyIndex indexValue must always be a string.");
  76. // We just use empty node, but it'll never be compared, since our comparator
  77. // only looks at name.
  78. return [[FNamedNode alloc] initWithName:key andNode:[FEmptyNode emptyNode]];
  79. }
  80. - (NSString *)queryDefinition {
  81. return @".key";
  82. }
  83. - (NSString *)description {
  84. return @"FKeyIndex";
  85. }
  86. - (id)copyWithZone:(NSZone *)zone {
  87. return self;
  88. }
  89. - (BOOL)isEqual:(id)other {
  90. // since we're a singleton.
  91. return (other == self);
  92. }
  93. - (NSUInteger)hash {
  94. return [@".key" hash];
  95. }
  96. + (id<FIndex>)keyIndex {
  97. static id<FIndex> keyIndex;
  98. static dispatch_once_t once;
  99. dispatch_once(&once, ^{
  100. keyIndex = [[FKeyIndex alloc] init];
  101. });
  102. return keyIndex;
  103. }
  104. @end