github.com/m3db/m3@v1.5.0/src/m3ninx/index/metadata_iterator.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 index 22 23 import ( 24 "errors" 25 26 "github.com/m3db/m3/src/m3ninx/doc" 27 "github.com/m3db/m3/src/m3ninx/postings" 28 ) 29 30 var errIteratorClosed = errors.New("iterator has been closed") 31 32 type idDocIterator struct { 33 retriever MetadataRetriever 34 postingsIter postings.Iterator 35 36 currDoc doc.Metadata 37 currID postings.ID 38 closed bool 39 err error 40 } 41 42 // NewIDDocIterator returns a new NewIDDocIterator. 43 func NewIDDocIterator(r MetadataRetriever, pi postings.Iterator) IDDocIterator { 44 return &idDocIterator{ 45 retriever: r, 46 postingsIter: pi, 47 } 48 } 49 50 func (it *idDocIterator) Next() bool { 51 if it.closed || it.err != nil || !it.postingsIter.Next() { 52 return false 53 } 54 id := it.postingsIter.Current() 55 it.currID = id 56 57 d, err := it.retriever.Metadata(id) 58 if err != nil { 59 it.err = err 60 return false 61 } 62 it.currDoc = d 63 return true 64 } 65 66 func (it *idDocIterator) Current() doc.Metadata { 67 return it.currDoc 68 } 69 70 func (it *idDocIterator) PostingsID() postings.ID { 71 return it.currID 72 } 73 74 func (it *idDocIterator) Err() error { 75 return it.err 76 } 77 78 func (it *idDocIterator) Close() error { 79 if it.closed { 80 return errIteratorClosed 81 } 82 it.closed = true 83 it.currDoc = doc.Metadata{} 84 it.currID = postings.ID(0) 85 err := it.postingsIter.Close() 86 return err 87 }