github.com/m3db/m3@v1.5.0/src/cluster/placement/compress.go (about) 1 // Copyright (c) 2021 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 placement 22 23 import ( 24 "github.com/gogo/protobuf/proto" 25 "github.com/klauspost/compress/zstd" 26 27 "github.com/m3db/m3/src/cluster/generated/proto/placementpb" 28 ) 29 30 const _decoderInitialBufferSize = 131072 31 32 func compressPlacementProto(p *placementpb.Placement) ([]byte, error) { 33 if p == nil { 34 return nil, errNilPlacementSnapshotsProto 35 } 36 37 w, err := zstd.NewWriter( 38 nil, 39 zstd.WithEncoderCRC(true), 40 zstd.WithEncoderConcurrency(1), 41 zstd.WithEncoderLevel(zstd.SpeedBestCompression)) 42 if err != nil { 43 return nil, err 44 } 45 defer w.Close() // nolint:errcheck 46 47 uncompressed, _ := p.Marshal() 48 49 compressed := make([]byte, 0, len(uncompressed)/2) // prealloc, assuming at least 50% space savings 50 compressed = w.EncodeAll(uncompressed, compressed) 51 52 return compressed, nil 53 } 54 55 func decompressPlacementProto(compressed []byte) (*placementpb.Placement, error) { 56 if compressed == nil { 57 return nil, errNilValue 58 } 59 60 decompressed := make([]byte, 0, _decoderInitialBufferSize) 61 r, err := zstd.NewReader( 62 nil, 63 zstd.WithDecoderConcurrency(1), 64 zstd.WithDecoderLowmem(false), 65 ) 66 if err != nil { 67 return nil, err 68 } 69 defer r.Close() // nolint:errcheck 70 71 decompressed, err = r.DecodeAll(compressed, decompressed) 72 if err != nil { 73 return nil, err 74 } 75 76 result := &placementpb.Placement{} 77 if err := proto.Unmarshal(decompressed, result); err != nil { 78 return nil, err 79 } 80 81 return result, nil 82 }