Files
firebase-ios-sdk/FirebaseDatabase/Sources/Core/FSyncPoint.m
T

302 lines
12 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.
*/
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Core/FSyncPoint.h"
#import "FirebaseDatabase/Sources/Core/FQueryParams.h"
#import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
#import "FirebaseDatabase/Sources/Core/FWriteTreeRef.h"
#import "FirebaseDatabase/Sources/Core/Operation/FOperation.h"
#import "FirebaseDatabase/Sources/Core/Operation/FOperationSource.h"
#import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
#import "FirebaseDatabase/Sources/Core/View/FCacheNode.h"
#import "FirebaseDatabase/Sources/Core/View/FDataEvent.h"
#import "FirebaseDatabase/Sources/Core/View/FEventRegistration.h"
#import "FirebaseDatabase/Sources/Core/View/FView.h"
#import "FirebaseDatabase/Sources/Core/View/FViewCache.h"
#import "FirebaseDatabase/Sources/Persistence/FPersistenceManager.h"
2020-07-31 07:22:02 -07:00
#import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabaseQuery.h"
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
#import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
#import "FirebaseDatabase/Sources/Snapshot/FNode.h"
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleRemovedQueriesEvents.h"
2017-05-15 12:27:07 -07:00
/**
2019-08-05 13:27:47 -07:00
* 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.)
*/
2017-05-15 12:27:07 -07:00
@interface FSyncPoint ()
/**
2019-08-05 13:27:47 -07:00
* 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;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@property(nonatomic, strong) FPersistenceManager *persistenceManager;
2017-05-15 12:27:07 -07:00
@end
@implementation FSyncPoint
2019-08-05 13:27:47 -07:00
- (id)initWithPersistenceManager:(FPersistenceManager *)persistence {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.persistenceManager = persistence;
self.views = [[NSMutableDictionary alloc] init];
}
return self;
}
2019-08-05 13:27:47 -07:00
- (BOOL)isEmpty {
2017-05-15 12:27:07 -07:00
return [self.views count] == 0;
}
2019-08-05 13:27:47 -07:00
- (NSArray *)applyOperation:(id<FOperation>)operation
toView:(FView *)view
writesCache:(FWriteTreeRef *)writesCache
serverCache:(id<FNode>)optCompleteServerCache {
FViewOperationResult *result = [view applyOperation:operation
writesCache:writesCache
serverCache:optCompleteServerCache];
2017-05-15 12:27:07 -07:00
if (!view.query.loadsAllData) {
NSMutableSet *removed = [NSMutableSet set];
NSMutableSet *added = [NSMutableSet set];
2019-08-05 13:27:47 -07:00
[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];
}
2017-05-15 12:27:07 -07:00
}];
if ([removed count] > 0 || [added count] > 0) {
2019-08-05 13:27:47 -07:00
[self.persistenceManager
updateTrackedQueryKeysWithAddedKeys:added
removedKeys:removed
forQuery:view.query];
2017-05-15 12:27:07 -07:00
}
}
return result.events;
}
2019-08-05 13:27:47 -07:00
- (NSArray *)applyOperation:(id<FOperation>)operation
writesCache:(FWriteTreeRef *)writesCache
serverCache:(id<FNode>)optCompleteServerCache {
2017-05-15 12:27:07 -07:00
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.");
2019-08-05 13:27:47 -07:00
return [self applyOperation:operation
toView:view
writesCache:writesCache
serverCache:optCompleteServerCache];
2017-05-15 12:27:07 -07:00
} else {
NSMutableArray *events = [[NSMutableArray alloc] init];
2019-08-05 13:27:47 -07:00
[self.views enumerateKeysAndObjectsUsingBlock:^(
FQueryParams *key, FView *view, BOOL *stop) {
NSArray *eventsForView = [self applyOperation:operation
toView:view
writesCache:writesCache
serverCache:optCompleteServerCache];
[events addObjectsFromArray:eventsForView];
2017-05-15 12:27:07 -07:00
}];
return events;
}
}
/**
2019-08-05 13:27:47 -07:00
* 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);
2017-05-15 12:27:07 -07:00
// TODO: make writesCache take flag for complete server node
2019-08-05 13:27:47 -07:00
id<FNode> eventCache = [writesCache
calculateCompleteEventCacheWithCompleteServerCache:
serverCache.isFullyInitialized ? serverCache.node : nil];
2017-05-15 12:27:07 -07:00
BOOL eventCacheComplete;
if (eventCache != nil) {
eventCacheComplete = YES;
} else {
2019-08-05 13:27:47 -07:00
eventCache = [writesCache
calculateCompleteEventChildrenWithCompleteServerChildren:serverCache
.node];
2017-05-15 12:27:07 -07:00
eventCacheComplete = NO;
}
2019-08-05 13:27:47 -07:00
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
2017-05-15 12:27:07 -07:00
if (!query.loadsAllData) {
NSMutableSet *allKeys = [NSMutableSet set];
2019-08-05 13:27:47 -07:00
[view.eventCache enumerateChildrenUsingBlock:^(
NSString *key, id<FNode> node, BOOL *stop) {
[allKeys addObject:key];
2017-05-15 12:27:07 -07:00
}];
[self.persistenceManager setTrackedQueryKeys:allKeys forQuery:query];
}
self.views[query.params] = view;
2019-08-05 13:27:47 -07:00
return [self addEventRegistration:eventRegistration
forExistingViewForQuery:query];
2017-05-15 12:27:07 -07:00
}
- (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];
}
/**
2019-08-05 13:27:47 -07:00
* 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 {
2017-05-15 12:27:07 -07:00
NSMutableArray *removedQueries = [[NSMutableArray alloc] init];
__block NSMutableArray *cancelEvents = [[NSMutableArray alloc] init];
BOOL hadCompleteView = [self hasCompleteView];
if ([query isDefault]) {
2019-08-05 13:27:47 -07:00
// 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];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// We'll deal with complete views later
if (![view.query loadsAllData]) {
[removedQueries addObject:view.query];
}
}
2017-05-15 12:27:07 -07:00
}];
} else {
// remove the callback from the specific view
FView *view = [self.views objectForKey:query.params];
if (view != nil) {
2019-08-05 13:27:47 -07:00
[cancelEvents addObjectsFromArray:
[view removeEventRegistration:eventRegistration
cancelError:cancelError]];
2017-05-15 12:27:07 -07:00
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]];
}
2019-08-05 13:27:47 -07:00
return [[FTupleRemovedQueriesEvents alloc]
initWithRemovedQueries:removedQueries
cancelEvents:cancelEvents];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (NSArray *)queryViews {
2017-05-15 12:27:07 -07:00
__block NSMutableArray *filteredViews = [[NSMutableArray alloc] init];
2019-08-05 13:27:47 -07:00
[self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key,
FView *view, BOOL *stop) {
if (![view.query loadsAllData]) {
[filteredViews addObject:view];
}
2017-05-15 12:27:07 -07:00
}];
return filteredViews;
}
2019-08-05 13:27:47 -07:00
- (id<FNode>)completeServerCacheAtPath:(FPath *)path {
2017-05-15 12:27:07 -07:00
__block id<FNode> serverCache = nil;
2019-08-05 13:27:47 -07:00
[self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key,
FView *view, BOOL *stop) {
serverCache = [view completeServerCacheFor:path];
*stop = (serverCache != nil);
2017-05-15 12:27:07 -07:00
}];
return serverCache;
}
2019-08-05 13:27:47 -07:00
- (FView *)viewForQuery:(FQuerySpec *)query {
2017-05-15 12:27:07 -07:00
return [self.views objectForKey:query.params];
}
2019-08-05 13:27:47 -07:00
- (BOOL)viewExistsForQuery:(FQuerySpec *)query {
2017-05-15 12:27:07 -07:00
return [self viewForQuery:query] != nil;
}
2019-08-05 13:27:47 -07:00
- (BOOL)hasCompleteView {
2017-05-15 12:27:07 -07:00
return [self completeView] != nil;
}
2019-08-05 13:27:47 -07:00
- (FView *)completeView {
2017-05-15 12:27:07 -07:00
__block FView *completeView = nil;
2019-08-05 13:27:47 -07:00
[self.views enumerateKeysAndObjectsUsingBlock:^(FQueryParams *key,
FView *view, BOOL *stop) {
if ([view.query loadsAllData]) {
completeView = view;
*stop = YES;
}
2017-05-15 12:27:07 -07:00
}];
return completeView;
}
@end