github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/index/iterator_test.go (about) 1 // Copyright (c) 2021 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 index 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/m3ninx/doc" 27 "github.com/m3db/m3/src/m3ninx/index/segment/fst/encoding" 28 "github.com/m3db/m3/src/m3ninx/postings" 29 xtest "github.com/m3db/m3/src/x/test" 30 31 "github.com/golang/mock/gomock" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestDocIterator(t *testing.T) { 36 mockCtrl := xtest.NewController(t) 37 defer mockCtrl.Finish() 38 39 docs := []doc.Metadata{ 40 { 41 ID: []byte("doc-id-1"), 42 Fields: []doc.Field{ 43 { 44 Name: []byte("apple"), 45 Value: []byte("red"), 46 }, 47 }, 48 }, 49 { 50 ID: []byte("doc-id-2"), 51 Fields: []doc.Field{ 52 { 53 Name: []byte("banana"), 54 Value: []byte("yellow"), 55 }, 56 }, 57 }, 58 } 59 60 encodedDocsWithIds := make([]docsWithIDs, 0, len(docs)) 61 for i, d := range docs { 62 encodedDocsWithIds = append(encodedDocsWithIds, docsWithIDs{ 63 id: postings.ID(i), 64 doc: doc.NewDocumentFromEncoded( 65 doc.Encoded{Bytes: docToBytes(d)}), 66 }) 67 } 68 69 retriever := NewMockDocRetriever(mockCtrl) 70 gomock.InOrder( 71 retriever.EXPECT().Doc(encodedDocsWithIds[0].id).Return(encodedDocsWithIds[0].doc, nil), 72 retriever.EXPECT().Doc(encodedDocsWithIds[1].id).Return(encodedDocsWithIds[1].doc, nil), 73 ) 74 75 postingsIter := postings.NewMockIterator(mockCtrl) 76 gomock.InOrder( 77 postingsIter.EXPECT().Next().Return(true), 78 postingsIter.EXPECT().Current().Return(encodedDocsWithIds[0].id), 79 postingsIter.EXPECT().Next().Return(true), 80 postingsIter.EXPECT().Current().Return(encodedDocsWithIds[1].id), 81 postingsIter.EXPECT().Next().Return(false), 82 postingsIter.EXPECT().Close().Return(nil), 83 ) 84 85 it := NewIterator(retriever, postingsIter) 86 87 require.True(t, it.Next()) 88 require.Equal(t, encodedDocsWithIds[0].doc, it.Current()) 89 require.True(t, it.Next()) 90 require.Equal(t, encodedDocsWithIds[1].doc, it.Current()) 91 require.False(t, it.Next()) 92 require.NoError(t, it.Err()) 93 94 require.NoError(t, it.Close()) 95 } 96 97 type docsWithIDs struct { 98 id postings.ID 99 doc doc.Document 100 } 101 102 func docToBytes(d doc.Metadata) []byte { 103 enc := encoding.NewEncoder(1024) 104 n := enc.PutBytes(d.ID) 105 n += enc.PutUvarint(uint64(len(d.Fields))) 106 for _, f := range d.Fields { // nolint:gocritic 107 n += enc.PutBytes(f.Name) 108 n += enc.PutBytes(f.Value) 109 } 110 111 return enc.Bytes() 112 }