Skip to content

Commit

Permalink
let dump command take a prefix as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Yao Yue committed Oct 27, 2018
1 parent 0d05b8d commit fd3cd70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/server/twemcache/admin/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ _admin_stats(struct response *rsp, struct request *req)
static void
_key_dump(struct response *rsp, struct request *req)
{
if (item_dump() == true) {
if (req->arg.len > 0) { /* skip initial space */
req->arg.len--;
req->arg.data++;
}
log_info("dump keys with prefix %.*s", req->arg.len, req->arg.data);

if (item_dump(&req->arg) == true) {
rsp->type = RSP_OK;
} else {
rsp->type = RSP_GENERIC;
Expand Down
20 changes: 12 additions & 8 deletions src/storage/slab/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ item_flush(void)
log_info("all keys flushed at %"PRIu32, flush_at);
}

/* this dumps all keys (matching a prefix if given) regardless of expiry status */
void
item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix)
{
Expand Down Expand Up @@ -415,7 +416,7 @@ item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix)
}

bool
item_dump(void)
item_dump(struct bstring *prefix)
{
int fd;
uint32_t nbucket = HASHSIZE(hash_table->hash_power);
Expand All @@ -433,13 +434,16 @@ item_dump(void)
struct item *it;

SLIST_FOREACH(it, entry, i_sle) {
if (write(fd, item_key(it), it->klen) < it->klen) {
log_error("write error, aborting at hash bucket %"PRIu32, i);
return false;
}
if (write(fd, CRLF, CRLF_LEN) < CRLF_LEN) {
log_error("write error, aborting at hash bucket %"PRIu32, i);
return false;
if (it->klen >= prefix->len &&
cc_bcmp(prefix->data, item_key(it), prefix->len) == 0) {
if (write(fd, item_key(it), it->klen) < it->klen) {
log_error("write error, aborting at hash bucket %"PRIu32, i);
return false;
}
if (write(fd, CRLF, CRLF_LEN) < CRLF_LEN) {
log_error("write error, aborting at hash bucket %"PRIu32, i);
return false;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/storage/slab/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,7 @@ bool item_delete(const struct bstring *key);
/* flush the cache */
void item_flush(void);

/* this surveys all keys (matching a prefix if given) regardless of expiry status */
void item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix);
bool item_dump(void);
/* this dumps all keys (matching a prefix if given) regardless of expiry status */
bool item_dump(struct bstring *prefix);

0 comments on commit fd3cd70

Please sign in to comment.