aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/Core/FQueryParams.m
blob: 792035802c6b009b3f7c4fa0ee7b9b0c865cda41 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * 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 "FQueryParams.h"
#import "FValidation.h"
#import "FConstants.h"
#import "FIndex.h"
#import "FPriorityIndex.h"
#import "FUtilities.h"
#import "FNodeFilter.h"
#import "FIndexedFilter.h"
#import "FLimitedFilter.h"
#import "FRangedFilter.h"
#import "FNode.h"
#import "FSnapshotUtilities.h"

@interface FQueryParams ()

@property (nonatomic, readwrite) BOOL limitSet;
@property (nonatomic, readwrite) NSInteger limit;

@property (nonatomic, strong, readwrite) NSString *viewFrom;
/**
* indexStartValue is anything you can store as a priority / value.
*/
@property (nonatomic, strong, readwrite) id<FNode> indexStartValue;
@property (nonatomic, strong, readwrite) NSString *indexStartKey;
/**
* indexStartValue is anything you can store as a priority / value.
*/
@property (nonatomic, strong, readwrite) id<FNode> indexEndValue;
@property (nonatomic, strong, readwrite) NSString *indexEndKey;

@property (nonatomic, strong, readwrite) id<FIndex> index;

@end

@implementation FQueryParams

+ (FQueryParams *) defaultInstance {
    static FQueryParams *defaultParams = nil;
    static dispatch_once_t defaultParamsToken;
    dispatch_once(&defaultParamsToken, ^{
        defaultParams = [[FQueryParams alloc] init];
    });
    return defaultParams;
}


- (id)init {
    self = [super init];
    if (self) {
        self->_limitSet = NO;
        self->_limit = 0;

        self->_viewFrom = nil;
        self->_indexStartValue = nil;
        self->_indexStartKey = nil;
        self->_indexEndValue = nil;
        self->_indexEndKey = nil;

        self->_index = [FPriorityIndex priorityIndex];
    }
    return self;
}

/**
* Only valid if hasStart is true
*/
- (id) indexStartValue {
    NSAssert([self hasStart], @"Only valid if start has been set");
    return _indexStartValue;
}

/**
* Only valid if hasStart is true.
* @return The starting key name for the range defined by these query parameters
*/
- (NSString *) indexStartKey {
    NSAssert([self hasStart], @"Only valid if start has been set");
    if (_indexStartKey == nil) {
        return [FUtilities minName];
    } else {
        return _indexStartKey;
    }
}

/**
* Only valid if hasEnd is true.
*/
- (id) indexEndValue {
    NSAssert([self hasEnd], @"Only valid if end has been set");
    return _indexEndValue;
}

/**
* Only valid if hasEnd is true.
* @return The end key name for the range defined by these query parameters
*/
- (NSString *) indexEndKey {
    NSAssert([self hasEnd], @"Only valid if end has been set");
    if (_indexEndKey == nil) {
        return [FUtilities maxName];
    } else {
        return _indexEndKey;
    }
}

/**
* @return true if a limit has been set and has been explicitly anchored
*/
- (BOOL) hasAnchoredLimit {
    return self.limitSet && self.viewFrom != nil;
}

/**
* Only valid to call if limitSet returns true
*/
- (NSInteger) limit {
    NSAssert(self.limitSet, @"Only valid if limit has been set");
    return _limit;
}

- (BOOL)hasStart {
    return self->_indexStartValue != nil;
}

- (BOOL)hasEnd {
    return self->_indexEndValue != nil;
}

- (id) copyWithZone:(NSZone *)zone {
    // Immutable
    return self;
}

- (id) mutableCopy {
    FQueryParams* other = [[[self class] alloc] init];
    // Maybe need to do extra copying here
    other->_limitSet = _limitSet;
    other->_limit = _limit;
    other->_indexStartValue = _indexStartValue;
    other->_indexStartKey = _indexStartKey;
    other->_indexEndValue = _indexEndValue;
    other->_indexEndKey = _indexEndKey;
    other->_viewFrom = _viewFrom;
    other->_index = _index;
    return other;
}

