/
arithmetic.c
90 lines (81 loc) · 3.65 KB
/
arithmetic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010, 2011 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal.h"
/**
* Spool an arithmetic request
*
* @author Trond Norbye
* @todo add documentation
*/
LIBCOUCHBASE_API
libcouchbase_error_t libcouchbase_arithmetic(libcouchbase_t instance,
const void *command_cookie,
const void *key, size_t nkey,
int64_t delta, time_t exp,
int create, uint64_t initial)
{
return libcouchbase_arithmetic_by_key(instance, command_cookie, NULL, 0, key,
nkey, delta, exp, create, initial);
}
LIBCOUCHBASE_API
libcouchbase_error_t libcouchbase_arithmetic_by_key(libcouchbase_t instance,
const void *command_cookie,
const void *hashkey,
size_t nhashkey,
const void *key, size_t nkey,
int64_t delta, time_t exp,
int create, uint64_t initial)
{
libcouchbase_server_t *server;
protocol_binary_request_incr req;
int vb, idx;
/* we need a vbucket config before we can start getting data.. */
if (instance->vbucket_config == NULL) {
return libcouchbase_synchandler_return(instance, LIBCOUCHBASE_ETMPFAIL);
}
if (nhashkey == 0) {
nhashkey = nkey;
hashkey = key;
}
(void)vbucket_map(instance->vbucket_config, hashkey, nhashkey, &vb, &idx);
server = instance->servers + (size_t)idx;
memset(&req, 0, sizeof(req));
req.message.header.request.magic = PROTOCOL_BINARY_REQ;
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_INCREMENT;
req.message.header.request.keylen = ntohs((uint16_t)nkey);
req.message.header.request.extlen = 20;
req.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
req.message.header.request.vbucket = ntohs((uint16_t)vb);
req.message.header.request.bodylen = ntohl((uint32_t)(nkey + 20));
req.message.header.request.opaque = ++instance->seqno;
req.message.body.delta = ntohll((uint64_t)(delta));
req.message.body.initial = ntohll(initial);
req.message.body.expiration = ntohl((uint32_t)exp);
if (delta < 0) {
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_DECREMENT;
req.message.body.delta = ntohll((uint64_t)(delta * -1));
}
if (!create) {
memset(&req.message.body.expiration, 0xff,
sizeof(req.message.body.expiration));
}
libcouchbase_server_start_packet(server, command_cookie, req.bytes, sizeof(req.bytes));
libcouchbase_server_write_packet(server, key, nkey);
libcouchbase_server_end_packet(server);
libcouchbase_server_send_packets(server);
return libcouchbase_synchandler_return(instance, LIBCOUCHBASE_SUCCESS);
}