James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_ |
| 3 | |
| 4 | #include <string_view> |
| 5 | |
| 6 | #include "absl/types/span.h" |
| 7 | #include "aos/containers/resizeable_buffer.h" |
| 8 | #include "aos/events/logging/buffer_encoder.h" |
| 9 | #include "aos/events/logging/logger_generated.h" |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 10 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | d5bd91a | 2022-09-16 15:11:54 -0700 | [diff] [blame] | 11 | #include "snappy-sinksource.h" |
| 12 | #include "snappy.h" |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos::logger { |
| 15 | |
| 16 | // Encodes buffers using snappy. |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 17 | class SnappyEncoder final : public DataEncoder { |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 18 | public: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 19 | explicit SnappyEncoder(size_t max_message_size, size_t chunk_size = 32768); |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 20 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 21 | void Encode(Copier *copy) final; |
| 22 | |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 23 | void Finish() final; |
| 24 | void Clear(int n) final; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 25 | absl::Span<const absl::Span<const uint8_t>> queue() final; |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 26 | size_t queued_bytes() const final; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 27 | bool HasSpace(size_t /*request*/) const final { |
| 28 | // Since the output always mallocs space, we have infinite output space. |
| 29 | return true; |
| 30 | } |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 31 | size_t total_bytes() const final { return total_bytes_; } |
| 32 | size_t queue_size() const final { return queue_.size(); } |
| 33 | |
| 34 | private: |
| 35 | class DetachedBufferSource : public snappy::Source { |
| 36 | public: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 37 | DetachedBufferSource(size_t buffer_size); |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 38 | size_t Available() const final; |
| 39 | const char *Peek(size_t *length) final; |
| 40 | void Skip(size_t n) final; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 41 | void Append(Copier *copy); |
| 42 | |
| 43 | uint32_t accumulated_checksum() const { |
| 44 | return accumulated_checksum_.value(); |
| 45 | } |
| 46 | |
| 47 | void ResetAccumulatedChecksum() { accumulated_checksum_.reset(); } |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 48 | |
| 49 | private: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 50 | ResizeableBuffer data_; |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 51 | size_t index_into_first_buffer_ = 0; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 52 | std::optional<uint32_t> accumulated_checksum_; |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | // Flushes buffer_source_ and stores the compressed buffer in queue_. |
| 56 | void EncodeCurrentBuffer(); |
| 57 | |
| 58 | // To queue up data: |
| 59 | // 1) When Encode is called, we use AppendBuffer to store the DetachedBuffer |
| 60 | // in buffer_source_. |
| 61 | // 2) Once we've queued up at least chunk_size_ data in buffer_source_, we |
| 62 | // use snappy to compress all the data. This flushes everything out of |
| 63 | // buffer_source_ and adds a single buffer to queue_. Note that we do |
| 64 | // not split up flatbuffer buffers to ensure that we produce chunks of |
| 65 | // exactly chunk_size_ uncompressed data--if we get a 1MB DetachedBuffer |
| 66 | // we will compress it all at once. |
| 67 | // 3) queue_ is the data that is actually read by queue() and cleared by |
| 68 | // Clear() to be written to disk. |
| 69 | const size_t chunk_size_; |
| 70 | DetachedBufferSource buffer_source_; |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 71 | std::vector<ResizeableBuffer> queue_; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame^] | 72 | |
| 73 | std::vector<absl::Span<const uint8_t>> return_queue_; |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 74 | size_t total_bytes_ = 0; |
| 75 | }; |
| 76 | |
| 77 | // Decompresses data with snappy. |
| 78 | class SnappyDecoder final : public DataDecoder { |
| 79 | public: |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 80 | static constexpr std::string_view kExtension = ".sz"; |
| 81 | |
James Kuszmaul | ef0e0cc | 2021-10-28 23:00:04 -0700 | [diff] [blame] | 82 | explicit SnappyDecoder(std::unique_ptr<DataDecoder> underlying_decoder) |
| 83 | : underlying_decoder_(std::move(underlying_decoder)) {} |
| 84 | explicit SnappyDecoder(std::string_view filename) |
| 85 | : SnappyDecoder(std::make_unique<DummyDecoder>(filename)) {} |
| 86 | |
| 87 | size_t Read(uint8_t *begin, uint8_t *end) final; |
| 88 | std::string_view filename() const final { |
| 89 | return underlying_decoder_->filename(); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | // decoder to use for reading data out of the file itself. |
| 94 | std::unique_ptr<DataDecoder> underlying_decoder_; |
| 95 | // Buffer to use for reading data from the file. This being a member variable |
| 96 | // is purely an optimization to avoid constant reallocations on every call to |
| 97 | // Read(). |
| 98 | ResizeableBuffer compressed_buffer_; |
| 99 | // Buffer of any uncompressed data that we've read but which hasn't yet been |
| 100 | // consumed by a call to Read(). |
| 101 | ResizeableBuffer uncompressed_buffer_; |
| 102 | |
| 103 | size_t total_output_ = 0; |
| 104 | }; |
| 105 | |
| 106 | } // namespace aos::logger |
| 107 | |
| 108 | #endif // AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_ |