Skip to content

Commit 5146529

Browse files
Merge branch 'mrschmidt/indexfree-2' into mrschmidt/indexfree-3
2 parents 8542008 + b77194a commit 5146529

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import static com.google.firebase.firestore.util.Assert.fail;
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

20+
import com.google.firebase.Timestamp;
2021
import com.google.firebase.database.collection.ImmutableSortedMap;
2122
import com.google.firebase.firestore.core.Query;
2223
import com.google.firebase.firestore.model.Document;
2324
import com.google.firebase.firestore.model.DocumentCollections;
2425
import com.google.firebase.firestore.model.DocumentKey;
2526
import com.google.firebase.firestore.model.MaybeDocument;
2627
import com.google.firebase.firestore.model.ResourcePath;
27-
import com.google.firebase.firestore.model.SnapshotVersion;
2828
import com.google.firebase.firestore.util.BackgroundQueue;
2929
import com.google.firebase.firestore.util.Executors;
3030
import com.google.protobuf.InvalidProtocolBufferException;
@@ -52,16 +52,18 @@ final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
5252
@Override
5353
public void add(MaybeDocument maybeDocument) {
5454
String path = pathForKey(maybeDocument.getKey());
55-
SnapshotVersion snapshotVersion = maybeDocument.getVersion();
55+
Timestamp timestamp = maybeDocument.getVersion().getTimestamp();
5656
MessageLite message = serializer.encodeMaybeDocument(maybeDocument);
5757

5858
statsCollector.recordRowsWritten(STATS_TAG, 1);
5959

6060
db.execute(
61-
"INSERT OR REPLACE INTO remote_documents (path, snapshot_version_micros, contents) "
62-
+ "VALUES (?, ?, ?)",
61+
"INSERT OR REPLACE INTO remote_documents "
62+
+ "(path, update_time_seconds, update_time_nanos, contents) "
63+
+ "VALUES (?, ?, ?, ?)",
6364
path,
64-
snapshotVersion.toMicroseconds(),
65+
timestamp.getSeconds(),
66+
timestamp.getNanoseconds(),
6567
message.toByteArray());
6668

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

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

Lines changed: 14 additions & 2 deletions
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
*/
4956
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

@@ -356,8 +364,12 @@ private void addSequenceNumber() {
356364
}
357365

358366
private void addUpdateTime() {
359-
if (!tableContainsColumn("remote_documents", "snapshot_version_micros")) {
360-
db.execSQL("ALTER TABLE remote_documents ADD COLUMN snapshot_version_micros INTEGER");
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");
361373
}
362374
}
363375

firebase-firestore/src/main/java/com/google/firebase/firestore/model/SnapshotVersion.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.firestore.model;
1616

1717
import com.google.firebase.Timestamp;
18-
import java.util.concurrent.TimeUnit;
1918

2019
/**
2120
* A version of a document in Firestore. This corresponds to the version timestamp, such as
@@ -37,12 +36,6 @@ public Timestamp getTimestamp() {
3736
return timestamp;
3837
}
3938

40-
/** Returns the microseconds since EPOCH that this snapshot version represents. */
41-
public long toMicroseconds() {
42-
return TimeUnit.MICROSECONDS.convert(timestamp.getSeconds(), TimeUnit.SECONDS)
43-
+ TimeUnit.MICROSECONDS.convert(timestamp.getNanoseconds(), TimeUnit.NANOSECONDS);
44-
}
45-
4639
@Override
4740
public int compareTo(SnapshotVersion other) {
4841
return timestamp.compareTo(other.timestamp);

0 commit comments

Comments
 (0)