Skip to content

Commit a6ce575

Browse files
Review
1 parent c2ab3f5 commit a6ce575

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

packages/firestore/src/core/target.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {
2121
FieldIndex,
2222
fieldIndexGetArraySegment,
2323
fieldIndexGetDirectionalSegments,
24-
IndexKind,
25-
IndexSegment
24+
IndexKind
2625
} from '../model/field_index';
2726
import { FieldPath, ResourcePath } from '../model/path';
2827
import {
@@ -308,8 +307,12 @@ export function targetGetLowerBound(
308307
for (const segment of fieldIndexGetDirectionalSegments(fieldIndex)) {
309308
const segmentBound =
310309
segment.kind === IndexKind.ASCENDING
311-
? targetGetAscendingBound(target, segment, target.startAt)
312-
: targetGetDescendingBound(target, segment, target.startAt);
310+
? targetGetLowerBoundForField(target, segment.fieldPath, target.startAt)
311+
: targetGetUpperBoundForField(
312+
target,
313+
segment.fieldPath,
314+
target.startAt
315+
);
313316

314317
if (!segmentBound.value) {
315318
// No lower bound exists
@@ -338,8 +341,8 @@ export function targetGetUpperBound(
338341
for (const segment of fieldIndexGetDirectionalSegments(fieldIndex)) {
339342
const segmentBound =
340343
segment.kind === IndexKind.ASCENDING
341-
? targetGetDescendingBound(target, segment, target.endAt)
342-
: targetGetAscendingBound(target, segment, target.endAt);
344+
? targetGetUpperBoundForField(target, segment.fieldPath, target.endAt)
345+
: targetGetLowerBoundForField(target, segment.fieldPath, target.endAt);
343346

344347
if (!segmentBound.value) {
345348
// No upper bound exists
@@ -352,19 +355,20 @@ export function targetGetUpperBound(
352355
return new Bound(values, inclusive);
353356
}
354357

355-
function targetGetAscendingBound(
358+
/**
359+
* Returns the value to use as the lower bound for ascending index segment at
360+
* the provided `fieldPath` (or the upper bound for an descending segment).
361+
*/
362+
function targetGetLowerBoundForField(
356363
target: Target,
357-
segment: IndexSegment,
364+
fieldPath: FieldPath,
358365
bound: Bound | null
359366
): { value: ProtoValue | undefined; inclusive: boolean } {
360367
let value: ProtoValue | undefined = undefined;
361368
let inclusive = true;
362369

363370
// Process all filters to find a value for the current field segment
364-
for (const fieldFilter of targetGetFieldFiltersForPath(
365-
target,
366-
segment.fieldPath
367-
)) {
371+
for (const fieldFilter of targetGetFieldFiltersForPath(target, fieldPath)) {
368372
let filterValue: ProtoValue | undefined = undefined;
369373
let filterInclusive = true;
370374

@@ -401,7 +405,7 @@ function targetGetAscendingBound(
401405
if (bound !== null) {
402406
for (let i = 0; i < target.orderBy.length; ++i) {
403407
const orderBy = target.orderBy[i];
404-
if (orderBy.field.isEqual(segment.fieldPath)) {
408+
if (orderBy.field.isEqual(fieldPath)) {
405409
const cursorValue = bound.position[i];
406410
if (valuesMax(value, cursorValue) === cursorValue) {
407411
value = cursorValue;
@@ -415,19 +419,20 @@ function targetGetAscendingBound(
415419
return { value, inclusive };
416420
}
417421

418-
function targetGetDescendingBound(
422+
/**
423+
* Returns the value to use as the upper bound for ascending index segment at
424+
* the provided `fieldPath` (or the lower bound for an descending segment).
425+
*/
426+
function targetGetUpperBoundForField(
419427
target: Target,
420-
segment: IndexSegment,
428+
fieldPath: FieldPath,
421429
bound: Bound | null
422430
): { value: ProtoValue | undefined; inclusive: boolean } {
423431
let value: ProtoValue | undefined = undefined;
424432
let inclusive = true;
425433

426434
// Process all filters to find a value for the current field segment
427-
for (const fieldFilter of targetGetFieldFiltersForPath(
428-
target,
429-
segment.fieldPath
430-
)) {
435+
for (const fieldFilter of targetGetFieldFiltersForPath(target, fieldPath)) {
431436
let filterValue: ProtoValue | undefined = undefined;
432437
let filterInclusive = true;
433438

@@ -465,7 +470,7 @@ function targetGetDescendingBound(
465470
if (bound !== null) {
466471
for (let i = 0; i < target.orderBy.length; ++i) {
467472
const orderBy = target.orderBy[i];
468-
if (orderBy.field.isEqual(segment.fieldPath)) {
473+
if (orderBy.field.isEqual(fieldPath)) {
469474
const cursorValue = bound.position[i];
470475
if (valuesMin(value, cursorValue) === cursorValue) {
471476
value = cursorValue;

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ export class IndexedDbIndexManager implements IndexManager {
295295
.loadFirst(indexRange, target.limit)
296296
.next(entries => {
297297
entries.forEach(entry => {
298-
const documentKey = new DocumentKey(
299-
decodeResourcePath(entry.documentKey)
298+
const documentKey = DocumentKey.fromSegments(
299+
entry.documentKey
300300
);
301301
if (!existingKeys.has(documentKey)) {
302302
existingKeys = existingKeys.add(documentKey);
@@ -737,7 +737,7 @@ export class IndexedDbIndexManager implements IndexManager {
737737
arrayValue: indexEntry.arrayValue,
738738
directionalValue: indexEntry.directionalValue,
739739
orderedDocumentKey: this.encodeDirectionalKey(fieldIndex, document.key),
740-
documentKey: encodeResourcePath(document.key.path)
740+
documentKey: document.key.path.toArray()
741741
});
742742
}
743743

@@ -754,7 +754,7 @@ export class IndexedDbIndexManager implements IndexManager {
754754
indexEntry.arrayValue,
755755
indexEntry.directionalValue,
756756
this.encodeDirectionalKey(fieldIndex, document.key),
757-
encodeResourcePath(document.key.path)
757+
document.key.path.toArray()
758758
]);
759759
}
760760

@@ -935,16 +935,16 @@ export class IndexedDbIndexManager implements IndexManager {
935935
this.uid,
936936
bounds[i].arrayValue,
937937
bounds[i].directionalValue,
938-
new Uint8Array(),
939-
''
938+
EMPTY_VALUE,
939+
[]
940940
] as DbIndexEntryKey,
941941
[
942942
bounds[i + 1].indexId,
943943
this.uid,
944944
bounds[i + 1].arrayValue,
945945
bounds[i + 1].directionalValue,
946-
new Uint8Array(),
947-
''
946+
EMPTY_VALUE,
947+
[]
948948
] as DbIndexEntryKey
949949
)
950950
);

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ export interface DbIndexEntry {
519519
* encoder to match the key order of the index.
520520
*/
521521
orderedDocumentKey: Uint8Array;
522-
/** The document key this entry points to. */
523-
documentKey: EncodedResourcePath;
522+
/** The segments of the document key this entry points to. */
523+
documentKey: string[];
524524
}
525525

526526
/**

packages/firestore/src/local/indexeddb_sentinels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export type DbIndexEntryKey = [
293293
Uint8Array,
294294
Uint8Array,
295295
Uint8Array,
296-
string
296+
string[]
297297
];
298298

299299
/** Name of the IndexedDb object store. */
@@ -313,7 +313,7 @@ export const DbIndexEntryDocumentKeyIndex = 'documentKeyIndex';
313313
export const DbIndexEntryDocumentKeyIndexPath = [
314314
'indexId',
315315
'uid',
316-
'documentKey'
316+
'orderedDocumentKey'
317317
];
318318

319319
export type DbDocumentOverlayKey = [

packages/firestore/src/model/field_index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function fieldIndexGetDirectionalSegments(
8080
}
8181

8282
/**
83-
* Returns the order of the document key component.
83+
* Returns the order of the document key component for the given index.
8484
*
8585
* PORTING NOTE: This is only used in the Web IndexedDb implementation.
8686
*/

0 commit comments

Comments
 (0)