FLeafNode.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/FLeafNode.h"
  17. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  18. #import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
  19. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  20. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  21. #import "FirebaseDatabase/Sources/Utilities/FStringUtilities.h"
  22. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  23. #import "FirebaseDatabase/Sources/third_party/FImmutableSortedDictionary/FImmutableSortedDictionary/FImmutableSortedDictionary.h"
  24. @interface FLeafNode ()
  25. @property(nonatomic, strong) id<FNode> priorityNode;
  26. @property(nonatomic, strong) NSString *lazyHash;
  27. @end
  28. @implementation FLeafNode
  29. @synthesize value;
  30. @synthesize priorityNode;
  31. - (id)initWithValue:(id)aValue {
  32. self = [super init];
  33. if (self) {
  34. self.value = aValue;
  35. self.priorityNode = [FEmptyNode emptyNode];
  36. }
  37. return self;
  38. }
  39. - (id)initWithValue:(id)aValue withPriority:(id<FNode>)aPriority {
  40. self = [super init];
  41. if (self) {
  42. self.value = aValue;
  43. [FSnapshotUtilities validatePriorityNode:aPriority];
  44. self.priorityNode = aPriority;
  45. }
  46. return self;
  47. }
  48. #pragma mark -
  49. #pragma mark FNode methods
  50. - (BOOL)isLeafNode {
  51. return YES;
  52. }
  53. - (id<FNode>)getPriority {
  54. return self.priorityNode;
  55. }
  56. - (id<FNode>)updatePriority:(id<FNode>)aPriority {
  57. return [[FLeafNode alloc] initWithValue:self.value withPriority:aPriority];
  58. }
  59. - (id<FNode>)getImmediateChild:(NSString *)childName {
  60. if ([childName isEqualToString:@".priority"]) {
  61. return self.priorityNode;
  62. } else {
  63. return [FEmptyNode emptyNode];
  64. }
  65. }
  66. - (id<FNode>)getChild:(FPath *)path {
  67. if (path.getFront == nil) {
  68. return self;
  69. } else if ([[path getFront] isEqualToString:@".priority"]) {
  70. return [self getPriority];
  71. } else {
  72. return [FEmptyNode emptyNode];
  73. }
  74. }
  75. - (BOOL)hasChild:(NSString *)childName {
  76. return
  77. [childName isEqualToString:@".priority"] && ![self getPriority].isEmpty;
  78. }
  79. - (NSString *)predecessorChildKey:(NSString *)childKey {
  80. return nil;
  81. }
  82. - (id<FNode>)updateImmediateChild:(NSString *)childName
  83. withNewChild:(id<FNode>)newChildNode {
  84. if ([childName isEqualToString:@".priority"]) {
  85. return [self updatePriority:newChildNode];
  86. } else if (newChildNode.isEmpty) {
  87. return self;
  88. } else {
  89. FChildrenNode *childrenNode = [[FChildrenNode alloc] init];
  90. childrenNode = [childrenNode updateImmediateChild:childName
  91. withNewChild:newChildNode];
  92. childrenNode = [childrenNode updatePriority:self.priorityNode];
  93. return childrenNode;
  94. }
  95. }
  96. - (id<FNode>)updateChild:(FPath *)path withNewChild:(id<FNode>)newChildNode {
  97. NSString *front = [path getFront];
  98. if (front == nil) {
  99. return newChildNode;
  100. } else if (newChildNode.isEmpty && ![front isEqualToString:@".priority"]) {
  101. return self;
  102. } else {
  103. NSAssert(![front isEqualToString:@".priority"] || path.length == 1,
  104. @".priority must be the last token in a path.");
  105. return [self updateImmediateChild:front
  106. withNewChild:[[FEmptyNode emptyNode]
  107. updateChild:[path popFront]
  108. withNewChild:newChildNode]];
  109. }
  110. }
  111. - (id)val {
  112. return [self valForExport:NO];
  113. }
  114. - (id)valForExport:(BOOL)exp {
  115. if (exp && !self.getPriority.isEmpty) {
  116. return @{
  117. kPayloadValue : self.value,
  118. kPayloadPriority : [[self getPriority] val]
  119. };
  120. } else {
  121. return self.value;
  122. }
  123. }
  124. - (BOOL)isEqual:(id<FNode>)other {
  125. if (other == self) {
  126. return YES;
  127. } else if (other.isLeafNode) {
  128. FLeafNode *otherLeaf = other;
  129. if ([FUtilities getJavascriptType:self.value] !=
  130. [FUtilities getJavascriptType:otherLeaf.value]) {
  131. return NO;
  132. }
  133. return [otherLeaf.value isEqual:self.value] &&
  134. [otherLeaf.priorityNode isEqual:self.priorityNode];
  135. } else {
  136. return NO;
  137. }
  138. }
  139. - (NSUInteger)hash {
  140. return [self.value hash] * 17 + self.priorityNode.hash;
  141. }
  142. - (id<FNode>)withIndex:(id<FIndex>)index {
  143. return self;
  144. }
  145. - (BOOL)isIndexed:(id<FIndex>)index {
  146. return YES;
  147. }
  148. - (BOOL)isEmpty {
  149. return NO;
  150. }
  151. - (int)numChildren {
  152. return 0;
  153. }
  154. - (void)enumerateChildrenUsingBlock:(void (^)(NSString *, id<FNode>,
  155. BOOL *))block {
  156. // Nothing to iterate over
  157. }
  158. - (void)enumerateChildrenReverse:(BOOL)reverse
  159. usingBlock:
  160. (void (^)(NSString *, id<FNode>, BOOL *))block {
  161. // Nothing to iterate over
  162. }
  163. - (NSEnumerator *)childEnumerator {
  164. // Nothing to iterate over
  165. return [@[] objectEnumerator];
  166. }
  167. - (NSString *)dataHash {
  168. if (self.lazyHash == nil) {
  169. NSMutableString *toHash = [[NSMutableString alloc] init];
  170. [FSnapshotUtilities
  171. appendHashRepresentationForLeafNode:self
  172. toString:toHash
  173. hashVersion:FDataHashVersionV1];
  174. self.lazyHash = [FStringUtilities base64EncodedSha1:toHash];
  175. }
  176. return self.lazyHash;
  177. }
  178. - (NSComparisonResult)compare:(id<FNode>)other {
  179. if (other == [FEmptyNode emptyNode]) {
  180. return NSOrderedDescending;
  181. } else if ([other isKindOfClass:[FChildrenNode class]]) {
  182. return NSOrderedAscending;
  183. } else {
  184. NSAssert(other.isLeafNode, @"Compared against unknown type of node.");
  185. return [self compareToLeafNode:(FLeafNode *)other];
  186. }
  187. }
  188. + (NSArray *)valueTypeOrder {
  189. static NSArray *valueOrder = nil;
  190. static dispatch_once_t once;
  191. dispatch_once(&once, ^{
  192. valueOrder = @[
  193. kJavaScriptObject, kJavaScriptBoolean, kJavaScriptNumber,
  194. kJavaScriptString
  195. ];
  196. });
  197. return valueOrder;
  198. }
  199. - (NSComparisonResult)compareToLeafNode:(FLeafNode *)other {
  200. NSString *thisLeafType = [FUtilities getJavascriptType:self.value];
  201. NSString *otherLeafType = [FUtilities getJavascriptType:other.value];
  202. NSUInteger thisIndex =
  203. [[FLeafNode valueTypeOrder] indexOfObject:thisLeafType];
  204. NSUInteger otherIndex =
  205. [[FLeafNode valueTypeOrder] indexOfObject:otherLeafType];
  206. assert(thisIndex >= 0 && otherIndex >= 0);
  207. if (otherIndex == thisIndex) {
  208. // Same type. Compare values.
  209. if (thisLeafType == kJavaScriptObject) {
  210. // Deferred value nodes are all equal, but we should also never get
  211. // to this point...
  212. return NSOrderedSame;
  213. } else if (thisLeafType == kJavaScriptString) {
  214. return [self.value compare:other.value options:NSLiteralSearch];
  215. } else {
  216. return [self.value compare:other.value];
  217. }
  218. } else {
  219. return thisIndex > otherIndex ? NSOrderedDescending
  220. : NSOrderedAscending;
  221. }
  222. }
  223. - (NSString *)description {
  224. return [[self valForExport:YES] description];
  225. }
  226. - (void)forEachChildDo:(fbt_bool_nsstring_node)action {
  227. // There are no children, so there is nothing to do.
  228. return;
  229. }
  230. @end