FCompoundWrite.m 9.7 KB

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