msg.response.message.header.response.bodylen= htonl(bodysize);
msg.response.message.header.response.extlen= 4;
+ release_item(item);
return response_handler(cookie, header, (void*)&msg);
}
else if (opcode == PROTOCOL_BINARY_CMD_GET || opcode == PROTOCOL_BINARY_CMD_GETK)
uint64_t initial= ntohll(req->message.body.initial);
uint64_t delta= ntohll(req->message.body.delta);
uint32_t expiration= ntohl(req->message.body.expiration);
+ uint32_t flags= 0;
void *key= req->bytes + sizeof(req->bytes);
protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_SUCCESS;
+ uint64_t value= initial;
+
struct item *item= get_item(key, keylen);
- if (item == NULL)
- {
- item= create_item(key, keylen, NULL, sizeof(initial), 0, (time_t)expiration);
- if (item == NULL)
- {
- rval= PROTOCOL_BINARY_RESPONSE_ENOMEM;
- }
- else
- {
- memcpy(item->data, &initial, sizeof(initial));
- put_item(item);
- }
- }
- else
+ if (item != NULL)
{
if (header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENT ||
header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENTQ)
{
- (*(uint64_t*)item->data) += delta;
+ value= (*(uint64_t*)item->data) + delta;
}
else
{
if (delta > *(uint64_t*)item->data)
{
- *(uint64_t*)item->data= 0;
+ value= 0;
}
else
{
- *(uint64_t*)item->data -= delta;
+ value= *(uint64_t*)item->data - delta;
}
}
+ expiration= (uint32_t)item->exp;
+ flags= item->flags;
- struct item *nitem= create_item(key, keylen, NULL, sizeof(initial), 0, item->exp);
- if (item == NULL)
- {
- rval= PROTOCOL_BINARY_RESPONSE_ENOMEM;
- delete_item(key, keylen);
- }
- else
- {
- memcpy(nitem->data, item->data, item->size);
- delete_item(key, keylen);
- put_item(nitem);
- item = nitem;
- }
+ release_item(item);
+ delete_item(key, keylen);
+ }
+
+ item= create_item(key, keylen, NULL, sizeof(value), flags, (time_t)expiration);
+ if (item == NULL)
+ {
+ rval= PROTOCOL_BINARY_RESPONSE_ENOMEM;
+ }
+ else
+ {
+ memcpy(item->data, &value, sizeof(value));
+ put_item(item);
}
response.message.header.response.status= htons(rval);
response.message.body.value= ntohll((*(uint64_t*)item->data));
response.message.header.response.cas= ntohll(item->cas);
+ release_item(item);
if (header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENTQ ||
header->request.opcode == PROTOCOL_BINARY_CMD_DECREMENTQ)
{
void *val= (char*)key + keylen;
struct item *item= get_item(key, keylen);
- struct item *nitem;
+ struct item *nitem= NULL;
+
if (item == NULL)
{
rval= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
else if ((nitem= create_item(key, keylen, NULL, item->size + vallen,
item->flags, item->exp)) == NULL)
{
+ release_item(item);
rval= PROTOCOL_BINARY_RESPONSE_ENOMEM;
}
else
memcpy(nitem->data, val, vallen);
memcpy(((char*)(nitem->data)) + vallen, item->data, item->size);
}
+ release_item(item);
delete_item(key, keylen);
put_item(nitem);
+ cas= nitem->cas;
+ release_item(nitem);
+
if (header->request.opcode == PROTOCOL_BINARY_CMD_APPEND ||
header->request.opcode == PROTOCOL_BINARY_CMD_PREPEND)
{
.opcode= header->request.opcode,
.status= htons(rval),
.opaque= header->request.opaque,
- .cas= htonll(nitem->cas),
+ .cas= htonll(cas),
}
}
};
{
/* validate cas */
struct item* item= get_item(key, keylen);
- if (item != NULL && item->cas != ntohll(header->request.cas))
+ if (item != NULL)
{
- response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS);
- return response_handler(cookie, header, (void*)&response);
+ if (item->cas != ntohll(header->request.cas))
+ {
+ release_item(item);
+ response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS);
+ return response_handler(cookie, header, (void*)&response);
+ }
+ release_item(item);
}
}
delete_item(key, keylen);
struct item* item= create_item(key, keylen, data, datalen, flags, timeout);
- if (item == 0)
+ if (item == NULL)
{
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_ENOMEM);
}
if (header->request.opcode == PROTOCOL_BINARY_CMD_SET)
{
response.message.header.response.cas= htonll(item->cas);
+ release_item(item);
return response_handler(cookie, header, (void*)&response);
}
+ release_item(item);
+
return PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
if (item == NULL)
{
item= create_item(key, keylen, data, datalen, flags, timeout);
- if (item == 0)
+ if (item == NULL)
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_ENOMEM);
else
{
if (header->request.opcode == PROTOCOL_BINARY_CMD_ADD)
{
response.message.header.response.cas= htonll(item->cas);
+ release_item(item);
return response_handler(cookie, header, (void*)&response);
}
+ release_item(item);
return PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
}
else
{
+ release_item(item);
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS);
}
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_ENOENT);
else if (header->request.cas == 0 || ntohll(header->request.cas) == item->cas)
{
+ release_item(item);
delete_item(key, keylen);
item= create_item(key, keylen, data, datalen, flags, timeout);
- if (item == 0)
+ if (item == NULL)
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_ENOMEM);
else
{
if (header->request.opcode == PROTOCOL_BINARY_CMD_REPLACE)
{
response.message.header.response.cas= htonll(item->cas);
+ release_item(item);
return response_handler(cookie, header, (void*)&response);
}
+ release_item(item);
return PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
}
else
+ {
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS);
+ release_item(item);
+ }
return response_handler(cookie, header, (void*)&response);
}
{
put_item(item);
*cas= item->cas;
+ release_item(item);
}
}
else
else if ((nitem= create_item(key, keylen, NULL, item->size + vallen,
item->flags, item->exp)) == NULL)
{
+ release_item(item);
rval= PROTOCOL_BINARY_RESPONSE_ENOMEM;
}
else
{
memcpy(nitem->data, item->data, item->size);
memcpy(((char*)(nitem->data)) + item->size, val, vallen);
+ release_item(item);
delete_item(key, keylen);
put_item(nitem);
*result_cas= nitem->cas;
+ release_item(nitem);
}
return rval;
val= *(uint64_t*)item->data - delta;
expiration= (uint32_t)item->exp;
+ release_item(item);
delete_item(key, keylen);
}
put_item(item);
*result= val;
*result_cas= item->cas;
+ release_item(item);
}
return rval;
if (cas != 0)
{
struct item *item= get_item(key, keylen);
- if (item != NULL && item->cas != cas)
+ if (item != NULL)
{
- return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
+ if (item->cas != cas)
+ {
+ release_item(item);
+ return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
+ }
+ release_item(item);
}
}
return PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
- return response_handler(cookie, key, (uint16_t)keylen,
+ protocol_binary_response_status rc;
+ rc= response_handler(cookie, key, (uint16_t)keylen,
item->data, (uint32_t)item->size, item->flags,
item->cas);
+ release_item(item);
+ return rc;
}
static protocol_binary_response_status increment_handler(const void *cookie,
{
val= (*(uint64_t*)item->data) + delta;
expiration= (uint32_t)item->exp;
+ release_item(item);
delete_item(key, keylen);
}
put_item(item);
*result= val;
*result_cas= item->cas;
+ release_item(item);
}
return rval;
protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_SUCCESS;
struct item *item= get_item(key, keylen);
- struct item *nitem;
+ struct item *nitem= NULL;
+
if (item == NULL)
{
rval= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
{
memcpy(nitem->data, val, vallen);
memcpy(((char*)(nitem->data)) + vallen, item->data, item->size);
+ release_item(item);
+ item= NULL;
delete_item(key, keylen);
put_item(nitem);
*result_cas= nitem->cas;
}
+ if (item)
+ release_item(item);
+
+ if (nitem)
+ release_item(nitem);
+
return rval;
}
}
else if (cas == 0 || cas == item->cas)
{
+ release_item(item);
delete_item(key, keylen);
item= create_item(key, keylen, data, datalen, flags, (time_t)exptime);
if (item == 0)
{
put_item(item);
*result_cas= item->cas;
+ release_item(item);
}
}
else
{
rval= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
+ release_item(item);
}
return rval;
if (item != NULL && cas != item->cas)
{
/* Invalid CAS value */
+ release_item(item);
return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
}
}
{
put_item(item);
*result_cas= item->cas;
+ release_item(item);
}
return rval;