-
Notifications
You must be signed in to change notification settings - Fork 944
Implement BundleCache for IDB and memory. #3170
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 13 commits
9602712
c5e783e
5e7fb89
1ee1615
18f0be1
aa455bf
78248cd
83160a1
24e10cb
9d6edc5
4cbe608
4313e51
296cfc4
de1d162
1775298
f572812
2dde827
6eafb6f
a09a67f
5b152df
06e3e77
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @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 { Query } from './query'; | ||
import { SnapshotVersion } from './snapshot_version'; | ||
|
||
/** | ||
* Represents a Firestore bundle saved by the SDK in its local storage. | ||
*/ | ||
export interface Bundle { | ||
readonly id: string; | ||
readonly version: number; | ||
// When the saved bundle is built from the server SDKs. | ||
readonly createTime: SnapshotVersion; | ||
} | ||
|
||
/** | ||
* Represents a Query saved by the SDK in its local storage. | ||
*/ | ||
export interface NamedQuery { | ||
readonly name: string; | ||
readonly query: Query; | ||
// When the results for this query are read to the saved bundle. | ||
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. |
||
readonly readTime: SnapshotVersion; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @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 { PersistenceTransaction } from './persistence'; | ||
import { PersistencePromise } from './persistence_promise'; | ||
import * as bundleProto from '../protos/firestore_bundle_proto'; | ||
import { Bundle, NamedQuery } from '../core/bundle'; | ||
|
||
/** | ||
* Provides interfaces to save and read Firestore bundles. | ||
*/ | ||
export interface BundleCache { | ||
/** | ||
* Gets a saved `Bundle` for a given `bundleId`, returns undefined if | ||
* no bundles are found under the given id. | ||
*/ | ||
getBundle( | ||
transaction: PersistenceTransaction, | ||
bundleId: string | ||
): PersistencePromise<Bundle | undefined>; | ||
|
||
/** | ||
* Saves a `BundleMetadata` from a bundle into local storage, using its id as the persistent key. | ||
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: Wrap at 80 characters. 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. |
||
*/ | ||
saveBundleMetadata( | ||
transaction: PersistenceTransaction, | ||
metadata: bundleProto.BundleMetadata | ||
): PersistencePromise<void>; | ||
|
||
/** | ||
* Gets a saved `NamedQuery` for the given query name. Returns undefined if | ||
* no queries are found under the given name. | ||
*/ | ||
getNamedQuery( | ||
transaction: PersistenceTransaction, | ||
queryName: string | ||
): PersistencePromise<NamedQuery | undefined>; | ||
|
||
/** | ||
* Saves a `NamedQuery` from a bundle, using its name as the persistent key. | ||
*/ | ||
saveNamedQuery( | ||
transaction: PersistenceTransaction, | ||
query: bundleProto.NamedQuery | ||
): PersistencePromise<void>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* @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 { PersistenceTransaction } from './persistence'; | ||
import { PersistencePromise } from './persistence_promise'; | ||
import * as bundleProto from '../protos/firestore_bundle_proto'; | ||
import { BundleCache } from './bundle_cache'; | ||
import { | ||
DbBundle, | ||
DbBundlesKey, | ||
DbNamedQuery, | ||
DbNamedQueriesKey | ||
} from './indexeddb_schema'; | ||
import { SimpleDbStore } from './simple_db'; | ||
import { IndexedDbPersistence } from './indexeddb_persistence'; | ||
import { LocalSerializer } from './local_serializer'; | ||
import { Bundle, NamedQuery } from '../core/bundle'; | ||
|
||
export class IndexedDbBundleCache implements BundleCache { | ||
constructor(private serializer: LocalSerializer) {} | ||
|
||
getBundle( | ||
transaction: PersistenceTransaction, | ||
bundleId: string | ||
): PersistencePromise<Bundle | undefined> { | ||
return bundlesStore(transaction) | ||
.get(bundleId) | ||
.next(bundle => { | ||
if (bundle) { | ||
return this.serializer.fromDbBundle(bundle!); | ||
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. You should be able to drop the bang. 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. |
||
} | ||
return undefined; | ||
}); | ||
} | ||
|
||
saveBundleMetadata( | ||
transaction: PersistenceTransaction, | ||
bundleMetadata: bundleProto.BundleMetadata | ||
): PersistencePromise<void> { | ||
return bundlesStore(transaction).put( | ||
this.serializer.toDbBundle(bundleMetadata) | ||
); | ||
} | ||
|
||
getNamedQuery( | ||
transaction: PersistenceTransaction, | ||
queryName: string | ||
): PersistencePromise<NamedQuery | undefined> { | ||
return namedQueriesStore(transaction) | ||
.get(queryName) | ||
.next(query => { | ||
if (query) { | ||
return this.serializer.fromDbNamedQuery(query!); | ||
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. You should be able to drop the bang. 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. |
||
} | ||
return undefined; | ||
}); | ||
} | ||
|
||
saveNamedQuery( | ||
transaction: PersistenceTransaction, | ||
query: bundleProto.NamedQuery | ||
): PersistencePromise<void> { | ||
return namedQueriesStore(transaction).put( | ||
this.serializer.toDbNamedQuery(query) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Helper to get a typed SimpleDbStore for the bundles object store. | ||
*/ | ||
function bundlesStore( | ||
txn: PersistenceTransaction | ||
): SimpleDbStore<DbBundlesKey, DbBundle> { | ||
return IndexedDbPersistence.getStore<DbBundlesKey, DbBundle>( | ||
txn, | ||
DbBundle.store | ||
); | ||
} | ||
|
||
/** | ||
* Helper to get a typed SimpleDbStore for the namedQueries object store. | ||
*/ | ||
function namedQueriesStore( | ||
txn: PersistenceTransaction | ||
): SimpleDbStore<DbNamedQueriesKey, DbNamedQuery> { | ||
return IndexedDbPersistence.getStore<DbNamedQueriesKey, DbNamedQuery>( | ||
txn, | ||
DbNamedQuery.store | ||
); | ||
} |
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.
Can you point out in the documentation that this is to SnapshotVersion.MIN if the above is not true? In its current form, the comment raises the question why the type is not nullable.
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.
I am not sure what do you mean here, sorry!
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.
Something like:
Set to the snapshot version of the bundle if created by the Server SDKs. Otherwise set to SnapshotVersion.MIN.
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.
Done.