PragmaTwice commented on code in PR #1684:
URL: https://github.com/apache/kvrocks/pull/1684#discussion_r1299992075


##########
src/storage/rdb.cc:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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 "rdb.h"
+
+#include "common/crc64.h"
+#include "common/encoding.h"
+#include "common/lzf.h"
+
+// Redis object encoding length
+constexpr const int RDB_6BITLEN = 0;
+constexpr const int RDB_14BITLEN = 1;
+constexpr const int RDB_ENCVAL = 3;
+constexpr const int RDB_32BITLEN = 0x08;
+constexpr const int RDB_64BITLEN = 0x81;
+
+constexpr const int RDB_ENC_INT8 = 0;
+constexpr const int RDB_ENC_INT16 = 1;
+constexpr const int RDB_ENC_INT32 = 2;
+constexpr const int RDB_ENC_LZF = 3;
+
+class ListPack {
+ public:
+  explicit ListPack(std::string_view input) : input_(input){};
+  ~ListPack() = default;
+
+  StatusOr<std::vector<std::string>> Elements() {
+    auto len = GET_OR_RET(length());
+    std::vector<std::string> elements;
+    while (len-- != 0) {
+      auto element = GET_OR_RET(next());
+      elements.emplace_back(element);
+    }
+    return elements;
+  }
+
+ private:
+  std::string_view input_;
+  uint64_t pos_ = 0;
+
+  StatusOr<uint32_t> length() {
+    constexpr const int listPackHeaderSize = 6;
+    if (input_.size() < listPackHeaderSize) {
+      return {Status::NotOK, "invalid listpack length"};
+    }
+    uint32_t total_bytes = (static_cast<uint32_t>(input_[0])) | 
(static_cast<uint32_t>(input_[1]) << 8) |
+                           (static_cast<uint32_t>(input_[2]) << 16) | 
(static_cast<uint32_t>(input_[3]) << 24);
+    uint32_t len = (static_cast<uint32_t>(input_[4])) | 
(static_cast<uint32_t>(input_[5]) << 8);
+    pos_ += listPackHeaderSize;
+    if (total_bytes != input_.size()) {
+      return {Status::NotOK, "invalid list pack length"};
+    }
+    return len;
+  }
+
+  static uint32_t encodeBackLen(uint32_t len) {
+    if (len <= 127) {
+      return 1;
+    } else if (len < 16383) {
+      return 2;
+    } else if (len < 2097151) {
+      return 3;
+    } else if (len < 268435455) {
+      return 4;
+    } else {
+      return 5;
+    }
+  }
+
+  Status peekOK(size_t n) {
+    if (pos_ + n > input_.size()) {
+      return {Status::NotOK, "invalid list pack entry"};
+    }
+    return Status::OK();
+  }
+
+  StatusOr<std::string> next() {
+    RET_IF_ERROR(peekOK(1));

Review Comment:
   You can just use GET_OR_RET, which is supported to return void.



-- 
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