github.com/m3db/m3@v1.5.0/src/dbnode/x/xio/types.go (about) 1 // Copyright (c) 2016 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 xio 22 23 import ( 24 "time" 25 26 "github.com/m3db/m3/src/dbnode/ts" 27 "github.com/m3db/m3/src/x/pool" 28 xresource "github.com/m3db/m3/src/x/resource" 29 xtime "github.com/m3db/m3/src/x/time" 30 ) 31 32 // BlockReader represents a block reader backed by a 33 // SegmentReader with start time and block size. 34 type BlockReader struct { 35 SegmentReader 36 Start xtime.UnixNano 37 BlockSize time.Duration 38 } 39 40 // EmptyBlockReader represents the default block reader. 41 var EmptyBlockReader = BlockReader{} 42 43 // SegmentReader implements the io reader interface backed by a segment. 44 type SegmentReader interface { 45 Reader64 46 xresource.Finalizer 47 48 // Segment gets the segment read by this reader. 49 Segment() (ts.Segment, error) 50 51 // Reset resets the reader to read a new segment. 52 Reset(segment ts.Segment) 53 54 // Clone returns a clone of a copy of the underlying data reset, 55 // with an optional byte pool to reduce allocations. 56 Clone(pool pool.CheckedBytesPool) (SegmentReader, error) 57 } 58 59 // SegmentReaderPool provides a pool for segment readers. 60 type SegmentReaderPool interface { 61 // Init will initialize the pool. 62 Init() 63 64 // Get provides a segment reader from the pool. 65 Get() SegmentReader 66 67 // Put returns a segment reader to the pool. 68 Put(sr SegmentReader) 69 } 70 71 // ReaderSliceOfSlicesIterator is an iterator that iterates 72 // through an array of reader arrays. 73 type ReaderSliceOfSlicesIterator interface { 74 // Next moves to the next item. 75 Next() bool 76 77 // CurrentReaders returns the current length, start time, and block size. 78 CurrentReaders() (length int, start xtime.UnixNano, blockSize time.Duration) 79 80 // CurrentReaderAt returns the current reader in the slice 81 // of readers at an index. 82 CurrentReaderAt(idx int) BlockReader 83 84 // Close closes the iterator. 85 Close() 86 87 // Size gives the size of bytes in this iterator. 88 Size() (int, error) 89 90 // RewindToIndex returns the iterator to a specific index. 91 // This operation is invalid if any of the block readers have been read. 92 RewindToIndex(idx int) 93 94 // Index returns the iterator's current index. 95 Index() int 96 } 97 98 // ReaderSliceOfSlicesFromBlockReadersIterator is an iterator 99 // that iterates through an array of reader arrays. 100 type ReaderSliceOfSlicesFromBlockReadersIterator interface { 101 ReaderSliceOfSlicesIterator 102 103 // Reset resets the iterator with a new array of block readers arrays. 104 Reset(blocks [][]BlockReader) 105 } 106 107 // Reader64 is a reader for reading 64 bit words. 108 type Reader64 interface { 109 110 // Read64 reads and returns a 64 bit word plus a number of bytes (up to 8) actually read. 111 Read64() (word uint64, n byte, err error) 112 113 // Read64 peeks and returns the next 64 bit word plus a number of bytes (up to 8) available. 114 Peek64() (word uint64, n byte, err error) 115 }