-
Notifications
You must be signed in to change notification settings - Fork 876
/
CompoundWrite.ts
198 lines (185 loc) · 6.78 KB
/
CompoundWrite.ts
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
/**
* @license
* 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 { Node, NamedNode } from './snap/Node';
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
import { assert } from '@firebase/util';
import { ChildrenNode } from './snap/ChildrenNode';
import { each } from './util/util';
/**
* 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.
*/
export class CompoundWrite {
constructor(private writeTree_: ImmutableTree<Node>) {}
static Empty = new CompoundWrite(new ImmutableTree(null));
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);
}
}
}
addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
let newWrite = this as CompoundWrite;
each(updates, (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 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 The path to check for
* @return 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 The path to get a complete write
* @return 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 A list of all complete children.
*/
getCompleteChildren(): NamedNode[] {
const children: NamedNode[] = [];
const node = this.writeTree_.value;
if (node != null) {
// If it's a leaf node, it has no children; so nothing to do.
if (!node.isLeafNode()) {
(node as ChildrenNode).forEachChild(
PRIORITY_INDEX,
(childName, childNode) => {
children.push(new NamedNode(childName, childNode));
}
);
}
} else {
this.writeTree_.children.inorderTraversal((childName, childTree) => {
if (childTree.value != null) {
children.push(new NamedNode(childName, childTree.value));
}
});
}
return children;
}
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 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 The node to apply this CompoundWrite to
* @return The node with all writes applied
*/
apply(node: Node): Node {
return applySubtreeWrite(Path.Empty, this.writeTree_, node);
}
}
function applySubtreeWrite(
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((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 = 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;
}
}