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/View/FView.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.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/FCancelEvent.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Core/View/FEventRegistration.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Core/View/FViewCache.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Core/View/Filter/FIndexedFilter.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/FEventGenerator.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/FViewProcessor.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/FViewProcessorResult.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/FEmptyNode.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Snapshot/FIndexedNode.h"
|
|
|
|
|
#import "FirebaseDatabase/Sources/Snapshot/FNode.h"
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@interface FViewOperationResult ()
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
@property(nonatomic, strong, readwrite) NSArray *changes;
|
|
|
|
|
@property(nonatomic, strong, readwrite) NSArray *events;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FViewOperationResult
|
|
|
|
|
|
|
|
|
|
- (id)initWithChanges:(NSArray *)changes events:(NSArray *)events {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil) {
|
|
|
|
|
self->_changes = changes;
|
|
|
|
|
self->_events = events;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-05 13:27:47 -07:00
|
|
|
* A view represents a specific location and query that has 1 or more event
|
|
|
|
|
* registrations.
|
|
|
|
|
*
|
|
|
|
|
* It does several things:
|
|
|
|
|
* - Maintains the list of event registration for this location/query.
|
|
|
|
|
* - Maintains a cache of the data visible for this location/query.
|
|
|
|
|
* - Applies new operations (via applyOperation), updates the cache, and based
|
|
|
|
|
* on the event registrations returns the set of events to be raised.
|
|
|
|
|
*/
|
2017-05-15 12:27:07 -07:00
|
|
|
@interface FView ()
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
@property(nonatomic, strong, readwrite) FQuerySpec *query;
|
|
|
|
|
@property(nonatomic, strong) FViewProcessor *processor;
|
|
|
|
|
@property(nonatomic, strong) FViewCache *viewCache;
|
|
|
|
|
@property(nonatomic, strong) NSMutableArray *eventRegistrations;
|
|
|
|
|
@property(nonatomic, strong) FEventGenerator *eventGenerator;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FView
|
2019-08-05 13:27:47 -07:00
|
|
|
- (id)initWithQuery:(FQuerySpec *)query
|
|
|
|
|
initialViewCache:(FViewCache *)initialViewCache {
|
2017-05-15 12:27:07 -07:00
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
self.query = query;
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIndexedFilter *indexFilter =
|
|
|
|
|
[[FIndexedFilter alloc] initWithIndex:query.index];
|
2017-05-15 12:27:07 -07:00
|
|
|
id<FNodeFilter> filter = query.params.nodeFilter;
|
|
|
|
|
self.processor = [[FViewProcessor alloc] initWithFilter:filter];
|
|
|
|
|
FCacheNode *initialServerCache = initialViewCache.cachedServerSnap;
|
|
|
|
|
FCacheNode *initialEventCache = initialViewCache.cachedEventSnap;
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// Don't filter server node with other filter than index, wait for
|
|
|
|
|
// tagged listen
|
|
|
|
|
FIndexedNode *emptyIndexedNode =
|
|
|
|
|
[FIndexedNode indexedNodeWithNode:[FEmptyNode emptyNode]
|
|
|
|
|
index:query.index];
|
|
|
|
|
FIndexedNode *serverSnap =
|
|
|
|
|
[indexFilter updateFullNode:emptyIndexedNode
|
|
|
|
|
withNewNode:initialServerCache.indexedNode
|
|
|
|
|
accumulator:nil];
|
|
|
|
|
FIndexedNode *eventSnap =
|
|
|
|
|
[filter updateFullNode:emptyIndexedNode
|
|
|
|
|
withNewNode:initialEventCache.indexedNode
|
|
|
|
|
accumulator:nil];
|
|
|
|
|
FCacheNode *newServerCache = [[FCacheNode alloc]
|
|
|
|
|
initWithIndexedNode:serverSnap
|
|
|
|
|
isFullyInitialized:initialServerCache.isFullyInitialized
|
|
|
|
|
isFiltered:indexFilter.filtersNodes];
|
|
|
|
|
FCacheNode *newEventCache = [[FCacheNode alloc]
|
|
|
|
|
initWithIndexedNode:eventSnap
|
|
|
|
|
isFullyInitialized:initialEventCache.isFullyInitialized
|
|
|
|
|
isFiltered:filter.filtersNodes];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
self.viewCache = [[FViewCache alloc] initWithEventCache:newEventCache
|
|
|
|
|
serverCache:newServerCache];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
self.eventRegistrations = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
|
|
self.eventGenerator = [[FEventGenerator alloc] initWithQuery:query];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (id<FNode>)serverCache {
|
2017-05-15 12:27:07 -07:00
|
|
|
return self.viewCache.cachedServerSnap.node;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (id<FNode>)eventCache {
|
2017-05-15 12:27:07 -07:00
|
|
|
return self.viewCache.cachedEventSnap.node;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 09:42:36 -08:00
|
|
|
- (id<FNode>)completeEventCache {
|
|
|
|
|
return self.viewCache.completeEventSnap;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (id<FNode>)completeServerCacheFor:(FPath *)path {
|
2017-05-15 12:27:07 -07:00
|
|
|
id<FNode> cache = self.viewCache.completeServerSnap;
|
2021-02-10 09:42:36 -08:00
|
|
|
if (cache) {
|
|
|
|
|
// If this isn't a "loadsAllData" view, then cache isn't actually a
|
|
|
|
|
// complete cache and we need to see if it contains the child we're
|
|
|
|
|
// interested in.
|
|
|
|
|
if ([self.query loadsAllData] ||
|
|
|
|
|
(!path.isEmpty &&
|
|
|
|
|
![cache getImmediateChild:path.getFront].isEmpty)) {
|
|
|
|
|
return [cache getChild:path];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id<FNode>)completeEventCacheFor:(FPath *)path {
|
|
|
|
|
id<FNode> cache = self.viewCache.completeEventSnap;
|
2017-05-15 12:27:07 -07:00
|
|
|
if (cache) {
|
2019-08-05 13:27:47 -07:00
|
|
|
// If this isn't a "loadsAllData" view, then cache isn't actually a
|
|
|
|
|
// complete cache and we need to see if it contains the child we're
|
|
|
|
|
// interested in.
|
|
|
|
|
if ([self.query loadsAllData] ||
|
|
|
|
|
(!path.isEmpty &&
|
|
|
|
|
![cache getImmediateChild:path.getFront].isEmpty)) {
|
2017-05-15 12:27:07 -07:00
|
|
|
return [cache getChild:path];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (BOOL)isEmpty {
|
2017-05-15 12:27:07 -07:00
|
|
|
return self.eventRegistrations.count == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)addEventRegistration:(id<FEventRegistration>)eventRegistration {
|
2017-05-15 12:27:07 -07:00
|
|
|
[self.eventRegistrations addObject:eventRegistration];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-05 13:27:47 -07:00
|
|
|
* @param eventRegistration If null, remove all callbacks.
|
|
|
|
|
* @param cancelError If a cancelError is provided, appropriate cancel events
|
|
|
|
|
* will be returned.
|
|
|
|
|
* @return Cancel events, if cancelError was provided.
|
|
|
|
|
*/
|
|
|
|
|
- (NSArray *)removeEventRegistration:(id<FEventRegistration>)eventRegistration
|
|
|
|
|
cancelError:(NSError *)cancelError {
|
2017-05-15 12:27:07 -07:00
|
|
|
NSMutableArray *cancelEvents = [[NSMutableArray alloc] init];
|
|
|
|
|
if (cancelError != nil) {
|
2019-08-05 13:27:47 -07:00
|
|
|
NSAssert(eventRegistration == nil,
|
|
|
|
|
@"A cancel should cancel all event registrations.");
|
2017-05-15 12:27:07 -07:00
|
|
|
FPath *path = self.query.path;
|
2019-08-05 13:27:47 -07:00
|
|
|
for (id<FEventRegistration> registration in self.eventRegistrations) {
|
|
|
|
|
FCancelEvent *maybeEvent =
|
|
|
|
|
[registration createCancelEventFromError:cancelError path:path];
|
2017-05-15 12:27:07 -07:00
|
|
|
if (maybeEvent) {
|
|
|
|
|
[cancelEvents addObject:maybeEvent];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventRegistration) {
|
|
|
|
|
NSUInteger i = 0;
|
|
|
|
|
while (i < self.eventRegistrations.count) {
|
|
|
|
|
id<FEventRegistration> existing = self.eventRegistrations[i];
|
|
|
|
|
if ([existing matches:eventRegistration]) {
|
|
|
|
|
[self.eventRegistrations removeObjectAtIndex:i];
|
|
|
|
|
} else {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
[self.eventRegistrations removeAllObjects];
|
|
|
|
|
}
|
|
|
|
|
return cancelEvents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-05 13:27:47 -07:00
|
|
|
* Applies the given Operation, updates our cache, and returns the appropriate
|
|
|
|
|
* events and changes
|
2017-05-15 12:27:07 -07:00
|
|
|
*/
|
2019-08-05 13:27:47 -07:00
|
|
|
- (FViewOperationResult *)applyOperation:(id<FOperation>)operation
|
|
|
|
|
writesCache:(FWriteTreeRef *)writesCache
|
|
|
|
|
serverCache:(id<FNode>)optCompleteServerCache {
|
|
|
|
|
if (operation.type == FOperationTypeMerge &&
|
|
|
|
|
operation.source.queryParams != nil) {
|
|
|
|
|
NSAssert(self.viewCache.completeServerSnap != nil,
|
|
|
|
|
@"We should always have a full cache before handling merges");
|
|
|
|
|
NSAssert(self.viewCache.completeEventSnap != nil,
|
|
|
|
|
@"Missing event cache, even though we have a server cache");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
FViewCache *oldViewCache = self.viewCache;
|
2019-08-05 13:27:47 -07:00
|
|
|
FViewProcessorResult *result =
|
|
|
|
|
[self.processor applyOperationOn:oldViewCache
|
|
|
|
|
operation:operation
|
|
|
|
|
writesCache:writesCache
|
|
|
|
|
completeCache:optCompleteServerCache];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSAssert(result.viewCache.cachedServerSnap.isFullyInitialized ||
|
|
|
|
|
!oldViewCache.cachedServerSnap.isFullyInitialized,
|
|
|
|
|
@"Once a server snap is complete, it should never go back.");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
self.viewCache = result.viewCache;
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *events = [self
|
|
|
|
|
generateEventsForChanges:result.changes
|
|
|
|
|
eventCache:result.viewCache.cachedEventSnap.indexedNode
|
|
|
|
|
registration:nil];
|
|
|
|
|
return [[FViewOperationResult alloc] initWithChanges:result.changes
|
|
|
|
|
events:events];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (NSArray *)initialEvents:(id<FEventRegistration>)registration {
|
2017-05-15 12:27:07 -07:00
|
|
|
FCacheNode *eventSnap = self.viewCache.cachedEventSnap;
|
|
|
|
|
NSMutableArray *initialChanges = [[NSMutableArray alloc] init];
|
2019-08-05 13:27:47 -07:00
|
|
|
[eventSnap.indexedNode.node enumerateChildrenUsingBlock:^(
|
|
|
|
|
NSString *key, id<FNode> node, BOOL *stop) {
|
|
|
|
|
FIndexedNode *indexed = [FIndexedNode indexedNodeWithNode:node];
|
|
|
|
|
FChange *change = [[FChange alloc] initWithType:FIRDataEventTypeChildAdded
|
|
|
|
|
indexedNode:indexed
|
|
|
|
|
childKey:key];
|
|
|
|
|
[initialChanges addObject:change];
|
2017-05-15 12:27:07 -07:00
|
|
|
}];
|
|
|
|
|
if (eventSnap.isFullyInitialized) {
|
2019-08-05 13:27:47 -07:00
|
|
|
FChange *change = [[FChange alloc] initWithType:FIRDataEventTypeValue
|
|
|
|
|
indexedNode:eventSnap.indexedNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
[initialChanges addObject:change];
|
|
|
|
|
}
|
2019-08-05 13:27:47 -07:00
|
|
|
return [self generateEventsForChanges:initialChanges
|
|
|
|
|
eventCache:eventSnap.indexedNode
|
|
|
|
|
registration:registration];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (NSArray *)generateEventsForChanges:(NSArray *)changes
|
|
|
|
|
eventCache:(FIndexedNode *)eventCache
|
|
|
|
|
registration:(id<FEventRegistration>)registration {
|
2017-05-15 12:27:07 -07:00
|
|
|
NSArray *registrations;
|
|
|
|
|
if (registration == nil) {
|
|
|
|
|
registrations = [[NSArray alloc] initWithArray:self.eventRegistrations];
|
|
|
|
|
} else {
|
|
|
|
|
registrations = [[NSArray alloc] initWithObjects:registration, nil];
|
|
|
|
|
}
|
2019-08-05 13:27:47 -07:00
|
|
|
return [self.eventGenerator generateEventsForChanges:changes
|
|
|
|
|
eventCache:eventCache
|
|
|
|
|
eventRegistrations:registrations];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (NSString *)description {
|
2017-05-15 12:27:07 -07:00
|
|
|
return [NSString stringWithFormat:@"FView (%@)", self.query];
|
|
|
|
|
}
|
|
|
|
|
@end
|