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
edda00b9
Commit
edda00b9
authored
Feb 07, 2015
by
antirez
Browse files
dict.c Rehashing visualization code snippet added to utils.
parent
05841a63
Changes
2
Hide whitespace changes
Inline
Side-by-side
utils/hashtable/README
0 → 100644
View file @
edda00b9
Hash table implementation related utilities.
rehashing.c
---
Visually show buckets in the two hash tables between rehashings. Also stress
test getRandomKeys() implementation, that may actually disappear from
Redis soon, however visualizaiton some code is reusable in new bugs
investigation.
Compile with:
cc -I ../../src/ rehashing.c ../../src/zmalloc.c ../../src/dict.c -o rehashing_test
utils/hashtable/rehashing.c
0 → 100644
View file @
edda00b9
#include "redis.h"
#include "dict.h"
void
_redisAssert
(
char
*
x
,
char
*
y
,
int
l
)
{
printf
(
"ASSERT: %s %s %d
\n
"
,
x
,
y
,
l
);
exit
(
1
);
}
unsigned
int
dictKeyHash
(
const
void
*
keyp
)
{
unsigned
long
key
=
(
unsigned
long
)
keyp
;
key
=
dictGenHashFunction
(
&
key
,
sizeof
(
key
));
key
+=
~
(
key
<<
15
);
key
^=
(
key
>>
10
);
key
+=
(
key
<<
3
);
key
^=
(
key
>>
6
);
key
+=
~
(
key
<<
11
);
key
^=
(
key
>>
16
);
return
key
;
}
int
dictKeyCompare
(
void
*
privdata
,
const
void
*
key1
,
const
void
*
key2
)
{
unsigned
long
k1
=
(
unsigned
long
)
key1
;
unsigned
long
k2
=
(
unsigned
long
)
key2
;
return
k1
==
k2
;
}
dictType
dictTypeTest
=
{
dictKeyHash
,
/* hash function */
NULL
,
/* key dup */
NULL
,
/* val dup */
dictKeyCompare
,
/* key compare */
NULL
,
/* key destructor */
NULL
/* val destructor */
};
void
showBuckets
(
dictht
ht
)
{
if
(
ht
.
table
==
NULL
)
{
printf
(
"NULL
\n
"
);
}
else
{
int
j
;
for
(
j
=
0
;
j
<
ht
.
size
;
j
++
)
{
printf
(
"%c"
,
ht
.
table
[
j
]
?
'1'
:
'0'
);
}
printf
(
"
\n
"
);
}
}
void
show
(
dict
*
d
)
{
int
j
;
if
(
d
->
rehashidx
!=
-
1
)
{
printf
(
"rhidx: "
);
for
(
j
=
0
;
j
<
d
->
rehashidx
;
j
++
)
printf
(
"."
);
printf
(
"|
\n
"
);
}
printf
(
"ht[0]: "
);
showBuckets
(
d
->
ht
[
0
]);
printf
(
"ht[1]: "
);
showBuckets
(
d
->
ht
[
1
]);
printf
(
"
\n
"
);
}
int
sortPointers
(
const
void
*
a
,
const
void
*
b
)
{
unsigned
long
la
,
lb
;
la
=
(
long
)
(
*
((
dictEntry
**
)
a
));
lb
=
(
long
)
(
*
((
dictEntry
**
)
b
));
return
la
-
lb
;
}
void
stressGetKeys
(
dict
*
d
,
int
times
)
{
int
j
;
dictEntry
**
des
=
zmalloc
(
sizeof
(
dictEntry
*
)
*
dictSize
(
d
));
for
(
j
=
0
;
j
<
times
;
j
++
)
{
int
requested
=
rand
()
%
(
dictSize
(
d
)
+
1
);
int
returned
=
dictGetRandomKeys
(
d
,
des
,
requested
);
if
(
requested
!=
returned
)
{
printf
(
"*** ERROR! Req: %d, Ret: %d
\n
"
,
requested
,
returned
);
exit
(
1
);
}
qsort
(
des
,
returned
,
sizeof
(
dictEntry
*
),
sortPointers
);
if
(
returned
>
1
)
{
int
i
;
for
(
i
=
0
;
i
<
returned
-
1
;
i
++
)
{
if
(
des
[
i
]
==
des
[
i
+
1
])
{
printf
(
"*** ERROR! Duplicated element detected
\n
"
);
exit
(
1
);
}
}
}
}
zfree
(
des
);
}
#define MAX1 120
#define MAX2 1000
int
main
(
void
)
{
dict
*
d
=
dictCreate
(
&
dictTypeTest
,
NULL
);
unsigned
long
i
;
srand
(
time
(
NULL
));
for
(
i
=
0
;
i
<
MAX1
;
i
++
)
{
dictAdd
(
d
,(
void
*
)
i
,
NULL
);
show
(
d
);
}
printf
(
"Size: %d
\n
"
,
(
int
)
dictSize
(
d
));
for
(
i
=
0
;
i
<
MAX1
;
i
++
)
{
dictDelete
(
d
,(
void
*
)
i
);
dictResize
(
d
);
show
(
d
);
}
dictRelease
(
d
);
d
=
dictCreate
(
&
dictTypeTest
,
NULL
);
printf
(
"Getkeys stress test
\n
"
);
for
(
i
=
0
;
i
<
MAX2
;
i
++
)
{
dictAdd
(
d
,(
void
*
)
i
,
NULL
);
stressGetKeys
(
d
,
100
);
}
for
(
i
=
0
;
i
<
MAX2
;
i
++
)
{
dictDelete
(
d
,(
void
*
)
i
);
dictResize
(
d
);
stressGetKeys
(
d
,
100
);
}
dictRelease
(
d
);
printf
(
"TEST PASSED!
\n
"
);
return
0
;
}
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