github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/aggregation/id_compress.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 aggregation 22 23 import ( 24 "fmt" 25 26 "github.com/willf/bitset" 27 ) 28 29 // IDCompressor can compress Types into an ID. 30 type IDCompressor interface { 31 // Compress compresses a set of aggregation types into an aggregation id. 32 Compress(aggTypes Types) (ID, error) 33 34 // MustCompress compresses a set of aggregation types into an aggregation id, 35 // and panics if an error is encountered. 36 MustCompress(aggTypes Types) ID 37 } 38 39 // IDDecompressor can decompress ID. 40 type IDDecompressor interface { 41 // Decompress decompresses an aggregation id into a set of aggregation types. 42 Decompress(id ID) (Types, error) 43 44 // MustDecompress decompresses an aggregation id into a set of aggregation types, 45 // and panics if an error is encountered. 46 MustDecompress(id ID) Types 47 } 48 49 type idCompressor struct { 50 bs *bitset.BitSet 51 } 52 53 // NewIDCompressor returns a new IDCompressor. 54 func NewIDCompressor() IDCompressor { 55 // NB(cw): If we start to support more than 64 types, the library will 56 // expand the underlying word list itself. 57 return &idCompressor{ 58 bs: bitset.New(maxTypeID), 59 } 60 } 61 62 func (c *idCompressor) Compress(aggTypes Types) (ID, error) { 63 c.bs.ClearAll() 64 for _, aggType := range aggTypes { 65 if !aggType.IsValid() { 66 return DefaultID, fmt.Errorf("could not compress invalid Type %v", aggType) 67 } 68 c.bs.Set(uint(aggType.ID())) 69 } 70 71 codes := c.bs.Bytes() 72 var id ID 73 // NB(cw) it's guaranteed that len(id) == len(codes) == IDLen, we need to copy 74 // the words in bitset out because the bitset contains a slice internally. 75 for i := 0; i < IDLen; i++ { 76 id[i] = codes[i] 77 } 78 return id, nil 79 } 80 81 func (c *idCompressor) MustCompress(aggTypes Types) ID { 82 id, err := c.Compress(aggTypes) 83 if err != nil { 84 panic(fmt.Errorf("unable to compress %v: %v", aggTypes, err)) 85 } 86 return id 87 } 88 89 type idDecompressor struct { 90 bs *bitset.BitSet 91 buf []uint64 92 pool TypesPool 93 } 94 95 // NewIDDecompressor returns a new IDDecompressor. 96 func NewIDDecompressor() IDDecompressor { 97 return NewPooledIDDecompressor(nil) 98 } 99 100 // NewPooledIDDecompressor returns a new pooled TypeDecompressor. 101 func NewPooledIDDecompressor(pool TypesPool) IDDecompressor { 102 bs := bitset.New(maxTypeID) 103 return &idDecompressor{ 104 bs: bs, 105 buf: bs.Bytes(), 106 pool: pool, 107 } 108 } 109 110 func (d *idDecompressor) Decompress(id ID) (Types, error) { 111 if id.IsDefault() { 112 return DefaultTypes, nil 113 } 114 // NB(cw) it's guaranteed that len(c.buf) == len(id) == IDLen, we need to copy 115 // the words from id into a slice to be used in bitset. 116 for i := range id { 117 d.buf[i] = id[i] 118 } 119 120 var res Types 121 if d.pool == nil { 122 res = make(Types, 0, maxTypeID) 123 } else { 124 res = d.pool.Get() 125 } 126 127 for i, e := d.bs.NextSet(0); e; i, e = d.bs.NextSet(i + 1) { 128 aggType := Type(i) 129 if !aggType.IsValid() { 130 return DefaultTypes, fmt.Errorf("invalid Type: %s", aggType.String()) 131 } 132 133 res = append(res, aggType) 134 } 135 136 return res, nil 137 } 138 139 func (d *idDecompressor) MustDecompress(id ID) Types { 140 aggTypes, err := d.Decompress(id) 141 if err != nil { 142 panic(fmt.Errorf("unable to decompress %v: %v", id, err)) 143 } 144 return aggTypes 145 }