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
cd5a96ee
Commit
cd5a96ee
authored
Mar 03, 2010
by
antirez
Browse files
zipampDel() implemented
parent
2f4d2242
Changes
1
Show whitespace changes
Inline
Side-by-side
zipmap.c
View file @
cd5a96ee
...
@@ -147,7 +147,7 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
...
@@ -147,7 +147,7 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
static
unsigned
char
*
zipmapLookupRaw
(
unsigned
char
*
zm
,
unsigned
char
*
key
,
unsigned
int
klen
,
unsigned
int
*
totlen
,
unsigned
int
*
freeoff
,
unsigned
int
*
freelen
)
{
static
unsigned
char
*
zipmapLookupRaw
(
unsigned
char
*
zm
,
unsigned
char
*
key
,
unsigned
int
klen
,
unsigned
int
*
totlen
,
unsigned
int
*
freeoff
,
unsigned
int
*
freelen
)
{
unsigned
char
*
p
=
zm
+
1
;
unsigned
char
*
p
=
zm
+
1
;
unsigned
int
l
;
unsigned
int
l
;
unsigned
int
reqfreelen
;
unsigned
int
reqfreelen
=
0
;
/* initialized just to prevent warning */
if
(
freelen
)
{
if
(
freelen
)
{
reqfreelen
=
*
freelen
;
reqfreelen
=
*
freelen
;
...
@@ -210,6 +210,15 @@ static unsigned int zipmapRawValueLength(unsigned char *p) {
...
@@ -210,6 +210,15 @@ static unsigned int zipmapRawValueLength(unsigned char *p) {
return
used
;
return
used
;
}
}
/* If 'p' points to a key, this function returns the total amount of
* bytes used to store this entry (entry = key + associated value + trailing
* free space if any). */
static
unsigned
int
zipmapRawEntryLength
(
unsigned
char
*
p
)
{
unsigned
int
l
=
zipmapRawKeyLength
(
p
);
return
l
+
zipmapRawValueLength
(
p
+
l
);
}
/* Set key to value, creating the key if it does not already exist. */
/* Set key to value, creating the key if it does not already exist. */
unsigned
char
*
zipmapSet
(
unsigned
char
*
zm
,
unsigned
char
*
key
,
unsigned
int
klen
,
unsigned
char
*
val
,
unsigned
int
vlen
)
{
unsigned
char
*
zipmapSet
(
unsigned
char
*
zm
,
unsigned
char
*
key
,
unsigned
int
klen
,
unsigned
char
*
val
,
unsigned
int
vlen
)
{
unsigned
int
oldlen
=
0
,
freeoff
=
0
,
freelen
;
unsigned
int
oldlen
=
0
,
freeoff
=
0
,
freelen
;
...
@@ -278,6 +287,23 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
...
@@ -278,6 +287,23 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
return
zm
;
return
zm
;
}
}
/* Remove the specified key. If 'deleted' is not NULL the pointed integer is
* set to 0 if the key was not found, to 1 if it was found and deleted. */
unsigned
char
*
zipmapDel
(
unsigned
char
*
zm
,
unsigned
char
*
key
,
unsigned
int
klen
,
int
*
deleted
)
{
unsigned
char
*
p
=
zipmapLookupRaw
(
zm
,
key
,
klen
,
NULL
,
NULL
,
NULL
);
if
(
p
)
{
unsigned
int
freelen
=
zipmapRawEntryLength
(
p
);
p
[
0
]
=
ZIPMAP_EMPTY
;
zipmapEncodeLength
(
p
+
1
,
freelen
);
zm
[
0
]
|=
ZIPMAP_STATUS_FRAGMENTED
;
if
(
deleted
)
*
deleted
=
1
;
}
else
{
if
(
deleted
)
*
deleted
=
0
;
}
return
zm
;
}
void
zipmapRepr
(
unsigned
char
*
p
)
{
void
zipmapRepr
(
unsigned
char
*
p
)
{
unsigned
int
l
;
unsigned
int
l
;
...
@@ -327,5 +353,7 @@ int main(void) {
...
@@ -327,5 +353,7 @@ int main(void) {
zipmapRepr
(
zm
);
zipmapRepr
(
zm
);
zm
=
zipmapSet
(
zm
,(
unsigned
char
*
)
"new"
,
3
,
(
unsigned
char
*
)
"xx"
,
2
);
zm
=
zipmapSet
(
zm
,(
unsigned
char
*
)
"new"
,
3
,
(
unsigned
char
*
)
"xx"
,
2
);
zipmapRepr
(
zm
);
zipmapRepr
(
zm
);
zm
=
zipmapDel
(
zm
,(
unsigned
char
*
)
"new"
,
3
,
NULL
);
zipmapRepr
(
zm
);
return
0
;
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