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)
| 46 | // Encode is concurrency-safe, however note the concurrency limits for the |
| 47 | // buffer of your choice. |
| 48 | func 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 | // |
searching dependent graphs…