Skip to content

Commit ce93196

Browse files
committed
WIP: refactor index code
1 parent 21a0902 commit ce93196

21 files changed

+98
-102
lines changed

src/database/api/DataSnapshot.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { validateArgCount, validateCallback } from "../../utils/validation";
22
import { validatePathString } from "../core/util/validation";
33
import { Path } from "../core/util/Path";
44
import { exportPropGetter } from "../core/util/util";
5-
import { PriorityIndex } from "../core/snap/IndexFactory";
5+
import { PRIORITY_INDEX } from "../core/snap/indexes/PriorityIndex";
66

77
/**
88
* Class representing a firebase data snapshot. It wraps a SnapshotNode and
@@ -90,7 +90,7 @@ DataSnapshot.prototype.child = function(childPathString) {
9090

9191
var childPath = new Path(childPathString);
9292
var childRef = this.query_.child(childPath);
93-
return new DataSnapshot(this.node_.getChild(childPath), childRef, PriorityIndex);
93+
return new DataSnapshot(this.node_.getChild(childPath), childRef, PRIORITY_INDEX);
9494
};
9595

9696
/**
@@ -138,7 +138,7 @@ DataSnapshot.prototype.forEach = function(action) {
138138
var self = this;
139139
// Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
140140
return !!childrenNode.forEachChild(this.index_, function(key, node) {
141-
return action(new DataSnapshot(node, self.query_.child(key), PriorityIndex));
141+
return action(new DataSnapshot(node, self.query_.child(key), PRIORITY_INDEX));
142142
});
143143
};
144144

src/database/api/Query.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { assert } from '../../utils/assert';
2-
import { KeyIndex, PriorityIndex, ValueIndex } from "../core/snap/IndexFactory";
2+
import { KEY_INDEX } from "../core/snap/indexes/KeyIndex";
3+
import { PRIORITY_INDEX } from "../core/snap/indexes/PriorityIndex";
4+
import { VALUE_INDEX } from "../core/snap/indexes/ValueIndex";
35
import { PathIndex } from "../core/snap/indexes/PathIndex";
46
import { MIN_NAME,MAX_NAME,ObjectToUniqueKey } from "../core/util/util";
57
import { Path } from "../core/util/Path";
@@ -15,6 +17,7 @@ import { ValueEventRegistration, ChildEventRegistration } from "../core/view/Eve
1517
import { Deferred, attachDummyErrorHandler } from "../../utils/promise";
1618
import { Reference } from "./Reference";
1719
import { Repo } from "../core/Repo";
20+
import { QueryParams } from "../core/view/QueryParams";
1821

1922
/**
2023
* A Query represents a filter to be applied to a firebase location. This object purely represents the
@@ -23,23 +26,8 @@ import { Repo } from "../core/Repo";
2326
* Since every Firebase reference is a query, Firebase inherits from this object.
2427
*/
2528
export class Query {
26-
repo: Repo;
27-
path;
28-
queryParams_;
29-
orderByCalled_;
3029

31-
/**
32-
* @param {!Repo} repo
33-
* @param {!Path} path
34-
* @param {!QueryParams} queryParams
35-
* @param {!boolean} orderByCalled
36-
*/
37-
constructor(repo, path, queryParams, orderByCalled) {
38-
this.repo = repo;
39-
this.path = path;
40-
this.queryParams_ = queryParams;
41-
this.orderByCalled_ = orderByCalled;
42-
}
30+
constructor(public repo: Repo, public path: Path, private queryParams_: QueryParams, private orderByCalled_: boolean) {}
4331

4432
/**
4533
* Validates start/end values for queries.
@@ -56,7 +44,7 @@ export class Query {
5644
endNode = params.getIndexEndValue();
5745
}
5846

59-
if (params.getIndex() === KeyIndex) {
47+
if (params.getIndex() === KEY_INDEX) {
6048
var tooManyArgsError = 'Query: When ordering by key, you may only pass one argument to ' +
6149
'startAt(), endAt(), or equalTo().';
6250
var wrongArgTypeError = 'Query: When ordering by key, the argument passed to startAt(), endAt(),' +
@@ -78,15 +66,15 @@ export class Query {
7866
}
7967
}
8068
}
81-
else if (params.getIndex() === PriorityIndex) {
69+
else if (params.getIndex() === PRIORITY_INDEX) {
8270
if ((startNode != null && !isValidPriority(startNode)) ||
8371
(endNode != null && !isValidPriority(endNode))) {
8472
throw new Error('Query: When ordering by priority, the first argument passed to startAt(), ' +
8573
'endAt(), or equalTo() must be a valid priority value (null, a number, or a string).');
8674
}
8775
} else {
8876
assert((params.getIndex() instanceof PathIndex) ||
89-
(params.getIndex() === ValueIndex), 'unknown index type.');
77+
(params.getIndex() === VALUE_INDEX), 'unknown index type.');
9078
if ((startNode != null && typeof startNode === 'object') ||
9179
(endNode != null && typeof endNode === 'object')) {
9280
throw new Error('Query: First argument passed to startAt(), endAt(), or equalTo() cannot be ' +
@@ -324,7 +312,7 @@ export class Query {
324312
orderByKey() {
325313
validateArgCount('Query.orderByKey', 0, 0, arguments.length);
326314
this.validateNoPreviousOrderByCall_('Query.orderByKey');
327-
var newParams = this.queryParams_.orderBy(KeyIndex);
315+
var newParams = this.queryParams_.orderBy(KEY_INDEX);
328316
this.validateQueryEndpoints_(newParams);
329317
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/true);
330318
}
@@ -336,7 +324,7 @@ export class Query {
336324
orderByPriority() {
337325
validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
338326
this.validateNoPreviousOrderByCall_('Query.orderByPriority');
339-
var newParams = this.queryParams_.orderBy(PriorityIndex);
327+
var newParams = this.queryParams_.orderBy(PRIORITY_INDEX);
340328
this.validateQueryEndpoints_(newParams);
341329
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/true);
342330
}
@@ -348,7 +336,7 @@ export class Query {
348336
orderByValue() {
349337
validateArgCount('Query.orderByValue', 0, 0, arguments.length);
350338
this.validateNoPreviousOrderByCall_('Query.orderByValue');
351-
var newParams = this.queryParams_.orderBy(ValueIndex);
339+
var newParams = this.queryParams_.orderBy(VALUE_INDEX);
352340
this.validateQueryEndpoints_(newParams);
353341
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/true);
354342
}

src/database/api/Reference.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
import { Deferred, attachDummyErrorHandler, PromiseImpl } from "../../utils/promise";
2323

2424
export class Reference extends Query {
25-
then;
25+
public then;
26+
public catch;
2627
/**
2728
* Call options:
2829
* new Reference(Repo, Path) or
@@ -34,23 +35,13 @@ export class Reference extends Query {
3435
* @param {(!Path)} path
3536
* @extends {Query}
3637
*/
37-
constructor(repo, path) {
38+
constructor(repo: Repo, path: Path) {
3839
if (!(repo instanceof Repo)) {
3940
throw new Error("new Reference() no longer supported - use app.database().");
4041
}
4142

4243
// call Query's constructor, passing in the repo and path.
43-
super(repo, path, QueryParams.DEFAULT, /*orderByCalled=*/false);
44-
45-
/**
46-
* When defined is the then function for the promise + Firebase hybrid
47-
* returned by push() When then is defined, catch will be as well, though
48-
* it's created using some hackery to get around conflicting ES3 and Closure
49-
* Compiler limitations.
50-
* @type {Function|undefined}
51-
*/
52-
this.then = void 0;
53-
/** @type {letMeUseMapAccessors} */ (this)['catch'] = void 0;
44+
super(repo, path, QueryParams.DEFAULT, false);
5445
}
5546

5647
/** @return {?string} */

src/database/core/CompoundWrite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ImmutableTree } from "./util/ImmutableTree";
22
import { Path } from "./util/Path";
33
import { forEach } from "../../utils/obj";
44
import { NamedNode } from "./snap/Node";
5-
import { PriorityIndex } from "./snap/IndexFactory";
5+
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex";
66
import { assert } from "../../utils/assert";
77

88
/**
@@ -121,7 +121,7 @@ export class CompoundWrite {
121121
// If it's a leaf node, it has no children; so nothing to do.
122122
if (!node.isLeafNode()) {
123123
node = /** @type {!ChildrenNode} */ (node);
124-
node.forEachChild(PriorityIndex, function(childName, childNode) {
124+
node.forEachChild(PRIORITY_INDEX, function(childName, childNode) {
125125
children.push(new NamedNode(childName, childNode));
126126
});
127127
}

src/database/core/Repo_transaction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Reference } from "../api/Reference";
33
import { DataSnapshot } from "../api/DataSnapshot";
44
import { Path } from "./util/Path";
55
import { Tree } from "./util/Tree";
6-
import { PriorityIndex } from "./snap/IndexFactory";
6+
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex";
77
import {
88
LUIDGenerator,
99
warn,
@@ -157,7 +157,7 @@ declare module './Repo' {
157157
if (transaction.onComplete) {
158158
// We just set the input snapshot, so this cast should be safe
159159
var snapshot = new DataSnapshot(/** @type {!Node} */ (transaction.currentInputSnapshot),
160-
new Reference(this, transaction.path), PriorityIndex);
160+
new Reference(this, transaction.path), PRIORITY_INDEX);
161161
transaction.onComplete(/*error=*/null, /*committed=*/false, snapshot);
162162
}
163163
} else {
@@ -294,7 +294,7 @@ declare module './Repo' {
294294
// We never unset the output snapshot, and given that this transaction is complete, it should be set
295295
var node = /** @type {!Node} */ (queue[i].currentOutputSnapshotResolved);
296296
var ref = new Reference(self, queue[i].path);
297-
var snapshot = new DataSnapshot(node, ref, PriorityIndex);
297+
var snapshot = new DataSnapshot(node, ref, PRIORITY_INDEX);
298298
callbacks.push(queue[i].onComplete.bind(null, null, true, snapshot));
299299
}
300300
queue[i].unwatcher();
@@ -443,7 +443,7 @@ declare module './Repo' {
443443
var ref = new Reference(this, queue[i].path);
444444
// We set this field immediately, so it's safe to cast to an actual snapshot
445445
var lastInput = /** @type {!Node} */ (queue[i].currentInputSnapshot);
446-
var snapshot = new DataSnapshot(lastInput, ref, PriorityIndex);
446+
var snapshot = new DataSnapshot(lastInput, ref, PRIORITY_INDEX);
447447
callbacks.push(queue[i].onComplete.bind(null, null, false, snapshot));
448448
} else {
449449
callbacks.push(queue[i].onComplete.bind(null, new Error(abortReason), false, null));

src/database/core/SparseSnapshotTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Path } from "./util/Path";
2-
import { PriorityIndex } from "./snap/IndexFactory";
2+
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex";
33
import { CountedSet } from "./util/CountedSet";
44

55
/**
@@ -98,7 +98,7 @@ export class SparseSnapshotTree {
9898
this.value_ = null;
9999

100100
var self = this;
101-
value.forEachChild(PriorityIndex, function(key, tree) {
101+
value.forEachChild(PRIORITY_INDEX, function(key, tree) {
102102
self.remember(new Path(key), tree);
103103
});
104104

src/database/core/WriteTree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { findKey, forEach } from "../../utils/obj";
22
import { assert, assertionError } from "../../utils/assert";
33
import { Path } from "./util/Path";
44
import { CompoundWrite } from "./CompoundWrite";
5-
import { PriorityIndex } from "./snap/IndexFactory";
5+
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex";
66
import { ChildrenNode } from "./snap/ChildrenNode";
77

88
/**
@@ -247,7 +247,7 @@ export class WriteTree {
247247
if (topLevelSet) {
248248
if (!topLevelSet.isLeafNode()) {
249249
// we're shadowing everything. Return the children.
250-
topLevelSet.forEachChild(PriorityIndex, function(childName, childSnap) {
250+
topLevelSet.forEachChild(PRIORITY_INDEX, function(childName, childSnap) {
251251
completeChildren = completeChildren.updateImmediateChild(childName, childSnap);
252252
});
253253
}
@@ -256,7 +256,7 @@ export class WriteTree {
256256
// Layer any children we have on top of this
257257
// We know we don't have a top-level set, so just enumerate existing children
258258
var merge = this.visibleWrites_.childCompoundWrite(treePath);
259-
completeServerChildren.forEachChild(PriorityIndex, function(childName, childNode) {
259+
completeServerChildren.forEachChild(PRIORITY_INDEX, function(childName, childNode) {
260260
var node = merge.childCompoundWrite(new Path(childName)).apply(childNode);
261261
completeChildren = completeChildren.updateImmediateChild(childName, node);
262262
});

src/database/core/snap/ChildrenNode.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
validatePriorityNode,
1111
priorityHashText,
1212
} from "./snap";
13-
import { PriorityIndex, KeyIndex } from "./IndexFactory";
13+
import { PRIORITY_INDEX } from "./indexes/PriorityIndex";
14+
import { KEY_INDEX } from "./indexes/KeyIndex";
1415
import { IndexMap } from "./IndexMap";
1516
import { NAME_COMPARATOR } from "./comparators";
1617

@@ -185,7 +186,7 @@ export class ChildrenNode implements Node {
185186

186187
var obj = { };
187188
var numKeys = 0, maxKey = 0, allIntegerKeys = true;
188-
this.forEachChild(PriorityIndex, function(key, childNode) {
189+
this.forEachChild(PRIORITY_INDEX, function(key, childNode) {
189190
obj[key] = childNode.val(opt_exportFormat);
190191

191192
numKeys++;
@@ -220,7 +221,7 @@ export class ChildrenNode implements Node {
220221
toHash += 'priority:' + priorityHashText(
221222
/**@type {(!string|!number)} */ (this.getPriority().val())) + ':';
222223

223-
this.forEachChild(PriorityIndex, function(key, childNode) {
224+
this.forEachChild(PRIORITY_INDEX, function(key, childNode) {
224225
var childHash = childNode.hash();
225226
if (childHash !== '')
226227
toHash += ':' + key + ':' + childHash;
@@ -394,7 +395,7 @@ export class ChildrenNode implements Node {
394395
* @inheritDoc
395396
*/
396397
withIndex(indexDefinition) {
397-
if (indexDefinition === KeyIndex || this.indexMap_.hasIndex(indexDefinition)) {
398+
if (indexDefinition === KEY_INDEX || this.indexMap_.hasIndex(indexDefinition)) {
398399
return this;
399400
} else {
400401
var newIndexMap = this.indexMap_.addIndex(indexDefinition, this.children_);
@@ -406,7 +407,7 @@ export class ChildrenNode implements Node {
406407
* @inheritDoc
407408
*/
408409
isIndexed(index) {
409-
return index === KeyIndex || this.indexMap_.hasIndex(index);
410+
return index === KEY_INDEX || this.indexMap_.hasIndex(index);
410411
};
411412

412413
/**
@@ -423,8 +424,8 @@ export class ChildrenNode implements Node {
423424
if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
424425
return false;
425426
} else if (this.children_.count() === otherChildrenNode.children_.count()) {
426-
var thisIter = this.getIterator(PriorityIndex);
427-
var otherIter = otherChildrenNode.getIterator(PriorityIndex);
427+
var thisIter = this.getIterator(PRIORITY_INDEX);
428+
var otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX);
428429
var thisCurrent = thisIter.getNext();
429430
var otherCurrent = otherIter.getNext();
430431
while (thisCurrent && otherCurrent) {
@@ -451,7 +452,7 @@ export class ChildrenNode implements Node {
451452
* @return {?SortedMap.<NamedNode, Node>}
452453
*/
453454
resolveIndex_(indexDefinition) {
454-
if (indexDefinition === KeyIndex) {
455+
if (indexDefinition === KEY_INDEX) {
455456
return null;
456457
} else {
457458
return this.indexMap_.get(indexDefinition.toString());
@@ -480,7 +481,7 @@ export class MaxNode extends ChildrenNode {
480481

481482

482483
equals(other) {
483-
// Not that we every compare it, but MAX_NODE_ is only ever equal to itself
484+
// Not that we every compare it, but MAX_NODE is only ever equal to itself
484485
return other === this;
485486
};
486487

@@ -502,10 +503,26 @@ export class MaxNode extends ChildrenNode {
502503

503504
/**
504505
* Marker that will sort higher than any other snapshot.
505-
* @type {!MAX_NODE_}
506+
* @type {!MAX_NODE}
506507
* @const
507508
*/
508509
export const MAX_NODE = new MaxNode();
509510

510-
(NamedNode as any).MIN = new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE);
511-
(NamedNode as any).MAX = new NamedNode(MAX_NAME, MAX_NODE);
511+
/**
512+
* Document NamedNode extensions
513+
*/
514+
declare module './Node' {
515+
interface NamedNode {
516+
MIN: NamedNode,
517+
MAX: NamedNode
518+
}
519+
}
520+
521+
Object.defineProperties(NamedNode, {
522+
MIN: {
523+
value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE)
524+
},
525+
MAX: {
526+
value: new NamedNode(MAX_NAME, MAX_NODE)
527+
}
528+
});

src/database/core/snap/IndexFactory.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)