All files / core CompoundWrite.ts

100% Statements 75/75
96.15% Branches 25/26
100% Functions 16/16
100% Lines 74/74
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                13x 13x 13x 13x 13x 13x                       13x 3506x       13x             13x 908x 58x   850x 850x 229x 229x 229x 229x 229x   621x 621x 621x                   13x 23x 23x 53x   23x                   13x 409x 1x   408x 408x                     13x 95x                   13x 4661x 4661x 514x       4147x                 13x 588x 588x 588x   1x 1x       2x       587x 130x 124x       588x             13x 2178x 1x   2177x 2177x 182x   1995x                 13x 599x                 13x 1039x                   13x         1126x   134x   992x 992x 107x     20x       20x   87x               992x 17x   992x     13x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { ImmutableTree } from './util/ImmutableTree';
import { Path } from './util/Path';
import { forEach } from '@firebase/util';
import { Node, NamedNode } from './snap/Node';
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
import { assert } from '@firebase/util';
import { ChildrenNode } from './snap/ChildrenNode';
 
/**
 * This class holds a collection of writes that can be applied to nodes in unison. It abstracts away the logic with
 * dealing with priority writes and multiple nested writes. At any given path there is only allowed to be one write
 * modifying that path. Any write to an existing path or shadowing an existing path will modify that existing write
 * to reflect the write added.
 *
 * @constructor
 * @param {!ImmutableTree.<!Node>} writeTree
 */
export class CompoundWrite {
  constructor(private writeTree_: ImmutableTree<Node>) {}
  /**
   * @type {!CompoundWrite}
   */
  static Empty = new CompoundWrite(new ImmutableTree(null));
 
  /**
   * @param {!Path} path
   * @param {!Node} node
   * @return {!CompoundWrite}
   */
  addWrite(path: Path, node: Node): CompoundWrite {
    if (path.isEmpty()) {
      return new CompoundWrite(new ImmutableTree(node));
    } else {
      const rootmost = this.writeTree_.findRootMostValueAndPath(path);
      if (rootmost != null) {
        const rootMostPath = rootmost.path;
        let value = rootmost.value;
        const relativePath = Path.relativePath(rootMostPath, path);
        value = value.updateChild(relativePath, node);
        return new CompoundWrite(this.writeTree_.set(rootMostPath, value));
      } else {
        const subtree = new ImmutableTree(node);
        const newWriteTree = this.writeTree_.setTree(path, subtree);
        return new CompoundWrite(newWriteTree);
      }
    }
  }
 
  /**
   * @param {!Path} path
   * @param {!Object.<string, !Node>} updates
   * @return {!CompoundWrite}
   */
  addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
    let newWrite = this as CompoundWrite;
    forEach(updates, function(childKey: string, node: Node) {
      newWrite = newWrite.addWrite(path.child(childKey), node);
    });
    return newWrite;
  }
 
  /**
   * Will remove a write at the given path and deeper paths. This will <em>not</em> modify a write at a higher
   * location, which must be removed by calling this method with that path.
   *
   * @param {!Path} path The path at which a write and all deeper writes should be removed
   * @return {!CompoundWrite} The new CompoundWrite with the removed path
   */
  removeWrite(path: Path): CompoundWrite {
    if (path.isEmpty()) {
      return CompoundWrite.Empty;
    } else {
      const newWriteTree = this.writeTree_.setTree(path, ImmutableTree.Empty);
      return new CompoundWrite(newWriteTree);
    }
  }
 
  /**
   * Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
   * considered "complete".
   *
   * @param {!Path} path The path to check for
   * @return {boolean} Whether there is a complete write at that path
   */
  hasCompleteWrite(path: Path): boolean {
    return this.getCompleteNode(path) != null;
  }
 
  /**
   * Returns a node for a path if and only if the node is a "complete" overwrite at that path. This will not aggregate
   * writes from deeper paths, but will return child nodes from a more shallow path.
   *
   * @param {!Path} path The path to get a complete write
   * @return {?Node} The node if complete at that path, or null otherwise.
   */
  getCompleteNode(path: Path): Node | null {
    const rootmost = this.writeTree_.findRootMostValueAndPath(path);
    if (rootmost != null) {
      return this.writeTree_
        .get(rootmost.path)
        .getChild(Path.relativePath(rootmost.path, path));
    } else {
      return null;
    }
  }
 
  /**
   * Returns all children that are guaranteed to be a complete overwrite.
   *
   * @return {!Array.<NamedNode>} A list of all complete children.
   */
  getCompleteChildren(): Array<NamedNode> {
    const children: NamedNode[] = [];
    let node = this.writeTree_.value;
    if (node != null) {
      // If it's a leaf node, it has no children; so nothing to do.
      Eif (!node.isLeafNode()) {
        (node as ChildrenNode).forEachChild(PRIORITY_INDEX, function(
          childName,
          childNode
        ) {
          children.push(new NamedNode(childName, childNode));
        });
      }
    } else {
      this.writeTree_.children.inorderTraversal(function(childName, childTree) {
        if (childTree.value != null) {
          children.push(new NamedNode(childName, childTree.value));
        }
      });
    }
    return children;
  }
 
  /**
   * @param {!Path} path
   * @return {!CompoundWrite}
   */
  childCompoundWrite(path: Path): CompoundWrite {
    if (path.isEmpty()) {
      return this;
    } else {
      const shadowingNode = this.getCompleteNode(path);
      if (shadowingNode != null) {
        return new CompoundWrite(new ImmutableTree(shadowingNode));
      } else {
        return new CompoundWrite(this.writeTree_.subtree(path));
      }
    }
  }
 
  /**
   * Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
   * @return {boolean} Whether this CompoundWrite is empty
   */
  isEmpty(): boolean {
    return this.writeTree_.isEmpty();
  }
 
  /**
   * Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
   * node
   * @param {!Node} node The node to apply this CompoundWrite to
   * @return {!Node} The node with all writes applied
   */
  apply(node: Node): Node {
    return CompoundWrite.applySubtreeWrite_(Path.Empty, this.writeTree_, node);
  }
 
  /**
   * @param {!Path} relativePath
   * @param {!ImmutableTree.<!Node>} writeTree
   * @param {!Node} node
   * @return {!Node}
   * @private
   */
  private static applySubtreeWrite_ = function(
    relativePath: Path,
    writeTree: ImmutableTree<Node>,
    node: Node
  ): Node {
    if (writeTree.value != null) {
      // Since there a write is always a leaf, we're done here
      return node.updateChild(relativePath, writeTree.value);
    } else {
      let priorityWrite = null;
      writeTree.children.inorderTraversal(function(childKey, childTree) {
        if (childKey === '.priority') {
          // Apply priorities at the end so we don't update priorities for either empty nodes or forget
          // to apply priorities to empty nodes that are later filled
          assert(
            childTree.value !== null,
            'Priority writes must always be leaf nodes'
          );
          priorityWrite = childTree.value;
        } else {
          node = CompoundWrite.applySubtreeWrite_(
            relativePath.child(childKey),
            childTree,
            node
          );
        }
      });
      // If there was a priority write, we only apply it if the node is not empty
      if (!node.getChild(relativePath).isEmpty() && priorityWrite !== null) {
        node = node.updateChild(relativePath.child('.priority'), priorityWrite);
      }
      return node;
    }
  };
}