Changeset 573446


Ignore:
Timestamp:
12/06/07 14:07:40 (4 years ago)
Author:
Tomash Brechko <tomash.brechko@…>
Branches:
master, ketama-compat
Children:
0acb6c
Parents:
7b1ed1
git-author:
Tomash Brechko <tomash.brechko@…> (12/04/07 12:23:24)
git-committer:
Tomash Brechko <tomash.brechko@…> (12/06/07 14:07:40)
Message:

Add incr and decr commands.

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • Fast.xs

    r77ace6 r573446  
    340340 
    341341 
     342void 
     343incr(memd, skey, ...) 
     344        Cache_Memcached_Fast *  memd 
     345        SV *                    skey 
     346    ALIAS: 
     347        decr  =  CMD_DECR 
     348    PROTOTYPE: $$;$ 
     349    PREINIT: 
     350        const char *key; 
     351        STRLEN key_len; 
     352        arith_type arg = 1, result; 
     353        int noreply, res; 
     354    PPCODE: 
     355        if (items > 2 && SvOK(ST(2))) 
     356          arg = SvUV(ST(2)); 
     357        key = SvPV(skey, key_len); 
     358        noreply = (GIMME_V == G_VOID); 
     359        res = client_arith(memd, ix, key, key_len, arg, &result, noreply); 
     360        if (! noreply && res == MEMCACHED_SUCCESS) 
     361          { 
     362            dXSTARG; 
     363 
     364            /* 
     365               NOTE: arith_type is at least 64 bit, but Perl UV is 32 
     366               bit. 
     367            */ 
     368            PUSHu(result); 
     369            XSRETURN(1); 
     370          } 
     371 
     372 
    342373bool 
    343374delete(memd, skey, ...) 
  • TODO

    refe05f r573446  
    2525 
    2626- Use better rounding in flush_all(). 
     27 
     28- Disable noreply in benchmark.pl before shipping. 
  • src/client.c

    re083b4 r573446  
    5050 
    5151 
     52struct arith_state 
     53{ 
     54  arith_type *result; 
     55}; 
     56 
     57 
     58static inline 
     59void 
     60arith_state_reset(struct arith_state *state, arith_type *result) 
     61{ 
     62  state->result = result; 
     63} 
     64 
     65 
    5266struct command_state; 
    5367typedef int (*parse_reply_func)(struct command_state *state); 
     
    99113  parse_reply_func parse_reply; 
    100114 
    101   struct value_state value; 
     115  union 
     116  { 
     117    struct value_state value; 
     118    struct arith_state arith; 
     119  }; 
    102120}; 
    103121 
     
    681699      return MEMCACHED_UNKNOWN; 
    682700    } 
     701} 
     702 
     703 
     704static 
     705int 
     706parse_arith_reply(struct command_state *state) 
     707{ 
     708  int res, match_count; 
     709  arith_type arith; 
     710 
     711  switch (state->match) 
     712    { 
     713    case MATCH_NOT_FOUND: 
     714      res = swallow_eol(state, 0, 1); 
     715 
     716      return (res == MEMCACHED_SUCCESS ? MEMCACHED_FAILURE : res); 
     717 
     718    default: 
     719      return MEMCACHED_UNKNOWN; 
     720 
     721    case MATCH_0: case MATCH_1: case MATCH_2: case MATCH_3: case MATCH_4: 
     722    case MATCH_5: case MATCH_6: case MATCH_7: case MATCH_8: case MATCH_9: 
     723      break; 
     724    } 
     725 
     726  --state->pos; 
     727 
     728  res = sscanf(state->pos, FMT_ARITH "%n", &arith, &match_count); 
     729  if (res != 1) 
     730    return MEMCACHED_UNKNOWN; 
     731 
     732  state->pos += match_count; 
     733 
     734  res = swallow_eol(state, 1, 1); 
     735  if (res != MEMCACHED_SUCCESS) 
     736    return res; 
     737 
     738  *state->arith.result = arith; 
     739 
     740  return MEMCACHED_SUCCESS; 
    683741} 
    684742 
     
    13031361 
    13041362int 
     1363client_arith(struct client *c, enum arith_cmd_e cmd, 
     1364             const char *key, size_t key_len, 
     1365             arith_type arg, arith_type *result, int noreply) 
     1366{ 
     1367  int use_noreply = (noreply && c->noreply); 
     1368  size_t request_size = 
     1369    (sizeof(struct iovec) * (c->prefix_len ? 4 : 3) 
     1370     + sizeof(" 18446744073709551616 noreply\r\n")); 
     1371  struct command_state *state; 
     1372  struct iovec *buf_iov; 
     1373  char *buf; 
     1374  int server_index, res; 
     1375 
     1376  client_reset_for_command(c); 
     1377 
     1378  server_index = client_get_server_index(c, key, key_len); 
     1379  if (server_index == -1) 
     1380    return MEMCACHED_CLOSED; 
     1381 
     1382  state = &c->servers[server_index].cmd_state; 
     1383  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, 
     1384                      (use_noreply ? NULL : parse_arith_reply)); 
     1385  arith_state_reset(&state->arith, result); 
     1386 
     1387  res = iov_buf_extend(state, request_size); 
     1388  if (res != MEMCACHED_SUCCESS) 
     1389    return res; 
     1390 
     1391  switch (cmd) 
     1392    { 
     1393    case CMD_INCR: 
     1394      iov_push(state, STR_WITH_LEN("incr ")); 
     1395      break; 
     1396 
     1397    case CMD_DECR: 
     1398      iov_push(state, STR_WITH_LEN("decr ")); 
     1399      break; 
     1400    } 
     1401  if (c->prefix_len) 
     1402    iov_push(state, c->prefix, c->prefix_len); 
     1403  iov_push(state, (void *) key, key_len); 
     1404 
     1405  res = push_key(state, 0); 
     1406  if (res != MEMCACHED_SUCCESS) 
     1407    return res; 
     1408 
     1409  buf_iov = &state->iov_buf[state->iov_count]; 
     1410  iov_push(state, NULL, 0); 
     1411  buf = (char *) &state->iov_buf[state->iov_count]; 
     1412  buf_iov->iov_base = buf; 
     1413  buf_iov->iov_len = sprintf(buf, " " FMT_ARITH "%s\r\n", arg, 
     1414                             (use_noreply ? " noreply" : "")); 
     1415 
     1416  return process_commands(c); 
     1417 
     1418} 
     1419 
     1420 
     1421int 
    13051422client_delete(struct client *c, const char *key, size_t key_len, 
    13061423              delay_type delay, int noreply) 
  • src/client.h

    r77ace6 r573446  
    1919enum set_cmd_e { CMD_SET, CMD_ADD, CMD_REPLACE, CMD_APPEND, CMD_PREPEND }; 
    2020 
     21enum arith_cmd_e { CMD_INCR, CMD_DECR }; 
     22 
    2123 
    2224typedef unsigned int flags_type; 
     
    3133typedef size_t value_size_type; 
    3234#define FMT_VALUE_SIZE "%zu" 
     35 
     36typedef unsigned long long arith_type; 
     37#define FMT_ARITH "%llu" 
    3338 
    3439 
     
    101106extern 
    102107int 
     108client_arith(struct client *c, enum arith_cmd_e cmd, 
     109             const char *key, size_t key_len, 
     110             arith_type arg, arith_type *result, int noreply); 
     111 
     112extern 
     113int 
    103114client_delete(struct client *c, const char *key, size_t key_len, 
    104115              delay_type delay, int noreply); 
  • src/reply.kw

    r99b316 r573446  
    2929VALUE 
    3030VERSION 
     31# incr and decr return non-negative number. 
     320 
     331 
     342 
     353 
     364 
     375 
     386 
     397 
     408 
     419 
  • t/Cache-Memcached-Fast.t

    r28082a r573446  
    66# change 'tests => 1' to 'tests => last_test_to_print'; 
    77 
    8 use Test::More tests => 35; 
     8use Test::More tests => 39; 
    99BEGIN { use_ok('Cache::Memcached::Fast') }; 
    1010 
     
    5959 
    6060 
     61$memd->set("arith", 10); 
     62is($memd->incr("arith", 5), 15); 
     63is($memd->decr("arith"), 14); 
     64is($memd->get("arith"), 14); 
     65 
     66 
    6167$res1 = $memd->get_multi("key_no_such_key", "key1", "key_no_such_key", 
    6268                         "key2", "key_no_such_key", "key_no_such_key", 
     
    8793   ok(1); 
    8894   ok(1); 
     95   ok(1); 
    8996} else { 
    9097    my $memd_noreply = Cache::Memcached::Fast->new({ 
     
    96103 
    97104    $memd_noreply->flush_all; 
     105 
    98106    $memd_noreply->add("k", "v"); 
    99107    $memd_noreply->set("k", "v"); 
     
    104112    $memd_noreply->delete("k"); 
    105113    is($memd_noreply->get("k"), undef); 
     114 
     115    $memd_noreply->set("arith", 10); 
     116    $memd_noreply->incr("arith", 5); 
     117    $memd_noreply->decr("arith"); 
     118    is($memd_noreply->get("arith"), 14); 
     119 
    106120    $memd_noreply->flush_all; 
    107121} 
Note: See TracChangeset for help on using the changeset viewer.