Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
e74825c2
Commit
e74825c2
authored
Mar 04, 2010
by
Pieter Noordhuis
Browse files
use rank to find starting point for ZRANGE and ZREVRANGE
parent
27b0ccca
Changes
1
Show whitespace changes
Inline
Side-by-side
redis.c
View file @
e74825c2
...
...
@@ -5081,6 +5081,26 @@ static unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
return
0
;
}
/* Finds an element by its rank. The rank argument needs to be 1-based. */
zskiplistNode
*
zslGetElementByRank
(
zskiplist
*
zsl
,
unsigned
long
rank
)
{
zskiplistNode
*
x
;
unsigned
long
traversed
=
0
;
int
i
;
x
=
zsl
->
header
;
for
(
i
=
zsl
->
level
-
1
;
i
>=
0
;
i
--
)
{
while
(
x
->
forward
[
i
]
&&
(
traversed
+
x
->
span
[
i
])
<=
rank
)
{
traversed
+=
x
->
span
[
i
];
x
=
x
->
forward
[
i
];
}
if
(
traversed
==
rank
)
{
return
x
;
}
}
return
NULL
;
}
/* The actual Z-commands implementations */
/* This generic command implements both ZADD and ZINCRBY.
...
...
@@ -5282,13 +5302,9 @@ static void zrangeGenericCommand(redisClient *c, int reverse) {
/* Return the result in form of a multi-bulk reply */
if
(
reverse
)
{
ln
=
zsl
->
tail
;
while
(
start
--
)
ln
=
ln
->
backward
;
ln
=
zslGetElementByRank
(
zsl
,
llen
-
start
);
}
else
{
ln
=
zsl
->
header
->
forward
[
0
];
while
(
start
--
)
ln
=
ln
->
forward
[
0
];
ln
=
zslGetElementByRank
(
zsl
,
start
+
1
);
}
addReplySds
(
c
,
sdscatprintf
(
sdsempty
(),
"*%d
\r\n
"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment