-
Notifications
You must be signed in to change notification settings - Fork 944
Implement bundle loading. #3201
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
Changes from 36 commits
9602712
c5e783e
5e7fb89
1ee1615
18f0be1
aa455bf
78248cd
83160a1
24e10cb
9d6edc5
4cbe608
4313e51
296cfc4
fff3d36
fb762de
1ec4182
cd3ab7a
d991c75
af097c5
e735e23
17ba434
f808d8d
db1d864
979ffd9
f7ff495
556a007
adf1504
b364ab0
b62e6ef
bc2021b
d5efcdf
17ab921
21d4d7c
bf085ce
8fbdd3e
985b205
685624a
5576963
85e3ac6
2f639ae
57a1c63
b6b261b
6094153
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -93,9 +93,38 @@ export class FirebaseFirestore { | |||||||||||||||||
|
||||||||||||||||||
terminate(): Promise<void>; | ||||||||||||||||||
|
||||||||||||||||||
loadBundle( | ||||||||||||||||||
bundleData: ArrayBuffer | ReadableStream<ArrayBuffer> | string | ||||||||||||||||||
): LoadBundleTask; | ||||||||||||||||||
|
||||||||||||||||||
INTERNAL: { delete: () => Promise<void> }; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export interface LoadBundleTask { | ||||||||||||||||||
onProgress( | ||||||||||||||||||
next?: (progress: LoadBundleTaskProgress) => any, | ||||||||||||||||||
error?: (error: Error) => any, | ||||||||||||||||||
complete?: () => void | ||||||||||||||||||
): Promise<void>; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be 'void'? What does it mean for the onProgess callback to return a Promise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||||||||||||||
|
||||||||||||||||||
then( | ||||||||||||||||||
onFulfilled?: (a: LoadBundleTaskProgress) => any, | ||||||||||||||||||
onRejected?: (a: Error) => any | ||||||||||||||||||
): Promise<any>; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ES5 definition for these types is:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, thanks! |
||||||||||||||||||
|
||||||||||||||||||
catch(onRejected: (a: Error) => any): Promise<any>; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Although it seems it should return a |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export interface LoadBundleTaskProgress { | ||||||||||||||||||
documentsLoaded: number; | ||||||||||||||||||
totalDocuments: number; | ||||||||||||||||||
bytesLoaded: number; | ||||||||||||||||||
totalBytes: number; | ||||||||||||||||||
taskState: TaskState; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export type TaskState = 'Error' | 'Running' | 'Success'; | ||||||||||||||||||
|
||||||||||||||||||
export class GeoPoint { | ||||||||||||||||||
constructor(latitude: number, longitude: number); | ||||||||||||||||||
|
||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
/** | ||||||
* @license | ||||||
* Copyright 2020 Google LLC | ||||||
* | ||||||
* 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 * as firestore from '@firebase/firestore-types'; | ||||||
import { Deferred } from '../util/promise'; | ||||||
|
||||||
export class LoadBundleTask implements firestore.LoadBundleTask { | ||||||
private _progressResolver = new Deferred<void>(); | ||||||
private _userProgressHandler?: ( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a PartialObserver here?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
progress: firestore.LoadBundleTaskProgress | ||||||
) => unknown; | ||||||
private _userProgressErrorHandler?: (err: Error) => unknown; | ||||||
private _userProgressCompleteHandler?: () => void; | ||||||
|
||||||
private _promiseResolver = new Deferred<firestore.LoadBundleTaskProgress>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name is still very non-descriptive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed, hopefully better? |
||||||
|
||||||
private _lastProgress: firestore.LoadBundleTaskProgress = { | ||||||
taskState: 'Running', | ||||||
totalBytes: 0, | ||||||
totalDocuments: 0, | ||||||
bytesLoaded: 0, | ||||||
documentsLoaded: 0 | ||||||
}; | ||||||
|
||||||
onProgress( | ||||||
next?: (progress: firestore.LoadBundleTaskProgress) => unknown, | ||||||
error?: (err: Error) => unknown, | ||||||
complete?: () => void | ||||||
): Promise<void> { | ||||||
this._userProgressHandler = next; | ||||||
this._userProgressErrorHandler = error; | ||||||
this._userProgressCompleteHandler = complete; | ||||||
return this._progressResolver.promise; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need to return this promise here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
} | ||||||
|
||||||
catch(onRejected: (a: Error) => unknown): Promise<unknown> { | ||||||
return this._promiseResolver.promise.catch(onRejected); | ||||||
} | ||||||
|
||||||
then( | ||||||
onFulfilled?: (a: firestore.LoadBundleTaskProgress) => unknown, | ||||||
onRejected?: (a: Error) => unknown | ||||||
): Promise<unknown> { | ||||||
return this._promiseResolver.promise.then(onFulfilled, onRejected); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Notifies the completion of loading a bundle, with a provided | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or: Notifies all observers that bundle loading has completed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
* `LoadBundleTaskProgress` object. | ||||||
*/ | ||||||
_completeWith(progress: firestore.LoadBundleTaskProgress): void { | ||||||
this._updateProgress(progress); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not need to set the taskState here (like you did below)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call sites set this up. I added an assert instead. |
||||||
if (this._userProgressCompleteHandler) { | ||||||
this._userProgressCompleteHandler(); | ||||||
} | ||||||
this._progressResolver.resolve(); | ||||||
|
||||||
this._promiseResolver.resolve(progress); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Notifies a failure of loading a bundle, with a provided `Error` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or: Notifies all observers that bundle loading has failed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
* as the reason. | ||||||
*/ | ||||||
_failedWith(error: Error): void { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/_failedWith/failWith/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
this._lastProgress.taskState = 'Error'; | ||||||
|
||||||
if (this._userProgressHandler) { | ||||||
this._userProgressHandler(this._lastProgress); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try to match this behavior: firebase-js-sdk/packages/storage/src/task.ts Line 626 in 0131e1f
Basically, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, we would keep current behaviour because these progress updates can be considered part of the progress update series. |
||||||
|
||||||
if (this._userProgressErrorHandler) { | ||||||
this._userProgressErrorHandler(error); | ||||||
} | ||||||
this._progressResolver.reject(error); | ||||||
|
||||||
this._promiseResolver.reject(error); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Notifies a progress update of loading a bundle. | ||||||
* @param progress The new progress. | ||||||
*/ | ||||||
_updateProgress(progress: firestore.LoadBundleTaskProgress): void { | ||||||
if (this._lastProgress.taskState === 'Error') { | ||||||
return; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be an assert? Why would we get additional progress updates after the task has failed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
} | ||||||
|
||||||
this._lastProgress = progress; | ||||||
if (this._userProgressHandler) { | ||||||
this._userProgressHandler(progress); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import * as firestore from '@firebase/firestore-types'; | ||
import { Query } from './query'; | ||
import { SnapshotVersion } from './snapshot_version'; | ||
import { | ||
|
@@ -28,6 +29,14 @@ import * as api from '../protos/firestore_proto_api'; | |
import { DocumentKey } from '../model/document_key'; | ||
import { MaybeDocument, NoDocument } from '../model/document'; | ||
import { debugAssert } from '../util/assert'; | ||
import { | ||
applyBundleDocuments, | ||
LocalStore, | ||
saveNamedQuery | ||
} from '../local/local_store'; | ||
import { SizedBundleElement } from '../util/bundle_reader'; | ||
import { MaybeDocumentMap } from '../model/collections'; | ||
import { BundleMetadata } from '../protos/firestore_bundle_proto'; | ||
|
||
/** | ||
* Represents a Firestore bundle saved by the SDK in its local storage. | ||
|
@@ -98,3 +107,147 @@ export class BundleConverter { | |
return fromVersion(time); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a `LoadBundleTaskProgress` representing the initial progress of | ||
* loading a bundle. | ||
*/ | ||
export function initialProgress( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is an exported function, can we add "bundle" to the name? Optional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
metadata: BundleMetadata | ||
): firestore.LoadBundleTaskProgress { | ||
return { | ||
taskState: 'Running', | ||
documentsLoaded: 0, | ||
bytesLoaded: 0, | ||
totalDocuments: metadata.totalDocuments!, | ||
totalBytes: metadata.totalBytes! | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a `LoadBundleTaskProgress` representing the progress if the bundle | ||
* is already loaded, and we are skipping current loading. | ||
*/ | ||
export function skipLoadingProgress( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be called something like "successProgress", "completedProgress" (again, preferably with Bundle in the name)?. The fact that we skipped loading the bundle seems of less importance here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
metadata: BundleMetadata | ||
): firestore.LoadBundleTaskProgress { | ||
return { | ||
taskState: 'Success', | ||
documentsLoaded: metadata.totalDocuments!, | ||
bytesLoaded: metadata.totalBytes!, | ||
totalDocuments: metadata.totalDocuments!, | ||
totalBytes: metadata.totalBytes! | ||
}; | ||
} | ||
|
||
export class LoadResult { | ||
constructor( | ||
readonly progress: firestore.LoadBundleTaskProgress, | ||
readonly changedDocs?: MaybeDocumentMap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to be optional anymore, which I think leads to one more simplification. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
) {} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is an exported class, it should have a less generic name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
/** | ||
* A class to process the elements from a bundle, load them into local | ||
* storage and provide progress update while loading. | ||
*/ | ||
export class BundleLoader { | ||
/** The current progress of loading */ | ||
private progress: firestore.LoadBundleTaskProgress; | ||
/** | ||
* The threshold multiplier used to determine whether enough elements are | ||
* batched to be loaded, and a progress update is needed. | ||
* | ||
* Applies to both `documentsBuffered` and `bytesBuffered`, triggers storage | ||
* update and reports progress when either of them cross the threshold. | ||
*/ | ||
private thresholdMultiplier = 0.01; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add "progress" to this name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer used actually. |
||
/** Batched queries to be saved into storage */ | ||
private queries: bundleProto.NamedQuery[] = []; | ||
/** Batched documents to be saved into storage */ | ||
private documents: BundledDocuments = []; | ||
/** | ||
* A BundleDocumentMetadata is added to the loader, it is saved here while | ||
* we wait for the actual document. | ||
*/ | ||
private unpairedDocumentMetadata: bundleProto.BundledDocumentMetadata | null = null; | ||
|
||
constructor( | ||
private metadata: bundleProto.BundleMetadata, | ||
private localStore: LocalStore | ||
) { | ||
this.progress = initialProgress(metadata); | ||
} | ||
|
||
/** | ||
* Adds an element from the bundle to the loader. | ||
* | ||
* If adding this element leads to actually saving the batched elements into | ||
* storage, the returned promise will resolve to a `LoadResult`, otherwise | ||
* it will resolve to null. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the documentation seems outdated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
*/ | ||
addSizedElement( | ||
element: SizedBundleElement | ||
): firestore.LoadBundleTaskProgress | null { | ||
debugAssert(!element.isBundleMetadata(), 'Unexpected bundle metadata.'); | ||
|
||
this.progress.bytesLoaded += element.byteLength; | ||
if (element.payload.namedQuery) { | ||
this.queries.push(element.payload.namedQuery); | ||
} | ||
|
||
if (element.payload.documentMetadata) { | ||
if (element.payload.documentMetadata.exists) { | ||
this.unpairedDocumentMetadata = element.payload.documentMetadata; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this push to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, in the next call, we would have to get the last element and make sure it is a metadata and has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks. |
||
} else { | ||
this.documents.push({ | ||
metadata: element.payload.documentMetadata, | ||
document: undefined | ||
}); | ||
this.progress.documentsLoaded += 1; | ||
} | ||
} | ||
|
||
if (element.payload.document) { | ||
debugAssert( | ||
!!this.unpairedDocumentMetadata, | ||
'Unexpected document when no pairing metadata is found' | ||
); | ||
this.documents.push({ | ||
metadata: this.unpairedDocumentMetadata!, | ||
document: element.payload.document | ||
}); | ||
this.progress.documentsLoaded += 1; | ||
this.unpairedDocumentMetadata = null; | ||
} | ||
|
||
// Loading a document metadata will not update progress. | ||
if (this.unpairedDocumentMetadata) { | ||
return null; | ||
} | ||
|
||
return { ...this.progress }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that I changed this in my diff to just retun this.progress, but I think you are correct and we need to return an immutable copy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
/** | ||
* Update the progress to 'Success' and return the updated progress. | ||
*/ | ||
async complete(): Promise<LoadResult> { | ||
debugAssert( | ||
!this.unpairedDocumentMetadata, | ||
'Unexpected document when no pairing metadata is found' | ||
); | ||
|
||
for (const q of this.queries) { | ||
await saveNamedQuery(this.localStore, q); | ||
} | ||
|
||
let changedDocs; | ||
if (this.documents.length > 0) { | ||
changedDocs = await applyBundleDocuments(this.localStore, this.documents); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How often do you think bundles will be empty? If this is rare, then we could just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
this.progress.taskState = 'Success'; | ||
return new LoadResult({ ...this.progress }, changedDocs); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, should we include an overload that takes an Observer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's actually in the API doc. I will add it in a followup PR.