github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/searcher/conjunction_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 TestConjunctionSearcher(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 require.NoError(t, secondPL1.Insert(postings.ID(50))) 54 secondPL2 := roaring.NewPostingsList() 55 require.NoError(t, secondPL2.Insert(postings.ID(64))) 56 require.NoError(t, secondPL2.Insert(postings.ID(72))) 57 secondSearcher := search.NewMockSearcher(mockCtrl) 58 59 // Third searcher. 60 thirdPL1 := roaring.NewPostingsList() 61 require.NoError(t, thirdPL1.Insert(postings.ID(42))) 62 require.NoError(t, thirdPL1.Insert(postings.ID(53))) 63 thirdPL2 := roaring.NewPostingsList() 64 require.NoError(t, thirdPL2.Insert(postings.ID(64))) 65 require.NoError(t, thirdPL2.Insert(postings.ID(89))) 66 thirdSearcher := search.NewMockSearcher(mockCtrl) 67 68 gomock.InOrder( 69 // Get the postings lists for the first Reader. 70 firstSearcher.EXPECT().Search(firstReader).Return(firstPL1, nil), 71 secondSearcher.EXPECT().Search(firstReader).Return(secondPL1, nil), 72 thirdSearcher.EXPECT().Search(firstReader).Return(thirdPL1, nil), 73 74 // Get the postings lists for the second Reader. 75 firstSearcher.EXPECT().Search(secondReader).Return(firstPL2, nil), 76 secondSearcher.EXPECT().Search(secondReader).Return(secondPL2, nil), 77 thirdSearcher.EXPECT().Search(secondReader).Return(thirdPL2, nil), 78 ) 79 80 var ( 81 searchers = []search.Searcher{firstSearcher, secondSearcher} 82 negations = []search.Searcher{thirdSearcher} 83 ) 84 85 s, err := NewConjunctionSearcher(searchers, negations) 86 require.NoError(t, err) 87 88 // Test the postings list from the first Reader. 89 var expected postings.List = firstPL1 90 expected, err = expected.Intersect(secondPL1) 91 require.NoError(t, err) 92 expected, err = expected.Difference(thirdPL1) 93 require.NoError(t, err) 94 95 pl, err := s.Search(firstReader) 96 require.NoError(t, err) 97 require.True(t, pl.Equal(expected)) 98 99 // Test the postings list from the second Reader. 100 expected = firstPL2 101 expected, err = expected.Intersect(secondPL2) 102 require.NoError(t, err) 103 expected, err = expected.Difference(thirdPL2) 104 require.NoError(t, err) 105 106 pl, err = s.Search(secondReader) 107 require.NoError(t, err) 108 require.True(t, pl.Equal(expected)) 109 } 110 111 func TestConjunctionSearcherError(t *testing.T) { 112 tests := []struct { 113 name string 114 searchers search.Searchers 115 negations search.Searchers 116 }{ 117 { 118 name: "empty list of searchers", 119 }, 120 } 121 122 for _, test := range tests { 123 t.Run(test.name, func(t *testing.T) { 124 _, err := NewConjunctionSearcher(test.searchers, test.negations) 125 require.Error(t, err) 126 }) 127 } 128 }