All files / core/snap IndexMap.ts

98.68% Statements 75/76
95.83% Branches 23/24
100% Functions 10/10
100% Lines 73/73
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                                15x 15x 15x 15x 15x 15x           15x               15x           15x 562x       562x           562x       1784x     1784x               15x 4520x 4520x   4520x     3550x   970x               15x 4320x               15x       47x       47x 47x 47x 47x 47x 67x   67x 67x     47x 17x   30x   47x 47x 47x 47x 47x 47x                 1369x       1369x     1380x 1380x 1380x   1125x   117x 117x 117x 117x 11x 8x   11x   117x 117x     1008x     255x 255x 255x 41x       255x       1369x                 15x       234x     245x   185x   60x 60x 59x         1x       234x   15x  
/**
 * 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 { assert } from '@firebase/util';
import { buildChildSet } from './childSet';
import { contains, clone, map, safeGet } from '@firebase/util';
import { NamedNode, Node } from './Node';
import { PRIORITY_INDEX } from './indexes/PriorityIndex';
import { KEY_INDEX } from './indexes/KeyIndex';
import { SortedMap } from '../util/SortedMap';
import { Index } from './indexes/Index';
 
let _defaultIndexMap: IndexMap;
 
const fallbackObject = {};
 
/**
 *
 * @param {Object.<string, FallbackType|SortedMap.<NamedNode, Node>>} indexes
 * @param {Object.<string, Index>} indexSet
 * @constructor
 */
export class IndexMap {
  /**
   * The default IndexMap for nodes without a priority
   * @type {!IndexMap}
   * @const
   */
  static get Default(): IndexMap {
    assert(
      fallbackObject && PRIORITY_INDEX,
      'ChildrenNode.ts has not been loaded'
    );
    _defaultIndexMap =
      _defaultIndexMap ||
      new IndexMap(
        { '.priority': fallbackObject },
        { '.priority': PRIORITY_INDEX }
      );
    return _defaultIndexMap;
  }
 
  constructor(
    private indexes_: {
      [k: string]: SortedMap<NamedNode, Node> | /*FallbackType*/ object;
    },
    private indexSet_: { [k: string]: Index }
  ) {}
 
  /**
   *
   * @param {!string} indexKey
   * @return {?SortedMap.<NamedNode, Node>}
   */
  get(indexKey: string): SortedMap<NamedNode, Node> | null {
    const sortedMap = safeGet(this.indexes_, indexKey);
    Iif (!sortedMap) throw new Error('No index defined for ' + indexKey);
 
    if (sortedMap === fallbackObject) {
      // The index exists, but it falls back to just name comparison. Return null so that the calling code uses the
      // regular child map
      return null;
    } else {
      return sortedMap;
    }
  }
 
  /**
   * @param {!Index} indexDefinition
   * @return {boolean}
   */
  hasIndex(indexDefinition: Index): boolean {
    return contains(this.indexSet_, indexDefinition.toString());
  }
 
  /**
   * @param {!Index} indexDefinition
   * @param {!SortedMap.<string, !Node>} existingChildren
   * @return {!IndexMap}
   */
  addIndex(
    indexDefinition: Index,
    existingChildren: SortedMap<string, Node>
  ): IndexMap {
    assert(
      indexDefinition !== KEY_INDEX,
      "KeyIndex always exists and isn't meant to be added to the IndexMap."
    );
    const childList = [];
    let sawIndexedValue = false;
    const iter = existingChildren.getIterator(NamedNode.Wrap);
    let next = iter.getNext();
    while (next) {
      sawIndexedValue =
        sawIndexedValue || indexDefinition.isDefinedOn(next.node);
      childList.push(next);
      next = iter.getNext();
    }
    let newIndex;
    if (sawIndexedValue) {
      newIndex = buildChildSet(childList, indexDefinition.getCompare());
    } else {
      newIndex = fallbackObject;
    }
    const indexName = indexDefinition.toString();
    const newIndexSet = clone(this.indexSet_);
    newIndexSet[indexName] = indexDefinition;
    const newIndexes = clone(this.indexes_);
    newIndexes[indexName] = newIndex;
    return new IndexMap(newIndexes, newIndexSet);
  }
 
  /**
   * Ensure that this node is properly tracked in any indexes that we're maintaining
   * @param {!NamedNode} namedNode
   * @param {!SortedMap.<string, !Node>} existingChildren
   * @return {!IndexMap}
   */
  addToIndexes(
    namedNode: NamedNode,
    existingChildren: SortedMap<string, Node>
  ): IndexMap {
    const newIndexes = map(
      this.indexes_,
      (indexedChildren: SortedMap<NamedNode, Node>, indexName: string) => {
        const index = safeGet(this.indexSet_, indexName);
        assert(index, 'Missing index implementation for ' + indexName);
        if (indexedChildren === fallbackObject) {
          // Check to see if we need to index everything
          if (index.isDefinedOn(namedNode.node)) {
            // We need to build this index
            const childList = [];
            const iter = existingChildren.getIterator(NamedNode.Wrap);
            let next = iter.getNext();
            while (next) {
              if (next.name != namedNode.name) {
                childList.push(next);
              }
              next = iter.getNext();
            }
            childList.push(namedNode);
            return buildChildSet(childList, index.getCompare());
          } else {
            // No change, this remains a fallback
            return fallbackObject;
          }
        } else {
          const existingSnap = existingChildren.get(namedNode.name);
          let newChildren = indexedChildren;
          if (existingSnap) {
            newChildren = newChildren.remove(
              new NamedNode(namedNode.name, existingSnap)
            );
          }
          return newChildren.insert(namedNode, namedNode.node);
        }
      }
    );
    return new IndexMap(newIndexes, this.indexSet_);
  }
 
  /**
   * Create a new IndexMap instance with the given value removed
   * @param {!NamedNode} namedNode
   * @param {!SortedMap.<string, !Node>} existingChildren
   * @return {!IndexMap}
   */
  removeFromIndexes(
    namedNode: NamedNode,
    existingChildren: SortedMap<string, Node>
  ): IndexMap {
    const newIndexes = map(this.indexes_, function(
      indexedChildren: SortedMap<NamedNode, Node>
    ) {
      if (indexedChildren === fallbackObject) {
        // This is the fallback. Just return it, nothing to do in this case
        return indexedChildren;
      } else {
        const existingSnap = existingChildren.get(namedNode.name);
        if (existingSnap) {
          return indexedChildren.remove(
            new NamedNode(namedNode.name, existingSnap)
          );
        } else {
          // No record of this child
          return indexedChildren;
        }
      }
    });
    return new IndexMap(newIndexes, this.indexSet_);
  }
}