github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/x/xio/reader64.go (about) 1 // Copyright (c) 2020 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 "encoding/binary" 25 "io" 26 ) 27 28 // BytesReader64 implements a Reader64 over a slice of bytes. 29 type BytesReader64 struct { 30 data []byte 31 index int 32 } 33 34 // NewBytesReader64 creates a new BytesReader64. 35 func NewBytesReader64(data []byte) *BytesReader64 { 36 return &BytesReader64{data: data} 37 } 38 39 // Read64 reads and returns a 64 bit word plus a number of bytes (up to 8) actually read. 40 func (r *BytesReader64) Read64() (word uint64, n byte, err error) { 41 if r.index+8 <= len(r.data) { 42 // NB: this compiles to a single 64 bit load followed by 43 // a BSWAPQ on amd64 gc 1.13 (https://godbolt.org/z/oTK1jx). 44 res := binary.BigEndian.Uint64(r.data[r.index:]) 45 r.index += 8 46 return res, 8, nil 47 } 48 if r.index >= len(r.data) { 49 return 0, 0, io.EOF 50 } 51 var res uint64 52 var bytes byte 53 for ; r.index < len(r.data); r.index++ { 54 res = (res << 8) | uint64(r.data[r.index]) 55 bytes++ 56 } 57 return res << (64 - 8*bytes), bytes, nil 58 } 59 60 // Peek64 peeks and returns the next 64 bit word plus a number of bytes (up to 8) available. 61 func (r *BytesReader64) Peek64() (word uint64, n byte, err error) { 62 if r.index+8 <= len(r.data) { 63 // NB: this compiles to a single 64 bit load followed by 64 // BSWAPQ on amd64 gc 1.13 (https://godbolt.org/z/oTK1jx). 65 res := binary.BigEndian.Uint64(r.data[r.index:]) 66 return res, 8, nil 67 } 68 69 if r.index >= len(r.data) { 70 return 0, 0, io.EOF 71 } 72 73 var res uint64 74 var bytes byte 75 for i := r.index; i < len(r.data); i++ { 76 res = (res << 8) | uint64(r.data[i]) 77 bytes++ 78 } 79 return res << (64 - 8*bytes), bytes, nil 80 } 81 82 // Reset resets the BytesReader64 for reuse. 83 func (r *BytesReader64) Reset(data []byte) { 84 r.data = data 85 r.index = 0 86 }