mapleFU commented on code in PR #1901:
URL: https://github.com/apache/kvrocks/pull/1901#discussion_r1400125723


##########
src/common/bitfield_util.h:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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 <stdexcept>
+#include <cstdint>
+#include <limits>
+#include <type_traits>
+
+enum class BitfieldOverflowBehavior : uint8_t { kWrap, kSat, kFail };
+
+struct BitfieldEncoding {
+  bool is_signed;
+  // 1 ~ 63 if unsigned, 1 ~ 64 if signed.

Review Comment:
   Should we `static_assert(sizeof(...))` for this?



##########
src/common/bitfield_util.h:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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 <stdexcept>
+#include <cstdint>
+#include <limits>
+#include <type_traits>
+
+enum class BitfieldOverflowBehavior : uint8_t { kWrap, kSat, kFail };
+
+struct BitfieldEncoding {
+  bool is_signed;
+  // 1 ~ 63 if unsigned, 1 ~ 64 if signed.
+  uint8_t bits;
+};
+
+struct BitfieldOperation {
+  // see https://redis.io/commands/bitfield/ to get more details.
+  enum class Type : uint8_t { kGet, kSet, kIncrBy };
+
+  Type type;
+  BitfieldOverflowBehavior overflow{BitfieldOverflowBehavior::kWrap};
+  BitfieldEncoding encoding;
+  uint32_t offset;
+  // INCRBY amount or SET value
+  int64_t value;
+};
+
+namespace detail {
+
+// safe cast from unsigned to signed.
+// see also "Integral conversions" on 
https://en.cppreference.com/w/cpp/language/implicit_conversion
+// If the destination type is signed, the result when overflow is 
implementation-defined until C++20
+union SafeInt64 {

Review Comment:
   Would we better just using `uint64_t` and maintaining a group of `SafeOp` 
rather than `SafeInt64`?



##########
src/types/redis_bitmap.cc:
##########
@@ -538,6 +545,302 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const 
std::string &op_name, co
   return storage_->Write(storage_->DefaultWriteOptions(), 
batch->GetWriteBatch());
 }
 
+namespace {
+union IntResult {

Review Comment:
   ditto. Seems this has duplicate IntResult...



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