Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
b287c9bb
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,发现更多精彩内容 >>
提交
b287c9bb
编写于
3月 08, 2010
作者:
P
Pieter Noordhuis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use ZMERGE as starting point
上级
cbba7dd7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
120 addition
and
0 deletion
+120
-0
redis-cli.c
redis-cli.c
+2
-0
redis.c
redis.c
+103
-0
test-redis.tcl
test-redis.tcl
+15
-0
未找到文件。
redis-cli.c
浏览文件 @
b287c9bb
...
...
@@ -101,6 +101,8 @@ static struct redisCommand cmdTable[] = {
{
"zincrby"
,
4
,
REDIS_CMD_BULK
},
{
"zrem"
,
3
,
REDIS_CMD_BULK
},
{
"zremrangebyscore"
,
4
,
REDIS_CMD_INLINE
},
{
"zmerge"
,
-
3
,
REDIS_CMD_INLINE
},
{
"zmergeweighed"
,
-
4
,
REDIS_CMD_INLINE
},
{
"zrange"
,
-
4
,
REDIS_CMD_INLINE
},
{
"zrank"
,
3
,
REDIS_CMD_BULK
},
{
"zrangebyscore"
,
-
4
,
REDIS_CMD_INLINE
},
...
...
redis.c
浏览文件 @
b287c9bb
...
...
@@ -675,6 +675,8 @@ static void substrCommand(redisClient *c);
static
void
zrankCommand
(
redisClient
*
c
);
static
void
hsetCommand
(
redisClient
*
c
);
static
void
hgetCommand
(
redisClient
*
c
);
static
void
zmergeCommand
(
redisClient
*
c
);
static
void
zmergeweighedCommand
(
redisClient
*
c
);
/*================================= Globals ================================= */
...
...
@@ -722,6 +724,8 @@ static struct redisCommand cmdTable[] = {
{
"zincrby"
,
zincrbyCommand
,
4
,
REDIS_CMD_BULK
|
REDIS_CMD_DENYOOM
,
1
,
1
,
1
},
{
"zrem"
,
zremCommand
,
3
,
REDIS_CMD_BULK
,
1
,
1
,
1
},
{
"zremrangebyscore"
,
zremrangebyscoreCommand
,
4
,
REDIS_CMD_INLINE
,
1
,
1
,
1
},
{
"zmerge"
,
zmergeCommand
,
-
3
,
REDIS_CMD_INLINE
|
REDIS_CMD_DENYOOM
,
2
,
-
1
,
1
},
{
"zmergeweighed"
,
zmergeweighedCommand
,
-
4
,
REDIS_CMD_INLINE
|
REDIS_CMD_DENYOOM
,
2
,
-
2
,
2
},
{
"zrange"
,
zrangeCommand
,
-
4
,
REDIS_CMD_INLINE
,
1
,
1
,
1
},
{
"zrangebyscore"
,
zrangebyscoreCommand
,
-
4
,
REDIS_CMD_INLINE
,
1
,
1
,
1
},
{
"zcount"
,
zcountCommand
,
4
,
REDIS_CMD_INLINE
,
1
,
1
,
1
},
...
...
@@ -5325,6 +5329,105 @@ static void zremrangebyscoreCommand(redisClient *c) {
}
}
/* This command merges 2 or more zsets to a destination. When an element
* does not exist in a certain set, score 0 is assumed. The score for an
* element across sets is summed. */
static
void
zmergeGenericCommand
(
redisClient
*
c
,
int
readweights
)
{
int
i
,
j
,
zsetnum
;
dict
**
srcdict
;
double
*
weights
;
robj
*
dstkey
=
c
->
argv
[
1
],
*
dstobj
;
zset
*
dst
;
dictIterator
*
di
;
dictEntry
*
de
;
zsetnum
=
c
->
argc
-
2
;
if
(
readweights
)
{
/* force number of arguments to be even */
if
(
zsetnum
%
2
>
0
)
{
addReplySds
(
c
,
sdsnew
(
"-ERR wrong number of arguments for ZMERGEWEIGHED
\r\n
"
));
return
;
}
zsetnum
/=
2
;
}
if
(
!
zsetnum
)
{
addReply
(
c
,
shared
.
syntaxerr
);
return
;
}
srcdict
=
zmalloc
(
sizeof
(
dict
*
)
*
zsetnum
);
weights
=
zmalloc
(
sizeof
(
double
)
*
zsetnum
);
for
(
i
=
0
;
i
<
zsetnum
;
i
++
)
{
if
(
readweights
)
{
j
=
2
+
2
*
i
;
weights
[
i
]
=
strtod
(
c
->
argv
[
j
+
1
]
->
ptr
,
NULL
);
}
else
{
j
=
2
+
i
;
weights
[
i
]
=
1
.
0
;
}
robj
*
zsetobj
=
lookupKeyWrite
(
c
->
db
,
c
->
argv
[
j
]);
if
(
!
zsetobj
)
{
srcdict
[
i
]
=
NULL
;
}
else
{
if
(
zsetobj
->
type
!=
REDIS_ZSET
)
{
zfree
(
srcdict
);
zfree
(
weights
);
addReply
(
c
,
shared
.
wrongtypeerr
);
return
;
}
srcdict
[
i
]
=
((
zset
*
)
zsetobj
->
ptr
)
->
dict
;
}
}
dstobj
=
createZsetObject
();
dst
=
dstobj
->
ptr
;
for
(
i
=
0
;
i
<
zsetnum
;
i
++
)
{
if
(
!
srcdict
[
i
])
continue
;
di
=
dictGetIterator
(
srcdict
[
i
]);
while
((
de
=
dictNext
(
di
))
!=
NULL
)
{
/* skip key when already processed */
if
(
dictFind
(
dst
->
dict
,
dictGetEntryKey
(
de
))
!=
NULL
)
continue
;
double
*
score
=
zmalloc
(
sizeof
(
double
));
*
score
=
0
.
0
;
for
(
j
=
0
;
j
<
zsetnum
;
j
++
)
{
if
(
!
srcdict
[
j
])
continue
;
dictEntry
*
other
=
dictFind
(
srcdict
[
j
],
dictGetEntryKey
(
de
));
if
(
other
)
{
*
score
=
*
score
+
weights
[
j
]
*
(
*
(
double
*
)
dictGetEntryVal
(
other
));
}
}
robj
*
o
=
dictGetEntryKey
(
de
);
dictAdd
(
dst
->
dict
,
o
,
score
);
incrRefCount
(
o
);
/* added to dictionary */
zslInsert
(
dst
->
zsl
,
*
score
,
o
);
incrRefCount
(
o
);
/* added to skiplist */
}
dictReleaseIterator
(
di
);
}
deleteKey
(
c
->
db
,
dstkey
);
dictAdd
(
c
->
db
->
dict
,
dstkey
,
dstobj
);
incrRefCount
(
dstkey
);
addReplyLong
(
c
,
dst
->
zsl
->
length
);
server
.
dirty
++
;
zfree
(
srcdict
);
zfree
(
weights
);
}
static
void
zmergeCommand
(
redisClient
*
c
)
{
zmergeGenericCommand
(
c
,
0
);
}
static
void
zmergeweighedCommand
(
redisClient
*
c
)
{
zmergeGenericCommand
(
c
,
1
);
}
static
void
zrangeGenericCommand
(
redisClient
*
c
,
int
reverse
)
{
robj
*
o
;
int
start
=
atoi
(
c
->
argv
[
2
]
->
ptr
);
...
...
test-redis.tcl
浏览文件 @
b287c9bb
...
...
@@ -1462,6 +1462,21 @@ proc main {server port} {
list
[
$r
zremrangebyscore zset -inf +inf
]
[
$r
zrange zset 0 -1
]
}
{
5
{}}
test
{
ZMERGE basics
}
{
$r del zseta zsetb zsetc
$r zadd zseta 1 a
$r zadd zseta 2 b
$r zadd zseta 3 c
$r zadd zsetb 1 b
$r zadd zsetb 2 c
$r zadd zsetb 3 d
list
[
$r
zmerge zsetc zseta zsetb
]
[
$r
zrange zsetc 0 -1 withscores
]
}
{
4
{
a 1 b 3 d 3 c 5
}}
test
{
ZMERGEWEIGHED basics
}
{
list
[
$r
zmergeweighed zsetc zseta 2 zsetb 3
]
[
$r
zrange zsetc 0 -1 withscores
]
}
{
4
{
a 2 b 7 d 9 c 12
}}
test
{
SORT against sorted sets
}
{
$r del zset
$r zadd zset 1 a
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录