FMerge.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "FMerge.h"
  17. #import "FOperationSource.h"
  18. #import "FPath.h"
  19. #import "FNode.h"
  20. #import "FOverwrite.h"
  21. #import "FCompoundWrite.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 path:(FPath *)aPath children:(FCompoundWrite *)someChildren {
  34. self = [super init];
  35. if (self) {
  36. self.source = aSource;
  37. self.type = FOperationTypeMerge;
  38. self.path = aPath;
  39. self.children = someChildren;
  40. }
  41. return self;
  42. }
  43. - (id<FOperation>) operationForChild:(NSString *)childKey {
  44. if ([self.path isEmpty]) {
  45. FCompoundWrite *childTree = [self.children childCompoundWriteAtPath:[[FPath alloc] initWith:childKey]];
  46. if (childTree.isEmpty) {
  47. return nil;
  48. } else if (childTree.rootWrite != nil) {
  49. // We have a snapshot for the child in question. This becomes an overwrite of the child.
  50. return [[FOverwrite alloc] initWithSource:self.source path:[FPath empty] snap:childTree.rootWrite];
  51. } else {
  52. // This is a merge at a deeper level
  53. return [[FMerge alloc] initWithSource:self.source path:[FPath empty] children:childTree];
  54. }
  55. } else {
  56. NSAssert([self.path.getFront isEqualToString:childKey], @"Can't get a merge for a child not on the path of the operation");
  57. return [[FMerge alloc] initWithSource:self.source path:[self.path popFront] children:self.children];
  58. }
  59. }
  60. - (NSString *) description {
  61. return [NSString stringWithFormat:@"FMerge { path=%@, soruce=%@ children=%@}", self.path, self.source, self.children];
  62. }
  63. @end