github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/encoding/m3tsz/int_sig_bits_tracker.go (about) 1 // Copyright (c) 2019 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 m3tsz 22 23 import "github.com/m3db/m3/src/dbnode/encoding" 24 25 // IntSigBitsTracker is used to track the number of significant bits 26 // which should be used to encode the delta between two integers. 27 type IntSigBitsTracker struct { 28 NumSig uint8 // current largest number of significant places for int diffs 29 CurHighestLowerSig uint8 30 NumLowerSig uint8 31 } 32 33 // WriteIntValDiff writes the provided val diff bits along with 34 // whether the bits are negative or not. 35 func (t *IntSigBitsTracker) WriteIntValDiff( 36 stream encoding.OStream, valBits uint64, neg bool) { 37 if neg { 38 stream.WriteBit(opcodeNegative) 39 } else { 40 stream.WriteBit(opcodePositive) 41 } 42 43 stream.WriteBits(valBits, int(t.NumSig)) 44 } 45 46 // WriteIntSig writes the number of significant bits of the diff if it has changed and 47 // updates the IntSigBitsTracker. 48 func (t *IntSigBitsTracker) WriteIntSig(stream encoding.OStream, sig uint8) { 49 if t.NumSig != sig { 50 stream.WriteBit(opcodeUpdateSig) 51 if sig == 0 { 52 stream.WriteBit(OpcodeZeroSig) 53 } else { 54 stream.WriteBit(OpcodeNonZeroSig) 55 stream.WriteBits(uint64(sig-1), NumSigBits) 56 } 57 } else { 58 stream.WriteBit(opcodeNoUpdateSig) 59 } 60 61 t.NumSig = sig 62 } 63 64 // TrackNewSig gets the new number of significant bits given the 65 // number of significant bits of the current diff. It takes into 66 // account thresholds to try and find a value that's best for the 67 // current data 68 func (t *IntSigBitsTracker) TrackNewSig(numSig uint8) uint8 { 69 newSig := t.NumSig 70 71 if numSig > t.NumSig { 72 newSig = numSig 73 } else if t.NumSig-numSig >= sigDiffThreshold { 74 if t.NumLowerSig == 0 { 75 t.CurHighestLowerSig = numSig 76 } else if numSig > t.CurHighestLowerSig { 77 t.CurHighestLowerSig = numSig 78 } 79 80 t.NumLowerSig++ 81 if t.NumLowerSig >= sigRepeatThreshold { 82 newSig = t.CurHighestLowerSig 83 t.NumLowerSig = 0 84 } 85 86 } else { 87 t.NumLowerSig = 0 88 } 89 90 return newSig 91 }