- (FQueryParams *) limitTo:(NSInteger)newLimit {
    FQueryParams *newParams = [self mutableCopy];
    newParams->_limitSet = YES;
    newParams->_limit = newLimit;
    newParams->_viewFrom = nil;
    return newParams;
}

- (FQueryParams *) limitToFirst:(NSInteger)newLimit {
    FQueryParams *newParams = [self mutableCopy];
    newParams->_limitSet = YES;
    newParams->_limit = newLimit;
    newParams->_viewFrom = kFQPViewFromLeft;
    return newParams;
}

- (FQueryParams *) limitToLast:(NSInteger)newLimit {
    FQueryParams *newParams = [self mutableCopy];
    newParams->_limitSet = YES;
    newParams->_limit = newLimit;
    newParams->_viewFrom = kFQPViewFromRight;
    return newParams;
}

- (FQueryParams *) startAt:(id<FNode>)indexValue childKey:(NSString *)key {
    NSAssert([indexValue isLeafNode] || [indexValue isEmpty], nil);
    FQueryParams *newParams = [self mutableCopy];
    newParams->_indexStartValue = indexValue;
    newParams->_indexStartKey = key;
    return newParams;
}

- (FQueryParams *) startAt:(id<FNode>)indexValue {
    return [self startAt:indexValue childKey:nil];
}

- (FQueryParams *) endAt:(id<FNode>)indexValue childKey:(NSString *)key {
    NSAssert([indexValue isLeafNode] || [indexValue isEmpty], nil);
    FQueryParams *newParams = [self mutableCopy];
    newParams->_indexEndValue = indexValue;
    newParams->_indexEndKey = key;
    return newParams;
}

- (FQueryParams *) endAt:(id<FNode>)indexValue {
    return [self endAt:indexValue childKey:nil];
}

- (FQueryParams *) orderBy:(id)newIndex {
    FQueryParams *newParams = [self mutableCopy];
    newParams->_index = newIndex;
    return newParams;
}

- (NSDictionary *) wireProtocolParams {
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    if ([self hasStart]) {
        [dict setObject:[self.indexStartValue valForExport:YES] forKey:kFQPIndexStartValue];

        // Don't use property as it will be [MIN-NAME]
        if (self->_indexStartKey != nil) {
            [dict setObject:self->_indexStartKey forKey:kFQPIndexStartName];
        }
    }

    if ([self hasEnd]) {
        [dict setObject:[self.indexEndValue valForExport:YES] forKey:kFQPIndexEndValue];

        // Don't use property as it will be [MAX-NAME]
        if (self->_indexEndKey != nil) {
            [dict setObject:self->_indexEndKey forKey:kFQPIndexEndName];
        }
    }

    if (self.limitSet) {
        [dict setObject:[NSNumber numberWithInteger:self.limit] forKey:kFQPLimit];
        NSString *vf = self.viewFrom;
        if (vf == nil) {
            // limit() rather than limitToFirst or limitToLast was called.
            // This means that only one of startSet or endSet is true. Use them
            // to calculate which side of the view to anchor to. If neither is set,
            // Anchor to end
            if ([self hasStart]) {
                vf = kFQPViewFromLeft;
            } else {
                vf = kFQPViewFromRight;
            }
        }
        [dict setObject:vf forKey:kFQPViewFrom];
    }

    // For now, priority index is the default, so we only specify if it's some other index.
    if (![self.index isEqual:[FPriorityIndex priorityIndex]]) {
        [dict setObject:[self.index queryDefinition] forKey:kFQPIndex];
    }

    return dict;
}

