github.com/m3db/m3@v1.5.0/src/dbnode/persist/fs/msgpack/summary_token.go (about) 1 // Copyright (c) 2017 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 msgpack 22 23 import ( 24 "gopkg.in/vmihailenco/msgpack.v2" 25 ) 26 27 // IndexSummaryToken can be used, along with the summaries file buffer, to 28 // quickly retrieve the ID or index file offset of an index summary entry. 29 // It's structured such that the ID and Index file offset can be retrieved 30 // quickly, while only requiring 64-bits of memory per entry. 31 type IndexSummaryToken struct { 32 // The offset into the underlying byte array at which the bytes for the ID begin 33 idStartOffset uint32 34 // The length of the bytes for the ID 35 idLength uint32 36 } 37 38 // ID returns the ID that the metadata corresponds to 39 func (m IndexSummaryToken) ID(buf []byte) []byte { 40 idStart := m.idStartOffset 41 idEnd := idStart + m.idLength 42 return buf[idStart:idEnd] 43 } 44 45 // IndexOffset returns the offset in the index file for the series that the 46 // metadata corresponds to. The buf, stream, and decoder arguments are passed in 47 // so that the IndexSummaryToken struct can be kept small, as well as 48 // so that the caller can have control over re-use and allocations. 49 func (m IndexSummaryToken) IndexOffset( 50 buf []byte, stream ByteDecoderStream, msgpackDecoder *msgpack.Decoder) (int64, error) { 51 idStart := m.idStartOffset 52 idEnd := idStart + m.idLength 53 indexOffsetStart := int(idEnd) 54 55 stream.Reset(buf[indexOffsetStart:]) 56 msgpackDecoder.Reset(stream) 57 indexOffset, err := msgpackDecoder.DecodeInt64() 58 // Should never happen, either something is really wrong with the code or 59 // the file on disk was corrupted 60 if err != nil { 61 return -1, err 62 } 63 return indexOffset, nil 64 } 65 66 // NewIndexSummaryToken returns a new IndexSummaryToken 67 func NewIndexSummaryToken(idStartOffset, idLength uint32) IndexSummaryToken { 68 return IndexSummaryToken{ 69 idStartOffset: idStartOffset, 70 idLength: idLength, 71 } 72 }