FIRMutableData.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Public/FirebaseDatabase/FIRMutableData.h"
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRMutableData_Private.h"
  18. #import "FirebaseDatabase/Sources/Core/FSnapshotHolder.h"
  19. #import "FirebaseDatabase/Sources/FNamedNode.h"
  20. #import "FirebaseDatabase/Sources/FTransformedEnumerator.h"
  21. #import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
  22. #import "FirebaseDatabase/Sources/Snapshot/FIndexedNode.h"
  23. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  24. @interface FIRMutableData ()
  25. - (id)initWithPrefixPath:(FPath *)path
  26. andSnapshotHolder:(FSnapshotHolder *)snapshotHolder;
  27. @property(strong, nonatomic) FSnapshotHolder *data;
  28. @property(strong, nonatomic) FPath *prefixPath;
  29. @end
  30. @implementation FIRMutableData
  31. @synthesize data;
  32. @synthesize prefixPath;
  33. - (id)initWithNode:(id<FNode>)node {
  34. FSnapshotHolder *holder = [[FSnapshotHolder alloc] init];
  35. FPath *path = [FPath empty];
  36. [holder updateSnapshot:path withNewSnapshot:node];
  37. return [self initWithPrefixPath:path andSnapshotHolder:holder];
  38. }
  39. - (id)initWithPrefixPath:(FPath *)path
  40. andSnapshotHolder:(FSnapshotHolder *)snapshotHolder {
  41. self = [super init];
  42. if (self) {
  43. self.prefixPath = path;
  44. self.data = snapshotHolder;
  45. }
  46. return self;
  47. }
  48. - (FIRMutableData *)childDataByAppendingPath:(NSString *)path {
  49. FPath *wholePath = [self.prefixPath childFromString:path];
  50. return [[FIRMutableData alloc] initWithPrefixPath:wholePath
  51. andSnapshotHolder:self.data];
  52. }
  53. - (FIRMutableData *)parent {
  54. if ([self.prefixPath isEmpty]) {
  55. return nil;
  56. } else {
  57. FPath *path = [self.prefixPath parent];
  58. return [[FIRMutableData alloc] initWithPrefixPath:path
  59. andSnapshotHolder:self.data];
  60. }
  61. }
  62. - (void)setValue:(id)aValue {
  63. id<FNode> node = [FSnapshotUtilities nodeFrom:aValue
  64. withValidationFrom:@"setValue:"];
  65. [self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
  66. }
  67. - (void)setPriority:(id)aPriority {
  68. id<FNode> node = [self.data getNode:self.prefixPath];
  69. id<FNode> pri = [FSnapshotUtilities nodeFrom:aPriority];
  70. node = [node updatePriority:pri];
  71. [self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
  72. }
  73. - (id)value {
  74. return [[self.data getNode:self.prefixPath] val];
  75. }
  76. - (id)priority {
  77. return [[[self.data getNode:self.prefixPath] getPriority] val];
  78. }
  79. - (BOOL)hasChildren {
  80. id<FNode> node = [self.data getNode:self.prefixPath];
  81. return ![node isLeafNode] && ![(FChildrenNode *)node isEmpty];
  82. }
  83. - (BOOL)hasChildAtPath:(NSString *)path {
  84. id<FNode> node = [self.data getNode:self.prefixPath];
  85. FPath *childPath = [[FPath alloc] initWith:path];
  86. return ![[node getChild:childPath] isEmpty];
  87. }
  88. - (NSUInteger)childrenCount {
  89. return [[self.data getNode:self.prefixPath] numChildren];
  90. }
  91. - (NSString *)key {
  92. return [self.prefixPath getBack];
  93. }
  94. - (id<FNode>)nodeValue {
  95. return [self.data getNode:self.prefixPath];
  96. }
  97. - (NSEnumerator<FIRMutableData *> *)children {
  98. FIndexedNode *indexedNode =
  99. [FIndexedNode indexedNodeWithNode:self.nodeValue];
  100. return [[FTransformedEnumerator alloc]
  101. initWithEnumerator:[indexedNode childEnumerator]
  102. andTransform:^id(FNamedNode *node) {
  103. FPath *childPath = [self.prefixPath childFromString:node.name];
  104. FIRMutableData *childData =
  105. [[FIRMutableData alloc] initWithPrefixPath:childPath
  106. andSnapshotHolder:self.data];
  107. return childData;
  108. }];
  109. }
  110. - (BOOL)isEqualToData:(FIRMutableData *)other {
  111. return self.data == other.data &&
  112. [[self.prefixPath description]
  113. isEqualToString:[other.prefixPath description]];
  114. }
  115. - (NSString *)description {
  116. if (self.key == nil) {
  117. return [NSString
  118. stringWithFormat:@"FIRMutableData (top-most transaction) %@ %@",
  119. self.key, self.value];
  120. } else {
  121. return [NSString
  122. stringWithFormat:@"FIRMutableData (%@) %@", self.key, self.value];
  123. }
  124. }
  125. @end