Skip to content

Tree-shake ViewCache #4613

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 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/database/src/core/SyncPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { CacheNode } from './view/CacheNode';
import { ChildrenNode } from './snap/ChildrenNode';
import { assert } from '@firebase/util';
import { ViewCache } from './view/ViewCache';
import { newViewCache } from './view/ViewCache';
import {
View,
viewAddEventRegistration,
Expand Down Expand Up @@ -140,7 +140,7 @@ export function syncPointGetView(
eventCache = ChildrenNode.EMPTY_NODE;
eventCacheComplete = false;
}
const viewCache = new ViewCache(
const viewCache = newViewCache(
new CacheNode(eventCache, eventCacheComplete, false),
new CacheNode(serverCache, serverCacheComplete, false)
);
Expand Down
8 changes: 4 additions & 4 deletions packages/database/src/core/view/CompleteChildSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CacheNode } from './CacheNode';
import { NamedNode, Node } from '../snap/Node';
import { Index } from '../snap/indexes/Index';
import { WriteTreeRef } from '../WriteTree';
import { ViewCache } from './ViewCache';
import { ViewCache, viewCacheGetCompleteServerSnap } from './ViewCache';

/**
* Since updates to filtered nodes might require nodes to be pulled in from "outside" the node, this interface
Expand Down Expand Up @@ -83,14 +83,14 @@ export class WriteTreeCompleteChildSource implements CompleteChildSource {
* @inheritDoc
*/
getCompleteChild(childKey: string): Node | null {
const node = this.viewCache_.getEventCache();
const node = this.viewCache_.eventCache;
if (node.isCompleteForChild(childKey)) {
return node.getNode().getImmediateChild(childKey);
} else {
const serverNode =
this.optCompleteServerCache_ != null
? new CacheNode(this.optCompleteServerCache_, true, false)
: this.viewCache_.getServerCache();
: this.viewCache_.serverCache;
return this.writes_.calcCompleteChild(childKey, serverNode);
}
}
Expand All @@ -106,7 +106,7 @@ export class WriteTreeCompleteChildSource implements CompleteChildSource {
const completeServerData =
this.optCompleteServerCache_ != null
? this.optCompleteServerCache_
: this.viewCache_.getCompleteServerSnap();
: viewCacheGetCompleteServerSnap(this.viewCache_);
const nodes = this.writes_.calcIndexedSlice(
completeServerData,
child,
Expand Down
31 changes: 18 additions & 13 deletions packages/database/src/core/view/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { IndexedFilter } from './filter/IndexedFilter';
import { ViewProcessor } from './ViewProcessor';
import { ChildrenNode } from '../snap/ChildrenNode';
import { CacheNode } from './CacheNode';
import { ViewCache } from './ViewCache';
import {
newViewCache,
ViewCache,
viewCacheGetCompleteEventSnap,
viewCacheGetCompleteServerSnap
} from './ViewCache';
import {
EventGenerator,
eventGeneratorGenerateEventsForChanges
Expand Down Expand Up @@ -59,8 +64,8 @@ export class View {

this.processor_ = new ViewProcessor(filter);

const initialServerCache = initialViewCache.getServerCache();
const initialEventCache = initialViewCache.getEventCache();
const initialServerCache = initialViewCache.serverCache;
const initialEventCache = initialViewCache.eventCache;

// Don't filter server node with other filter than index, wait for tagged listen
const serverSnap = indexFilter.updateFullNode(
Expand All @@ -84,7 +89,7 @@ export class View {
filter.filtersNodes()
);

this.viewCache_ = new ViewCache(newEventCache, newServerCache);
this.viewCache_ = newViewCache(newEventCache, newServerCache);
this.eventGenerator_ = new EventGenerator(this.query_);
}

Expand All @@ -94,18 +99,18 @@ export class View {
}

export function viewGetServerCache(view: View): Node | null {
return view.viewCache_.getServerCache().getNode();
return view.viewCache_.serverCache.getNode();
}

export function viewGetCompleteNode(view: View): Node | null {
return view.viewCache_.getCompleteEventSnap();
return viewCacheGetCompleteEventSnap(view.viewCache_);
}

export function viewGetCompleteServerCache(
view: View,
path: Path
): Node | null {
const cache = view.viewCache_.getCompleteServerSnap();
const cache = viewCacheGetCompleteServerSnap(view.viewCache_);
if (cache) {
// If this isn't a "loadsAllData" view, then cache isn't actually a complete cache and
// we need to see if it contains the child we're interested in.
Expand Down Expand Up @@ -189,11 +194,11 @@ export function viewApplyOperation(
operation.source.queryId !== null
) {
assert(
view.viewCache_.getCompleteServerSnap(),
viewCacheGetCompleteServerSnap(view.viewCache_),
'We should always have a full cache before handling merges'
);
assert(
view.viewCache_.getCompleteEventSnap(),
viewCacheGetCompleteEventSnap(view.viewCache_),
'Missing event cache, even though we have a server cache'
);
}
Expand All @@ -208,8 +213,8 @@ export function viewApplyOperation(
view.processor_.assertIndexed(result.viewCache);

assert(
result.viewCache.getServerCache().isFullyInitialized() ||
!oldViewCache.getServerCache().isFullyInitialized(),
result.viewCache.serverCache.isFullyInitialized() ||
!oldViewCache.serverCache.isFullyInitialized(),
'Once a server snap is complete, it should never go back'
);

Expand All @@ -218,7 +223,7 @@ export function viewApplyOperation(
return viewGenerateEventsForChanges_(
view,
result.changes,
result.viewCache.getEventCache().getNode(),
result.viewCache.eventCache.getNode(),
null
);
}
Expand All @@ -227,7 +232,7 @@ export function viewGetInitialEvents(
view: View,
registration: EventRegistration
): Event[] {
const eventSnap = view.viewCache_.getEventCache();
const eventSnap = view.viewCache_.eventCache;
const initialChanges: Change[] = [];
if (!eventSnap.getNode().isLeafNode()) {
const eventNode = eventSnap.getNode() as ChildrenNode;
Expand Down
87 changes: 45 additions & 42 deletions packages/database/src/core/view/ViewCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,54 @@ import { Node } from '../snap/Node';
*
* serverSnap is the cached server data, eventSnap is the cached event data (server data plus any local writes).
*/
export class ViewCache {
constructor(
private readonly eventCache_: CacheNode,
private readonly serverCache_: CacheNode
) {}

updateEventSnap(
eventSnap: Node,
complete: boolean,
filtered: boolean
): ViewCache {
return new ViewCache(
new CacheNode(eventSnap, complete, filtered),
this.serverCache_
);
}
export interface ViewCache {
readonly eventCache: CacheNode;
readonly serverCache: CacheNode;
}

updateServerSnap(
serverSnap: Node,
complete: boolean,
filtered: boolean
): ViewCache {
return new ViewCache(
this.eventCache_,
new CacheNode(serverSnap, complete, filtered)
);
}
export function newViewCache(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: How about createViewCache(), which sounds more natural for a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new is the pattern I have been using both in Firestore and in Database.

eventCache: CacheNode,
serverCache: CacheNode
): ViewCache {
return { eventCache, serverCache };
}

getEventCache(): CacheNode {
return this.eventCache_;
}
export function viewCacheUpdateEventSnap(
viewCache: ViewCache,
eventSnap: Node,
complete: boolean,
filtered: boolean
): ViewCache {
return newViewCache(
new CacheNode(eventSnap, complete, filtered),
viewCache.serverCache
);
}

getCompleteEventSnap(): Node | null {
return this.eventCache_.isFullyInitialized()
? this.eventCache_.getNode()
: null;
}
export function viewCacheUpdateServerSnap(
viewCache: ViewCache,
serverSnap: Node,
complete: boolean,
filtered: boolean
): ViewCache {
return newViewCache(
viewCache.eventCache,
new CacheNode(serverSnap, complete, filtered)
);
}

getServerCache(): CacheNode {
return this.serverCache_;
}
export function viewCacheGetCompleteEventSnap(
viewCache: ViewCache
): Node | null {
return viewCache.eventCache.isFullyInitialized()
? viewCache.eventCache.getNode()
: null;
}

getCompleteServerSnap(): Node | null {
return this.serverCache_.isFullyInitialized()
? this.serverCache_.getNode()
: null;
}
export function viewCacheGetCompleteServerSnap(
viewCache: ViewCache
): Node | null {
return viewCache.serverCache.isFullyInitialized()
? viewCache.serverCache.getNode()
: null;
}
Loading