Skip to content

Commit a3c1666

Browse files
Mixficsolwuxianrong
andauthored
fix: Change the maximum range of setbit's offset to fit redis (#2995)
* Change the maximum range of setbit's offset to fit redis --------- Co-authored-by: wuxianrong <[email protected]>
1 parent 95d4be6 commit a3c1666

File tree

10 files changed

+53
-13
lines changed

10 files changed

+53
-13
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ set(ZSTD_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
367367
ExternalProject_Add(fmt
368368
DEPENDS
369369
URL
370-
https://github.com/fmtlib/fmt/archive/refs/tags/7.1.0.tar.gz
370+
https://github.com/fmtlib/fmt/archive/refs/tags/10.2.1.tar.gz
371371
URL_HASH
372-
MD5=32af902636d373641f4ef9032fc65b3a
372+
MD5=dc09168c94f90ea890257995f2c497a5
373373
DOWNLOAD_NO_PROGRESS
374374
1
375375
UPDATE_COMMAND

include/pika_define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ const std::string kInnerReplOk = "ok";
366366
const std::string kInnerReplWait = "wait";
367367

368368
const unsigned int kMaxBitOpInputKey = 12800;
369-
const int kMaxBitOpInputBit = 21;
369+
const int kMaxBitOpInputBit = 32;
370370
/*
371371
* db sync
372372
*/

src/pika_bit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void BitPosCmd::Do() {
241241
s_ = db_->storage()->BitPos(key_, static_cast<int32_t>(bit_val_), start_offset_, end_offset_, &pos);
242242
}
243243
if (s_.ok()) {
244-
res_.AppendInteger(static_cast<int>(pos));
244+
res_.AppendInteger(pos);
245245
} else if (s_.IsInvalidArgument()) {
246246
res_.SetRes(CmdRes::kMultiKey);
247247
} else {

src/storage/src/redis_strings.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,12 +1066,12 @@ Status Redis::Strlen(const Slice& key, int32_t* len) {
10661066
return s;
10671067
}
10681068

1069-
int32_t GetBitPos(const unsigned char* s, unsigned int bytes, int bit) {
1069+
int64_t GetBitPos(const unsigned char* s, unsigned int bytes, int bit) {
10701070
uint64_t word = 0;
10711071
uint64_t skip_val = 0;
10721072
auto value = const_cast<unsigned char*>(s);
10731073
auto l = reinterpret_cast<uint64_t*>(value);
1074-
int pos = 0;
1074+
int64_t pos = 0;
10751075
if (bit == 0) {
10761076
skip_val = std::numeric_limits<uint64_t>::max();
10771077
} else {
@@ -1149,7 +1149,7 @@ Status Redis::BitPos(const Slice& key, int32_t bit, int64_t* ret) {
11491149
pos = -1;
11501150
}
11511151
if (pos != -1) {
1152-
pos = pos + 8 * start_offset;
1152+
pos += 8 * start_offset;
11531153
}
11541154
*ret = pos;
11551155
}

tests/integration/string_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ var _ = Describe("String Commands", func() {
277277
Expect(getBit.Err()).NotTo(HaveOccurred())
278278
Expect(getBit.Val()).To(Equal(int64(0)))
279279
})
280-
280+
281281
It("should GetRange", func() {
282282
set := client.Set(ctx, "key", "This is a string", 0)
283283
Expect(set.Err()).NotTo(HaveOccurred())

tests/unit/type/bitops.tcl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,46 @@ start_server {tags {"bitops"}} {
131131
r get s
132132
} "\x55\xff\x00\xaa"
133133

134+
test {SetBit and GetBit with large offset} {
135+
set max_offset [expr {2**32 - 1}]
136+
set invalid_offset [expr {2**32}]
137+
138+
r setbit large_key $max_offset 1
139+
set result [r getbit large_key $max_offset]
140+
set invalid_result [catch {r setbit large_key $invalid_offset 1} err]
141+
142+
list $result $invalid_result $err
143+
} {1 1 {ERR bit offset is not an integer or out of range}}
144+
145+
test {BITCOUNT with large offset} {
146+
r setbit count_key 0 1
147+
r setbit count_key 100 1
148+
r setbit count_key [expr {2**32 - 1}] 1
149+
150+
set total_count [r bitcount count_key]
151+
set range_count [r bitcount count_key 0 12]
152+
153+
list $total_count $range_count
154+
} {3 2}
155+
156+
test {BITPOS with large offset} {
157+
r setbit pos_key [expr {2**32 - 1}] 1
158+
set first_one [r bitpos pos_key 1]
159+
set first_zero [r bitpos pos_key 0]
160+
list $first_one $first_zero
161+
} {4294967295 0}
162+
163+
test {BITOP operations} {
164+
r setbit key1 0 1
165+
r setbit key2 [expr {2**32 - 1}] 1
166+
r bitop or result_key key1 key2
167+
168+
set result_bit1 [r getbit result_key 0]
169+
set result_bit2 [r getbit result_key [expr {2**32 - 1}]]
170+
171+
list $result_bit1 $result_bit2
172+
} {1 1}
173+
134174
test {BITOP AND|OR|XOR don't change the string with single input key} {
135175
r set a "\x01\x02\xff"
136176
r bitop and res1 a

tools/manifest_generator/include/pika_define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ const std::string kInnerReplOk = "ok";
292292
const std::string kInnerReplWait = "wait";
293293

294294
const unsigned int kMaxBitOpInputKey = 12800;
295-
const int kMaxBitOpInputBit = 21;
295+
const int kMaxBitOpInputBit = 32;
296296
/*
297297
* db sync
298298
*/

tools/pika-port/pika_port_3/pika_define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const std::string kInnerReplOk = "ok";
117117
const std::string kInnerReplWait = "wait";
118118

119119
const unsigned int kMaxBitOpInputKey = 12800;
120-
const int kMaxBitOpInputBit = 21;
120+
const int kMaxBitOpInputBit = 32;
121121
/*
122122
* db sync
123123
*/

tools/pika_migrate/include/pika_define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ const std::string kInnerReplOk = "ok";
413413
const std::string kInnerReplWait = "wait";
414414

415415
const unsigned int kMaxBitOpInputKey = 12800;
416-
const int kMaxBitOpInputBit = 21;
416+
const int kMaxBitOpInputBit = 32;
417417
/*
418418
* db sync
419419
*/

tools/pika_migrate/src/pika_bit.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void BitSetCmd::DoInitial() {
2727
res_.SetRes(CmdRes::kInvalidBitOffsetInt);
2828
return;
2929
}
30-
// value no bigger than 2^18
30+
// value no bigger than 2^32
3131
if ( (bit_offset_ >> kMaxBitOpInputBit) > 0) {
3232
res_.SetRes(CmdRes::kInvalidBitOffsetInt);
3333
return;
@@ -168,7 +168,7 @@ void BitPosCmd::Do(std::shared_ptr<Partition> partition) {
168168
s = partition->db()->BitPos(key_, bit_val_, start_offset_, end_offset_, &pos);
169169
}
170170
if (s.ok()) {
171-
res_.AppendInteger((int)pos);
171+
res_.AppendInteger(pos);
172172
} else {
173173
res_.SetRes(CmdRes::kErrOther, s.ToString());
174174
}

0 commit comments

Comments
 (0)