github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/searcher/disjunction_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package searcher 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/m3ninx/index" 27 "github.com/m3db/m3/src/m3ninx/postings" 28 "github.com/m3db/m3/src/m3ninx/postings/roaring" 29 "github.com/m3db/m3/src/m3ninx/search" 30 31 "github.com/golang/mock/gomock" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestDisjunctionSearcher(t *testing.T) { 36 mockCtrl := gomock.NewController(t) 37 defer mockCtrl.Finish() 38 39 firstReader := index.NewMockReader(mockCtrl) 40 secondReader := index.NewMockReader(mockCtrl) 41 42 // First searcher. 43 firstPL1 := roaring.NewPostingsList() 44 require.NoError(t, firstPL1.Insert(postings.ID(42))) 45 require.NoError(t, firstPL1.Insert(postings.ID(50))) 46 firstPL2 := roaring.NewPostingsList() 47 require.NoError(t, firstPL2.Insert(postings.ID(64))) 48 firstSearcher := search.NewMockSearcher(mockCtrl) 49 50 // Second searcher. 51 secondPL1 := roaring.NewPostingsList() 52 require.NoError(t, secondPL1.Insert(postings.ID(53))) 53 secondPL2 := roaring.NewPostingsList() 54 require.NoError(t, secondPL2.Insert(postings.ID(64))) 55 require.NoError(t, secondPL2.Insert(postings.ID(72))) 56 secondSearcher := search.NewMockSearcher(mockCtrl) 57 58 // Third searcher. 59 thirdPL1 := roaring.NewPostingsList() 60 require.NoError(t, thirdPL1.Insert(postings.ID(53))) 61 thirdPL2 := roaring.NewPostingsList() 62 require.NoError(t, thirdPL2.Insert(postings.ID(72))) 63 require.NoError(t, thirdPL2.Insert(postings.ID(89))) 64 thirdSearcher := search.NewMockSearcher(mockCtrl) 65 66 gomock.InOrder( 67 // Get the postings lists for the first Reader. 68 firstSearcher.EXPECT().Search(firstReader).Return(firstPL1, nil), 69 secondSearcher.EXPECT().Search(firstReader).Return(secondPL1, nil), 70 thirdSearcher.EXPECT().Search(firstReader).Return(thirdPL1, nil), 71 72 // Get the postings lists for the second Reader. 73 firstSearcher.EXPECT().Search(secondReader).Return(firstPL2, nil), 74 secondSearcher.EXPECT().Search(secondReader).Return(secondPL2, nil), 75 thirdSearcher.EXPECT().Search(secondReader).Return(thirdPL2, nil), 76 ) 77 78 searchers := []search.Searcher{firstSearcher, secondSearcher, thirdSearcher} 79 80 s, err := NewDisjunctionSearcher(searchers) 81 require.NoError(t, err) 82 83 // Test the postings list from the first Reader. 84 expected := firstPL1.CloneAsMutable() 85 require.NoError(t, expected.UnionInPlace(secondPL1)) 86 require.NoError(t, expected.UnionInPlace(thirdPL1)) 87 pl, err := s.Search(firstReader) 88 require.NoError(t, err) 89 require.True(t, pl.Equal(expected)) 90 91 // Test the postings list from the second Reader. 92 expected = firstPL2.CloneAsMutable() 93 require.NoError(t, expected.UnionInPlace(secondPL2)) 94 require.NoError(t, expected.UnionInPlace(thirdPL2)) 95 pl, err = s.Search(secondReader) 96 require.NoError(t, err) 97 require.True(t, pl.Equal(expected)) 98 } 99 100 func TestDisjunctionSearcherError(t *testing.T) { 101 tests := []struct { 102 name string 103 numReaders int 104 searchers search.Searchers 105 }{ 106 { 107 name: "empty list of searchers", 108 numReaders: 3, 109 }, 110 } 111 112 for _, test := range tests { 113 t.Run(test.name, func(t *testing.T) { 114 _, err := NewDisjunctionSearcher(test.searchers) 115 require.Error(t, err) 116 }) 117 } 118 }