5151create (Db , Selector0 , Opts , Kind ) ->
5252 Selector = mango_selector :normalize (Selector0 ),
5353 {UsableIndexes , Trace } = mango_idx :get_usable_indexes (Db , Selector , Opts , Kind ),
54+ UseIndex = use_index (Opts ),
5455 case maybe_filter_indexes_by_ddoc (UsableIndexes , Opts ) of
5556 [] ->
5657 % use_index doesn't match a valid index - determine how
5758 % this shall be handled by the further settings
58- case allow_fallback (Opts ) of
59+ case allow_fallback (Opts ) orelse ( UseIndex == [] andalso length ( UsableIndexes ) > 1 ) of
5960 true ->
6061 % fall back to a valid index
6162 create_cursor (Db , {UsableIndexes , Trace }, Selector , Opts );
6263 false ->
63- % return an error
64+ % no usable index but all_docs, no fallback allowed: return an error
6465 Details =
65- case use_index ( Opts ) of
66+ case UseIndex of
6667 [] -> [];
6768 [DesignId ] -> [ddoc_name (DesignId )];
6869 [DesignId , ViewName ] -> [ddoc_name (DesignId ), ViewName ]
@@ -598,8 +599,8 @@ create_test_() ->
598599 ? TDEF_FE (t_create_regular , 10 ),
599600 ? TDEF_FE (t_create_user_specified_index , 10 ),
600601 ? TDEF_FE (t_create_invalid_user_specified_index , 10 ),
601- ? TDEF_FE (t_create_invalid_user_specified_index_no_fallback_1 , 10 ),
602- ? TDEF_FE (t_create_invalid_user_specified_index_no_fallback_2 , 10 )
602+ ? TDEF_FE (t_create_invalid_user_specified_index_no_fallback , 10 ),
603+ ? TDEF_FE (t_create_no_suitable_index_no_fallback , 10 )
603604 ]
604605 }.
605606
@@ -690,7 +691,7 @@ t_create_invalid_user_specified_index(_) ->
690691 ),
691692 ? assertEqual (view_cursor , create (db , selector , Options , target )).
692693
693- t_create_invalid_user_specified_index_no_fallback_1 (_ ) ->
694+ t_create_invalid_user_specified_index_no_fallback (_ ) ->
694695 IndexSpecial = # idx {type = <<" special" >>, def = all_docs },
695696 IndexView1 = # idx {type = <<" json" >>, ddoc = <<" _design/view_idx1" >>},
696697 IndexView2 = # idx {type = <<" json" >>, ddoc = <<" _design/view_idx2" >>},
@@ -721,21 +722,17 @@ t_create_invalid_user_specified_index_no_fallback_1(_) ->
721722 Exception = {mango_error , mango_cursor , {invalid_index , UseIndex }},
722723 ? assertThrow (Exception , create (db , selector , Options , target )).
723724
724- t_create_invalid_user_specified_index_no_fallback_2 (_ ) ->
725+ t_create_no_suitable_index_no_fallback (_ ) ->
725726 IndexSpecial = # idx {type = <<" special" >>, def = all_docs },
726- IndexView1 = # idx {type = <<" json" >>, ddoc = <<" _design/view_idx1" >>},
727- IndexView2 = # idx {type = <<" json" >>, ddoc = <<" _design/view_idx2" >>},
728- IndexView3 = # idx {type = <<" json" >>, ddoc = <<" _design/view_idx3" >>},
729- UsableIndexes = [IndexSpecial , IndexView1 , IndexView2 , IndexView3 ],
730- IndexesOfType = [IndexView1 , IndexView2 , IndexView3 ],
727+ UsableIndexes = [IndexSpecial ],
728+ IndexesOfType = [],
731729 Trace1 = #{},
732730 Trace2 =
733731 #{
734732 filtered_indexes => sets :from_list (UsableIndexes ),
735733 indexes_of_type => sets :from_list (IndexesOfType )
736734 },
737- UseIndex = [],
738- Options = [{use_index , UseIndex }, {allow_fallback , false }],
735+ Options = [{use_index , []}, {allow_fallback , false }],
739736 meck :expect (mango_selector , normalize , [selector ], meck :val (normalized_selector )),
740737 meck :expect (
741738 mango_idx ,
@@ -749,7 +746,7 @@ t_create_invalid_user_specified_index_no_fallback_2(_) ->
749746 [db , {IndexesOfType , Trace2 }, normalized_selector , Options ],
750747 meck :val (view_cursor )
751748 ),
752- Exception = {mango_error , mango_cursor , {invalid_index , UseIndex }},
749+ Exception = {mango_error , mango_cursor , {invalid_index , [] }},
753750 ? assertThrow (Exception , create (db , selector , Options , target )).
754751
755752enhance_candidates_test () ->
0 commit comments