Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LZMA_ENCODER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LZMA_ENCODER_H_ |
| 3 | |
| 4 | #include "absl/types/span.h" |
| 5 | #include "flatbuffers/flatbuffers.h" |
| 6 | #include "lzma.h" |
| 7 | |
| 8 | #include "aos/containers/resizeable_buffer.h" |
| 9 | #include "aos/events/logging/buffer_encoder.h" |
| 10 | #include "aos/events/logging/logger_generated.h" |
| 11 | |
| 12 | namespace aos::logger { |
| 13 | |
| 14 | // Encodes buffers using liblzma. |
| 15 | class LzmaEncoder final : public DetachedBufferEncoder { |
| 16 | public: |
| 17 | // Initializes the LZMA stream and encoder. |
| 18 | explicit LzmaEncoder(uint32_t compression_preset); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 19 | LzmaEncoder(const LzmaEncoder &) = delete; |
| 20 | LzmaEncoder(LzmaEncoder &&other) = delete; |
| 21 | LzmaEncoder &operator=(const LzmaEncoder &) = delete; |
| 22 | LzmaEncoder &operator=(LzmaEncoder &&other) = delete; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 23 | // Gracefully shuts down the encoder. |
| 24 | ~LzmaEncoder() final; |
| 25 | |
| 26 | void Encode(flatbuffers::DetachedBuffer &&in) final; |
| 27 | void Finish() final; |
| 28 | void Clear(int n) final; |
| 29 | std::vector<absl::Span<const uint8_t>> queue() const final; |
| 30 | size_t queued_bytes() const final; |
| 31 | size_t total_bytes() const final { return total_bytes_; } |
| 32 | size_t queue_size() const final { return queue_.size(); } |
| 33 | |
| 34 | private: |
| 35 | static constexpr size_t kEncodedBufferSizeBytes{1024}; |
| 36 | |
| 37 | void RunLzmaCode(lzma_action action); |
| 38 | |
| 39 | lzma_stream stream_; |
| 40 | uint32_t compression_preset_; |
| 41 | std::vector<ResizeableBuffer> queue_; |
| 42 | bool finished_ = false; |
| 43 | // Total bytes that resulted from encoding raw data since the last call to |
| 44 | // Reset. |
| 45 | size_t total_bytes_ = 0; |
| 46 | }; |
| 47 | |
| 48 | // Decompresses data with liblzma. |
| 49 | class LzmaDecoder final : public DataDecoder { |
| 50 | public: |
| 51 | explicit LzmaDecoder(std::string_view filename); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 52 | LzmaDecoder(const LzmaDecoder &) = delete; |
| 53 | LzmaDecoder(LzmaDecoder &&other) = delete; |
| 54 | LzmaDecoder &operator=(const LzmaDecoder &) = delete; |
| 55 | LzmaDecoder &operator=(LzmaDecoder &&other) = delete; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 56 | ~LzmaDecoder(); |
| 57 | |
| 58 | size_t Read(uint8_t *begin, uint8_t *end) final; |
| 59 | |
| 60 | private: |
| 61 | // Size of temporary buffer to use. |
| 62 | static constexpr size_t kBufSize{256 * 1024}; |
| 63 | |
| 64 | // Temporary buffer for storing compressed data. |
| 65 | ResizeableBuffer compressed_data_; |
| 66 | // Used for reading data from the file. |
| 67 | DummyDecoder dummy_decoder_; |
| 68 | // Stream for decompression. |
| 69 | lzma_stream stream_; |
| 70 | // The current action. This is LZMA_RUN until we've run out of data to read |
| 71 | // from the file. |
| 72 | lzma_action action_ = LZMA_RUN; |
| 73 | // Flag that represents whether or not all the data from the file has been |
| 74 | // successfully decoded. |
| 75 | bool finished_ = false; |
| 76 | }; |
| 77 | |
| 78 | } // namespace aos::logger |
| 79 | |
| 80 | #endif // AOS_EVENTS_LOGGING_LZMA_ENCODER_H_ |