FMerge.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/Operation/FMerge.h"
  17. #import "FirebaseDatabase/Sources/Core/Operation/FOperationSource.h"
  18. #import "FirebaseDatabase/Sources/Core/Operation/FOverwrite.h"
  19. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  20. #import "FirebaseDatabase/Sources/Snapshot/FCompoundWrite.h"
  21. #import "FirebaseDatabase/Sources/Snapshot/FNode.h"
  22. @interface FMerge ()
  23. @property(nonatomic, strong, readwrite) FOperationSource *source;
  24. @property(nonatomic, readwrite) FOperationType type;
  25. @property(nonatomic, strong, readwrite) FPath *path;
  26. @property(nonatomic, strong) FCompoundWrite *children;
  27. @end
  28. @implementation FMerge
  29. @synthesize source;
  30. @synthesize type;
  31. @synthesize path;
  32. @synthesize children;
  33. - (id)initWithSource:(FOperationSource *)aSource
  34. path:(FPath *)aPath
  35. children:(FCompoundWrite *)someChildren {
  36. self = [super init];
  37. if (self) {
  38. self.source = aSource;
  39. self.type = FOperationTypeMerge;
  40. self.path = aPath;
  41. self.children = someChildren;
  42. }
  43. return self;
  44. }
  45. - (id<FOperation>)operationForChild:(NSString *)childKey {
  46. if ([self.path isEmpty]) {
  47. FCompoundWrite *childTree = [self.children
  48. childCompoundWriteAtPath:[[FPath alloc] initWith:childKey]];
  49. if (childTree.isEmpty) {
  50. return nil;
  51. } else if (childTree.rootWrite != nil) {
  52. // We have a snapshot for the child in question. This becomes an
  53. // overwrite of the child.
  54. return [[FOverwrite alloc] initWithSource:self.source
  55. path:[FPath empty]
  56. snap:childTree.rootWrite];
  57. } else {
  58. // This is a merge at a deeper level
  59. return [[FMerge alloc] initWithSource:self.source
  60. path:[FPath empty]
  61. children:childTree];
  62. }
  63. } else {
  64. NSAssert(
  65. [self.path.getFront isEqualToString:childKey],
  66. @"Can't get a merge for a child not on the path of the operation");
  67. return [[FMerge alloc] initWithSource:self.source
  68. path:[self.path popFront]
  69. children:self.children];
  70. }
  71. }
  72. - (NSString *)description {
  73. return
  74. [NSString stringWithFormat:@"FMerge { path=%@, soruce=%@ children=%@}",
  75. self.path, self.source, self.children];
  76. }
  77. @end