Changeset 674

Show
Ignore:
Timestamp:
12/11/07 03:29:00 (1 year ago)
Author:
dormando
Message:

Binary server CAS command by Dustin Sallings and Chris Goffinet

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/binary/server/memcached.c

    r673 r674  
    10111011 
    10121012static void complete_update_bin(conn *c) { 
     1013    int eno=-1, ret=0; 
    10131014    assert(c != NULL); 
    10141015 
     
    10241025    *(ITEM_data(it) + it->nbytes - 1) = '\n'; 
    10251026 
    1026     if (store_item(it, c->item_comm)) { 
    1027         /* Stored */ 
    1028         write_bin_response(c, NULL, 0); 
    1029     } else { 
    1030         /* not Stored */ 
    1031         int eno=-1; 
    1032         if(c->item_comm == NREAD_ADD) { 
    1033             eno=ERR_EXISTS; 
    1034         } else if(c->item_comm == NREAD_REPLACE) { 
    1035             eno=ERR_NOT_FOUND; 
    1036         } else { 
    1037             eno=ERR_NOT_STORED; 
    1038         } 
    1039         write_bin_error(c, eno, 0); 
     1027    switch (store_item(it, c->item_comm)) { 
     1028        case 1: 
     1029            /* Stored */ 
     1030            write_bin_response(c, NULL, 0); 
     1031            break; 
     1032        case 2: 
     1033            write_bin_error(c, ERR_EXISTS, 0); 
     1034            break; 
     1035        case 3: 
     1036            write_bin_error(c, ERR_NOT_FOUND, 0); 
     1037            break; 
     1038        default: 
     1039            if(c->item_comm == NREAD_ADD) { 
     1040                eno=ERR_EXISTS; 
     1041            } else if(c->item_comm == NREAD_REPLACE) { 
     1042                eno=ERR_NOT_FOUND; 
     1043            } else { 
     1044                eno=ERR_NOT_STORED; 
     1045            } 
     1046            write_bin_error(c, eno, 0); 
    10401047    } 
    10411048 
     
    10591066 
    10601067        /* the length has two unnecessary bytes, and then we write four more */ 
    1061         add_bin_header(c, 0, it->nbytes - 2 + 4); 
    1062         /* Flags and value */ 
     1068        add_bin_header(c, 0, it->nbytes - 2 + 4 + (c->cmd == CMD_GETS?8:0)); 
     1069        /* Flags */ 
    10631070        add_iov(c, flags, 4); 
     1071        /* if it's a gets, add the identifier */ 
     1072        if(c->cmd == CMD_GETS) { 
     1073            uint64_t* identifier; 
     1074            identifier=(uint64_t*)(c->wbuf + MIN_BIN_PKT_LENGTH + 4); 
     1075            *identifier=swap64((uint32_t)it->cas_id); 
     1076            add_iov(c, identifier, 8); 
     1077        } 
    10641078        /* bytes minus the CRLF */ 
    10651079        add_iov(c, ITEM_data(it), it->nbytes - 2); 
     
    11061120            bin_read_key(c, bin_reading_set_header, BIN_SET_HDR_LEN); 
    11071121            break; 
     1122        case CMD_CAS: 
     1123            bin_read_key(c, bin_reading_cas_header, BIN_CAS_HDR_LEN); 
     1124            break; 
     1125        case CMD_GETS: 
    11081126        case CMD_GETQ: 
    11091127        case CMD_GET: 
     
    11211139} 
    11221140 
    1123 static void process_bin_update(conn *c) { 
     1141static void process_bin_update(conn *c, bool cas) { 
    11241142    char *key; 
    11251143    int nkey; 
     
    11291147    item *it; 
    11301148    int comm; 
     1149    int hdrlen=cas ? BIN_CAS_HDR_LEN : BIN_SET_HDR_LEN; 
    11311150 
    11321151    assert(c != NULL); 
    11331152 
    1134     key=c->rbuf + BIN_SET_HDR_LEN
     1153    key=c->rbuf + hdrlen
    11351154    nkey=c->keylen; 
    11361155    key[nkey]=0x00; 
     
    11381157    flags = ntohl(*((int*)(c->rbuf))); 
    11391158    exptime = ntohl(*((int*)(c->rbuf + 4))); 
    1140     vlen = c->bin_header[2] - (nkey + BIN_SET_HDR_LEN); 
     1159    vlen = c->bin_header[2] - (nkey + hdrlen); 
     1160 
     1161    if(settings.verbose) { 
     1162        fprintf(stderr, "Value len is %d\n", vlen); 
     1163    } 
    11411164 
    11421165    if (settings.detail_enabled) { 
     
    11601183 
    11611184    it = item_alloc(key, nkey, flags, realtime(exptime), vlen+2); 
     1185    if(cas) { 
     1186        it->cas_id = (uint64_t)swap64(*((int64_t*)(c->rbuf + 8))); 
     1187    } 
    11621188 
    11631189    if (it == 0) { 
     
    11751201        case CMD_ADD: 
    11761202            c->item_comm = NREAD_ADD; 
     1203            break; 
     1204        case CMD_CAS: 
     1205            c->item_comm = NREAD_CAS; 
    11771206            break; 
    11781207        case CMD_SET: 
     
    12921321        switch(c->substate) { 
    12931322            case bin_reading_set_header: 
    1294                 process_bin_update(c); 
     1323                process_bin_update(c, false); 
     1324                break; 
     1325            case bin_reading_cas_header: 
     1326                process_bin_update(c, true); 
    12951327                break; 
    12961328            case bin_read_set_value: 
     
    13641396          do_item_replace(old_it, it); 
    13651397          stored = 1; 
    1366         } 
    1367         else 
    1368         { 
     1398        } else { 
     1399          if(settings.verbose > 1) { 
     1400            fprintf(stderr, "CAS:  failure: expected %llu, got %llu\n", 
     1401                                old_it->cas_id, it->cas_id); 
     1402          } 
    13691403          stored = 2; 
    13701404        } 
  • branches/binary/server/memcached.h

    r673 r674  
    4343/* flags:32, expiration:32 */ 
    4444#define BIN_SET_HDR_LEN 8 
     45/* Same as set, but with another 64 bits for the CAS identifier */ 
     46#define BIN_CAS_HDR_LEN (BIN_SET_HDR_LEN + 8) 
    4547/* incr:64, initial:64, expiration:32 */ 
    4648#define BIN_INCR_HDR_LEN 20 
     
    6365#define CMD_NOOP 9 
    6466#define CMD_VERSION 10 
     67 
     68#define CMD_GETS 50 
     69#define CMD_CAS 51 
    6570 
    6671#define ERR_UNKNOWN_CMD 0x81 
     
    182187    bin_no_state, 
    183188    bin_reading_set_header, 
     189    bin_reading_cas_header, 
    184190    bin_read_set_value, 
    185191    bin_reading_get_key,