1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* 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 "FRangedFilter.h"
#import "FChildChangeAccumulator.h"
#import "FNamedNode.h"
#import "FQueryParams.h"
#import "FIndexedFilter.h"
#import "FQueryParams.h"
#import "FEmptyNode.h"
#import "FChildrenNode.h"
#import "FIndexedNode.h"
@interface FRangedFilter ()
@property (nonatomic, strong, readwrite) id<FNodeFilter> indexedFilter;
@property (nonatomic, strong, readwrite) id<FIndex> index;
@property (nonatomic, strong, readwrite) FNamedNode *startPost;
@property (nonatomic, strong, readwrite) FNamedNode *endPost;
@end
@implementation FRangedFilter
- (id) initWithQueryParams:(FQueryParams *)params {
self = [super init];
if (self) {
self.indexedFilter = [[FIndexedFilter alloc] initWithIndex:params.index];
self.index = params.index;
self.startPost = [FRangedFilter startPostFromQueryParams:params];
self.endPost = [FRangedFilter endPostFromQueryParams:params];
}
return self;
}
+ (FNamedNode *) startPostFromQueryParams:(FQueryParams *)params {
if ([params hasStart]) {
NSString *startKey = params.indexStartKey;
return [params.index makePost:params.indexStartValue name:startKey];
} else {
return params.index.minPost;
}
}
+ (FNamedNode *) endPostFromQueryParams:(FQueryParams *)params {
if ([params hasEnd]) {
NSString *endKey = params.indexEndKey;
return [params.index makePost:params.indexEndValue name:endKey];
} else {
return params.index.maxPost;
}
}
- (BOOL) matchesKey:(NSString *)key andNode:(id<FNode>)node {
return ([self.index compareKey:self.startPost.name andNode:self.startPost.node toOtherKey:key andNode:node] <= NSOrderedSame &&
[self.index compareKey:key andNode:node toOtherKey:self.endPost.name andNode:self.endPost.node] <= NSOrderedSame);
}
- (FIndexedNode *)updateChildIn:(FIndexedNode *)oldSnap
forChildKey:(NSString *)childKey
newChild:(id<FNode>)newChildSnap
affectedPath:(FPath *)affectedPath
fromSource:(id<FCompleteChildSource>)source
accumulator:(FChildChangeAccumulator *)optChangeAccumulator
{
if (![self matchesKey:childKey andNode:newChildSnap]) {
newChildSnap = [FEmptyNode emptyNode];
}
return [self.indexedFilter updateChildIn:oldSnap
forChildKey:childKey
newChild:newChildSnap
affectedPath:affectedPath
fromSource:source
accumulator:optChangeAccumulator];
}
- (FIndexedNode *) updateFullNode:(FIndexedNode *)oldSnap
withNewNode:(FIndexedNode *)newSnap
accumulator:(FChildChangeAccumulator *)optChangeAccumulator
{
__block FIndexedNode *filtered;
if (newSnap.node.isLeafNode) {
// Make sure we have a children node with the correct index, not a leaf node
filtered = [FIndexedNode indexedNodeWithNode:[FEmptyNode emptyNode] index:self.index];
} else {
// Dont' support priorities on queries
filtered = [newSnap updatePriority:[FEmptyNode emptyNode]];
[newSnap.node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
if (![self matchesKey:key andNode:node]) {
filtered = [filtered updateChild:key withNewChild:[FEmptyNode emptyNode]];
}
}];
}
return [self.indexedFilter updateFullNode:oldSnap withNewNode:filtered accumulator:optChangeAccumulator];
}
- (FIndexedNode *) updatePriority:(id<FNode>)priority forNode:(FIndexedNode *)oldSnap
{
// Don't support priorities on queries
return oldSnap;
}
- (BOOL) filtersNodes {
return YES;
}
@end
|