Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add gat command #781

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/ascii-protocol/ch04-command-key-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ END\r\n

mget ๋ช…๋ น์—์„œ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์œผ๋กœ ์ผ๋ถ€ key์— ๋Œ€ํ•ด์„œ๋งŒ ์ •์ƒ ์กฐํšŒํ•œ ํ›„ ์‹คํŒจํ•œ ๊ฒฝ์šฐ, ์ „์ฒด ์—ฐ์‚ฐ์„ ์„œ๋ฒ„ ์—๋Ÿฌ ์ฒ˜๋ฆฌํ•œ๋‹ค.

## gat/gats (get and touch)

gat, gats ๋ช…๋ น์€ item์„ ๊ฐ€์ ธ์˜ด๊ณผ ๋™์‹œ์— exptime์„ ์žฌ์„ค์ •ํ•œ๋‹ค.
์œ„ ๋‘ ๋ช…๋ น์€ item์ด collection ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ๋™์ž‘ํ•œ๋‹ค.

```
gat <exptime> <key>*\r\n
gats <exptime> <key>*\r\n
```

- \<key\>* - ๋Œ€์ƒ item์˜ key string ๋ณต์ˆ˜๊ฐœ์˜ key๋ฅผ ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
- \<exptime\> - ์žฌ์„ค์ •ํ•  expiretime ๊ฐ’ ์ด๋‹ค.

gat ๋ช…๋ น์ด ์ •์ƒ ์ˆ˜ํ–‰๋˜์—ˆ์„ ๊ฒฝ์šฐ, Response string ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค.

- key hit๋œ ์•„์ดํ…œ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅ
- key miss๋œ ์•„์ดํ…œ์€ ๋ณ„๋„ response ์—†์ด ์ƒ๋žต
- ์‘๋‹ต์˜ ๋์— "END\r\n" ์ถœ๋ ฅ

gats ๋ช…๋ น์˜ ๊ฒฝ์šฐ cas value ๋„ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค.

์‹คํŒจ์‹œ string์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

| Response String | ์„ค๋ช… |
|----------------------|------------------------ |
| "CLIENT_ERROR" | ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž˜๋ชป๋œ ์งˆ์˜๋ฅผ ํ–ˆ์Œ์„ ์˜๋ฏธ. ์ด์–ด ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ์˜ค๋ฅ˜์˜ ์›์ธ์„ ํŒŒ์•… ๊ฐ€๋Šฅ. ์˜ˆ) bad command line format
| "SERVER ERROR" | ์„œ๋ฒ„ ์ธก์˜ ์˜ค๋ฅ˜๋กœ ์กฐํšŒํ•˜์ง€ ๋ชปํ–ˆ์Œ์„ ์˜๋ฏธ. ์ด์–ด ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ์˜ค๋ฅ˜์˜ ์›์ธ์„ ํŒŒ์•… ๊ฐ€๋Šฅ. ์˜ˆ) out of memory writing get response

## deletion ๋ช…๋ น

delete ๋ช…๋ น์ด ์žˆ์œผ๋ฉฐ syntax๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
Expand Down
51 changes: 45 additions & 6 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -2977,7 +2977,7 @@ static char *get_suffix_buffer(conn *c)
}

static ENGINE_ERROR_CODE
process_get_single(conn *c, char *key, size_t nkey, bool return_cas)
process_get_single(conn *c, char *key, size_t nkey, bool return_cas, int64_t *exptime)
{
item *it;
char *cas_val = NULL;
Expand Down Expand Up @@ -3020,6 +3020,26 @@ process_get_single(conn *c, char *key, size_t nkey, bool return_cas)
cas_len = snprintf(cas_val, SUFFIX_SIZE, " %"PRIu64"\r\n", c->hinfo.cas);
}

/* if gat command touch exptime */
if (exptime != NULL) {
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved
ENGINE_ITEM_ATTR attr_id = ATTR_EXPIRETIME;
item_attr attr_data;

attr_data.exptime = realtime(*exptime);
ret = mc_engine.v1->setattr(mc_engine.v0, c, key, nkey,
&attr_id, 1, &attr_data, 0);
CONN_CHECK_AND_SET_EWOULDBLOCK(ret, c);
if (settings.detail_enabled) {
stats_prefix_record_setattr(key, nkey);
}
if (ret != ENGINE_SUCCESS) {
STATS_MISSES(c, setattr, key, nkey);
mc_engine.v1->release(mc_engine.v0, c, it);
return ENGINE_SUCCESS;
}
STATS_HITS(c, setattr, key, nkey);
}

