Changeset 619

Show
Ignore:
Timestamp:
10/03/07 20:45:31 (1 year ago)
Author:
plindner
Message:

Switch to unsigned 64-bit increment/decrement counters from Evan Miller and Dusgtin Sallings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/server/ChangeLog

    r618 r619  
    1212        * Fix for do_item_cachedump() which was returning 
    1313          an incorrect timestamp. 
     14         
     15        * Switch to unsigned 64-bit increment/decrement counters 
     16          from Evan Miller and Dusgtin Sallings. 
    1417 
    15182007-08-21 Paul Lindner <lindner@inuus.com> 
  • trunk/server/doc/protocol.txt

    r615 r619  
    263263Commands "incr" and "decr" are used to change data for some item 
    264264in-place, incrementing or decrementing it. The data for the item is 
    265 treated as decimal representation of a 32-bit unsigned integer. If the 
     265treated as decimal representation of a 64-bit unsigned integer. If the 
    266266current data value does not conform to such a representation, the 
    267267commands behave as if the value were 0. Also, the item must already 
     
    280280 
    281281- <value> is the amount by which the client wants to increase/decrease 
    282 the item. It is a decimal representation of a 32-bit unsigned integer. 
     282the item. It is a decimal representation of a 64-bit unsigned integer. 
    283283 
    284284The response will be one of: 
     
    290290 
    291291Note that underflow in the "decr" command is caught: if a client tries 
    292 to decrease the value below 0, the new value will be 0.  Overflow in the 
    293 "incr" command will wrap around the 32 bit mark. 
     292to decrease the value below 0, the new value will be 0.  Overflow in 
     293the "incr" command will wrap around the 64 bit mark. 
    294294 
    295295Note also that decrementing a number such that it loses length isn't 
  • trunk/server/memcached.c

    r615 r619  
    12591259} 
    12601260 
    1261 static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) { 
    1262     char temp[32]; 
     1261static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const bool incr) { 
     1262    char temp[sizeof("18446744073709551615")]; 
    12631263    item *it; 
    1264     unsigned int delta; 
     1264    int64_t delta; 
    12651265    char *key; 
    12661266    size_t nkey; 
     
    12891289    } 
    12901290 
    1291     delta = strtoul(tokens[2].value, NULL, 10); 
     1291    delta = strtoll(tokens[2].value, NULL, 10); 
    12921292 
    12931293    if(errno == ERANGE) { 
     
    13161316 * returns a response string to send back to the client. 
    13171317 */ 
    1318 char *do_add_delta(item *it, const int incr, const unsigned int delta, char *buf) { 
     1318char *do_add_delta(item *it, const bool incr, const int64_t delta, char *buf) { 
    13191319    char *ptr; 
    1320     uint32_t value; 
     1320    int64_t value; 
    13211321    int res; 
    13221322 
     
    13241324    while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++;    // BUG: can't be true 
    13251325 
    1326     value = strtoul(ptr, NULL, 10); 
     1326    value = strtoull(ptr, NULL, 10); 
    13271327 
    13281328    if(errno == ERANGE) { 
     
    13301330    } 
    13311331 
    1332     if (incr != 0
     1332    if (incr
    13331333        value += delta; 
    13341334    else { 
     
    13361336        else value -= delta; 
    13371337    } 
    1338     snprintf(buf, 32, "%u", value); 
     1338    sprintf(buf, "%llu", value); 
    13391339    res = strlen(buf); 
    13401340    if (res + 2 > it->nbytes) { /* need to realloc */ 
  • trunk/server/memcached.h

    r603 r619  
    223223char *do_defer_delete(item *item, time_t exptime); 
    224224void do_run_deferred_deletes(void); 
    225 char *do_add_delta(item *item, int incr, const unsigned int delta, char *buf); 
     225char *do_add_delta(item *item, const bool incr, const int64_t delta, char *buf); 
    226226int do_store_item(item *item, int comm); 
    227227conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp, struct event_base *base); 
     
    254254 
    255255/* Lock wrappers for cache functions that are called from main loop. */ 
    256 char *mt_add_delta(item *item, const int incr, const unsigned int delta, char *buf); 
     256char *mt_add_delta(item *item, const int incr, const int64_t delta, char *buf); 
    257257void mt_assoc_move_next_bucket(void); 
    258258conn *mt_conn_from_freelist(void); 
  • trunk/server/t/incrdecr.t

    r608 r619  
    22 
    33use strict; 
    4 use Test::More tests => 16
     4use Test::More tests => 17
    55use FindBin qw($Bin); 
    66use lib "$Bin/lib"; 
     
    3131is(scalar <$sock>, "0\r\n", "- 5 = 0"); 
    3232 
    33 print $sock "incr num ".(2**32-2)."\r\n"; 
    34 is(scalar <$sock>, (2**32-2)."\r\n", "+ ".(2**32-2)." = ".(2**32-2)); 
     33printf $sock "set num 0 0 10\r\n4294967296\r\n"; 
     34is(scalar <$sock>, "STORED\r\n", "stored 2**32"); 
    3535 
    3636print $sock "incr num 1\r\n"; 
    37 is(scalar <$sock>, (2**32-1)."\r\n", "+ 1 = ".(2**32-1)); 
     37is(scalar <$sock>, "4294967297\r\n", "4294967296 + 1 = 4294967297"); 
     38 
     39printf $sock "set num 0 0 %d\r\n18446744073709551615\r\n", length("18446744073709551615"); 
     40is(scalar <$sock>, "STORED\r\n", "stored 2**64-1"); 
    3841 
    3942print $sock "incr num 1\r\n"; 
    40 is(scalar <$sock>, "0\r\n", "+ 1 = 0"); 
     43is(scalar <$sock>, "0\r\n", "(2**64 - 1) + 1 = 0"); 
    4144 
    4245print $sock "decr bogus 5\r\n"; 
  • trunk/server/thread.c

    r608 r619  
    463463 * Does arithmetic on a numeric item value. 
    464464 */ 
    465 char *mt_add_delta(item *item, int incr, const unsigned int delta, char *buf) { 
     465char *mt_add_delta(item *item, int incr, const int64_t delta, char *buf) { 
    466466    char *ret; 
    467467