FMockStorageEngine.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FMockStorageEngine.h"
  17. #import "FWriteRecord.h"
  18. #import "FCompoundWrite.h"
  19. #import "FNode.h"
  20. #import "FEmptyNode.h"
  21. #import "FTrackedQuery.h"
  22. #import "FPruneForest.h"
  23. #import "FCompoundWrite.h"
  24. @interface FMockStorageEngine ()
  25. @property (nonatomic) BOOL closed;
  26. @property (nonatomic, strong) NSMutableDictionary *userWritesDict;
  27. @property (nonatomic, strong) FCompoundWrite *serverCache;
  28. @property (nonatomic, strong) NSMutableDictionary *trackedQueries;
  29. @property (nonatomic, strong) NSMutableDictionary *trackedQueryKeys;
  30. @end
  31. @implementation FMockStorageEngine
  32. - (id)init {
  33. self = [super init];
  34. if (self != nil) {
  35. self->_userWritesDict = [NSMutableDictionary dictionary];
  36. self->_serverCache = [FCompoundWrite emptyWrite];
  37. self->_trackedQueries = [NSMutableDictionary dictionary];
  38. self->_trackedQueryKeys = [NSMutableDictionary dictionary];
  39. }
  40. return self;
  41. }
  42. - (void)close {
  43. self.closed = YES;
  44. }
  45. - (void)saveUserOverwrite:(id<FNode>)node atPath:(FPath *)path writeId:(NSUInteger)writeId {
  46. FWriteRecord *writeRecord = [[FWriteRecord alloc] initWithPath:path overwrite:node writeId:writeId visible:YES];
  47. self.userWritesDict[@(writeId)] = writeRecord;
  48. }
  49. - (void)saveUserMerge:(FCompoundWrite *)merge atPath:(FPath *)path writeId:(NSUInteger)writeId {
  50. FWriteRecord *writeRecord = [[FWriteRecord alloc] initWithPath:path merge:merge writeId:writeId];
  51. self.userWritesDict[@(writeId)] = writeRecord;
  52. }
  53. - (void)removeUserWrite:(NSUInteger)writeId {
  54. [self.userWritesDict removeObjectForKey:@(writeId)];
  55. }
  56. - (void)removeAllUserWrites {
  57. [self.userWritesDict removeAllObjects];
  58. }
  59. - (NSArray *)userWrites {
  60. return [[self.userWritesDict allValues] sortedArrayUsingComparator:^NSComparisonResult(FWriteRecord *obj1, FWriteRecord *obj2) {
  61. if (obj1.writeId < obj2.writeId) {
  62. return NSOrderedAscending;
  63. } else if (obj1.writeId > obj2.writeId) {
  64. return NSOrderedDescending;
  65. } else {
  66. return NSOrderedSame;
  67. }
  68. }];
  69. }
  70. - (id<FNode>)serverCacheAtPath:(FPath *)path {
  71. return [[self.serverCache childCompoundWriteAtPath:path] applyToNode:[FEmptyNode emptyNode]];
  72. }
  73. - (id<FNode>)serverCacheForKeys:(NSSet *)keys atPath:(FPath *)path {
  74. __block id<FNode> children = [FEmptyNode emptyNode];
  75. id<FNode> fullNode = [[self.serverCache childCompoundWriteAtPath:path] applyToNode:[FEmptyNode emptyNode]];
  76. [keys enumerateObjectsUsingBlock:^(NSString *key, BOOL *stop) {
  77. children = [children updateImmediateChild:key withNewChild:[fullNode getImmediateChild:key]];
  78. }];
  79. return children;
  80. }
  81. - (void)updateServerCache:(id<FNode>)node atPath:(FPath *)path merge:(BOOL)merge {
  82. if (merge) {
  83. [node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> childNode, BOOL *stop) {
  84. self.serverCache = [self.serverCache addWrite:childNode atPath:[path childFromString:key]];
  85. }];
  86. } else {
  87. self.serverCache = [self.serverCache addWrite:node atPath:path];
  88. }
  89. }
  90. - (void)updateServerCacheWithMerge:(FCompoundWrite *)merge atPath:(FPath *)path {
  91. self.serverCache = [self.serverCache addCompoundWrite:merge atPath:path];
  92. }
  93. - (NSUInteger)serverCacheEstimatedSizeInBytes {
  94. id data = [[self.serverCache applyToNode:[FEmptyNode emptyNode]] valForExport:YES];
  95. return [NSJSONSerialization dataWithJSONObject:data options:0 error:nil].length;
  96. }
  97. - (void)pruneCache:(FPruneForest *)pruneForest atPath:(FPath *)prunePath {
  98. [self.serverCache enumerateWrites:^(FPath *absolutePath, id<FNode> node, BOOL *stop) {
  99. NSAssert([prunePath isEqual:absolutePath] || ![absolutePath contains:prunePath], @"Pruning at %@ but we found data higher up!", prunePath);
  100. if ([prunePath contains:absolutePath]) {
  101. FPath *relativePath = [FPath relativePathFrom:prunePath to:absolutePath];
  102. if ([pruneForest shouldPruneUnkeptDescendantsAtPath:relativePath]) {
  103. __block FCompoundWrite *newCache = [FCompoundWrite emptyWrite];
  104. [[pruneForest childAtPath:relativePath] enumarateKeptNodesUsingBlock:^(FPath *keepPath) {
  105. newCache = [newCache addWrite:[node getChild:keepPath] atPath:keepPath];
  106. }];
  107. self.serverCache = [[self.serverCache removeWriteAtPath:absolutePath] addCompoundWrite:newCache atPath:absolutePath];
  108. } else {
  109. // NOTE: This is technically a valid scenario (e.g. you ask to prune at / but only want to prune
  110. // 'foo' and 'bar' and ignore everything else). But currently our pruning will explicitly
  111. // prune or keep everything we know about, so if we hit this it means our tracked queries and
  112. // the server cache are out of sync.
  113. NSAssert([pruneForest shouldKeepPath:relativePath], @"We have data at %@ that is neither pruned nor kept.", relativePath);
  114. }
  115. }
  116. }];
  117. }
  118. - (NSArray *)loadTrackedQueries {
  119. return self.trackedQueries.allValues;
  120. }
  121. - (void)removeTrackedQuery:(NSUInteger)queryId {
  122. [self.trackedQueries removeObjectForKey:@(queryId)];
  123. [self.trackedQueryKeys removeObjectForKey:@(queryId)];
  124. }
  125. - (void)saveTrackedQuery:(FTrackedQuery *)query {
  126. self.trackedQueries[@(query.queryId)] = query;
  127. }
  128. - (void)setTrackedQueryKeys:(NSSet *)keys forQueryId:(NSUInteger)queryId {
  129. self.trackedQueryKeys[@(queryId)] = keys;
  130. }
  131. - (void)updateTrackedQueryKeysWithAddedKeys:(NSSet *)added removedKeys:(NSSet *)removed forQueryId:(NSUInteger)queryId {
  132. NSSet *oldKeys = [self trackedQueryKeysForQuery:queryId];
  133. NSMutableSet *newKeys = [NSMutableSet setWithSet:oldKeys];
  134. [newKeys minusSet:removed];
  135. [newKeys unionSet:added];
  136. self.trackedQueryKeys[@(queryId)] = newKeys;
  137. }
  138. - (NSSet *)trackedQueryKeysForQuery:(NSUInteger)queryId {
  139. NSSet *keys = self.trackedQueryKeys[@(queryId)];
  140. return keys != nil ? keys : [NSSet set];
  141. }
  142. @end