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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 2833x 15x 9088x 2833x 2833x 2833x 2833x 1635x 2833x 117x 15x 7886x 15x 9181x 15x 539x 37x 502x 15x 4875x 2x 4873x 4873x 15x 3879x 3879x 2716x 15x 2546x 15x 1605x 1605x 2x 1603x 1603x 1603x 234x 234x 1369x 1369x 1603x 1603x 15x 993x 993x 556x 437x 437x 437x 15x 22831x 15x 672x 15x 15x 3278x 535x 535x 535x 535x 535x 1363x 1363x 1363x 229x 1134x 535x 3x 12x 3x 532x 100x 532x 15x 492x 150x 150x 15x 150x 274x 274x 150x 492x 15x 688x 688x 267x 267x 421x 15x 118x 118x 37x 37x 81x 15x 118x 118x 118x 15x 12x 12x 12x 15x 12x 12x 12x 15x 3240x 3240x 603x 2074x 2637x 15x 232x 15x 235x 235x 34x 220x 220x 220x 220x 15x 234x 15x 259x 259x 48x 121x 211x 211x 211x 211x 15x 2776x 2776x 1651x 1125x 15x 2101x 2054x 47x 47x 15x 2262x 15x 1677x 1323x 354x 270x 84x 84x 84x 61x 61x 61x 61x 61x 82x 9x 73x 73x 52x 23x 15x 4552x 32x 4520x 15x 15x 15x 15x 15x 5x 15x 15x 810x 15x 15x 15x 15x 15x 15x 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 { sha1, MAX_NAME, MIN_NAME } from '../util/util'; import { SortedMap, SortedMapIterator } from '../util/SortedMap'; import { Node, NamedNode } from './Node'; import { validatePriorityNode, priorityHashText, setMaxNode } from './snap'; import { PRIORITY_INDEX, setMaxNode as setPriorityMaxNode } from './indexes/PriorityIndex'; import { KEY_INDEX, KeyIndex } from './indexes/KeyIndex'; import { IndexMap } from './IndexMap'; import { LeafNode } from './LeafNode'; import { NAME_COMPARATOR } from './comparators'; import { Index } from './indexes/Index'; import { Path } from '../util/Path'; export interface ChildrenNodeConstructor { new ( children_: SortedMap<string, Node>, priorityNode_: Node | null, indexMap_: IndexMap ): ChildrenNode; EMPTY_NODE: ChildrenNode; } // TODO: For memory savings, don't store priorityNode_ if it's empty. let EMPTY_NODE: ChildrenNode; /** * ChildrenNode is a class for storing internal nodes in a DataSnapshot * (i.e. nodes with children). It implements Node and stores the * list of children in the children property, sorted by child name. * * @constructor * @implements {Node} */ export class ChildrenNode implements Node { private lazyHash_: string | null = null; static get EMPTY_NODE(): ChildrenNode { return ( EMPTY_NODE || (EMPTY_NODE = new ChildrenNode( new SortedMap<string, Node>(NAME_COMPARATOR), null, IndexMap.Default )) ); } /** * * @param {!SortedMap.<string, !Node>} children_ List of children * of this node.. * @param {?Node} priorityNode_ The priority of this node (as a snapshot node). * @param {!IndexMap} indexMap_ */ constructor( private readonly children_: SortedMap<string, Node>, private readonly priorityNode_: Node | null, private indexMap_: IndexMap ) { /** * Note: The only reason we allow null priority is for EMPTY_NODE, since we can't use * EMPTY_NODE as the priority of EMPTY_NODE. We might want to consider making EMPTY_NODE its own * class instead of an empty ChildrenNode. */ if (this.priorityNode_) { validatePriorityNode(this.priorityNode_); } if (this.children_.isEmpty()) { assert( !this.priorityNode_ || this.priorityNode_.isEmpty(), 'An empty node cannot have a priority' ); } } /** @inheritDoc */ isLeafNode(): boolean { return false; } /** @inheritDoc */ getPriority(): Node { return this.priorityNode_ || EMPTY_NODE; } /** @inheritDoc */ updatePriority(newPriorityNode: Node): Node { if (this.children_.isEmpty()) { // Don't allow priorities on empty nodes return this; } else { return new ChildrenNode(this.children_, newPriorityNode, this.indexMap_); } } /** @inheritDoc */ getImmediateChild(childName: string): Node { // Hack to treat priority as a regular child if (childName === '.priority') { return this.getPriority(); } else { const child = this.children_.get(childName); return child === null ? EMPTY_NODE : child; } } /** @inheritDoc */ getChild(path: Path): Node { const front = path.getFront(); if (front === null) return this; return this.getImmediateChild(front).getChild(path.popFront()); } /** @inheritDoc */ hasChild(childName: string): boolean { return this.children_.get(childName) !== null; } /** @inheritDoc */ updateImmediateChild(childName: string, newChildNode: Node): Node { assert(newChildNode, 'We should always be passing snapshot nodes'); if (childName === '.priority') { return this.updatePriority(newChildNode); } else { const namedNode = new NamedNode(childName, newChildNode); let newChildren, newIndexMap, newPriority; if (newChildNode.isEmpty()) { newChildren = this.children_.remove(childName); newIndexMap = this.indexMap_.removeFromIndexes( namedNode, this.children_ ); } else { newChildren = this.children_.insert(childName, newChildNode); newIndexMap = this.indexMap_.addToIndexes(namedNode, this.children_); } newPriority = newChildren.isEmpty() ? EMPTY_NODE : this.priorityNode_; return new ChildrenNode(newChildren, newPriority, newIndexMap); } } /** @inheritDoc */ updateChild(path: Path, newChildNode: Node): Node { const front = path.getFront(); if (front === null) { return newChildNode; } else { assert( path.getFront() !== '.priority' || path.getLength() === 1, '.priority must be the last token in a path' ); const newImmediateChild = this.getImmediateChild(front).updateChild( path.popFront(), newChildNode ); return this.updateImmediateChild(front, newImmediateChild); } } /** @inheritDoc */ isEmpty(): boolean { return this.children_.isEmpty(); } /** @inheritDoc */ numChildren(): number { return this.children_.count(); } /** * @private * @type {RegExp} */ private static INTEGER_REGEXP_ = /^(0|[1-9]\d*)$/; /** @inheritDoc */ val(exportFormat?: boolean): object { if (this.isEmpty()) return null; const obj: { [k: string]: Object } = {}; let numKeys = 0, maxKey = 0, allIntegerKeys = true; this.forEachChild(PRIORITY_INDEX, function(key: string, childNode: Node) { obj[key] = childNode.val(exportFormat); numKeys++; if (allIntegerKeys && ChildrenNode.INTEGER_REGEXP_.test(key)) { maxKey = Math.max(maxKey, Number(key)); } else { allIntegerKeys = false; } }); if (!exportFormat && allIntegerKeys && maxKey < 2 * numKeys) { // convert to array. const array: Object[] = []; for (let key in obj) array[(key as any) as number] = obj[key]; return array; } else { if (exportFormat && !this.getPriority().isEmpty()) { obj['.priority'] = this.getPriority().val(); } return obj; } } /** @inheritDoc */ hash(): string { if (this.lazyHash_ === null) { let toHash = ''; if (!this.getPriority().isEmpty()) toHash += 'priority:' + priorityHashText(this.getPriority().val() as string | number) + ':'; this.forEachChild(PRIORITY_INDEX, function(key, childNode) { const childHash = childNode.hash(); Eif (childHash !== '') toHash += ':' + key + ':' + childHash; }); this.lazyHash_ = toHash === '' ? '' : sha1(toHash); } return this.lazyHash_; } /** @inheritDoc */ getPredecessorChildName( childName: string, childNode: Node, index: Index ): string { const idx = this.resolveIndex_(index); if (idx) { const predecessor = idx.getPredecessorKey( new NamedNode(childName, childNode) ); return predecessor ? predecessor.name : null; } else { return this.children_.getPredecessorKey(childName); } } /** * @param {!Index} indexDefinition * @return {?string} */ getFirstChildName(indexDefinition: Index): string | null { const idx = this.resolveIndex_(indexDefinition); if (idx) { const minKey = idx.minKey(); return minKey && minKey.name; } else { return this.children_.minKey(); } } /** * @param {!Index} indexDefinition * @return {?NamedNode} */ getFirstChild(indexDefinition: Index): NamedNode | null { const minKey = this.getFirstChildName(indexDefinition); Eif (minKey) { return new NamedNode(minKey, this.children_.get(minKey)); } else { return null; } } /** * Given an index, return the key name of the largest value we have, according to that index * @param {!Index} indexDefinition * @return {?string} */ getLastChildName(indexDefinition: Index): string | null { const idx = this.resolveIndex_(indexDefinition); Iif (idx) { const maxKey = idx.maxKey(); return maxKey && maxKey.name; } else { return this.children_.maxKey(); } } /** * @param {!Index} indexDefinition * @return {?NamedNode} */ getLastChild(indexDefinition: Index): NamedNode | null { const maxKey = this.getLastChildName(indexDefinition); Eif (maxKey) { return new NamedNode(maxKey, this.children_.get(maxKey)); } else { return null; } } /** * @inheritDoc */ forEachChild(index: Index, action: (key: string, node: Node) => void): any { const idx = this.resolveIndex_(index); if (idx) { return idx.inorderTraversal(function(wrappedNode) { return action(wrappedNode.name, wrappedNode.node); }); } else { return this.children_.inorderTraversal(action); } } /** * @param {!Index} indexDefinition * @return {SortedMapIterator} */ getIterator( indexDefinition: Index ): SortedMapIterator<string | NamedNode, Node, NamedNode> { return this.getIteratorFrom(indexDefinition.minPost(), indexDefinition); } /** * * @param {!NamedNode} startPost * @param {!Index} indexDefinition * @return {!SortedMapIterator} */ getIteratorFrom( startPost: NamedNode, indexDefinition: Index ): SortedMapIterator<string | NamedNode, Node, NamedNode> { const idx = this.resolveIndex_(indexDefinition); if (idx) { return idx.getIteratorFrom(startPost, key => key); } else { const iterator = this.children_.getIteratorFrom( startPost.name, NamedNode.Wrap ); let next = iterator.peek(); while (next != null && indexDefinition.compare(next, startPost) < 0) { iterator.getNext(); next = iterator.peek(); } return iterator; } } /** * @param {!Index} indexDefinition * @return {!SortedMapIterator} */ getReverseIterator( indexDefinition: Index ): SortedMapIterator<string | NamedNode, Node, NamedNode> { return this.getReverseIteratorFrom( indexDefinition.maxPost(), indexDefinition ); } /** * @param {!NamedNode} endPost * @param {!Index} indexDefinition * @return {!SortedMapIterator} */ getReverseIteratorFrom( endPost: NamedNode, indexDefinition: Index ): SortedMapIterator<string | NamedNode, Node, NamedNode> { const idx = this.resolveIndex_(indexDefinition); if (idx) { return idx.getReverseIteratorFrom(endPost, function(key) { return key; }); } else { const iterator = this.children_.getReverseIteratorFrom( endPost.name, NamedNode.Wrap ); let next = iterator.peek(); while (next != null && indexDefinition.compare(next, endPost) > 0) { iterator.getNext(); next = iterator.peek(); } return iterator; } } /** * @inheritDoc */ compareTo(other: ChildrenNode): number { Eif (this.isEmpty()) { if (other.isEmpty()) { return 0; } else { return -1; } } else if (other.isLeafNode() || other.isEmpty()) { return 1; } else if (other === MAX_NODE) { return -1; } else { // Must be another node with children. return 0; } } /** * @inheritDoc */ withIndex(indexDefinition: Index): Node { if ( indexDefinition === KEY_INDEX || this.indexMap_.hasIndex(indexDefinition) ) { return this; } else { const newIndexMap = this.indexMap_.addIndex( indexDefinition, this.children_ ); return new ChildrenNode(this.children_, this.priorityNode_, newIndexMap); } } /** * @inheritDoc */ isIndexed(index: Index): boolean { return index === KEY_INDEX || this.indexMap_.hasIndex(index); } /** * @inheritDoc */ equals(other: Node): boolean { if (other === this) { return true; } else if (other.isLeafNode()) { return false; } else { const otherChildrenNode = other as ChildrenNode; Iif (!this.getPriority().equals(otherChildrenNode.getPriority())) { return false; } else if ( this.children_.count() === otherChildrenNode.children_.count() ) { const thisIter = this.getIterator(PRIORITY_INDEX); const otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX); let thisCurrent = thisIter.getNext(); let otherCurrent = otherIter.getNext(); while (thisCurrent && otherCurrent) { if ( thisCurrent.name !== otherCurrent.name || !thisCurrent.node.equals(otherCurrent.node) ) { return false; } thisCurrent = thisIter.getNext(); otherCurrent = otherIter.getNext(); } return thisCurrent === null && otherCurrent === null; } else { return false; } } } /** * Returns a SortedMap ordered by index, or null if the default (by-key) ordering can be used * instead. * * @private * @param {!Index} indexDefinition * @return {?SortedMap.<NamedNode, Node>} */ private resolveIndex_( indexDefinition: Index ): SortedMap<NamedNode, Node> | null { if (indexDefinition === KEY_INDEX) { return null; } else { return this.indexMap_.get(indexDefinition.toString()); } } } /** * @constructor * @extends {ChildrenNode} * @private */ export class MaxNode extends ChildrenNode { constructor() { super( new SortedMap<string, Node>(NAME_COMPARATOR), ChildrenNode.EMPTY_NODE, IndexMap.Default ); } compareTo(other: Node): number { if (other === this) { return 0; } else { return 1; } } equals(other: Node): boolean { // Not that we every compare it, but MAX_NODE is only ever equal to itself return other === this; } getPriority(): MaxNode { return this; } getImmediateChild(childName: string): ChildrenNode { return ChildrenNode.EMPTY_NODE; } isEmpty(): boolean { return false; } } /** * Marker that will sort higher than any other snapshot. * @type {!MAX_NODE} * @const */ export const MAX_NODE = new MaxNode(); /** * Document NamedNode extensions */ declare module './Node' { interface NamedNode { MIN: NamedNode; MAX: NamedNode; } } Object.defineProperties(NamedNode, { MIN: { value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE) }, MAX: { value: new NamedNode(MAX_NAME, MAX_NODE) } }); /** * Reference Extensions */ KeyIndex.__EMPTY_NODE = ChildrenNode.EMPTY_NODE; LeafNode.__childrenNodeConstructor = ChildrenNode; setMaxNode(MAX_NODE); setPriorityMaxNode(MAX_NODE); |