Files
firebase-ios-sdk/Firebase/Database/FValueIndex.m
T

112 lines
2.9 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FValueIndex.h"
2019-08-05 13:27:47 -07:00
#import "FMaxNode.h"
2017-05-15 12:27:07 -07:00
#import "FNamedNode.h"
#import "FSnapshotUtilities.h"
#import "FUtilities.h"
@implementation FValueIndex
2019-08-05 13:27:47 -07:00
- (NSComparisonResult)compareKey:(NSString *)key1
andNode:(id<FNode>)node1
toOtherKey:(NSString *)key2
andNode:(id<FNode>)node2 {
2017-05-15 12:27:07 -07:00
NSComparisonResult indexCmp = [node1 compare:node2];
if (indexCmp == NSOrderedSame) {
return [FUtilities compareKey:key1 toKey:key2];
} else {
return indexCmp;
}
}
2019-08-05 13:27:47 -07:00
- (NSComparisonResult)compareKey:(NSString *)key1
andNode:(id<FNode>)node1
toOtherKey:(NSString *)key2
andNode:(id<FNode>)node2
reverse:(BOOL)reverse {
2017-05-15 12:27:07 -07:00
if (reverse) {
2019-08-05 13:27:47 -07:00
return [self compareKey:key2
andNode:node2
toOtherKey:key1
andNode:node1];
2017-05-15 12:27:07 -07:00
} else {
2019-08-05 13:27:47 -07:00
return [self compareKey:key1
andNode:node1
toOtherKey:key2
andNode:node2];
2017-05-15 12:27:07 -07:00
}
}
2019-08-05 13:27:47 -07:00
- (NSComparisonResult)compareNamedNode:(FNamedNode *)namedNode1
toNamedNode:(FNamedNode *)namedNode2 {
return [self compareKey:namedNode1.name
andNode:namedNode1.node
toOtherKey:namedNode2.name
andNode:namedNode2.node];
2017-05-15 12:27:07 -07:00
}
- (BOOL)isDefinedOn:(id<FNode>)node {
return YES;
}
- (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
return ![oldNode isEqual:newNode];
}
- (FNamedNode *)minPost {
return FNamedNode.min;
}
- (FNamedNode *)maxPost {
return FNamedNode.max;
}
2019-08-05 13:27:47 -07:00
- (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
2017-05-15 12:27:07 -07:00
return [[FNamedNode alloc] initWithName:name andNode:indexValue];
}
- (NSString *)queryDefinition {
return @".value";
}
2019-08-05 13:27:47 -07:00
- (NSString *)description {
2017-05-15 12:27:07 -07:00
return @"FValueIndex";
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
2019-08-05 13:27:47 -07:00
- (BOOL)isEqual:(id)other {
2017-05-15 12:27:07 -07:00
// since we're a singleton.
return (other == self);
}
2019-08-05 13:27:47 -07:00
- (NSUInteger)hash {
2017-05-15 12:27:07 -07:00
return [@".value" hash];
}
2019-08-05 13:27:47 -07:00
+ (id<FIndex>)valueIndex {
2017-05-15 12:27:07 -07:00
static id<FIndex> valueIndex;
static dispatch_once_t once;
dispatch_once(&once, ^{
2019-08-05 13:27:47 -07:00
valueIndex = [[FValueIndex alloc] init];
2017-05-15 12:27:07 -07:00
});
return valueIndex;
}
@end