Skip to content

Commit cf1880e

Browse files
Simplify
1 parent e55c5e7 commit cf1880e

File tree

1 file changed

+35
-65
lines changed

1 file changed

+35
-65
lines changed

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -847,82 +847,52 @@ export class IndexedDbIndexManager implements IndexManager {
847847
return indexRanges;
848848
}
849849

850-
const result: IDBKeyRange[] = [];
851-
for (const indexRange of indexRanges) {
852-
// Remove notIn values that are not applicable to this index range
853-
const lowerBound = new Uint8Array(indexRange.lower[3]);
854-
const upperBound = new Uint8Array(indexRange.upper[3]);
855-
const filteredRanges = notInValues.filter(
856-
v =>
857-
compareByteArrays(v, lowerBound) >= 0 &&
858-
compareByteArrays(v, upperBound) <= 0
859-
);
860-
861-
if (filteredRanges.length === 0) {
862-
result.push(indexRange);
863-
} else {
864-
// Use the existing bounds and interleave the notIn values. This means
865-
// that we would split an existing range into multiple ranges that exclude
866-
// the values from any notIn filter.
867-
result.push(...this.interleaveRanges(indexRange, filteredRanges));
868-
}
869-
}
870-
return result;
871-
}
872-
873-
/**
874-
* Splits up the range defined by `indexRange` and removes any values
875-
* contained in `barrier`. As an example, if the original range is [1,4] and
876-
* the barrier is [2,3], then this method would return [1,2), (2,3), (3,4].
877-
*/
878-
private interleaveRanges(
879-
indexRange: IDBKeyRange,
880-
barriers: Uint8Array[]
881-
): IDBKeyRange[] {
882850
// The values need to be sorted so that we can return a sorted set of
883851
// non-overlapping ranges.
884-
barriers.sort((l, r) => compareByteArrays(l, r));
852+
notInValues.sort((l, r) => compareByteArrays(l, r));
885853

886854
const ranges: IDBKeyRange[] = [];
887855

888-
// The first index range starts with the lower bound and ends before the
889-
// first barrier.
890-
ranges.push(
891-
IDBKeyRange.bound(
892-
indexRange.lower,
893-
this.generateNotInBound(indexRange.lower, barriers[0]),
894-
indexRange.lowerOpen,
895-
/* upperOpen= */ true
896-
)
897-
);
856+
// Use the existing bounds and interleave the notIn values. This means
857+
// that we would split an existing range into multiple ranges that exclude
858+
// the values from any notIn filter.
859+
for (const indexRange of indexRanges) {
860+
const lowerBound = new Uint8Array(indexRange.lower[3]);
861+
const upperBound = new Uint8Array(indexRange.upper[3]);
862+
let lastLower = indexRange.lower;
863+
let lastOpen = indexRange.lowerOpen;
864+
865+
for (const notInValue of notInValues) {
866+
if (
867+
compareByteArrays(notInValue, lowerBound) >= 0 &&
868+
compareByteArrays(notInValue, upperBound) <= 0
869+
) {
870+
ranges.push(
871+
IDBKeyRange.bound(
872+
lastLower,
873+
this.generateNotInBound(indexRange.lower, notInValue),
874+
lastOpen,
875+
/* upperOpen= */ true
876+
)
877+
);
878+
879+
lastLower = this.generateNotInBound(
880+
indexRange.lower,
881+
successor(notInValue)
882+
);
883+
lastOpen = true;
884+
}
885+
}
898886

899-
for (let i = 1; i < barriers.length - 1; ++i) {
900-
// Each index range that we need to scan starts after the last barrier
901-
// and ends before the next.
902887
ranges.push(
903888
IDBKeyRange.bound(
904-
this.generateNotInBound(indexRange.lower, barriers[i - 1]),
905-
this.generateNotInBound(indexRange.lower, successor(barriers[i])),
906-
/* lowerOpen= */ false,
907-
/* upperOpen= */ true
889+
lastLower,
890+
indexRange.upper,
891+
lastOpen,
892+
indexRange.upperOpen
908893
)
909894
);
910895
}
911-
912-
// The last index range starts after the last barrier and ends at the upper
913-
// bound
914-
ranges.push(
915-
IDBKeyRange.bound(
916-
this.generateNotInBound(
917-
indexRange.lower,
918-
successor(barriers[barriers.length - 1])
919-
),
920-
indexRange.upper,
921-
/* lowerOpen= */ false,
922-
indexRange.upperOpen
923-
)
924-
);
925-
926896
return ranges;
927897
}
928898

0 commit comments

Comments
 (0)