Skip to content

Commit

Permalink
FEATURE: touch command
Browse files Browse the repository at this point in the history
add touch command
  • Loading branch information
cheesecrust committed Jul 9, 2024
1 parent f1995f0 commit 91f0f86
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/ascii-protocol/ch10-command-item-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,23 @@ setattr <key> <name>=<value> [<name>=<value> ...]\r\n
| "ATTR_ERROR not found" | 인자로 지정한 attribute가 존재하지 않거나 해당 item 유형에서 지원되지 않는 attribute임.
| "ATTR_ERROR bad value" | 해당 attribute에 대해 새로 변경하고자 하는 value가 allowed value가 아님.
| "CLIENT_ERROR bad command line format" | protocol syntax 틀림


## touch (Key의 expire time 변경)

key 의 expire time 을 item 을 fetch 하지 않고 재설정하는 명령은 아래와 같다. 이 명령은 key value 타입과 collection 타입의 key에 모두 적용된다.

```
touch <key> <exptime> [noreply]\r\n
```

- \<key\> - 대상 item의 key string
- \<exptime\> - 재설정할 expire time 값

이 명령의 response string과 그 의미는 아래와 같다.

| Response String | 설명 |
|-----------------------------------------|------------------------ |
| "TOUCHED" | 성공
| "NOT_FOUND" | key miss
| "CLIENT_ERROR bad value" | expire time 값이 올바르지 않은 경우
51 changes: 51 additions & 0 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -13085,6 +13085,53 @@ static void process_setattr_command(conn *c, token_t *tokens, const size_t ntoke
}
}

static void process_touch_command(conn *c, token_t *tokens, const size_t ntokens)
{
assert(c != NULL);
assert(c->ewouldblock == false);
char *key = tokens[KEY_TOKEN].value;
size_t nkey = tokens[KEY_TOKEN].length;

if (nkey > KEY_MAX_LENGTH) {
out_string(c, "CLIENT_ERROR bad command line format");
return;
}

ENGINE_ERROR_CODE ret;
item_attr attr_data;
ENGINE_ITEM_ATTR attr_id = ATTR_EXPIRETIME;
int64_t exptime;

set_noreply_maybe(c, tokens, ntokens);

if (! safe_strtoll(tokens[KEY_TOKEN+1].value, &exptime)) {
ret = ENGINE_EBADVALUE;
} else {
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);
}
}

switch (ret) {
case ENGINE_SUCCESS:
STATS_HITS(c, setattr, key, nkey);
out_string(c, "TOUCHED");
break;
case ENGINE_KEY_ENOENT:
STATS_MISSES(c, setattr, key, nkey);
out_string(c, "NOT_FOUND");
break;
default:
STATS_CMD_NOKEY(c, setattr);
if (ret == ENGINE_EBADVALUE) out_string(c, "CLIENT_ERROR bad value");
else handle_unexpected_errorcode_ascii(c, __func__, ret);
}
}

static void process_command_ascii(conn *c, char *command, int cmdlen)
{
/* One more token is reserved in tokens strucure
Expand Down Expand Up @@ -13206,6 +13253,10 @@ static void process_command_ascii(conn *c, char *command, int cmdlen)
{
process_config_command(c, tokens, ntokens);
}
else if ((ntokens >= 4 && ntokens <= 5) && (strcmp(tokens[COMMAND_TOKEN].value, "touch") == 0))
{
process_touch_command(c, tokens, ntokens);
}
#ifdef ENABLE_ZK_INTEGRATION
else if ((ntokens >= 3) && (strcmp(tokens[COMMAND_TOKEN].value, "zkensemble") == 0))
{
Expand Down
1 change: 1 addition & 0 deletions t/tlist/engine_default_b.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@
./t/nested_prefix.t
./t/keyscan.t
./t/prefixscan.t
./t/touch.t
1 change: 1 addition & 0 deletions t/tlist/engine_default_s.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@
./t/nested_prefix.t
./t/keyscan.t
./t/prefixscan.t
./t/touch.t
89 changes: 89 additions & 0 deletions t/touch.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/perl

use strict;
use Test::More tests => 22; # test case 개수 즉 mem_cmd_is 개수
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 3"; $val = "key"; $rst = "STORED";
mem_cmd_is($sock, $cmd, $val, $rst);
$cmd = "lop create list 0 0 1 error"; $rst = "CREATED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "sop create set 0 0 1 error"; $rst = "CREATED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "mop create map 0 0 1 error"; $rst = "CREATED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "bop create btree 0 0 1 error"; $rst = "CREATED";
mem_cmd_is($sock, $cmd, "", $rst);

# Success Cases
$cmd = "touch key 1"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr key expiretime";
$rst = "ATTR expiretime=1
END";
# list type
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "touch list 1"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr list expiretime";
$rst = "ATTR expiretime=1
END";
# set
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "touch set 1"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr set expiretime";
$rst = "ATTR expiretime=1
END";
# map
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "touch map 1"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr map expiretime";
$rst = "ATTR expiretime=1
END";
# btree
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "touch btree 1"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "getattr btree expiretime";
$rst = "ATTR expiretime=1
END";
mem_cmd_is($sock, $cmd, "", $rst);
# set with unix time and expired
$cmd = "set unix_time 0 0 4"; $val = "unix"; $rst = "STORED";
mem_cmd_is($sock, $cmd, $val, $rst);
$expire = time() - 1;
$cmd = "touch unix_time $expire"; $rst = "TOUCHED";
mem_cmd_is($sock, $cmd, "", $rst);
$cmd = "get unix_time"; $rst = "END";
mem_cmd_is($sock, $cmd, "", $rst);

# Fail Cases
# not exist key
$cmd = "touch not_exist 1"; $rst = "NOT_FOUND";
mem_cmd_is($sock, $cmd, "", $rst);
# bad value
$cmd = "set bad_value 0 0 3"; $val = "bad"; $rst = "STORED";
mem_cmd_is($sock, $cmd, $val, $rst);
$cmd = "touch bad_value str"; $rst = "CLIENT_ERROR bad value";
mem_cmd_is($sock, $cmd, "", $rst);

# Finalize
$cmd = "delete bad_value"; $rst = "DELETED";
mem_cmd_is($sock, $cmd, "", $rst);

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

0 comments on commit 91f0f86

Please sign in to comment.