Skip to content

Commit c0e8e84

Browse files
authored
Merge 32f93ff into 94ee69a
2 parents 94ee69a + 32f93ff commit c0e8e84

File tree

3 files changed

+277
-2
lines changed

3 files changed

+277
-2
lines changed

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { BatchId, ListenSequenceNumber, TargetId } from '../core/types';
1919
import { ResourcePath } from '../model/path';
20+
import { BundledQuery } from '../protos/firestore_bundle_proto';
2021
import * as api from '../protos/firestore_proto_api';
2122
import { hardAssert, debugAssert } from '../util/assert';
2223

@@ -52,8 +53,9 @@ import { SimpleDbSchemaConverter, SimpleDbTransaction } from './simple_db';
5253
* 9. Change RemoteDocumentChanges store to be keyed by readTime rather than
5354
* an auto-incrementing ID. This is required for Index-Free queries.
5455
* 10. Rewrite the canonical IDs to the explicit Protobuf-based format.
56+
* 11. Add bundles and named_queries for bundle support.
5557
*/
56-
export const SCHEMA_VERSION = 10;
58+
export const SCHEMA_VERSION = 11;
5759

5860
/** Performs database creation and schema upgrades. */
5961
export class SchemaConverter implements SimpleDbSchemaConverter {
@@ -154,6 +156,13 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
154156
if (fromVersion < 10 && toVersion >= 10) {
155157
p = p.next(() => this.rewriteCanonicalIds(simpleDbTransaction));
156158
}
159+
160+
if (fromVersion < 11 && toVersion >= 11) {
161+
p = p.next(() => {
162+
createBundlesStore(db);
163+
createNamedQueriesStore(db);
164+
});
165+
}
157166
return p;
158167
}
159168

@@ -1072,6 +1081,60 @@ function createClientMetadataStore(db: IDBDatabase): void {
10721081
});
10731082
}
10741083