/* Construct the response. Each hit adds three elements to the
* outgoing data list:
* VALUE <key> <falgs> <bytes>[ <cas>]\r\n" + "<data>\r\n"
Expand Down Expand Up @@ -3087,7 +3107,7 @@ static void process_mget_complete(conn *c, bool return_cas)
/* do get operation for each key */
for (int k = 0; k < c->coll_numkeys; k++) {
ret = process_get_single(c, key_tokens[k].value, key_tokens[k].length,
return_cas);
return_cas, NULL);
if (ret != ENGINE_SUCCESS) {
break; /* ret == ENGINE_ENOMEM*/
}
Expand Down Expand Up @@ -8345,11 +8365,22 @@ static void process_stats_command(conn *c, token_t *tokens, const size_t ntokens
}

/* ntokens is overwritten here... shrug.. */
static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens, bool return_cas)
static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens, bool return_cas, bool should_touch)
{
assert(c != NULL);

token_t *key_token = &tokens[KEY_TOKEN];
ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
int64_t exptime = 0;

if (should_touch) {
// For get and touch commands, use first token as exptime
if (!safe_strtoll(tokens[1].value, &exptime)) {
out_string(c, "CLIENT_ERROR invalid exptime argument");
return;
}
key_token++;
}

do {
while (key_token->length != 0) {
Expand All @@ -8358,7 +8389,7 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
}
/* do get operation for each key */
ret = process_get_single(c, key_token->value, key_token->length,
return_cas);
return_cas, should_touch ? &exptime : NULL);
if (ret != ENGINE_SUCCESS) {
break; /* ret == ENGINE_ENOMEM */
}
Expand Down Expand Up @@ -13124,11 +13155,19 @@ static void process_command_ascii(conn *c, char *command, int cmdlen)

if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "get") == 0))
{
process_get_command(c, tokens, ntokens, false);
process_get_command(c, tokens, ntokens, false, false);
}
else if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "gets") == 0))
{
process_get_command(c, tokens, ntokens, true);
process_get_command(c, tokens, ntokens, true, false);
}
else if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "gat") == 0))
{
process_get_command(c, tokens, ntokens, false, true);
}
else if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "gats") == 0))
{
process_get_command(c, tokens, ntokens, true, true);
}
else if ((ntokens == 4) && (strcmp(tokens[COMMAND_TOKEN].value, "mget") == 0))
{
Expand Down
76 changes: 76 additions & 0 deletions t/gat.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/perl

use strict;
use Test::More tests =>13;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;

my $engine = shift;
my $server = get_memcached($engine);
my $sock = $server->sock;

my $cmd;
my $val;
my $rst;
my $expire;

# Initialize
$cmd = "set key 0 0 5"; $val = "datum"; $rst = "STORED";
mem_cmd_is($sock, $cmd, $val, $rst);

# Success Cases
# key value
$cmd = "gat 1 key";
$rst = "VALUE key 0 5\n"
. "datum\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr key expiretime";
$rst = "ATTR expiretime=1\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);
# gats command
$cmd = "gats 1 key";
$rst = "VALUE key 0 5 1\n"
. "datum\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr key expiretime";
$rst = "ATTR expiretime=1\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);

# Fail Cases
# bad value
$cmd = "set key 0 0 5"; $val = "datum"; $rst = "STORED";
mem_cmd_is($sock, $cmd, $val, $rst);
$cmd = "gat str key"; $rst = "CLIENT_ERROR invalid exptime argument";
mem_cmd_is($sock, $cmd, "", $rst);
# exist key and not exist key
$cmd = "gat 1 key key1";
$rst = "VALUE key 0 5\n"
. "datum\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);
# not exist key
$expire = time() - 1;
$cmd = "gat $expire key";
$rst = "VALUE key 0 5\n"
. "datum\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "gat 1 key"; $rst = "END";
mem_cmd_is($sock, $cmd, "", $rst);
# collection type
$cmd = "lop create lkey 0 0 5"; $rst = "CREATED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "gat 1 lkey"; $rst = "END";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr lkey expiretime";
$rst = "ATTR expiretime=0\n"
. "END";
mem_cmd_is($sock, $cmd, "", $rst);

# after test
release_memcached($engine, $server);
Loading