github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/utils.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 fst 22 23 import ( 24 "bytes" 25 "testing" 26 27 sgmt "github.com/m3db/m3/src/m3ninx/index/segment" 28 "github.com/m3db/m3/src/x/mmap" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 // ToTestSegment returns a FST segment equivalent to the provide mutable segment. 34 func ToTestSegment(t *testing.T, s sgmt.MutableSegment, opts Options) sgmt.Segment { 35 return newFSTSegment(t, s, opts) 36 } 37 38 func newFSTSegmentWithVersion( 39 t *testing.T, 40 s sgmt.MutableSegment, 41 opts Options, 42 writerVersion, readerVersion Version, 43 ) sgmt.Segment { 44 s.Seal() 45 w, err := newWriterWithVersion(WriterOptions{}, &writerVersion) 46 require.NoError(t, err) 47 require.NoError(t, w.Reset(s)) 48 49 var ( 50 docsDataBuffer bytes.Buffer 51 docsIndexBuffer bytes.Buffer 52 postingsBuffer bytes.Buffer 53 fstTermsBuffer bytes.Buffer 54 fstFieldsBuffer bytes.Buffer 55 ) 56 57 require.NoError(t, w.WriteDocumentsData(&docsDataBuffer)) 58 require.NoError(t, w.WriteDocumentsIndex(&docsIndexBuffer)) 59 require.NoError(t, w.WritePostingsOffsets(&postingsBuffer)) 60 require.NoError(t, w.WriteFSTTerms(&fstTermsBuffer)) 61 require.NoError(t, w.WriteFSTFields(&fstFieldsBuffer)) 62 63 data := SegmentData{ 64 Version: readerVersion, 65 Metadata: w.Metadata(), 66 DocsData: mmap.Descriptor{Bytes: docsDataBuffer.Bytes()}, 67 DocsIdxData: mmap.Descriptor{Bytes: docsIndexBuffer.Bytes()}, 68 PostingsData: mmap.Descriptor{Bytes: postingsBuffer.Bytes()}, 69 FSTTermsData: mmap.Descriptor{Bytes: fstTermsBuffer.Bytes()}, 70 FSTFieldsData: mmap.Descriptor{Bytes: fstFieldsBuffer.Bytes()}, 71 } 72 reader, err := NewSegment(data, opts) 73 require.NoError(t, err) 74 75 return reader 76 } 77 78 func newFSTSegment(t *testing.T, s sgmt.MutableSegment, opts Options) sgmt.Segment { 79 return newFSTSegmentWithVersion(t, s, opts, CurrentVersion, CurrentVersion) 80 }