lordgamez commented on code in PR #1974:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1974#discussion_r2368673344


##########
libminifi/include/sitetosite/CompressionConsts.h:
##########
@@ -0,0 +1,28 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "io/InputStream.h"
+#include "io/BufferStream.h"
+
+namespace org::apache::nifi::minifi::sitetosite {
+
+static constexpr size_t COMPRESSION_BUFFER_SIZE = 65536;
+static constexpr std::array<char, 4> SYNC_BYTES = { 'S', 'Y', 'N', 'C' };

Review Comment:
   Updated in 
https://github.com/apache/nifi-minifi-cpp/pull/1974/commits/a623c4ad576d2e4b3c934b216b9b158157ebfa36



##########
libminifi/include/sitetosite/CompressionInputStream.h:
##########
@@ -0,0 +1,54 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "io/InputStream.h"
+#include "io/BufferStream.h"
+#include "CompressionConsts.h"
+#include "core/logging/LoggerFactory.h"
+
+namespace org::apache::nifi::minifi::sitetosite {
+
+class CompressionInputStream : public io::InputStreamImpl {
+ public:
+  explicit CompressionInputStream(gsl::not_null<io::InputStream*> 
internal_stream)
+      : internal_stream_(internal_stream) {
+    buffer_.resize(COMPRESSION_BUFFER_SIZE);

Review Comment:
   Good idea, updated in 
https://github.com/apache/nifi-minifi-cpp/pull/1974/commits/a623c4ad576d2e4b3c934b216b9b158157ebfa36



##########
libminifi/src/sitetosite/CompressionInputStream.cpp:
##########
@@ -0,0 +1,148 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "sitetosite/CompressionInputStream.h"
+#include "io/ZlibStream.h"
+
+namespace org::apache::nifi::minifi::sitetosite {
+
+size_t CompressionInputStream::decompressData() {
+  if (eof_) {
+    return 0;
+  }
+
+  std::vector<std::byte> local_buffer(COMPRESSION_BUFFER_SIZE);
+  auto ret = internal_stream_->read(std::span(local_buffer).subspan(0, 
SYNC_BYTES.size()));
+  if (ret != SYNC_BYTES.size() ||
+      !std::equal(SYNC_BYTES.begin(), SYNC_BYTES.end(), local_buffer.begin(), 
[](char sync_char, std::byte read_byte) { return 
static_cast<std::byte>(sync_char) == read_byte;})) {
+    logger_->log_error("Failed to read sync bytes or sync bytes do not match");
+    return io::STREAM_ERROR;
+  }
+
+  uint32_t original_size = 0;
+  ret = internal_stream_->read(original_size);
+  if (io::isError(ret) || ret != 4) {
+    logger_->log_error("Failed to read original size, ret: {}", ret);
+    return io::STREAM_ERROR;
+  }
+
+  uint32_t compressed_size = 0;
+  ret = internal_stream_->read(compressed_size);
+  if (io::isError(ret) || ret != 4) {
+    logger_->log_error("Failed to read compressed size, ret: {}", ret);
+    return io::STREAM_ERROR;
+  }
+
+  ret = internal_stream_->read(std::span(local_buffer).subspan(0, 
compressed_size));
+  if (io::isError(ret) || ret != compressed_size) {
+    logger_->log_error("Failed to read compressed data, ret: {}", ret);
+    return io::STREAM_ERROR;
+  }
+
+  if (compressed_size == 0 && original_size != 0) {
+    logger_->log_error("Compressed size is 0 but original size is not");
+    return io::STREAM_ERROR;
+  }
+
+  if (compressed_size > COMPRESSION_BUFFER_SIZE) {
+    logger_->log_error("Compressed size exceeds buffer size");
+    return io::STREAM_ERROR;
+  }

Review Comment:
   Good catch, updated in 
https://github.com/apache/nifi-minifi-cpp/pull/1974/commits/a623c4ad576d2e4b3c934b216b9b158157ebfa36



##########
libminifi/src/sitetosite/CompressionOutputStream.cpp:
##########
@@ -0,0 +1,128 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "sitetosite/CompressionOutputStream.h"
+
+#include "io/ZlibStream.h"
+#include "io/StreamPipe.h"
+#include "io/BufferStream.h"
+#include "core/logging/LoggerFactory.h"
+
+namespace org::apache::nifi::minifi::sitetosite {
+
+size_t CompressionOutputStream::write(const uint8_t *value, size_t len) {
+  if (value == nullptr || len == 0) {
+    return 0;
+  }
+
+  size_t bytes_left_to_write = len;
+  size_t bytes_written = 0;
+  while (bytes_left_to_write > 0) {
+    size_t free_spaces_left_in_buffer = buffer_.size() - buffer_offset_;
+    size_t bytes_to_write = std::min(bytes_left_to_write, 
free_spaces_left_in_buffer);
+    std::memcpy(reinterpret_cast<uint8_t*>(buffer_.data()) + buffer_offset_, 
value + bytes_written, bytes_to_write);
+    bytes_written += bytes_to_write;
+    bytes_left_to_write -= bytes_to_write;
+    buffer_offset_ += bytes_to_write;
+    gsl_Assert(buffer_offset_ <= buffer_.size());
+    if (buffer_offset_ == buffer_.size()) {
+      auto ret = compressAndWrite();
+      if (io::isError(ret)) {
+        return ret;
+      }
+    }
+  }
+  return bytes_written;
+}
+
+size_t CompressionOutputStream::compressAndWrite() {
+  if (was_data_written_) {
+    // Write a continue byte to indicate that there is more data to follow
+    auto ret = internal_stream_->write(static_cast<uint8_t>(1));
+    if (io::isError(ret)) {
+      logger_->log_error("Failed to write continue byte before compression: 
{}", ret);
+      return ret;
+    }
+  }
+  auto ret = internal_stream_->write(reinterpret_cast<const uint8_t 
*>(SYNC_BYTES.data()), SYNC_BYTES.size());
+  if (io::isError(ret)) {
+    logger_->log_error("Failed to write sync bytes before compression: {}", 
ret);
+    return ret;
+  }
+
+  was_data_written_ = true;

Review Comment:
   Updated in 
https://github.com/apache/nifi-minifi-cpp/pull/1974/commits/a623c4ad576d2e4b3c934b216b9b158157ebfa36



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to