Skip to content

Commit 3d2d896

Browse files
Add update_time to SQLRemoteDocumentCache schema
1 parent cce8fd0 commit 3d2d896

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.firebase.firestore.model.DocumentKey;
2525
import com.google.firebase.firestore.model.MaybeDocument;
2626
import com.google.firebase.firestore.model.ResourcePath;
27+
import com.google.firebase.firestore.model.SnapshotVersion;
2728
import com.google.firebase.firestore.util.BackgroundQueue;
2829
import com.google.firebase.firestore.util.Executors;
2930
import com.google.protobuf.InvalidProtocolBufferException;
@@ -51,13 +52,16 @@ final class SQLiteRemoteDocumentCache implements RemoteDocumentCache {
5152
@Override
5253
public void add(MaybeDocument maybeDocument) {
5354
String path = pathForKey(maybeDocument.getKey());
55+
SnapshotVersion snapshotVersion = maybeDocument.getVersion();
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 (path, snapshot_version_micros, contents) "
62+
+ "VALUES (?, ?, ?)",
6063
path,
64+
snapshotVersion.toMicroseconds(),
6165
message.toByteArray());
6266

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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SQLiteSchema {
4646
* The version of the schema. Increase this by one for each migration added to runMigrations
4747
* below.
4848
*/
49-
static final int VERSION = 8;
49+
static final int VERSION = 9;
5050
// Remove this constant and increment VERSION to enable indexing support
5151
static final int INDEXING_SUPPORT_VERSION = VERSION + 1;
5252

@@ -127,6 +127,10 @@ void runMigrations(int fromVersion, int toVersion) {
127127
createV8CollectionParentsIndex();
128128
}
129129

130+
if (fromVersion < 9 && toVersion >= 9) {
131+
addUpdateTime();
132+
}
133+
130134
/*
131135
* Adding a new migration? READ THIS FIRST!
132136
*
@@ -351,6 +355,12 @@ private void addSequenceNumber() {
351355
}
352356
}
353357

358+
private void addUpdateTime() {
359+
if (!tableContainsColumn("remote_documents", "snapshot_version_micros")) {
360+
db.execSQL("ALTER TABLE remote_documents ADD COLUMN snapshot_version_micros INTEGER");
361+
}
362+
}
363+
354364
/**
355365
* Ensures that each entry in the remote document cache has a corresponding sentinel row. Any
356366
* entries that lack a sentinel row are given one with the sequence number set to the highest

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

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

1717
import com.google.firebase.Timestamp;
18+
import java.util.concurrent.TimeUnit;
1819

1920
/**
2021
* A version of a document in Firestore. This corresponds to the version timestamp, such as
@@ -36,6 +37,12 @@ public Timestamp getTimestamp() {
3637
return timestamp;
3738
}
3839

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+
3946
@Override
4047
public int compareTo(SnapshotVersion other) {
4148
return timestamp.compareTo(other.timestamp);

0 commit comments

Comments
 (0)