FWriteTreeRef.m 5.2 KB

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