Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
30407e1f
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
30407e1f
编写于
12月 15, 2010
作者:
P
Pieter Noordhuis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make SETBIT return original bit value
上级
6a246b1e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
28 deletion
+37
-28
src/t_string.c
src/t_string.c
+25
-16
tests/unit/basic.tcl
tests/unit/basic.tcl
+12
-12
未找到文件。
src/t_string.c
浏览文件 @
30407e1f
...
...
@@ -111,17 +111,18 @@ void setbitCommand(redisClient *c) {
robj
*
o
;
char
*
err
=
"bit is not an integer or out of range"
;
size_t
bitoffset
;
long
long
bitvalue
;
int
byte
,
bit
,
on
;
int
byte
,
bit
;
int
byteval
,
bitval
;
long
on
;
if
(
getBitOffsetFromArgument
(
c
,
c
->
argv
[
2
],
&
bitoffset
)
!=
REDIS_OK
)
return
;
if
(
getLong
LongFromObjectOrReply
(
c
,
c
->
argv
[
3
],
&
bitvalue
,
err
)
!=
REDIS_OK
)
if
(
getLong
FromObjectOrReply
(
c
,
c
->
argv
[
3
],
&
on
,
err
)
!=
REDIS_OK
)
return
;
/*
A bit can only be set to be on or off
... */
if
(
bitvalue
&
~
1
)
{
/*
Bits can only be set or cleared
... */
if
(
on
&
~
1
)
{
addReplyError
(
c
,
err
);
return
;
}
...
...
@@ -142,23 +143,30 @@ void setbitCommand(redisClient *c) {
}
}
/* Grow sds value to the right length if necessary */
byte
=
bitoffset
>>
3
;
bit
=
7
-
(
bitoffset
&
0x7
);
on
=
bitvalue
&
0x1
;
o
->
ptr
=
sdsgrowzero
(
o
->
ptr
,
byte
+
1
);
((
char
*
)
o
->
ptr
)[
byte
]
|=
on
<<
bit
;
((
char
*
)
o
->
ptr
)[
byte
]
&=
~
((
!
on
)
<<
bit
);
/* Get current values */
byteval
=
((
char
*
)
o
->
ptr
)[
byte
];
bit
=
7
-
(
bitoffset
&
0x7
);
bitval
=
byteval
&
(
1
<<
bit
);
/* Update byte with new bit value and return original value */
byteval
&=
~
(
1
<<
bit
);
byteval
|=
((
on
&
0x1
)
<<
bit
);
((
char
*
)
o
->
ptr
)[
byte
]
=
byteval
;
touchWatchedKey
(
c
->
db
,
c
->
argv
[
1
]);
server
.
dirty
++
;
addReply
(
c
,
shared
.
cone
);
addReply
(
c
,
bitval
?
shared
.
cone
:
shared
.
czero
);
}
void
getbitCommand
(
redisClient
*
c
)
{
robj
*
o
;
size_t
bitoffset
,
byte
,
bitmask
;
int
on
=
0
;
char
llbuf
[
32
];
size_t
bitoffset
;
size_t
byte
,
bit
;
size_t
bitval
=
0
;
if
(
getBitOffsetFromArgument
(
c
,
c
->
argv
[
2
],
&
bitoffset
)
!=
REDIS_OK
)
return
;
...
...
@@ -167,15 +175,16 @@ void getbitCommand(redisClient *c) {
checkType
(
c
,
o
,
REDIS_STRING
))
return
;
byte
=
bitoffset
>>
3
;
bit
mask
=
1
<<
(
7
-
(
bitoffset
&
0x7
)
);
bit
=
7
-
(
bitoffset
&
0x7
);
if
(
o
->
encoding
!=
REDIS_ENCODING_RAW
)
{
if
(
byte
<
(
size_t
)
ll2string
(
llbuf
,
sizeof
(
llbuf
),(
long
)
o
->
ptr
))
on
=
llbuf
[
byte
]
&
bitmask
;
bitval
=
llbuf
[
byte
]
&
(
1
<<
bit
)
;
}
else
{
if
(
byte
<
sdslen
(
o
->
ptr
))
on
=
((
sds
)
o
->
ptr
)[
byte
]
&
bitmask
;
bitval
=
((
char
*
)
o
->
ptr
)[
byte
]
&
(
1
<<
bit
)
;
}
addReply
(
c
,
on
?
shared
.
cone
:
shared
.
czero
);
addReply
(
c
,
bitval
?
shared
.
cone
:
shared
.
czero
);
}
void
setrangeCommand
(
redisClient
*
c
)
{
...
...
tests/unit/basic.tcl
浏览文件 @
30407e1f
...
...
@@ -377,29 +377,29 @@ start_server {tags {"basic"}} {
test
"SETBIT against non-existing key"
{
r del mykey
# Setting 2nd bit to on is integer 64, ascii
"@"
assert_equal 1
[
r setbit mykey 1 1
]
assert_equal
"@"
[
r get mykey
]
assert_equal 0
[
r setbit mykey 1 1
]
assert_equal
[
binary format B* 01000000
]
[
r get mykey
]
}
test
"SETBIT against string-encoded key"
{
#
Single byte with 2nd bit set
#
Ascii
"@"
is integer 64 = 01 00 00 00
r set mykey
"@"
# 64 + 32 = 96 => ascii
"`"
(
backtick
)
assert_equal 1
[
r setbit mykey 2 1
]
assert_equal
"`"
[
r get mykey
]
assert_equal 0
[
r setbit mykey 2 1
]
assert_equal
[
binary format B* 01100000
]
[
r get mykey
]
assert_equal 1
[
r setbit mykey 1 0
]
assert_equal
[
binary format B* 00100000
]
[
r get mykey
]
}
test
"SETBIT against integer-encoded key"
{
# Ascii
"1"
is integer 49 = 00 11 00 01
r set mykey 1
assert_encoding int mykey
# Ascii
"1"
is integer 49 = 00 11 00 01
# Setting 7th bit = 51 => ascii
"3"
assert_equal 1
[
r setbit mykey
6 1
]
assert_equal
"3"
[
r get mykey
]
assert_equal 0
[
r setbit mykey 6 1
]
assert_equal
[
binary format B* 00110011
]
[
r get mykey
]
assert_equal 1
[
r setbit mykey
2 0
]
assert_equal
[
binary format B* 00010011
]
[
r get mykey
]
}
test
"SETBIT against key with wrong type"
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录