FValueIndex.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/FValueIndex.h"
  17. #import "FirebaseDatabase/Sources/FMaxNode.h"
  18. #import "FirebaseDatabase/Sources/FNamedNode.h"
  19. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  20. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  21. @implementation FValueIndex
  22. - (NSComparisonResult)compareKey:(NSString *)key1
  23. andNode:(id<FNode>)node1
  24. toOtherKey:(NSString *)key2
  25. andNode:(id<FNode>)node2 {
  26. NSComparisonResult indexCmp = [node1 compare:node2];
  27. if (indexCmp == NSOrderedSame) {
  28. return [FUtilities compareKey:key1 toKey:key2];
  29. } else {
  30. return indexCmp;
  31. }
  32. }
  33. - (NSComparisonResult)compareKey:(NSString *)key1
  34. andNode:(id<FNode>)node1
  35. toOtherKey:(NSString *)key2
  36. andNode:(id<FNode>)node2
  37. reverse:(BOOL)reverse {
  38. if (reverse) {
  39. return [self compareKey:key2
  40. andNode:node2
  41. toOtherKey:key1
  42. andNode:node1];
  43. } else {
  44. return [self compareKey:key1
  45. andNode:node1
  46. toOtherKey:key2
  47. andNode:node2];
  48. }
  49. }
  50. - (NSComparisonResult)compareNamedNode:(FNamedNode *)namedNode1
  51. toNamedNode:(FNamedNode *)namedNode2 {
  52. return [self compareKey:namedNode1.name
  53. andNode:namedNode1.node
  54. toOtherKey:namedNode2.name
  55. andNode:namedNode2.node];
  56. }
  57. - (BOOL)isDefinedOn:(id<FNode>)node {
  58. return YES;
  59. }
  60. - (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
  61. return ![oldNode isEqual:newNode];
  62. }
  63. - (FNamedNode *)minPost {
  64. return FNamedNode.min;
  65. }
  66. - (FNamedNode *)maxPost {
  67. return FNamedNode.max;
  68. }
  69. - (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
  70. return [[FNamedNode alloc] initWithName:name andNode:indexValue];
  71. }
  72. - (NSString *)queryDefinition {
  73. return @".value";
  74. }
  75. - (NSString *)description {
  76. return @"FValueIndex";
  77. }
  78. - (id)copyWithZone:(NSZone *)zone {
  79. return self;
  80. }
  81. - (BOOL)isEqual:(id)other {
  82. // since we're a singleton.
  83. return (other == self);
  84. }
  85. - (NSUInteger)hash {
  86. return [@".value" hash];
  87. }
  88. + (id<FIndex>)valueIndex {
  89. static id<FIndex> valueIndex;
  90. static dispatch_once_t once;
  91. dispatch_once(&once, ^{
  92. valueIndex = [[FValueIndex alloc] init];
  93. });
  94. return valueIndex;
  95. }
  96. @end