@@ -51,7 +51,7 @@ import { FieldPath, ResourcePath } from '../model/path';
51
51
import { TargetIndexMatcher } from '../model/target_index_matcher' ;
52
52
import { isArray , refValue } from '../model/values' ;
53
53
import { Value as ProtoValue } from '../protos/firestore_proto_api' ;
54
- import { debugAssert , hardAssert } from '../util/assert' ;
54
+ import { debugAssert , fail , hardAssert } from '../util/assert' ;
55
55
import { logDebug } from '../util/log' ;
56
56
import { immediateSuccessor , primitiveComparator } from '../util/misc' ;
57
57
import { ObjectMap } from '../util/obj_map' ;
@@ -970,53 +970,22 @@ export class IndexedDbIndexManager implements IndexManager {
970
970
transaction : PersistenceTransaction ,
971
971
collectionGroup : string
972
972
) : PersistencePromise < IndexOffset > {
973
- return this . getFieldIndexes ( transaction , collectionGroup )
974
- . next ( fieldIndexes => this . getMinOffsetFromFieldIndexes ( fieldIndexes ) ) ;
975
- }
976
-
977
- getMinOffsetFromFieldIndexes ( fieldIndexes : FieldIndex [ ] ) : IndexOffset {
978
- hardAssert (
979
- fieldIndexes . length !== 0 ,
980
- "Found empty index group when looking for least recent index offset." ) ;
981
-
982
- let minOffset : IndexOffset = fieldIndexes [ 0 ] . indexState . offset ;
983
- let maxBatchId : number = minOffset . largestBatchId ;
984
- for ( const fieldIndex of fieldIndexes ) {
985
- const newOffset : IndexOffset = fieldIndex . indexState . offset ;
986
- if ( indexOffsetComparator ( newOffset , minOffset ) < 0 ) {
987
- minOffset = newOffset ;
988
- }
989
- if ( maxBatchId < newOffset . largestBatchId ) {
990
- maxBatchId = newOffset . largestBatchId ;
991
- }
992
- }
993
- return new IndexOffset (
994
- minOffset . readTime ,
995
- minOffset . documentKey ,
996
- maxBatchId
973
+ return this . getFieldIndexes ( transaction , collectionGroup ) . next (
974
+ getMinOffsetFromFieldIndexes
997
975
) ;
998
976
}
999
977
1000
978
getMinOffset (
1001
979
transaction : PersistenceTransaction ,
1002
980
target : Target
1003
981
) : PersistencePromise < IndexOffset > {
1004
- let offset : IndexOffset | undefined ;
1005
- return PersistencePromise . forEach (
982
+ return PersistencePromise . mapArray (
1006
983
this . getSubTargets ( target ) ,
1007
- ( subTarget : Target ) => {
1008
- return this . getFieldIndex ( transaction , subTarget ) . next ( index => {
1009
- if ( ! index ) {
1010
- offset = IndexOffset . min ( ) ;
1011
- } else if (
1012
- ! offset ||
1013
- indexOffsetComparator ( index . indexState . offset , offset ) < 0
1014
- ) {
1015
- offset = index . indexState . offset ;
1016
- }
1017
- } ) ;
1018
- }
1019
- ) . next ( ( ) => offset ! ) ;
984
+ ( subTarget : Target ) =>
985
+ this . getFieldIndex ( transaction , subTarget ) . next ( index =>
986
+ ! index ? fail ( 'Target cannot be served from index' ) : index
987
+ )
988
+ ) . next ( getMinOffsetFromFieldIndexes ) ;
1020
989
}
1021
990
}
1022
991
@@ -1062,3 +1031,23 @@ function indexStateStore(
1062
1031
) : SimpleDbStore < DbIndexStateKey , DbIndexState > {
1063
1032
return getStore < DbIndexStateKey , DbIndexState > ( txn , DbIndexStateStore ) ;
1064
1033
}
1034
+
1035
+ function getMinOffsetFromFieldIndexes ( fieldIndexes : FieldIndex [ ] ) : IndexOffset {
1036
+ hardAssert (
1037
+ fieldIndexes . length !== 0 ,
1038
+ 'Found empty index group when looking for least recent index offset.'
1039
+ ) ;
1040
+
1041
+ let minOffset : IndexOffset = fieldIndexes [ 0 ] . indexState . offset ;
1042
+ let maxBatchId : number = minOffset . largestBatchId ;
1043
+ for ( let i = 1 ; i < fieldIndexes . length ; i ++ ) {
1044
+ const newOffset : IndexOffset = fieldIndexes [ i ] . indexState . offset ;
1045
+ if ( indexOffsetComparator ( newOffset , minOffset ) < 0 ) {
1046
+ minOffset = newOffset ;
1047
+ }
1048
+ if ( maxBatchId < newOffset . largestBatchId ) {
1049
+ maxBatchId = newOffset . largestBatchId ;
1050
+ }
1051
+ }
1052
+ return new IndexOffset ( minOffset . readTime , minOffset . documentKey , maxBatchId ) ;
1053
+ }
0 commit comments