Skip to content

tree shake WriteTree and WriteTreeRef #4630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/database/src/core/SyncPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
viewRemoveEventRegistration
} from './view/View';
import { Operation } from './operation/Operation';
import { WriteTreeRef } from './WriteTree';
import {
WriteTreeRef,
writeTreeRefCalcCompleteEventCache,
writeTreeRefCalcCompleteEventChildren
} from './WriteTree';
import { Query } from '../api/Query';
import { EventRegistration } from './view/EventRegistration';
import { Node } from './snap/Node';
Expand Down Expand Up @@ -127,14 +131,18 @@ export function syncPointGetView(
const view = syncPoint.views.get(queryId);
if (!view) {
// TODO: make writesCache take flag for complete server node
let eventCache = writesCache.calcCompleteEventCache(
let eventCache = writeTreeRefCalcCompleteEventCache(
writesCache,
serverCacheComplete ? serverCache : null
);
let eventCacheComplete = false;
if (eventCache) {
eventCacheComplete = true;
} else if (serverCache instanceof ChildrenNode) {
eventCache = writesCache.calcCompleteEventChildren(serverCache);
eventCache = writeTreeRefCalcCompleteEventChildren(
writesCache,
serverCache
);
eventCacheComplete = false;
} else {
eventCache = ChildrenNode.EMPTY_NODE;
Expand Down
50 changes: 37 additions & 13 deletions packages/database/src/core/SyncTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ import {
syncPointViewExistsForQuery,
syncPointViewForQuery
} from './SyncPoint';
import { WriteTree, WriteTreeRef } from './WriteTree';
import {
newWriteTree,
writeTreeAddMerge,
writeTreeAddOverwrite,
writeTreeCalcCompleteEventCache,
writeTreeChildWrites,
writeTreeGetWrite,
WriteTreeRef,
writeTreeRefChild,
writeTreeRemoveWrite
} from './WriteTree';
import { Query } from '../api/Query';
import { Node } from './snap/Node';
import { Event } from './view/Event';
Expand Down Expand Up @@ -116,7 +126,7 @@ export class SyncTree {
/**
* A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
*/
pendingWriteTree_ = new WriteTree();
pendingWriteTree_ = newWriteTree();

readonly tagToQueryMap: Map<number, string> = new Map();
readonly queryToTagMap: Map<string, number> = new Map();
Expand All @@ -141,7 +151,13 @@ export function syncTreeApplyUserOverwrite(
visible?: boolean
): Event[] {
// Record pending write.
syncTree.pendingWriteTree_.addOverwrite(path, newData, writeId, visible);
writeTreeAddOverwrite(
syncTree.pendingWriteTree_,
path,
newData,
writeId,
visible
);

if (!visible) {
return [];
Expand All @@ -165,7 +181,7 @@ export function syncTreeApplyUserMerge(
writeId: number
): Event[] {
// Record pending merge.
syncTree.pendingWriteTree_.addMerge(path, changedChildren, writeId);
writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);

const changeTree = ImmutableTree.fromObject(changedChildren);

Expand All @@ -186,8 +202,11 @@ export function syncTreeAckUserWrite(
writeId: number,
revert: boolean = false
) {
const write = syncTree.pendingWriteTree_.getWrite(writeId);
const needToReevaluate = syncTree.pendingWriteTree_.removeWrite(writeId);
const write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
const needToReevaluate = writeTreeRemoveWrite(
syncTree.pendingWriteTree_,
writeId
);
if (!needToReevaluate) {
return [];
} else {
Expand Down Expand Up @@ -522,7 +541,7 @@ export function syncTreeAddEventRegistration(
syncTree.queryToTagMap.set(queryKey, tag);
syncTree.tagToQueryMap.set(tag, queryKey);
}
const writesCache = syncTree.pendingWriteTree_.childWrites(path);
const writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
let events = syncPointAddEventRegistration(
syncPoint,
query,
Expand Down Expand Up @@ -569,7 +588,8 @@ export function syncTreeCalcCompleteEventCache(
}
}
);
return writeTree.calcCompleteEventCache(
return writeTreeCalcCompleteEventCache(
writeTree,
path,
serverCache,
writeIdsToExclude,
Expand Down Expand Up @@ -602,7 +622,8 @@ export function syncTreeGetServerValue(
const serverCacheNode: CacheNode | null = serverCacheComplete
? new CacheNode(serverCache, true, false)
: null;
const writesCache: WriteTreeRef | null = syncTree.pendingWriteTree_.childWrites(
const writesCache: WriteTreeRef | null = writeTreeChildWrites(
syncTree.pendingWriteTree_,
query.path
);
const view: View = syncPointGetView(
Expand Down Expand Up @@ -636,7 +657,7 @@ function syncTreeApplyOperationToSyncPoints_(
operation,
syncTree.syncPointTree_,
/*serverCache=*/ null,
syncTree.pendingWriteTree_.childWrites(newEmptyPath())
writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath())
);
}

Expand Down Expand Up @@ -672,7 +693,7 @@ function syncTreeApplyOperationHelper_(
const childServerCache = serverCache
? serverCache.getImmediateChild(childName)
: null;
const childWritesCache = writesCache.child(childName);
const childWritesCache = writeTreeRefChild(writesCache, childName);
events = events.concat(
syncTreeApplyOperationHelper_(
childOperation,
Expand Down Expand Up @@ -714,7 +735,7 @@ function syncTreeApplyOperationDescendantsHelper_(
const childServerCache = serverCache
? serverCache.getImmediateChild(childName)
: null;
const childWritesCache = writesCache.child(childName);
const childWritesCache = writeTreeRefChild(writesCache, childName);
const childOperation = operation.operationForChild(childName);
if (childOperation) {
events = events.concat(
Expand Down Expand Up @@ -823,7 +844,10 @@ function syncTreeApplyTaggedOperation_(
): Event[] {
const syncPoint = syncTree.syncPointTree_.get(queryPath);
assert(syncPoint, "Missing sync point for query tag that we're tracking");
const writesCache = syncTree.pendingWriteTree_.childWrites(queryPath);
const writesCache = writeTreeChildWrites(
syncTree.pendingWriteTree_,
queryPath
);
return syncPointApplyOperation(syncPoint, operation, writesCache, null);
}

Expand Down
Loading