aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/Core/FSyncPoint.m
blob: cd429f180991c5510de93ccc69fe8b5be8d0ce02 (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
/*
 * 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 "FSyncPoint.h"
#import "FOperation.h"
#import "FWriteTreeRef.h"
#import "FNode.h"
#import "FEventRegistration.h"
#import "FIRDatabaseQuery.h"
#import "FChildrenNode.h"
#import "FTupleRemovedQueriesEvents.h"
#import "FView.h"
#import "FOperationSource.h"
#import "FQuerySpec.h"
#import "FQueryParams.h"
#import "FPath.h"
#import "FEmptyNode.h"
#import "FViewCache.h"
#import "FCacheNode.h"
#import "FPersistenceManager.h"
#import "FDataEvent.h"

/**
* SyncPoint represents a single location in a SyncTree with 1 or more event registrations, meaning we need to
* maintain 1 or more Views at this location to cache server data and raise appropriate events for server changes
* and user writes (set, transaction, update).
*
* It's responsible for:
*  - Maintaining the set of 1 or more views necessary at this location (a SyncPoint with 0 views should be removed).
*  - Proxying user / server operations to the views as appropriate (i.e. applyServerOverwrite,
*    applyUserOverwrite, etc.)
*/
@interface FSyncPoint ()
/**
* The Views being tracked at this location in the tree, stored as a map where the key is a
* queryParams and the value is the View for that query.
*
* NOTE: This list will be quite small (usually 1, but perhaps 2 or 3; any more is an odd use case).
*
* Maps NSString -> FView
*/
@property (nonatomic, strong) NSMutableDictionary *views;

@property (nonatomic, strong) FPersistenceManager *persistenceManager;
@end

@implementation FSyncPoint

- (id) initWithPersistenceManager:(FPersistenceManager *)persistence {
    self = [super init];
    if (self) {
        self.persistenceManager = persistence;
        self.views = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (BOOL) isEmpty {
    return [self.views count] == 0;
}

- (NSArray *) applyOperation:(id<FOperation>)operation
                      toView:(FView *)view
                 writesCache:(FWriteTreeRef *)writesCache
                 serverCache:(id<FNode>)optCompleteServerCache {
    FViewOperationResult *result = [view applyOperation:operation writesCache:writesCache serverCache:optCompleteServerCache];
    if (!view.query.loadsAllData) {
        NSMutableSet *removed = [NSMutableSet set];
        NSMutableSet *added = [NSMutableSet set];
        [result.changes enumerateObjectsUsingBlock:^(FChange *change, NSUInteger idx, BOOL *stop) {
            if (change.type == FIRDataEventTypeChildAdded) {
                [added addObject:change.childKey];
            } else if (change.type == FIRDataEventTypeChildRemoved) {
                [removed addObject:change.childKey];
            }
        }];
        if ([removed count] > 0 || [added count] > 0) {
            [self.persistenceManager updateTrackedQueryKeysWithAddedKeys:added removedKeys:removed forQuery:view.query];
        }
    }
    return result.events;
}

- (NSArray *) applyOperation:(id <FOperation>)operation writesCache:(FWriteTreeRef *)writesCache serverCache:(id <FNode>)optCompleteServerCache {
    FQueryParams *queryParams = operation.source.queryParams;
    if (queryParams != nil) {
        FView *view = [self.views objectForKey:queryParams];
        NSAssert(view != nil, @"SyncTree gave us an op for an invalid query.");
        return [self applyOperation:operation toView:view writesCache:writesCache serverCache:optCompleteServerCache];
    } else {
        NSMutableArray *events = [[NSMutableArray alloc] init];
        [self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key, FView *view, BOOL *stop) {
            NSArray *eventsForView = [self applyOperation:operation toView:view writesCache:writesCache serverCache:optCompleteServerCache];
            [events addObjectsFromArray:eventsForView];
        }];
        return events;
    }
}

/**
* Add an event callback for the specified query
* Returns Array of FEvent events to raise.
*/
- (NSArray *) addEventRegistration:(id <FEventRegistration>)eventRegistration
        forNonExistingViewForQuery:(FQuerySpec *)query
                       writesCache:(FWriteTreeRef *)writesCache
                       serverCache:(FCacheNode *)serverCache {
    NSAssert(self.views[query.params] == nil, @"Found view for query: %@", query.params);
    // TODO: make writesCache take flag for complete server node
    id<FNode> eventCache = [writesCache calculateCompleteEventCacheWithCompleteServerCache:serverCache.isFullyInitialized ? serverCache.node : nil];
    BOOL eventCacheComplete;
    if (eventCache != nil) {
        eventCacheComplete = YES;
    } else {
        eventCache = [writesCache calculateCompleteEventChildrenWithCompleteServerChildren:serverCache.node];
        eventCacheComplete = NO;
    }

    FIndexedNode *indexed = [FIndexedNode indexedNodeWithNode:eventCache index:query.index];
    FCacheNode *eventCacheNode = [[FCacheNode alloc] initWithIndexedNode:indexed
                                                      isFullyInitialized:eventCacheComplete
                                                              isFiltered:NO];
    FViewCache *viewCache = [[FViewCache alloc] initWithEventCache:eventCacheNode serverCache:serverCache];
    FView *view = [[FView alloc] initWithQuery:query initialViewCache:viewCache];
    // If this is a non-default query we need to tell persistence our current view of the data
    if (!query.loadsAllData) {
        NSMutableSet *allKeys = [NSMutableSet set];
        [view.eventCache enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node, BOOL *stop) {
            [allKeys addObject:key];
        }];
        [self.persistenceManager setTrackedQueryKeys:allKeys forQuery:query];
    }
    self.views[query.params] = view;
    return [self addEventRegistration:eventRegistration forExistingViewForQuery:query];
}

- (NSArray *)addEventRegistration:(id<FEventRegistration>)eventRegistration
          forExistingViewForQuery:(FQuerySpec *)query {
    FView *view = self.views[query.params];
    NSAssert(view != nil, @"No view for query: %@", query);
    [view addEventRegistration:eventRegistration];
    return [view initialEvents:eventRegistration];
}

/**
* Remove event callback(s). Return cancelEvents if a cancelError is specified.
*
* If query is the default query, we'll check all views for the specified eventRegistration.
* If eventRegistration is nil, we'll remove all callbacks for the specified view(s).
*
* @return FTupleRemovedQueriesEvents removed queries and any cancel events
*/
- (FTupleRemovedQueriesEvents *) removeEventRegistration:(id <FEventRegistration>)eventRegistration
                                                forQuery:(FQuerySpec *)query
                                             cancelError:(NSError *)cancelError {
    NSMutableArray *removedQueries = [[NSMutableArray alloc] init];
    __block NSMutableArray *cancelEvents = [[NSMutableArray alloc] init];
    BOOL hadCompleteView = [self hasCompleteView];
    if ([query isDefault]) {
        // When you do [ref removeObserverWithHandle:], we search all views for the registration to remove.
        [self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *viewQueryParams, FView *view, BOOL *stop) {
            [cancelEvents addObjectsFromArray:[view removeEventRegistration:eventRegistration cancelError:cancelError]];
            if ([view isEmpty]) {
                [self.views removeObjectForKey:viewQueryParams];

                // We'll deal with complete views later
                if (![view.query loadsAllData]) {
                    [removedQueries addObject:view.query];
                }
            }
        }];
    } else {
        // remove the callback from the specific view
        FView *view = [self.views objectForKey:query.params];
        if (view != nil) {
            [cancelEvents addObjectsFromArray:[view removeEventRegistration:eventRegistration cancelError:cancelError]];

            if ([view isEmpty]) {
                [self.views removeObjectForKey:query.params];

                // We'll deal with complete views later
                if (![view.query loadsAllData]) {
                    [removedQueries addObject:view.query];
                }
            }
        }
    }

    if (hadCompleteView && ![self hasCompleteView]) {
        // We removed our last complete view
        [removedQueries addObject:[FQuerySpec defaultQueryAtPath:query.path]];
    }

    return [[FTupleRemovedQueriesEvents alloc] initWithRemovedQueries:removedQueries cancelEvents:cancelEvents];
}

- (NSArray *) queryViews {
    __block NSMutableArray *filteredViews = [[NSMutableArray alloc] init];

    [self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key, FView *view, BOOL *stop) {
        if (![view.query loadsAllData]) {
            [filteredViews addObject:view];
        }
    }];

    return filteredViews;
}

- (id <FNode>) completeServerCacheAtPath:(FPath *)path {
    __block id<FNode> serverCache = nil;
    [self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key, FView *view, BOOL *stop) {
        serverCache = [view completeServerCacheFor:path];
        *stop = (serverCache != nil);
    }];
    return serverCache;
}

- (FView *) viewForQuery:(FQuerySpec *)query {
    return [self.views objectForKey:query.params];
}

- (BOOL) viewExistsForQuery:(FQuerySpec *)query {
    return [self viewForQuery:query] != nil;
}

- (BOOL) hasCompleteView {
    return [self completeView] != nil;
}

- (FView *) completeView {
    __block FView *completeView = nil;

    [self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key, FView *view, BOOL *stop) {
        if ([view.query loadsAllData]) {
            completeView = view;
            *stop = YES;
        }
    }];

    return completeView;
}


@end