FKeyIndex.m 3.1 KB

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