Skip to content

Commit 8531e06

Browse files
Change removeMutationBatch to remove a single batch
1 parent 7005c97 commit 8531e06

File tree

5 files changed

+37
-65
lines changed

5 files changed

+37
-65
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ private void startMutationQueue() {
176176
if (!batches.isEmpty()) {
177177
// NOTE: This could be more efficient if we had a removeBatchesThroughBatchID, but
178178
// this set should be very small and this code should go away eventually.
179-
mutationQueue.removeMutationBatches(batches);
179+
for (MutationBatch batch : batches) {
180+
mutationQueue.removeMutationBatch(batch);
181+
}
180182
}
181183
}
182184
});
@@ -641,9 +643,9 @@ private Set<DocumentKey> removeMutationBatches(List<MutationBatch> batches) {
641643
for (Mutation mutation : batch.getMutations()) {
642644
affectedDocs.add(mutation.getKey());
643645
}
646+
mutationQueue.removeMutationBatch(batch);
644647
}
645648

646-
mutationQueue.removeMutationBatches(batches);
647649
return affectedDocs;
648650
}
649651

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

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -328,69 +328,40 @@ private List<MutationBatch> lookupMutationBatches(ImmutableSortedSet<Integer> ba
328328
}
329329

330330
@Override
331-
public void removeMutationBatches(List<MutationBatch> batches) {
332-
int batchCount = batches.size();
333-
hardAssert(batchCount > 0, "Should not remove mutations when none exist.");
334-
335-
int firstBatchId = batches.get(0).getBatchId();
336-
337-
int queueCount = queue.size();
338-
331+
public void removeMutationBatch(MutationBatch batch) {
339332
// Find the position of the first batch for removal. This need not be the first entry in the
340333
// queue.
341-
int startIndex = indexOfExistingBatchId(firstBatchId, "removed");
334+
int batchIndex = indexOfExistingBatchId(batch.getBatchId(), "removed");
342335
hardAssert(
343-
queue.get(startIndex).getBatchId() == firstBatchId,
336+
queue.get(batchIndex).getBatchId() == batch.getBatchId(),
344337
"Removed batches must exist in the queue");
345338

346-
// Check that removed batches are contiguous (while excluding tombstones).
347-
int batchIndex = 1;
348-
int queueIndex = startIndex + 1;
349-
while (batchIndex < batchCount && queueIndex < queueCount) {
350-
MutationBatch batch = queue.get(queueIndex);
351-
if (batch.isTombstone()) {
352-
queueIndex++;
353-
continue;
354-
}
355-
356-
hardAssert(
357-
batch.getBatchId() == batches.get(batchIndex).getBatchId(),
358-
"Removed batches must be contiguous in the queue");
359-
batchIndex++;
360-
queueIndex++;
361-
}
362-
363339
// Only actually remove batches if removing at the front of the queue. Previously rejected
364340
// batches may have left tombstones in the queue, so expand the removal range to include any
365341
// tombstones.
366-
if (startIndex == 0) {
367-
for (; queueIndex < queueCount; queueIndex++) {
368-
MutationBatch batch = queue.get(queueIndex);
369-
if (!batch.isTombstone()) {
342+
if (batchIndex == 0) {
343+
int endIndex = 1;
344+
for (; endIndex < queue.size(); endIndex++) {
345+
MutationBatch currentBatch = queue.get(endIndex);
346+
if (!currentBatch.isTombstone()) {
370347
break;
371348
}
372349
}
373350

374-
queue.subList(startIndex, queueIndex).clear();
351+
queue.subList(batchIndex, endIndex).clear();
375352

376353
} else {
377-
// Mark tombstones
378-
for (int i = startIndex; i < queueIndex; i++) {
379-
queue.set(i, queue.get(i).toTombstone());
380-
}
354+
queue.set(batchIndex, queue.get(batchIndex).toTombstone());
381355
}
382356

383357
// Remove entries from the index too.
384358
ImmutableSortedSet<DocumentReference> references = batchesByDocumentKey;
385-
for (MutationBatch batch : batches) {
386-
int batchId = batch.getBatchId();
387-
for (Mutation mutation : batch.getMutations()) {
388-
DocumentKey key = mutation.getKey();
389-
persistence.getReferenceDelegate().removeMutationReference(key);
390-
391-
DocumentReference reference = new DocumentReference(key, batchId);
392-
references = references.remove(reference);
393-
}
359+
for (Mutation mutation : batch.getMutations()) {
360+
DocumentKey key = mutation.getKey();
361+
persistence.getReferenceDelegate().removeMutationReference(key);
362+
363+
DocumentReference reference = new DocumentReference(key, batch.getBatchId());
364+
references = references.remove(reference);
394365
}
395366
batchesByDocumentKey = references;
396367
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,14 @@ List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(
137137
List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query);
138138

139139
/**
140-
* Removes the given mutation batches from the queue. This is useful in two circumstances:
140+
* Removes the given mutation batch from the queue. This is useful in two circumstances:
141141
*
142142
* <ul>
143143
* <li>Removing applied mutations from the head of the queue
144144
* <li>Removing rejected mutations from anywhere in the queue
145145
* </ul>
146-
*
147-
* <p>In both cases, the array of mutations to remove must be a contiguous range of batchIds. This
148-
* is most easily accomplished by loading mutations with {@link
149-
* #getAllMutationBatchesThroughBatchId}.
150146
*/
151-
void removeMutationBatches(List<MutationBatch> batches);
147+
void removeMutationBatch(MutationBatch batch);
152148

153149
/** Performs a consistency check, examining the mutation queue for any leaks, if possible. */
154150
void performConsistencyCheck();

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,23 +421,21 @@ public List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query) {
421421
}
422422

423423
@Override
424-
public void removeMutationBatches(List<MutationBatch> batches) {
424+
public void removeMutationBatch(MutationBatch batch) {
425425
SQLiteStatement mutationDeleter =
426426
db.prepare("DELETE FROM mutations WHERE uid = ? AND batch_id = ?");
427427

428428
SQLiteStatement indexDeleter =
429429
db.prepare("DELETE FROM document_mutations WHERE uid = ? AND path = ? AND batch_id = ?");
430430

431-
for (MutationBatch batch : batches) {
432-
int batchId = batch.getBatchId();
433-
int deleted = db.execute(mutationDeleter, uid, batchId);
434-
hardAssert(deleted != 0, "Mutation batch (%s, %d) did not exist", uid, batch.getBatchId());
431+
int batchId = batch.getBatchId();
432+
int deleted = db.execute(mutationDeleter, uid, batchId);
433+
hardAssert(deleted != 0, "Mutation batch (%s, %d) did not exist", uid, batch.getBatchId());
435434

436-
for (Mutation mutation : batch.getMutations()) {
437-
DocumentKey key = mutation.getKey();
438-
String path = EncodedPath.encode(key.getPath());
439-
db.execute(indexDeleter, uid, path, batchId);
440-
}
435+
for (Mutation mutation : batch.getMutations()) {
436+
DocumentKey key = mutation.getKey();
437+
String path = EncodedPath.encode(key.getPath());
438+
db.execute(indexDeleter, uid, path, batchId);
441439
}
442440
}
443441

firebase-firestore/src/test/java/com/google/firebase/firestore/local/MutationQueueTestCase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void testAcknowledgeThenRemove() {
143143
name.getMethodName(),
144144
() -> {
145145
mutationQueue.acknowledgeBatch(batch1, WriteStream.EMPTY_STREAM_TOKEN);
146-
mutationQueue.removeMutationBatches(asList(batch1));
146+
mutationQueue.removeMutationBatch(batch1);
147147
});
148148

149149
assertEquals(0, batchCount());
@@ -536,7 +536,12 @@ private void acknowledgeBatch(MutationBatch batch) {
536536
/** Calls removeMutationBatches on the mutation queue in a new transaction and commits. */
537537
private void removeMutationBatches(MutationBatch... batches) {
538538
persistence.runTransaction(
539-
"Remove mutation batches", () -> mutationQueue.removeMutationBatches(asList(batches)));
539+
"Remove mutation batches",
540+
() -> {
541+
for (MutationBatch batch : batches) {
542+
mutationQueue.removeMutationBatch(batch);
543+
}
544+
});
540545
}
541546

542547
/** Returns the number of mutation batches in the mutation queue. */

0 commit comments

Comments
 (0)