FWriteTreeRef.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/FWriteTreeRef.h"
  17. #import "FirebaseDatabase/Sources/Core/FWriteRecord.h"
  18. #import "FirebaseDatabase/Sources/Core/FWriteTree.h"
  19. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  20. #import "FirebaseDatabase/Sources/Core/View/FCacheNode.h"
  21. #import "FirebaseDatabase/Sources/FIndex.h"
  22. #import "FirebaseDatabase/Sources/FNamedNode.h"
  23. #import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
  24. #import "FirebaseDatabase/Sources/Snapshot/FNode.h"
  25. @interface FWriteTreeRef ()
  26. /**
  27. * The path to this particular FWriteTreeRef. Used for calling methods on
  28. * writeTree while exposing a simpler interface to callers.
  29. */
  30. @property(nonatomic, strong) FPath *path;
  31. /**
  32. * A reference to the actual tree of the write data. All methods are
  33. * pass-through to the tree, but with the appropriate path prefixed.
  34. *
  35. * This lets us make cheap references to points in the tree for sync points
  36. * without having to copy and maintain all of the data.
  37. */
  38. @property(nonatomic, strong) FWriteTree *writeTree;
  39. @end
  40. /**
  41. * A FWriteTreeRef wraps a FWriteTree and a FPath, for convenient access to a
  42. * particular subtree. All the methods just proxy to the underlying FWriteTree.
  43. */
  44. @implementation FWriteTreeRef
  45. - (id)initWithPath:(FPath *)aPath writeTree:(FWriteTree *)tree {
  46. self = [super init];
  47. if (self) {
  48. self.path = aPath;
  49. self.writeTree = tree;
  50. }
  51. return self;
  52. }
  53. /**
  54. * @return If possible, returns a complete event cache, using the underlying
  55. * server data if possible. In addition, can be used to get a cache that
  56. * includes hidden writes, and excludes arbitrary writes. Note that customizing
  57. * the returned node can lead to a more expensive calculation.
  58. */
  59. - (id<FNode>)calculateCompleteEventCacheWithCompleteServerCache:
  60. (id<FNode>)completeServerCache {
  61. return [self.writeTree calculateCompleteEventCacheAtPath:self.path
  62. completeServerCache:completeServerCache
  63. excludeWriteIds:nil
  64. includeHiddenWrites:NO];
  65. }
  66. /**
  67. * @return If possible, returns a children node containing all of the complete
  68. * children we have data for. The returned data is a mix of the given server
  69. * data and write data.
  70. */
  71. - (FChildrenNode *)calculateCompleteEventChildrenWithCompleteServerChildren:
  72. (id<FNode>)completeServerChildren {
  73. return [self.writeTree
  74. calculateCompleteEventChildrenAtPath:self.path
  75. completeServerChildren:completeServerChildren];
  76. }
  77. /**
  78. * Given that either the underlying server data has updated or the outstanding
  79. * writes have been updating, determine what, if anything, needs to be applied
  80. * to the event cache.
  81. *
  82. * Possibilities:
  83. *
  84. * 1. No writes are shadowing. Events should be raised, the snap to be applied
  85. * comes from the server data.
  86. *
  87. * 2. Some writes are completly shadowing. No events to be raised.
  88. *
  89. * 3. Is partially shadowed. Events should be raised.
  90. *
  91. * Either existingEventSnap or existingServerSnap must exist, this is validated
  92. * via an assert.
  93. */
  94. - (id<FNode>)
  95. calculateEventCacheAfterServerOverwriteWithChildPath:(FPath *)childPath
  96. existingEventSnap:
  97. (id<FNode>)existingEventSnap
  98. existingServerSnap:
  99. (id<FNode>)existingServerSnap {
  100. return [self.writeTree
  101. calculateEventCacheAfterServerOverwriteAtPath:self.path
  102. childPath:childPath
  103. existingEventSnap:existingEventSnap
  104. existingServerSnap:existingServerSnap];
  105. }
  106. /**
  107. * Returns a node if there is a complete overwrite for this path. More
  108. * specifically, if there is a write at a higher path, this will return the
  109. * child of that write relative to the write and this path. Returns nil if there
  110. * is no write at this path.
  111. */
  112. - (id<FNode>)shadowingWriteAtPath:(FPath *)path {
  113. return [self.writeTree shadowingWriteAtPath:[self.path child:path]];
  114. }
  115. /**
  116. * This method is used when processing child remove events on a query. If we
  117. * can, we pull in children that are outside the window, but may now be in the
  118. * window.
  119. */
  120. - (FNamedNode *)calculateNextNodeAfterPost:(FNamedNode *)post
  121. completeServerData:(id<FNode>)completeServerData
  122. reverse:(BOOL)reverse
  123. index:(id<FIndex>)index {
  124. return [self.writeTree calculateNextNodeAfterPost:post
  125. atPath:self.path
  126. completeServerData:completeServerData
  127. reverse:reverse
  128. index:index];
  129. }
  130. /**
  131. * Returns a complete child for a given server snap after applying all user
  132. * writes or nil if there is no complete child for this child key.
  133. */
  134. - (id<FNode>)calculateCompleteChild:(NSString *)childKey
  135. cache:(FCacheNode *)existingServerCache {
  136. return [self.writeTree calculateCompleteChildAtPath:self.path
  137. childKey:childKey
  138. cache:existingServerCache];
  139. }
  140. /**
  141. * @return a WriteTreeref for a child.
  142. */
  143. - (FWriteTreeRef *)childWriteTreeRef:(NSString *)childKey {
  144. return
  145. [[FWriteTreeRef alloc] initWithPath:[self.path childFromString:childKey]
  146. writeTree:self.writeTree];
  147. }
  148. @end