14
14
15
15
package com .google .firebase .firestore .local ;
16
16
17
- import static com .google .firebase .firestore .util .Assert .fail ;
18
17
import static com .google .firebase .firestore .util .Assert .hardAssert ;
19
18
20
19
import android .content .ContentValues ;
21
20
import android .database .DatabaseUtils ;
22
21
import android .database .sqlite .SQLiteDatabase ;
23
22
import android .database .sqlite .SQLiteStatement ;
24
23
import com .google .common .base .Preconditions ;
25
- import com .google .firebase .firestore .model .DocumentKey ;
26
- import com .google .firebase .firestore .model .mutation .Mutation ;
27
- import com .google .firebase .firestore .model .mutation .MutationBatch ;
28
- import com .google .protobuf .InvalidProtocolBufferException ;
29
24
30
25
/**
31
26
* Migrates schemas from version 0 (empty) to whatever the current version is.
@@ -46,11 +41,10 @@ class SQLiteSchema {
46
41
static final int VERSION = (Persistence .INDEXING_SUPPORT_ENABLED ) ? 7 : 6 ;
47
42
48
43
private final SQLiteDatabase db ;
49
- private final LocalSerializer serializer ;
50
44
51
- SQLiteSchema (SQLiteDatabase db , LocalSerializer serializer ) {
45
+ // PORTING NOTE: The Android client doesn't need to use a serializer to remove held write acks.
46
+ SQLiteSchema (SQLiteDatabase db ) {
52
47
this .db = db ;
53
- this .serializer = serializer ;
54
48
}
55
49
56
50
void runMigrations () {
@@ -148,47 +142,24 @@ private void removeAcknowledgedMutations() {
148
142
149
143
SQLitePersistence .Query mutationsQuery =
150
144
new SQLitePersistence .Query (
151
- db , "SELECT mutations FROM mutations WHERE uid = ? AND batch_id <= ?" )
145
+ db , "SELECT batch_id FROM mutations WHERE uid = ? AND batch_id <= ?" )
152
146
.binding (uid , lastAcknowledgedBatchId );
153
- mutationsQuery .forEach (
154
- value -> {
155
- try {
156
- MutationBatch batch =
157
- serializer .decodeMutationBatch (
158
- com .google .firebase .firestore .proto .WriteBatch .parseFrom (
159
- value .getBlob (0 )));
160
- removeMutationBatch (uid , batch );
161
- } catch (InvalidProtocolBufferException e ) {
162
- throw fail ("MutationBatch failed to parse: %s" , e );
163
- }
164
- });
147
+ mutationsQuery .forEach (value -> removeMutationBatch (uid , value .getInt (0 )));
165
148
});
166
149
}
167
150
168
- private void removeMutationBatch (String uid , MutationBatch batch ) {
169
- int batchId = batch .getBatchId ();
170
-
151
+ private void removeMutationBatch (String uid , int batchId ) {
171
152
SQLiteStatement mutationDeleter =
172
153
db .compileStatement ("DELETE FROM mutations WHERE uid = ? AND batch_id = ?" );
173
154
mutationDeleter .bindString (1 , uid );
174
155
mutationDeleter .bindLong (2 , batchId );
175
156
int deleted = mutationDeleter .executeUpdateDelete ();
176
- hardAssert (deleted != 0 , "Mutation batch (%s, %d) did not exist" , uid , batch .getBatchId ());
177
-
178
- SQLiteStatement indexDeleter =
179
- db .compileStatement (
180
- "DELETE FROM document_mutations WHERE uid = ? AND path = ? AND batch_id = ?" );
181
-
182
- for (Mutation mutation : batch .getMutations ()) {
183
- DocumentKey key = mutation .getKey ();
184
- String path = EncodedPath .encode (key .getPath ());
185
- indexDeleter .bindString (1 , uid );
186
- indexDeleter .bindString (2 , path );
187
- indexDeleter .bindLong (3 , batchId );
188
- deleted = indexDeleter .executeUpdateDelete ();
189
- hardAssert (
190
- deleted != 0 , "Index entry (%s, %s, %d) did not exist" , uid , key , batch .getBatchId ());
191
- }
157
+ hardAssert (deleted != 0 , "Mutatiohn batch (%s, %d) did not exist" , uid , batchId );
158
+
159
+ // Delete all index entries for this batch
160
+ db .execSQL (
161
+ "DELETE FROM document_mutations WHERE uid = ? AND batch_id = ?" ,
162
+ new Object [] {uid , batchId });
192
163
}
193
164
194
165
private void createQueryCache () {
0 commit comments