+ (FQueryParams *)fromQueryObject:(NSDictionary *)dict {
    if (dict.count == 0) {
        return [FQueryParams defaultInstance];
    }

    FQueryParams *params = [[FQueryParams alloc] init];
    if (dict[kFQPLimit] != nil) {
        params->_limitSet = YES;
        params->_limit = [dict[kFQPLimit] integerValue];
    }

    if (dict[kFQPIndexStartValue] != nil) {
        params->_indexStartValue = [FSnapshotUtilities nodeFrom:dict[kFQPIndexStartValue]];
        if (dict[kFQPIndexStartName] != nil) {
            params->_indexStartKey = dict[kFQPIndexStartName];
        }
    }

    if (dict[kFQPIndexEndValue] != nil) {
        params->_indexEndValue = [FSnapshotUtilities nodeFrom:dict[kFQPIndexEndValue]];
        if (dict[kFQPIndexEndName] != nil) {
            params->_indexEndKey = dict[kFQPIndexEndName];
        }
    }

    if (dict[kFQPViewFrom] != nil) {
        NSString *viewFrom = dict[kFQPViewFrom];
        if (![viewFrom isEqualToString:kFQPViewFromLeft] && ![viewFrom isEqualToString:kFQPViewFromRight]) {
            [NSException raise:NSInvalidArgumentException format:@"Unknown view from paramter: %@", viewFrom];
        }
        params->_viewFrom = viewFrom;
    }

    NSString *index = dict[kFQPIndex];
    if (index != nil) {
        params->_index = [FIndex indexFromQueryDefinition:index];
    }

    return params;
}

- (BOOL) isViewFromLeft {
    if (self.viewFrom != nil) {
        // Not null, we can just check
        return [self.viewFrom isEqualToString:kFQPViewFromLeft];
    } else {
        // If start is set, it's view from left. Otherwise not.
        return self.hasStart;
    }
}

- (id<FNodeFilter>) nodeFilter {
    if (self.loadsAllData) {
        return [[FIndexedFilter alloc] initWithIndex:self.index];
    } else if (self.limitSet) {
        return [[FLimitedFilter alloc] initWithQueryParams:self];
    } else {
        return [[FRangedFilter alloc] initWithQueryParams:self];
    }
}


- (BOOL) isValid {
    return !(self.hasStart && self.hasEnd && self.limitSet && !self.hasAnchoredLimit);
}

- (BOOL) loadsAllData {
    return !(self.hasStart || self.hasEnd || self.limitSet);
}

- (BOOL) isDefault {
    return [self loadsAllData] && [self.index isEqual:[FPriorityIndex priorityIndex]];
}

- (NSString *) description {
    return [[self wireProtocolParams] description];
}

- (BOOL) isEqual:(id)obj {
    if (self == obj) {
        return YES;
    }
    if (![obj isKindOfClass:[self class]]) {
        return NO;
    }
    FQueryParams *other = (FQueryParams *)obj;
    if (self->_limitSet != other->_limitSet) return NO;
    if (self->_limit != other->_limit) return NO;
    if ((self->_index != other->_index) && ![self->_index isEqual:other->_index]) return NO;
    if ((self->_indexStartKey != other->_indexStartKey) && ![self->_indexStartKey isEqualToString:other->_indexStartKey]) return NO;
    if ((self->_indexStartValue != other->_indexStartValue) && ![self->_indexStartValue isEqual:other->_indexStartValue]) return NO;
    if ((self->_indexEndKey != other->_indexEndKey) && ![self->_indexEndKey isEqualToString:other->_indexEndKey]) return NO;
    if ((self->_indexEndValue != other->_indexEndValue) && ![self->_indexEndValue isEqual:other->_indexEndValue]) return NO;
    if ([self isViewFromLeft] != [other isViewFromLeft]) return NO;

    return YES;
}

- (NSUInteger) hash {
    NSUInteger result = _limitSet ? _limit : 0;
    result = 31 * result + ([self isViewFromLeft] ? 1231 : 1237);
    result = 31 * result + [_indexStartKey hash];
    result = 31 * result + [_indexStartValue hash];
    result = 31 * result + [_indexEndKey hash];
    result = 31 * result + [_indexEndValue hash];
    result = 31 * result + [_index hash];
    return result;
}

@end