FSparseSnapshotTree.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/Core/FSparseSnapshotTree.h"
  17. #import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
  18. @interface FSparseSnapshotTree () {
  19. id<FNode> value;
  20. NSMutableDictionary *children;
  21. }
  22. @end
  23. @implementation FSparseSnapshotTree
  24. - (id)init {
  25. self = [super init];
  26. if (self) {
  27. value = nil;
  28. children = nil;
  29. }
  30. return self;
  31. }
  32. - (id<FNode>)findPath:(FPath *)path {
  33. if (value != nil) {
  34. return [value getChild:path];
  35. } else if (![path isEmpty] && children != nil) {
  36. NSString *childKey = [path getFront];
  37. path = [path popFront];
  38. FSparseSnapshotTree *childTree = children[childKey];
  39. if (childTree != nil) {
  40. return [childTree findPath:path];
  41. } else {
  42. return nil;
  43. }
  44. } else {
  45. return nil;
  46. }
  47. }
  48. - (void)rememberData:(id<FNode>)data onPath:(FPath *)path {
  49. if ([path isEmpty]) {
  50. value = data;
  51. children = nil;
  52. } else if (value != nil) {
  53. value = [value updateChild:path withNewChild:data];
  54. } else {
  55. if (children == nil) {
  56. children = [[NSMutableDictionary alloc] init];
  57. }
  58. NSString *childKey = [path getFront];
  59. if (children[childKey] == nil) {
  60. children[childKey] = [[FSparseSnapshotTree alloc] init];
  61. }
  62. FSparseSnapshotTree *child = children[childKey];
  63. path = [path popFront];
  64. [child rememberData:data onPath:path];
  65. }
  66. }
  67. - (BOOL)forgetPath:(FPath *)path {
  68. if ([path isEmpty]) {
  69. value = nil;
  70. children = nil;
  71. return YES;
  72. } else {
  73. if (value != nil) {
  74. if ([value isLeafNode]) {
  75. // non-empty path at leaf. the path leads to nowhere
  76. return NO;
  77. } else {
  78. id<FNode> tmp = value;
  79. value = nil;
  80. [tmp enumerateChildrenUsingBlock:^(NSString *key,
  81. id<FNode> node, BOOL *stop) {
  82. [self rememberData:node onPath:[[FPath alloc] initWith:key]];
  83. }];
  84. // we've cleared out the value and set children. Call ourself
  85. // again to hit the next case
  86. return [self forgetPath:path];
  87. }
  88. } else if (children != nil) {
  89. NSString *childKey = [path getFront];
  90. path = [path popFront];
  91. if (children[childKey] != nil) {
  92. FSparseSnapshotTree *child = children[childKey];
  93. BOOL safeToRemove = [child forgetPath:path];
  94. if (safeToRemove) {
  95. [children removeObjectForKey:childKey];
  96. }
  97. }
  98. if ([children count] == 0) {
  99. children = nil;
  100. return YES;
  101. } else {
  102. return NO;
  103. }
  104. } else {
  105. return YES;
  106. }
  107. }
  108. }
  109. - (void)forEachTreeAtPath:(FPath *)prefixPath do:(fbt_void_path_node)func {
  110. if (value != nil) {
  111. func(prefixPath, value);
  112. } else {
  113. [self forEachChild:^(NSString *key, FSparseSnapshotTree *tree) {
  114. FPath *path = [prefixPath childFromString:key];
  115. [tree forEachTreeAtPath:path do:func];
  116. }];
  117. }
  118. }
  119. - (void)forEachChild:(fbt_void_nsstring_sstree)func {
  120. if (children != nil) {
  121. for (NSString *key in children) {
  122. FSparseSnapshotTree *tree = [children objectForKey:key];
  123. func(key, tree);
  124. }
  125. }
  126. }
  127. @end