Skip to content

Commit 05522e2

Browse files
committed
Fix according to PR comments
1 parent f24b0f6 commit 05522e2

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

packages/firestore/src/core/component_provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class MemoryOfflineComponentProvider
126126
cfg,
127127
this.localStore
128128
);
129-
this.indexBackfillerScheduler = this.createIndexBackfiller(
129+
this.indexBackfillerScheduler = this.createIndexBackfillerScheduler(
130130
cfg,
131131
this.localStore
132132
);
@@ -139,7 +139,7 @@ export class MemoryOfflineComponentProvider
139139
return null;
140140
}
141141

142-
createIndexBackfiller(
142+
createIndexBackfillerScheduler(
143143
cfg: ComponentConfiguration,
144144
localStore: LocalStore
145145
): Scheduler | null {
@@ -236,7 +236,7 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
236236
return new LruScheduler(garbageCollector, cfg.asyncQueue, localStore);
237237
}
238238

239-
createIndexBackfiller(
239+
createIndexBackfillerScheduler(
240240
cfg: ComponentConfiguration,
241241
localStore: LocalStore
242242
): Scheduler | null {

packages/firestore/src/local/index_backfiller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const REGULAR_BACKFILL_DELAY_MS = 1;
4242
/** The maximum number of documents to process each time backfill() is called. */
4343
const MAX_DOCUMENTS_TO_PROCESS = 50;
4444

45+
/** This class is responsible for the scheduling of Index Backfiller. */
4546
export class IndexBackfillerScheduler implements Scheduler {
4647
private task: DelayedOperation<void> | null;
4748

@@ -102,11 +103,13 @@ export class IndexBackfillerScheduler implements Scheduler {
102103
}
103104
}
104105

106+
/** Implements the steps for backfilling indexes. */
105107
export class IndexBackfiller {
106108
constructor(
107109
private readonly localStore: LocalStore,
108110
private readonly persistence: Persistence
109111
) {}
112+
110113
async backfill(
111114
maxDocumentsToProcess: number = MAX_DOCUMENTS_TO_PROCESS
112115
): Promise<number> {
@@ -125,7 +128,7 @@ export class IndexBackfiller {
125128
const processedCollectionGroups = new Set<string>();
126129
let documentsRemaining = maxDocumentsToProcess;
127130
let continueLoop = true;
128-
return PersistencePromise.loopUntil(
131+
return PersistencePromise.doWhile(
129132
() => continueLoop === true && documentsRemaining > 0,
130133
() => {
131134
return this.localStore.indexManager

packages/firestore/src/local/index_manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export interface IndexManager {
165165
target: Target
166166
): PersistencePromise<IndexOffset>;
167167

168+
/** Returns the minimum offset for the given collection group. */
168169
getMinOffsetFromCollectionGroup(
169170
transaction: PersistenceTransaction,
170171
collectionGroup: string

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { FieldPath, ResourcePath } from '../model/path';
5151
import { TargetIndexMatcher } from '../model/target_index_matcher';
5252
import { isArray, refValue } from '../model/values';
5353
import { Value as ProtoValue } from '../protos/firestore_proto_api';
54-
import { debugAssert } from '../util/assert';
54+
import {debugAssert, hardAssert} from '../util/assert';
5555
import { logDebug } from '../util/log';
5656
import { immediateSuccessor, primitiveComparator } from '../util/misc';
5757
import { ObjectMap } from '../util/obj_map';
@@ -199,8 +199,6 @@ export class IndexedDbIndexManager implements IndexManager {
199199
if (index.indexState) {
200200
const states = indexStateStore(transaction);
201201
return result.next(indexId => {
202-
if (index.indexState) {
203-
}
204202
states.put(
205203
toDbIndexState(
206204
indexId,
@@ -972,12 +970,15 @@ export class IndexedDbIndexManager implements IndexManager {
972970
transaction: PersistenceTransaction,
973971
collectionGroup: string
974972
): PersistencePromise<IndexOffset> {
975-
return this.getFieldIndexes(transaction, collectionGroup).next(
976-
this.getMinOffsetFromFieldIndexes
977-
);
973+
return this.getFieldIndexes(transaction, collectionGroup)
974+
.next(fieldIndexes => this.getMinOffsetFromFieldIndexes(fieldIndexes));
978975
}
979976

980977
getMinOffsetFromFieldIndexes(fieldIndexes: FieldIndex[]): IndexOffset {
978+
hardAssert(
979+
fieldIndexes.length !== 0,
980+
"Found empty index group when looking for least recent index offset.");
981+
981982
let minOffset: IndexOffset = fieldIndexes[0].indexState.offset;
982983
let maxBatchId: number = minOffset.largestBatchId;
983984
for (const fieldIndex of fieldIndexes) {
@@ -1003,8 +1004,8 @@ export class IndexedDbIndexManager implements IndexManager {
10031004
let offset: IndexOffset | undefined;
10041005
return PersistencePromise.forEach(
10051006
this.getSubTargets(target),
1006-
(target: Target) => {
1007-
return this.getFieldIndex(transaction, target).next(index => {
1007+
(subTarget: Target) => {
1008+
return this.getFieldIndex(transaction, subTarget).next(index => {
10081009
if (!index) {
10091010
offset = IndexOffset.min();
10101011
} else if (

packages/firestore/src/local/persistence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export interface Persistence {
253253
}
254254

255255
/**
256-
* Interface to control LRU and IndexBackfiller.
256+
* Interface to schedule periodic tasks within SDK.
257257
*/
258258
export interface Scheduler {
259259
readonly started: boolean;

packages/firestore/src/local/persistence_promise.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,21 @@ export class PersistencePromise<T> {
248248
* An alternative to recursive PersistencePromise calls, that avoids
249249
* potential memory problems from unbounded chains of promises.
250250
*
251-
* The `action` will be called first, then `condition`. If `condition`
252-
* return `true`, the process repeats, until `condition` returns false.
253-
* @param condition
254-
* @param action
251+
* The `action` will be called repeatedly while `condition` is true.
255252
*/
256-
static loopUntil(
253+
static doWhile(
257254
condition: () => boolean,
258255
action: () => PersistencePromise<void>
259256
): PersistencePromise<void> {
260257
return new PersistencePromise<void>((resolve, reject) => {
261-
const process = (): void => {
262-
action().next(() => {
263-
if (condition() === true) {
258+
const process = () => {
259+
if (condition() === true) {
260+
action().next(() => {
264261
process();
265-
} else {
266-
resolve();
267-
}
268-
}, reject);
262+
}, reject)
263+
} else {
264+
resolve();
265+
}
269266
};
270267
process();
271268
});

packages/firestore/test/unit/specs/spec_test_components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class MockMultiTabOfflineComponentProvider extends MultiTabOfflineCompone
160160
return null;
161161
}
162162

163-
createIndexBackfiller(
163+
createIndexBackfillerScheduler(
164164
cfg: ComponentConfiguration,
165165
localStore: LocalStore
166166
): Scheduler | null {

0 commit comments

Comments
 (0)