Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
5574b53e
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,发现更多精彩内容 >>
提交
5574b53e
编写于
11月 12, 2011
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
INCRBYFLOAT implementation
上级
64c7499e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
88 addition
and
2 deletion
+88
-2
src/object.c
src/object.c
+57
-2
src/redis.c
src/redis.c
+1
-0
src/redis.h
src/redis.h
+4
-0
src/t_string.c
src/t_string.c
+26
-0
未找到文件。
src/object.c
浏览文件 @
5574b53e
...
...
@@ -44,6 +44,21 @@ robj *createStringObjectFromLongLong(long long value) {
return
o
;
}
/* Note: this function is defined into object.c since here it is where it
* belongs but it is actually designed to be used just for INCRBYFLOAT */
robj
*
createStringObjectFromLongDouble
(
long
double
value
)
{
char
buf
[
256
];
int
len
;
/* We use 17 digits precision since with 128 bit floats that precision
* after rouding is able to represent most small decimal numbers in a way
* that is "non surprising" for the user (that is, most small decimal
* numbers will be represented in a way that when converted back into
* a string are exactly the same as what the user typed.) */
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.17Lg"
,
value
);
return
createStringObject
(
buf
,
len
);
}
robj
*
dupStringObject
(
robj
*
o
)
{
redisAssertWithInfo
(
NULL
,
o
,
o
->
encoding
==
REDIS_ENCODING_RAW
);
return
createStringObject
(
o
->
ptr
,
sdslen
(
o
->
ptr
));
...
...
@@ -350,8 +365,10 @@ int getDoubleFromObject(robj *o, double *target) {
}
else
{
redisAssertWithInfo
(
NULL
,
o
,
o
->
type
==
REDIS_STRING
);
if
(
o
->
encoding
==
REDIS_ENCODING_RAW
)
{
errno
=
0
;
value
=
strtod
(
o
->
ptr
,
&
eptr
);
if
(
eptr
[
0
]
!=
'\0'
||
isnan
(
value
))
return
REDIS_ERR
;
if
(
eptr
[
0
]
!=
'\0'
||
errno
==
ERANGE
||
isnan
(
value
))
return
REDIS_ERR
;
}
else
if
(
o
->
encoding
==
REDIS_ENCODING_INT
)
{
value
=
(
long
)
o
->
ptr
;
}
else
{
...
...
@@ -369,7 +386,45 @@ int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const ch
if
(
msg
!=
NULL
)
{
addReplyError
(
c
,(
char
*
)
msg
);
}
else
{
addReplyError
(
c
,
"value is not a double"
);
addReplyError
(
c
,
"value is not a valid float"
);
}
return
REDIS_ERR
;
}
*
target
=
value
;
return
REDIS_OK
;
}
int
getLongDoubleFromObject
(
robj
*
o
,
long
double
*
target
)
{
long
double
value
;
char
*
eptr
;
if
(
o
==
NULL
)
{
value
=
0
;
}
else
{
redisAssertWithInfo
(
NULL
,
o
,
o
->
type
==
REDIS_STRING
);
if
(
o
->
encoding
==
REDIS_ENCODING_RAW
)
{
errno
=
0
;
value
=
strtold
(
o
->
ptr
,
&
eptr
);
if
(
eptr
[
0
]
!=
'\0'
||
errno
==
ERANGE
||
isnan
(
value
))
return
REDIS_ERR
;
}
else
if
(
o
->
encoding
==
REDIS_ENCODING_INT
)
{
value
=
(
long
)
o
->
ptr
;
}
else
{
redisPanic
(
"Unknown string encoding"
);
}
}
*
target
=
value
;
return
REDIS_OK
;
}
int
getLongDoubleFromObjectOrReply
(
redisClient
*
c
,
robj
*
o
,
long
double
*
target
,
const
char
*
msg
)
{
long
double
value
;
if
(
getLongDoubleFromObject
(
o
,
&
value
)
!=
REDIS_OK
)
{
if
(
msg
!=
NULL
)
{
addReplyError
(
c
,(
char
*
)
msg
);
}
else
{
addReplyError
(
c
,
"value is not a valid float"
);
}
return
REDIS_ERR
;
}
...
...
src/redis.c
浏览文件 @
5574b53e
...
...
@@ -165,6 +165,7 @@ struct redisCommand redisCommandTable[] = {
{
"hexists"
,
hexistsCommand
,
3
,
"r"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"incrby"
,
incrbyCommand
,
3
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"decrby"
,
decrbyCommand
,
3
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"incrbyfloat"
,
incrbyfloatCommand
,
3
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"getset"
,
getsetCommand
,
3
,
"wm"
,
0
,
NULL
,
1
,
1
,
1
,
0
,
0
},
{
"mset"
,
msetCommand
,
-
3
,
"wm"
,
0
,
NULL
,
1
,
-
1
,
2
,
0
,
0
},
{
"msetnx"
,
msetnxCommand
,
-
3
,
"wm"
,
0
,
NULL
,
1
,
-
1
,
2
,
0
,
0
},
...
...
src/redis.h
浏览文件 @
5574b53e
...
...
@@ -811,6 +811,7 @@ robj *tryObjectEncoding(robj *o);
robj
*
getDecodedObject
(
robj
*
o
);
size_t
stringObjectLen
(
robj
*
o
);
robj
*
createStringObjectFromLongLong
(
long
long
value
);
robj
*
createStringObjectFromLongDouble
(
long
double
value
);
robj
*
createListObject
(
void
);
robj
*
createZiplistObject
(
void
);
robj
*
createSetObject
(
void
);
...
...
@@ -823,6 +824,8 @@ int checkType(redisClient *c, robj *o, int type);
int
getLongLongFromObjectOrReply
(
redisClient
*
c
,
robj
*
o
,
long
long
*
target
,
const
char
*
msg
);
int
getDoubleFromObjectOrReply
(
redisClient
*
c
,
robj
*
o
,
double
*
target
,
const
char
*
msg
);
int
getLongLongFromObject
(
robj
*
o
,
long
long
*
target
);
int
getLongDoubleFromObject
(
robj
*
o
,
long
double
*
target
);
int
getLongDoubleFromObjectOrReply
(
redisClient
*
c
,
robj
*
o
,
long
double
*
target
,
const
char
*
msg
);
char
*
strEncoding
(
int
encoding
);
int
compareStringObjects
(
robj
*
a
,
robj
*
b
);
int
equalStringObjects
(
robj
*
a
,
robj
*
b
);
...
...
@@ -1004,6 +1007,7 @@ void incrCommand(redisClient *c);
void
decrCommand
(
redisClient
*
c
);
void
incrbyCommand
(
redisClient
*
c
);
void
decrbyCommand
(
redisClient
*
c
);
void
incrbyfloatCommand
(
redisClient
*
c
);
void
selectCommand
(
redisClient
*
c
);
void
randomkeyCommand
(
redisClient
*
c
);
void
keysCommand
(
redisClient
*
c
);
...
...
src/t_string.c
浏览文件 @
5574b53e
#include "redis.h"
#include <math.h>
/* isnan(), isinf() */
/*-----------------------------------------------------------------------------
* String Commands
...
...
@@ -382,6 +383,31 @@ void decrbyCommand(redisClient *c) {
incrDecrCommand
(
c
,
-
incr
);
}
void
incrbyfloatCommand
(
redisClient
*
c
)
{
long
double
incr
,
value
;
robj
*
o
,
*
new
;
o
=
lookupKeyWrite
(
c
->
db
,
c
->
argv
[
1
]);
if
(
o
!=
NULL
&&
checkType
(
c
,
o
,
REDIS_STRING
))
return
;
if
(
getLongDoubleFromObjectOrReply
(
c
,
o
,
&
value
,
NULL
)
!=
REDIS_OK
||
getLongDoubleFromObjectOrReply
(
c
,
c
->
argv
[
2
],
&
incr
,
NULL
)
!=
REDIS_OK
)
return
;
value
+=
incr
;
if
(
isnan
(
value
)
||
isinf
(
value
))
{
addReplyError
(
c
,
"increment would produce NaN or Infinity"
);
return
;
}
new
=
createStringObjectFromLongDouble
(
value
);
if
(
o
)
dbOverwrite
(
c
->
db
,
c
->
argv
[
1
],
new
);
else
dbAdd
(
c
->
db
,
c
->
argv
[
1
],
new
);
signalModifiedKey
(
c
->
db
,
c
->
argv
[
1
]);
server
.
dirty
++
;
addReplyBulk
(
c
,
new
);
}
void
appendCommand
(
redisClient
*
c
)
{
size_t
totlen
;
robj
*
o
,
*
append
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录