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

123 lines
3.3 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 "FKeyIndex.h"
2019-08-05 13:27:47 -07:00
#import "FEmptyNode.h"
2017-05-15 12:27:07 -07:00
#import "FNamedNode.h"
#import "FSnapshotUtilities.h"
#import "FUtilities.h"
@interface FKeyIndex ()
2019-08-05 13:27:47 -07:00
@property(nonatomic, strong) FNamedNode *maxPost;
2017-05-15 12:27:07 -07:00
@end
@implementation FKeyIndex
- (id)init {
self = [super init];
if (self) {
2019-08-05 13:27:47 -07:00
self.maxPost = [[FNamedNode alloc] initWithName:[FUtilities maxName]
andNode:[FEmptyNode emptyNode]];
2017-05-15 12:27:07 -07:00
}
return self;
}
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
return [FUtilities compareKey:key1 toKey:key2];
}
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
}
2019-08-05 13:27:47 -07:00
- (BOOL)isDefinedOn:(id<FNode>)node {
2017-05-15 12:27:07 -07:00
return YES;
}
2019-08-05 13:27:47 -07:00
- (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
return NO; // The key for a node never changes.
2017-05-15 12:27:07 -07:00
}
- (FNamedNode *)minPost {
return [FNamedNode min];
}
2019-08-05 13:27:47 -07:00
- (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
2017-05-15 12:27:07 -07:00
NSString *key = indexValue.val;
2019-08-05 13:27:47 -07:00
NSAssert([key isKindOfClass:[NSString class]],
@"KeyIndex indexValue must always be a string.");
// We just use empty node, but it'll never be compared, since our comparator
// only looks at name.
2017-05-15 12:27:07 -07:00
return [[FNamedNode alloc] initWithName:key andNode:[FEmptyNode emptyNode]];
}
2019-08-05 13:27:47 -07:00
- (NSString *)queryDefinition {
2017-05-15 12:27:07 -07:00
return @".key";
}
2019-08-05 13:27:47 -07:00
- (NSString *)description {
2017-05-15 12:27:07 -07:00
return @"FKeyIndex";
}
- (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 [@".key" hash];
}
2019-08-05 13:27:47 -07:00
+ (id<FIndex>)keyIndex {
2017-05-15 12:27:07 -07:00
static id<FIndex> keyIndex;
static dispatch_once_t once;
dispatch_once(&once, ^{
2019-08-05 13:27:47 -07:00
keyIndex = [[FKeyIndex alloc] init];
2017-05-15 12:27:07 -07:00
});
return keyIndex;
}
@end