Skip to content

Commit f7af296

Browse files
authored
Merge 1ada8af into 3a465b4
2 parents 3a465b4 + 1ada8af commit f7af296

File tree

6 files changed

+787
-666
lines changed

6 files changed

+787
-666
lines changed

packages/database/src/core/SyncPoint.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ import {
2929
viewRemoveEventRegistration
3030
} from './view/View';
3131
import { Operation } from './operation/Operation';
32-
import { WriteTreeRef } from './WriteTree';
32+
import {
33+
WriteTreeRef,
34+
writeTreeRefCalcCompleteEventCache,
35+
writeTreeRefCalcCompleteEventChildren
36+
} from './WriteTree';
3337
import { Query } from '../api/Query';
3438
import { EventRegistration } from './view/EventRegistration';
3539
import { Node } from './snap/Node';
@@ -127,14 +131,18 @@ export function syncPointGetView(
127131
const view = syncPoint.views.get(queryId);
128132
if (!view) {
129133
// TODO: make writesCache take flag for complete server node
130-
let eventCache = writesCache.calcCompleteEventCache(
134+
let eventCache = writeTreeRefCalcCompleteEventCache(
135+
writesCache,
131136
serverCacheComplete ? serverCache : null
132137
);
133138
let eventCacheComplete = false;
134139
if (eventCache) {
135140
eventCacheComplete = true;
136141
} else if (serverCache instanceof ChildrenNode) {
137-
eventCache = writesCache.calcCompleteEventChildren(serverCache);
142+
eventCache = writeTreeRefCalcCompleteEventChildren(
143+
writesCache,
144+
serverCache
145+
);
138146
eventCacheComplete = false;
139147
} else {
140148
eventCache = ChildrenNode.EMPTY_NODE;

packages/database/src/core/SyncTree.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ import {
5050
syncPointViewExistsForQuery,
5151
syncPointViewForQuery
5252
} from './SyncPoint';
53-
import { WriteTree, WriteTreeRef } from './WriteTree';
53+
import {
54+
newWriteTree,
55+
writeTreeAddMerge,
56+
writeTreeAddOverwrite,
57+
writeTreeCalcCompleteEventCache,
58+
writeTreeChildWrites,
59+
writeTreeGetWrite,
60+
WriteTreeRef,
61+
writeTreeRefChild,
62+
writeTreeRemoveWrite
63+
} from './WriteTree';
5464
import { Query } from '../api/Query';
5565
import { Node } from './snap/Node';
5666
import { Event } from './view/Event';
@@ -116,7 +126,7 @@ export class SyncTree {
116126
/**
117127
* A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
118128
*/
119-
pendingWriteTree_ = new WriteTree();
129+
pendingWriteTree_ = newWriteTree();
120130

121131
readonly tagToQueryMap: Map<number, string> = new Map();
122132
readonly queryToTagMap: Map<string, number> = new Map();
@@ -141,7 +151,13 @@ export function syncTreeApplyUserOverwrite(
141151
visible?: boolean
142152
): Event[] {
143153
// Record pending write.
144-
syncTree.pendingWriteTree_.addOverwrite(path, newData, writeId, visible);
154+
writeTreeAddOverwrite(
155+
syncTree.pendingWriteTree_,
156+
path,
157+
newData,
158+
writeId,
159+
visible
160+
);
145161

146162
if (!visible) {
147163
return [];
@@ -165,7 +181,7 @@ export function syncTreeApplyUserMerge(
165181
writeId: number
166182
): Event[] {
167183
// Record pending merge.
168-
syncTree.pendingWriteTree_.addMerge(path, changedChildren, writeId);
184+
writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);
169185

170186
const changeTree = ImmutableTree.fromObject(changedChildren);
171187

@@ -186,8 +202,11 @@ export function syncTreeAckUserWrite(
186202
writeId: number,
187203
revert: boolean = false
188204
) {
189-
const write = syncTree.pendingWriteTree_.getWrite(writeId);
190-
const needToReevaluate = syncTree.pendingWriteTree_.removeWrite(writeId);
205+
const write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
206+
const needToReevaluate = writeTreeRemoveWrite(
207+
syncTree.pendingWriteTree_,
208+
writeId
209+
);
191210
if (!needToReevaluate) {
192211
return [];
193212
} else {
@@ -522,7 +541,7 @@ export function syncTreeAddEventRegistration(
522541
syncTree.queryToTagMap.set(queryKey, tag);
523542
syncTree.tagToQueryMap.set(tag, queryKey);
524543
}
525-
const writesCache = syncTree.pendingWriteTree_.childWrites(path);
544+
const writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
526545
let events = syncPointAddEventRegistration(
527546
syncPoint,
528547
query,
@@ -569,7 +588,8 @@ export function syncTreeCalcCompleteEventCache(
569588
}
570589
}
571590
);
572-
return writeTree.calcCompleteEventCache(
591+
return writeTreeCalcCompleteEventCache(
592+
writeTree,
573593
path,
574594
serverCache,
575595
writeIdsToExclude,
@@ -602,7 +622,8 @@ export function syncTreeGetServerValue(
602622
const serverCacheNode: CacheNode | null = serverCacheComplete
603623
? new CacheNode(serverCache, true, false)
604624
: null;
605-
const writesCache: WriteTreeRef | null = syncTree.pendingWriteTree_.childWrites(
625+
const writesCache: WriteTreeRef | null = writeTreeChildWrites(
626+
syncTree.pendingWriteTree_,
606627
query.path
607628
);
608629
const view: View = syncPointGetView(
@@ -636,7 +657,7 @@ function syncTreeApplyOperationToSyncPoints_(
636657
operation,
637658
syncTree.syncPointTree_,
638659
/*serverCache=*/ null,
639-
syncTree.pendingWriteTree_.childWrites(newEmptyPath())
660+
writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath())
640661
);
641662
}
642663

@@ -672,7 +693,7 @@ function syncTreeApplyOperationHelper_(
672693
const childServerCache = serverCache
673694
? serverCache.getImmediateChild(childName)
674695
: null;
675-
const childWritesCache = writesCache.child(childName);
696+
const childWritesCache = writeTreeRefChild(writesCache, childName);
676697
events = events.concat(
677698
syncTreeApplyOperationHelper_(
678699
childOperation,
@@ -714,7 +735,7 @@ function syncTreeApplyOperationDescendantsHelper_(
714735
const childServerCache = serverCache
715736
? serverCache.getImmediateChild(childName)
716737
: null;
717-
const childWritesCache = writesCache.child(childName);
738+
const childWritesCache = writeTreeRefChild(writesCache, childName);
718739
const childOperation = operation.operationForChild(childName);
719740
if (childOperation) {
720741
events = events.concat(
@@ -823,7 +844,10 @@ function syncTreeApplyTaggedOperation_(
823844
): Event[] {
824845
const syncPoint = syncTree.syncPointTree_.get(queryPath);
825846
assert(syncPoint, "Missing sync point for query tag that we're tracking");
826-
const writesCache = syncTree.pendingWriteTree_.childWrites(queryPath);
847+
const writesCache = writeTreeChildWrites(
848+
syncTree.pendingWriteTree_,
849+
queryPath
850+
);
827851
return syncPointApplyOperation(syncPoint, operation, writesCache, null);
828852
}
829853

0 commit comments

Comments
 (0)