1084+
export type DbBundlesKey = string;
1085+
1086+
/**
1087+
* A object representing a bundle loaded by the SDK.
1088+
*/
1089+
export class DbBundles {
1090+
/** Name of the IndexedDb object store. */
1091+
static store = 'bundles';
1092+
1093+
static keyPath = ['bundleId'];
1094+
1095+
constructor(
1096+
/** The ID of the loaded bundle. */
1097+
public bundleId: string,
1098+
/** The create time of the loaded bundle. */
1099+
public createTime: DbTimestamp,
1100+
/** The schema version of the loaded bundle. */
1101+
public version: number
1102+
) {}
1103+
}
1104+
1105+
function createBundlesStore(db: IDBDatabase): void {
1106+
db.createObjectStore(DbBundles.store, {
1107+
keyPath: DbBundles.keyPath
1108+
});
1109+
}
1110+
1111+
export type DbNamedQueriesKey = string;
1112+
1113+
/**
1114+
* A object representing a named query loaded by the SDK via a bundle.
1115+
*/
1116+
export class DbNamedQueries {
1117+
/** Name of the IndexedDb object store. */
1118+
static store = 'namedQueries';
1119+
1120+
static keyPath = ['name'];
1121+
1122+
constructor(
1123+
/** The name of the query. */
1124+
public name: string,
1125+
/** The read time of the results saved in the bundle from the named query. */
1126+
public readTime: DbTimestamp,
1127+
/** The query saved in the bundle. */
1128+
public bundledQuery: BundledQuery
1129+
) {}
1130+
}
1131+
1132+
function createNamedQueriesStore(db: IDBDatabase): void {
1133+
db.createObjectStore(DbNamedQueries.store, {
1134+
keyPath: DbNamedQueries.keyPath
1135+
});
1136+
}
1137+
10751138
// Visible for testing
10761139
export const V1_STORES = [
10771140
DbMutationQueue.store,
@@ -1105,9 +1168,11 @@ export const V8_STORES = [...V6_STORES, DbCollectionParent.store];
11051168

11061169
// V10 does not change the set of stores.
11071170

1171+
export const V11_STORES = [...V8_STORES, DbCollectionParent.store];
1172+
11081173
/**
11091174
* The list of all default IndexedDB stores used throughout the SDK. This is
11101175
* used when creating transactions so that access across all stores is done
11111176
* atomically.
11121177
*/
1113-
export const ALL_STORES = V8_STORES;
1178+
export const ALL_STORES = V11_STORES;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2020 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// This file defines the format of Firestore bundle file/stream. It is not a part of the
17+
// Firestore API, only a specification used by Server and Client SDK to write and read
18+
// bundles.
19+
20+
syntax = "proto3";
21+
22+
package firestore;
23+
24+
import "google/firestore/v1/document.proto";
25+
import "google/firestore/v1/query.proto";
26+
import "google/protobuf/timestamp.proto";
27+
28+
option csharp_namespace = "Firestore.Proto";
29+
option go_package = "google.golang.org/genproto/firestore/proto;firestore";
30+
option java_multiple_files = true;
31+
option java_outer_classname = "BundleProto";
32+
option java_package = "com.google.firestore.proto";
33+
option objc_class_prefix = "FSTPB";
34+
option php_namespace = "Firestore\\Proto";
35+
36+
// Describes a query saved in the bundle.
37+
message BundledQuery {
38+
// The parent resource name.
39+
string parent = 1;
40+
41+
// The query to run.
42+
oneof query_type {
43+
// A structured query.
44+
google.firestore.v1.StructuredQuery structured_query = 2;
45+
}
46+
47+
// If the query is a limit query, should the limit be applied to the beginning or
48+
// the end of results.
49+
enum LimitType {
50+
FIRST = 0;
51+
LAST = 1;
52+
}
53+
LimitType limit_type = 3;
54+
}
55+
56+
// A Query associated with a name, created as part of the bundle file, and can be read
57+
// by client SDKs once the bundle containing them is loaded.
58+
message NamedQuery {
59+
// Name of the query, such that client can use the name to load this query
60+
// from bundle, and resume from when the query results are materialized
61+
// into this bundle.
62+
string name = 1;
63+
64+
// The query saved in the bundle.
65+
BundledQuery bundled_query = 2;
66+
67+
// The read time of the query, when it is used to build the bundle. This is useful to
68+
// resume the query from the bundle, once it is loaded by client SDKs.
69+
google.protobuf.Timestamp read_time = 3;
70+
}
71+
72+
// Metadata describing a Firestore document saved in the bundle.
73+
message BundledDocumentMetadata {
74+
// The document key of a bundled document.
75+
string name = 1;
76+
77+
// The snapshot version of the document data bundled.
78+
google.protobuf.Timestamp read_time = 2;
79+
80+
// Whether the document exists.
81+
bool exists = 3;
82+
}
83+
84+
// Metadata describing the bundle file/stream.
85+
message BundleMetadata {
86+
// The ID of the bundle.
87+
string id = 1;
88+
89+
// Time at which the documents snapshot is taken for this bundle.
90+
google.protobuf.Timestamp create_time = 2;
91+
92+
// The schema version of the bundle.
93+
uint32 version = 3;
94+
95+
// The number of documents in the bundle.
96+
uint32 total_documents = 4;
97+
98+
// The size of the bundle in bytes, excluding this `BundleMetadata`.
99+
uint64 total_bytes = 5;
100+
}
101+
102+
// A Firestore bundle is a length-prefixed stream of JSON representations of
103+
// `BundleElement`.
104+
// Only one `BundleMetadata` is expected, and it should be the first element.
105+
// The named queries follow after `metadata`. If a document exists when the
106+
// bundle is built, `document_metadata` is immediately followed by the
107+
// `document`, otherwise `document_metadata` will present by itself.
108+
message BundleElement {
109+
oneof element_type {
110+
BundleMetadata metadata = 1;
111+
112+
NamedQuery named_query = 2;
113+
114+
BundledDocumentMetadata document_metadata = 3;
115+
116+
google.firestore.v1.Document document = 4;
117+
}
118+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as api from './firestore_proto_api';
19+
20+
/** Properties of a BundledQuery. */
21+
export interface BundledQuery {
22+
/** BundledQuery parent */
23+
parent?: string | null;
24+
25+
/** BundledQuery structuredQuery */
26+
structuredQuery?: api.StructuredQuery | null;
27+
28+
/** BundledQuery limitType */
29+
limitType?: BundledQuery.LimitType | null;
30+
}
31+
32+
export namespace BundledQuery {
33+
/** LimitType enum. */
34+
type LimitType = 'FIRST' | 'LAST';
35+
}
36+
37+
/** Properties of a NamedQuery. */
38+
export interface NamedQuery {
39+
/** NamedQuery name */
40+
name?: string | null;
41+
42+
/** NamedQuery bundledQuery */
43+
bundledQuery?: BundledQuery | null;
44+
45+
/** NamedQuery readTime */
46+
readTime?: api.Timestamp | null;
47+
}
48+
49+
/** Properties of a BundledDocumentMetadata. */
50+
export interface BundledDocumentMetadata {
51+
/** BundledDocumentMetadata name */
52+
name?: string | null;
53+
54+
/** BundledDocumentMetadata readTime */
55+
readTime?: api.Timestamp | null;
56+
57+
/** BundledDocumentMetadata exists */
58+
exists?: boolean | null;
59+
}
60+
61+
/** Properties of a BundleMetadata. */
62+
interface BundleMetadata {
63+
/** BundleMetadata id */
64+
id?: string | null;
65+
66+
/** BundleMetadata createTime */
67+
createTime?: api.Timestamp | null;
68+
69+
/** BundleMetadata version */
70+
version?: number | null;
71+
72+
/** BundleMetadata totalDocuments */
73+
totalDocuments?: number | null;
74+
75+
/** BundleMetadata totalBytes */
76+
totalBytes?: number | null;
77+
}
78+
79+
/** Properties of a BundleElement. */
80+
interface BundleElement {
81+
/** BundleElement metadata */
82+
metadata?: BundleMetadata | null;
83+
84+
/** BundleElement namedQuery */
85+
namedQuery?: NamedQuery | null;
86+
87+
/** BundleElement documentMetadata */
88+
documentMetadata?: BundledDocumentMetadata | null;
89+
90+
/** BundleElement document */
91+
document?: api.Document | null;
92+
}

0 commit comments

Comments
 (0)