Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
923c6c19
Commit
923c6c19
authored
Jan 23, 2023
by
Vitaly Arbuzov
Browse files
Put DB iterator on stack
parent
88bc7687
Changes
6
Show whitespace changes
Inline
Side-by-side
src/aof.c
View file @
923c6c19
...
@@ -2239,8 +2239,9 @@ int rewriteAppendOnlyFileRio(rio *aof) {
...
@@ -2239,8 +2239,9 @@ int rewriteAppendOnlyFileRio(rio *aof) {
char
selectcmd
[]
=
"*2
\r\n
$6
\r\n
SELECT
\r\n
"
;
char
selectcmd
[]
=
"*2
\r\n
$6
\r\n
SELECT
\r\n
"
;
redisDb
*
db
=
server
.
db
+
j
;
redisDb
*
db
=
server
.
db
+
j
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
if
(
dictSize
(
d
)
==
0
)
continue
;
if
(
dictSize
(
d
)
==
0
)
continue
;
di
=
dictGetSafeIterator
(
d
);
di
=
dictGetSafeIterator
(
d
);
...
@@ -2317,7 +2318,6 @@ int rewriteAppendOnlyFileRio(rio *aof) {
...
@@ -2317,7 +2318,6 @@ int rewriteAppendOnlyFileRio(rio *aof) {
dictReleaseIterator
(
di
);
dictReleaseIterator
(
di
);
di
=
NULL
;
di
=
NULL
;
}
}
dbReleaseIterator
(
dbit
);
}
}
return
C_OK
;
return
C_OK
;
...
...
src/db.c
View file @
923c6c19
...
@@ -49,10 +49,10 @@ int expireIfNeeded(redisDb *db, robj *key, int flags);
...
@@ -49,10 +49,10 @@ int expireIfNeeded(redisDb *db, robj *key, int flags);
int
keyIsExpired
(
redisDb
*
db
,
robj
*
key
);
int
keyIsExpired
(
redisDb
*
db
,
robj
*
key
);
/* Returns next dictionary from the iterator, or NULL if iteration is complete. */
/* Returns next dictionary from the iterator, or NULL if iteration is complete. */
dict
*
dbNextDict
(
dbIterator
*
it
er
)
{
dict
*
dbNextDict
(
dbIterator
*
db
it
)
{
while
(
it
er
->
index
<
it
er
->
db
->
dict_count
-
1
)
{
while
(
db
it
->
index
<
db
it
->
db
->
dict_count
-
1
)
{
it
er
->
index
++
;
db
it
->
index
++
;
dict
*
d
=
it
er
->
db
->
dict
[
it
er
->
index
];
dict
*
d
=
db
it
->
db
->
dict
[
db
it
->
index
];
/* There is a single dictionary in non cluster mode,
/* There is a single dictionary in non cluster mode,
* in cluster mode return first non-empty sub-dictionary. */
* in cluster mode return first non-empty sub-dictionary. */
if
(
!
server
.
cluster_enabled
||
dictSize
(
d
)
>
0
)
return
d
;
if
(
!
server
.
cluster_enabled
||
dictSize
(
d
)
>
0
)
return
d
;
...
@@ -62,10 +62,8 @@ dict *dbNextDict(dbIterator *iter) {
...
@@ -62,10 +62,8 @@ dict *dbNextDict(dbIterator *iter) {
void
dbInitIteratorAt
(
dbIterator
*
dbit
,
redisDb
*
db
,
int
slot
)
{
void
dbInitIteratorAt
(
dbIterator
*
dbit
,
redisDb
*
db
,
int
slot
)
{
serverAssert
(
slot
==
0
||
server
.
cluster_enabled
);
serverAssert
(
slot
==
0
||
server
.
cluster_enabled
);
dbIterator
*
iter
=
zmalloc
(
sizeof
(
*
iter
));
dbit
->
db
=
db
;
iter
->
db
=
db
;
dbit
->
index
=
slot
-
1
;
/* Start one slot ahead, as dbNextDict increments index right away. */
iter
->
index
=
slot
-
1
;
/* Start one slot ahead, as dbNextDict increments index right away. */
return
iter
;
}
}
/* Returns DB iterator that can be used to iterate through sub-dictionaries.
/* Returns DB iterator that can be used to iterate through sub-dictionaries.
...
@@ -78,22 +76,18 @@ void dbInitIterator(dbIterator *dbit, redisDb *db) {
...
@@ -78,22 +76,18 @@ void dbInitIterator(dbIterator *dbit, redisDb *db) {
/* Returns next dictionary strictly after provided slot and updates slot id in the supplied reference. */
/* Returns next dictionary strictly after provided slot and updates slot id in the supplied reference. */
dict
*
dbGetNextUnvisitedSlot
(
redisDb
*
db
,
int
*
slot
)
{
dict
*
dbGetNextUnvisitedSlot
(
redisDb
*
db
,
int
*
slot
)
{
if
(
*
slot
<
db
->
dict_count
-
1
)
{
if
(
*
slot
<
db
->
dict_count
-
1
)
{
dbIterator
*
dbit
=
dbGetIteratorAt
(
db
,
*
slot
+
1
);
/* Scan on the current slot has already returned 0, find next non-empty dict. */
dbIterator
dbit
;
dict
*
dict
=
dbNextDict
(
dbit
);
dbInitIteratorAt
(
&
dbit
,
db
,
*
slot
+
1
);
/* Scan on the current slot has already returned 0, find next non-empty dict. */
dict
*
dict
=
dbNextDict
(
&
dbit
);
if
(
dict
!=
NULL
)
{
if
(
dict
!=
NULL
)
{
*
slot
=
dbit
->
index
;
*
slot
=
dbit
.
index
;
return
dict
;
return
dict
;
}
}
dbReleaseIterator
(
dbit
);
}
}
*
slot
=
-
1
;
*
slot
=
-
1
;
return
NULL
;
return
NULL
;
}
}
void
dbReleaseIterator
(
dbIterator
*
iter
)
{
zfree
(
iter
);
}
/* Update LFU when an object is accessed.
/* Update LFU when an object is accessed.
* Firstly, decrement the counter if the decrement time is reached.
* Firstly, decrement the counter if the decrement time is reached.
* Then logarithmically increment the counter, and update the access time. */
* Then logarithmically increment the counter, and update the access time. */
...
@@ -832,8 +826,9 @@ void keysCommand(client *c) {
...
@@ -832,8 +826,9 @@ void keysCommand(client *c) {
long
numkeys
=
0
;
long
numkeys
=
0
;
void
*
replylen
=
addReplyDeferredLen
(
c
);
void
*
replylen
=
addReplyDeferredLen
(
c
);
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
c
->
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
c
->
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
if
(
dictSize
(
d
)
==
0
)
continue
;
if
(
dictSize
(
d
)
==
0
)
continue
;
di
=
dictGetSafeIterator
(
d
);
di
=
dictGetSafeIterator
(
d
);
allkeys
=
(
pattern
[
0
]
==
'*'
&&
plen
==
1
);
allkeys
=
(
pattern
[
0
]
==
'*'
&&
plen
==
1
);
...
@@ -853,7 +848,6 @@ void keysCommand(client *c) {
...
@@ -853,7 +848,6 @@ void keysCommand(client *c) {
break
;}
break
;}
dictReleaseIterator
(
di
);
dictReleaseIterator
(
di
);
}
}
dbReleaseIterator
(
dbit
);
setDeferredArrayLen
(
c
,
replylen
,
numkeys
);
setDeferredArrayLen
(
c
,
replylen
,
numkeys
);
}
}
...
@@ -1154,22 +1148,22 @@ void dbsizeCommand(client *c) {
...
@@ -1154,22 +1148,22 @@ void dbsizeCommand(client *c) {
unsigned
long
long
int
dbSize
(
redisDb
*
db
)
{
unsigned
long
long
int
dbSize
(
redisDb
*
db
)
{
unsigned
long
long
size
=
0
;
unsigned
long
long
size
=
0
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
size
+=
dictSize
(
d
);
size
+=
dictSize
(
d
);
}
}
dbReleaseIterator
(
dbit
);
return
size
;
return
size
;
}
}
unsigned
long
dbSlots
(
redisDb
*
db
)
{
unsigned
long
dbSlots
(
redisDb
*
db
)
{
unsigned
long
slots
=
0
;
unsigned
long
slots
=
0
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
slots
+=
dictSlots
(
d
);
slots
+=
dictSlots
(
d
);
}
}
dbReleaseIterator
(
dbit
);
return
slots
;
return
slots
;
}
}
...
@@ -1843,12 +1837,12 @@ int expireIfNeeded(redisDb *db, robj *key, int flags) {
...
@@ -1843,12 +1837,12 @@ int expireIfNeeded(redisDb *db, robj *key, int flags) {
void
expandDb
(
const
redisDb
*
db
,
uint64_t
db_size
)
{
void
expandDb
(
const
redisDb
*
db
,
uint64_t
db_size
)
{
if
(
server
.
cluster_enabled
)
{
if
(
server
.
cluster_enabled
)
{
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
((
redisDb
*
)
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
(
redisDb
*
)
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
/* We don't exact number of keys that would fall into each slot, but we can approximate it, assuming even distribution. */
/* We don't exact number of keys that would fall into each slot, but we can approximate it, assuming even distribution. */
dictExpand
(
d
,
(
db_size
/
server
.
cluster
->
myself
->
numslots
));
dictExpand
(
d
,
(
db_size
/
server
.
cluster
->
myself
->
numslots
));
}
}
dbReleaseIterator
(
dbit
);
}
else
{
}
else
{
dictExpand
(
db
->
dict
[
0
],
db_size
);
dictExpand
(
db
->
dict
[
0
],
db_size
);
}
}
...
@@ -2599,8 +2593,9 @@ void dbGetStats(char *buf, size_t bufsize, redisDb *db) {
...
@@ -2599,8 +2593,9 @@ void dbGetStats(char *buf, size_t bufsize, redisDb *db) {
dictStats
*
mainHtStats
=
NULL
;
dictStats
*
mainHtStats
=
NULL
;
dictStats
*
rehashHtStats
=
NULL
;
dictStats
*
rehashHtStats
=
NULL
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
dictStats
*
stats
=
dictGetStatsHt
(
d
,
0
);
dictStats
*
stats
=
dictGetStatsHt
(
d
,
0
);
if
(
!
mainHtStats
)
mainHtStats
=
stats
;
if
(
!
mainHtStats
)
mainHtStats
=
stats
;
else
{
else
{
...
@@ -2616,7 +2611,6 @@ void dbGetStats(char *buf, size_t bufsize, redisDb *db) {
...
@@ -2616,7 +2611,6 @@ void dbGetStats(char *buf, size_t bufsize, redisDb *db) {
}
}
}
}
}
}
dbReleaseIterator
(
dbit
);
l
=
dictGetStatsMsg
(
buf
,
bufsize
,
mainHtStats
);
l
=
dictGetStatsMsg
(
buf
,
bufsize
,
mainHtStats
);
dictFreeStats
(
mainHtStats
);
dictFreeStats
(
mainHtStats
);
buf
+=
l
;
buf
+=
l
;
...
...
src/debug.c
View file @
923c6c19
...
@@ -288,8 +288,9 @@ void computeDatasetDigest(unsigned char *final) {
...
@@ -288,8 +288,9 @@ void computeDatasetDigest(unsigned char *final) {
redisDb
*
db
=
server
.
db
+
j
;
redisDb
*
db
=
server
.
db
+
j
;
int
hasEntries
=
0
;
int
hasEntries
=
0
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
if
(
dictSize
(
d
)
==
0
)
continue
;
if
(
dictSize
(
d
)
==
0
)
continue
;
hasEntries
=
1
;
hasEntries
=
1
;
di
=
dictGetSafeIterator
(
d
);
di
=
dictGetSafeIterator
(
d
);
...
@@ -314,7 +315,6 @@ void computeDatasetDigest(unsigned char *final) {
...
@@ -314,7 +315,6 @@ void computeDatasetDigest(unsigned char *final) {
}
}
dictReleaseIterator
(
di
);
dictReleaseIterator
(
di
);
}
}
dbReleaseIterator
(
dbit
);
if
(
hasEntries
)
{
if
(
hasEntries
)
{
/* hash the DB id, so the same dataset moved in a different DB will lead to a different digest */
/* hash the DB id, so the same dataset moved in a different DB will lead to a different digest */
aux
=
htonl
(
j
);
aux
=
htonl
(
j
);
...
...
src/dict.c
View file @
923c6c19
...
@@ -1101,7 +1101,7 @@ unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count) {
...
@@ -1101,7 +1101,7 @@ unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count) {
/* Reallocate the dictEntry, key and value allocations in a bucket using the
/* Reallocate the dictEntry, key and value allocations in a bucket using the
* provided allocation functions in order to defrag them. */
* provided allocation functions in order to defrag them. */
static
void
dictDefragBucket
(
dict
*
d
,
dictEntry
**
bucketref
,
dictDefragFunctions
*
defragfns
)
{
static
void
dictDefragBucket
(
dictEntry
**
bucketref
,
dictDefragFunctions
*
defragfns
)
{
dictDefragAllocFunction
*
defragalloc
=
defragfns
->
defragAlloc
;
dictDefragAllocFunction
*
defragalloc
=
defragfns
->
defragAlloc
;
dictDefragAllocFunction
*
defragkey
=
defragfns
->
defragKey
;
dictDefragAllocFunction
*
defragkey
=
defragfns
->
defragKey
;
dictDefragAllocFunction
*
defragval
=
defragfns
->
defragVal
;
dictDefragAllocFunction
*
defragval
=
defragfns
->
defragVal
;
...
...
src/rdb.c
View file @
923c6c19
...
@@ -1322,8 +1322,9 @@ ssize_t rdbSaveDb(rio *rdb, int dbid, int rdbflags, long *key_counter) {
...
@@ -1322,8 +1322,9 @@ ssize_t rdbSaveDb(rio *rdb, int dbid, int rdbflags, long *key_counter) {
written
+=
res
;
written
+=
res
;
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
db
);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
db
);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
if
(
!
dictSize
(
d
))
continue
;
if
(
!
dictSize
(
d
))
continue
;
di
=
dictGetSafeIterator
(
d
);
di
=
dictGetSafeIterator
(
d
);
/* Iterate this DB writing every entry */
/* Iterate this DB writing every entry */
...
@@ -1359,7 +1360,6 @@ ssize_t rdbSaveDb(rio *rdb, int dbid, int rdbflags, long *key_counter) {
...
@@ -1359,7 +1360,6 @@ ssize_t rdbSaveDb(rio *rdb, int dbid, int rdbflags, long *key_counter) {
dictReleaseIterator
(
di
);
dictReleaseIterator
(
di
);
di
=
NULL
;
di
=
NULL
;
}
}
dbReleaseIterator
(
dbit
);
return
written
;
return
written
;
werr:
werr:
...
...
src/server.c
View file @
923c6c19
...
@@ -589,12 +589,12 @@ int htNeedsResize(dict *dict) {
...
@@ -589,12 +589,12 @@ int htNeedsResize(dict *dict) {
* we resize the hash table to save memory */
* we resize the hash table to save memory */
void
tryResizeHashTables
(
int
dbid
)
{
void
tryResizeHashTables
(
int
dbid
)
{
dict
*
d
;
dict
*
d
;
dbIterator
*
dbit
=
dbGetIterator
(
&
server
.
db
[
dbid
]);
dbIterator
dbit
;
while
((
d
=
dbNextDict
(
dbit
)))
{
dbInitIterator
(
&
dbit
,
&
server
.
db
[
dbid
]);
while
((
d
=
dbNextDict
(
&
dbit
)))
{
if
(
htNeedsResize
(
d
))
if
(
htNeedsResize
(
d
))
dictResize
(
d
);
dictResize
(
d
);
}
}
dbReleaseIterator
(
dbit
);
if
(
htNeedsResize
(
server
.
db
[
dbid
].
expires
))
if
(
htNeedsResize
(
server
.
db
[
dbid
].
expires
))
dictResize
(
server
.
db
[
dbid
].
expires
);
dictResize
(
server
.
db
[
dbid
].
expires
);
}
}
...
...
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