FCompoundWrite.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "FirebaseDatabase/Sources/Snapshot/FCompoundWrite.h"
  17. #import "FirebaseDatabase/Sources/Core/Utilities/FImmutableTree.h"
  18. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  19. #import "FirebaseDatabase/Sources/FNamedNode.h"
  20. #import "FirebaseDatabase/Sources/Snapshot/FNode.h"
  21. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  22. @interface FCompoundWrite ()
  23. @property(nonatomic, strong) FImmutableTree *writeTree;
  24. @end
  25. @implementation FCompoundWrite
  26. - (id)initWithWriteTree:(FImmutableTree *)tree {
  27. self = [super init];
  28. if (self) {
  29. self.writeTree = tree;
  30. }
  31. return self;
  32. }
  33. + (FCompoundWrite *)compoundWriteWithValueDictionary:
  34. (NSDictionary *)dictionary {
  35. __block FImmutableTree *writeTree = [FImmutableTree empty];
  36. [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *pathString,
  37. id value, BOOL *stop) {
  38. id<FNode> node = [FSnapshotUtilities nodeFrom:value];
  39. FImmutableTree *tree = [[FImmutableTree alloc] initWithValue:node];
  40. writeTree = [writeTree setTree:tree
  41. atPath:[[FPath alloc] initWith:pathString]];
  42. }];
  43. return [[FCompoundWrite alloc] initWithWriteTree:writeTree];
  44. }
  45. + (FCompoundWrite *)compoundWriteWithNodeDictionary:(NSDictionary *)dictionary {
  46. __block FImmutableTree *writeTree = [FImmutableTree empty];
  47. [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *pathString,
  48. id node, BOOL *stop) {
  49. FImmutableTree *tree = [[FImmutableTree alloc] initWithValue:node];
  50. writeTree = [writeTree setTree:tree
  51. atPath:[[FPath alloc] initWith:pathString]];
  52. }];
  53. return [[FCompoundWrite alloc] initWithWriteTree:writeTree];
  54. }
  55. + (FCompoundWrite *)emptyWrite {
  56. static dispatch_once_t pred = 0;
  57. static FCompoundWrite *empty = nil;
  58. dispatch_once(&pred, ^{
  59. empty = [[FCompoundWrite alloc]
  60. initWithWriteTree:[[FImmutableTree alloc] initWithValue:nil]];
  61. });
  62. return empty;
  63. }
  64. - (FCompoundWrite *)addWrite:(id<FNode>)node atPath:(FPath *)path {
  65. if (path.isEmpty) {
  66. return [[FCompoundWrite alloc]
  67. initWithWriteTree:[[FImmutableTree alloc] initWithValue:node]];
  68. } else {
  69. FTuplePathValue *rootMost =
  70. [self.writeTree findRootMostValueAndPath:path];
  71. if (rootMost != nil) {
  72. FPath *relativePath = [FPath relativePathFrom:rootMost.path
  73. to:path];
  74. id<FNode> value = [rootMost.value updateChild:relativePath
  75. withNewChild:node];
  76. return [[FCompoundWrite alloc]
  77. initWithWriteTree:[self.writeTree setValue:value
  78. atPath:rootMost.path]];
  79. } else {
  80. FImmutableTree *subtree =
  81. [[FImmutableTree alloc] initWithValue:node];
  82. FImmutableTree *newWriteTree = [self.writeTree setTree:subtree
  83. atPath:path];
  84. return [[FCompoundWrite alloc] initWithWriteTree:newWriteTree];
  85. }
  86. }
  87. }
  88. - (FCompoundWrite *)addWrite:(id<FNode>)node atKey:(NSString *)key {
  89. return [self addWrite:node atPath:[[FPath alloc] initWith:key]];
  90. }
  91. - (FCompoundWrite *)addCompoundWrite:(FCompoundWrite *)compoundWrite
  92. atPath:(FPath *)path {
  93. __block FCompoundWrite *newWrite = self;
  94. [compoundWrite.writeTree forEach:^(FPath *childPath, id<FNode> value) {
  95. newWrite = [newWrite addWrite:value atPath:[path child:childPath]];
  96. }];
  97. return newWrite;
  98. }
  99. /**
  100. * Will remove a write at the given path and deeper paths. This will
  101. * <em>not</em> modify a write at a higher location, which must be removed by
  102. * calling this method with that path.
  103. * @param path The path at which a write and all deeper writes should be
  104. * removed.
  105. * @return The new FWriteCompound with the removed path.
  106. */
  107. - (FCompoundWrite *)removeWriteAtPath:(FPath *)path {
  108. if (path.isEmpty) {
  109. return [FCompoundWrite emptyWrite];
  110. } else {
  111. FImmutableTree *newWriteTree =
  112. [self.writeTree setTree:[FImmutableTree empty] atPath:path];
  113. return [[FCompoundWrite alloc] initWithWriteTree:newWriteTree];
  114. }
  115. }
  116. /**
  117. * Returns whether this FCompoundWrite will fully overwrite a node at a given
  118. * location and can therefore be considered "complete".
  119. * @param path The path to check for
  120. * @return Whether there is a complete write at that path.
  121. */
  122. - (BOOL)hasCompleteWriteAtPath:(FPath *)path {
  123. return [self completeNodeAtPath:path] != nil;
  124. }
  125. /**
  126. * Returns a node for a path if and only if the node is a "complete" overwrite
  127. * at that path. This will not aggregate writes from depeer paths, but will
  128. * return child nodes from a more shallow path.
  129. * @param path The path to get a complete write
  130. * @return The node if complete at that path, or nil otherwise.
  131. */
  132. - (id<FNode>)completeNodeAtPath:(FPath *)path {
  133. FTuplePathValue *rootMost = [self.writeTree findRootMostValueAndPath:path];
  134. if (rootMost != nil) {
  135. FPath *relativePath = [FPath relativePathFrom:rootMost.path to:path];
  136. return [rootMost.value getChild:relativePath];
  137. } else {
  138. return nil;
  139. }
  140. }
  141. // TODO: change into traversal method...
  142. - (NSArray *)completeChildren {
  143. NSMutableArray *children = [[NSMutableArray alloc] init];
  144. if (self.writeTree.value != nil) {
  145. id<FNode> node = self.writeTree.value;
  146. [node enumerateChildrenUsingBlock:^(NSString *key, id<FNode> node,
  147. BOOL *stop) {
  148. [children addObject:[[FNamedNode alloc] initWithName:key
  149. andNode:node]];
  150. }];
  151. } else {
  152. [self.writeTree.children
  153. enumerateKeysAndObjectsUsingBlock:^(
  154. NSString *childKey, FImmutableTree *childTree, BOOL *stop) {
  155. if (childTree.value != nil) {
  156. [children addObject:[[FNamedNode alloc]
  157. initWithName:childKey
  158. andNode:childTree.value]];
  159. }
  160. }];
  161. }
  162. return children;
  163. }
  164. // TODO: change into enumarate method
  165. - (NSDictionary *)childCompoundWrites {
  166. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  167. [self.writeTree.children
  168. enumerateKeysAndObjectsUsingBlock:^(
  169. NSString *key, FImmutableTree *childWrite, BOOL *stop) {
  170. dict[key] = [[FCompoundWrite alloc] initWithWriteTree:childWrite];
  171. }];
  172. return dict;
  173. }
  174. - (FCompoundWrite *)childCompoundWriteAtPath:(FPath *)path {
  175. if (path.isEmpty) {
  176. return self;
  177. } else {
  178. id<FNode> shadowingNode = [self completeNodeAtPath:path];
  179. if (shadowingNode != nil) {
  180. return [[FCompoundWrite alloc]
  181. initWithWriteTree:[[FImmutableTree alloc]
  182. initWithValue:shadowingNode]];
  183. } else {
  184. return [[FCompoundWrite alloc]
  185. initWithWriteTree:[self.writeTree subtreeAtPath:path]];
  186. }
  187. }
  188. }
  189. - (id<FNode>)applySubtreeWrite:(FImmutableTree *)subtreeWrite
  190. atPath:(FPath *)relativePath
  191. toNode:(id<FNode>)node {
  192. if (subtreeWrite.value != nil) {
  193. // Since a write there is always a leaf, we're done here.
  194. return [node updateChild:relativePath withNewChild:subtreeWrite.value];
  195. } else {
  196. __block id<FNode> priorityWrite = nil;
  197. __block id<FNode> blockNode = node;
  198. [subtreeWrite.children
  199. enumerateKeysAndObjectsUsingBlock:^(
  200. NSString *childKey, FImmutableTree *childTree, BOOL *stop) {
  201. if ([childKey isEqualToString:@".priority"]) {
  202. // Apply priorities at the end so we don't update priorities
  203. // for either empty nodes or forget to apply priorities to
  204. // empty nodes that are later filled.
  205. NSAssert(childTree.value != nil,
  206. @"Priority writes must always be leaf nodes");
  207. priorityWrite = childTree.value;
  208. } else {
  209. blockNode = [self
  210. applySubtreeWrite:childTree
  211. atPath:[relativePath childFromString:childKey]
  212. toNode:blockNode];
  213. }
  214. }];
  215. // If there was a priority write, we only apply it if the node is not
  216. // empty
  217. if (![blockNode getChild:relativePath].isEmpty &&
  218. priorityWrite != nil) {
  219. blockNode = [blockNode
  220. updateChild:[relativePath childFromString:@".priority"]
  221. withNewChild:priorityWrite];
  222. }
  223. return blockNode;
  224. }
  225. }
  226. - (void)enumerateWrites:(void (^)(FPath *, id<FNode>, BOOL *))block {
  227. __block BOOL stop = NO;
  228. // TODO: add stop to tree iterator...
  229. [self.writeTree forEach:^(FPath *path, id value) {
  230. if (!stop) {
  231. block(path, value, &stop);
  232. }
  233. }];
  234. }
  235. /**
  236. * Applies this FCompoundWrite to a node. The node is returned with all writes
  237. * from this FCompoundWrite applied to the node.
  238. * @param node The node to apply this FCompoundWrite to
  239. * @return The node with all writes applied
  240. */
  241. - (id<FNode>)applyToNode:(id<FNode>)node {
  242. return [self applySubtreeWrite:self.writeTree
  243. atPath:[FPath empty]
  244. toNode:node];
  245. }
  246. /**
  247. * Return true if this CompoundWrite is empty and therefore does not modify any
  248. * nodes.
  249. * @return Whether this CompoundWrite is empty
  250. */
  251. - (BOOL)isEmpty {
  252. return self.writeTree.isEmpty;
  253. }
  254. - (id<FNode>)rootWrite {
  255. return self.writeTree.value;
  256. }
  257. - (BOOL)isEqual:(id)object {
  258. if (![object isKindOfClass:[FCompoundWrite class]]) {
  259. return NO;
  260. }
  261. FCompoundWrite *other = (FCompoundWrite *)object;
  262. return
  263. [[self valForExport:YES] isEqualToDictionary:[other valForExport:YES]];
  264. }
  265. - (NSUInteger)hash {
  266. return [[self valForExport:YES] hash];
  267. }
  268. - (NSDictionary *)valForExport:(BOOL)exportFormat {
  269. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  270. [self.writeTree forEach:^(FPath *path, id<FNode> value) {
  271. dictionary[path.wireFormat] = [value valForExport:exportFormat];
  272. }];
  273. return dictionary;
  274. }
  275. - (NSString *)description {
  276. return [[self valForExport:YES] description];
  277. }
  278. @end