Skip to content

Commit 3d66aaa

Browse files
Add update_time to SQLLiteRemoteDocument schema
1 parent 0b13b10 commit 3d66aaa

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

2020
import androidx.annotation.Nullable;
21+
import com.google.firebase.Timestamp;
2122
import com.google.firebase.database.collection.ImmutableSortedMap;
2223
import com.google.firebase.firestore.core.Query;
2324
import com.google.firebase.firestore.model.Document;
@@ -51,13 +52,18 @@ final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
5152
@Override
5253
public void add(MaybeDocument maybeDocument) {
5354
String path = pathForKey(maybeDocument.getKey());
55+
Timestamp timestamp = maybeDocument.getVersion().getTimestamp();
5456
MessageLite message = serializer.encodeMaybeDocument(maybeDocument);
5557

5658
statsCollector.recordRowsWritten(STATS_TAG, 1);
5759

5860
db.execute(
59-
"INSERT OR REPLACE INTO remote_documents (path, contents) VALUES (?, ?)",
61+
"INSERT OR REPLACE INTO remote_documents "
62+
+ "(path, update_time_seconds, update_time_nanos, contents) "
63+
+ "VALUES (?, ?, ?, ?)",
6064
path,
65+
timestamp.getSeconds(),
66+
timestamp.getNanoseconds(),
6167
message.toByteArray());
6268

6369
db.getIndexManager().addToCollectionParentIndex(maybeDocument.getKey().getPath().popLast());

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteSchema.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ class SQLiteSchema {
4545
/**
4646
* The version of the schema. Increase this by one for each migration added to runMigrations
4747
* below.
48+
*
49+
* <p>TODO(index-free): The migration to schema version 9 doesn't backfill `update_time` as this
50+
* requires rewriting the RemoteDocumentCache. For index-free queries to efficiently handle
51+
* existing documents, we still need to populate update_time for all existing entries, drop the
52+
* RemoteDocumentCache or ask users to invoke `clearPersistence()` manually. If we decide to
53+
* backfill or drop the contents of the RemoteDocumentCache, we need to perform an additional
54+
* schema migration.
4855
*/
49-
static final int VERSION = 8;
56+
static final int VERSION = 9;
57+
5058
// Remove this constant and increment VERSION to enable indexing support
5159
static final int INDEXING_SUPPORT_VERSION = VERSION + 1;
5260

@@ -127,6 +135,10 @@ void runMigrations(int fromVersion, int toVersion) {
127135
createV8CollectionParentsIndex();
128136
}
129137

138+
if (fromVersion < 9 && toVersion >= 9) {
139+
addUpdateTime();
140+
}
141+
130142
/*
131143
* Adding a new migration? READ THIS FIRST!
132144
*
@@ -351,6 +363,16 @@ private void addSequenceNumber() {
351363
}
352364
}
353365

366+
private void addUpdateTime() {
367+
if (!tableContainsColumn("remote_documents", "update_time_seconds")) {
368+
hardAssert(
369+
!tableContainsColumn("remote_documents", "update_time_nanos"),
370+
"Table contained update_time_seconds, but is missing update_time_nanos");
371+
db.execSQL("ALTER TABLE remote_documents ADD COLUMN update_time_seconds INTEGER");
372+
db.execSQL("ALTER TABLE remote_documents ADD COLUMN update_time_nanos INTEGER");
373+
}
374+
}
375+
354376
/**
355377
* Ensures that each entry in the remote document cache has a corresponding sentinel row. Any
356378
* entries that lack a sentinel row are given one with the sequence number set to the highest

0 commit comments

Comments
 (0)