github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/encoding/docs/slice.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 Softwarw. 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 docs 22 23 import ( 24 "errors" 25 26 "github.com/m3db/m3/src/m3ninx/doc" 27 "github.com/m3db/m3/src/m3ninx/index" 28 "github.com/m3db/m3/src/m3ninx/postings" 29 ) 30 31 var ( 32 errDocNotFound = errors.New("doc not found") 33 ) 34 35 var _ Reader = (*SliceReader)(nil) 36 var _ index.MetadataRetriever = (*SliceReader)(nil) 37 38 // SliceReader is a docs slice reader for use with documents 39 // stored in memory. 40 type SliceReader struct { 41 docs []doc.Metadata 42 } 43 44 // NewSliceReader returns a new docs slice reader. 45 func NewSliceReader(docs []doc.Metadata) *SliceReader { 46 return &SliceReader{docs: docs} 47 } 48 49 // Len returns the number of documents in the slice reader. 50 func (r *SliceReader) Len() int { 51 return len(r.docs) 52 } 53 54 // Read returns a document from the docs slice reader. 55 func (r *SliceReader) Read(id postings.ID) (doc.Metadata, error) { 56 idx := int(id) 57 if idx < 0 || idx >= len(r.docs) { 58 return doc.Metadata{}, errDocNotFound 59 } 60 61 return r.docs[idx], nil 62 } 63 64 // Metadata implements MetadataRetriever and reads the document with postings ID. 65 func (r *SliceReader) Metadata(id postings.ID) (doc.Metadata, error) { 66 return r.Read(id) 67 } 68 69 // Iter returns a docs iterator. 70 func (r *SliceReader) Iter() index.IDDocIterator { 71 postingsIter := postings.NewRangeIterator(0, postings.ID(r.Len())) 72 return index.NewIDDocIterator(r, postingsIter) 73 }