MCPcopy
hub / github.com/prometheus/prometheus / Encode

Function Encode

util/compression/compression.go:48–83  ·  view source on GitHub ↗

Encode returns the encoded form of src for the given compression type. For None or empty message the encoding is not attempted. The buf allows passing various buffer implementations that make encoding more efficient. See NewSyncEncodeBuffer and NewConcurrentEncodeBuffer for further details. For non

(t Type, src []byte, buf EncodeBuffer)

Source from the content-addressed store, hash-verified

46// Encode is concurrency-safe, however note the concurrency limits for the
47// buffer of your choice.
48func Encode(t Type, src []byte, buf EncodeBuffer) (ret []byte, err error) {
49 if len(src) == 0 || t == "" || t == None {
50 return src, nil
51 }
52 if t == Snappy {
53 // If MaxEncodedLen is less than 0 the record is too large to be compressed.
54 if snappy.MaxEncodedLen(len(src)) < 0 {
55 return src, fmt.Errorf("compression: Snappy can't encode such a large message: %v", len(src))
56 }
57 var b []byte
58 if buf != nil {
59 b = buf.get()
60 defer func() {
61 buf.set(ret)
62 }()
63 }
64
65 // The snappy library uses `len` to calculate if we need a new buffer.
66 // In order to allocate as few buffers as possible make the length
67 // equal to the capacity.
68 b = b[:cap(b)]
69 return snappy.Encode(b, src), nil
70 }
71 if t == Zstd {
72 if buf == nil {
73 return nil, errors.New("zstd requested but EncodeBuffer was not provided")
74 }
75 b := buf.get()
76 defer func() {
77 buf.set(ret)
78 }()
79
80 return buf.zstdEncBuf().EncodeAll(src, b[:0]), nil
81 }
82 return nil, fmt.Errorf("unsupported compression type: %s", t)
83}
84
85// Decode returns the decoded form of src for the given compression type.
86//

Callers 6

logMethod · 0.92
buildWriteRequestFunction · 0.92
buildV2WriteRequestFunction · 0.92
TestEncodeDecodeFunction · 0.70
BenchmarkEncodeFunction · 0.70
BenchmarkDecodeFunction · 0.70

Calls 4

getMethod · 0.65
setMethod · 0.65
EncodeMethod · 0.65
zstdEncBufMethod · 0.65

Tested by 3

TestEncodeDecodeFunction · 0.56
BenchmarkEncodeFunction · 0.56
BenchmarkDecodeFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…