c1ce2b830ad4c485776e5fb7e337eac7e09302c4
[awesomized/libmemcached] / lib / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9
10 #include "common.h"
11
12 static memcached_return memcached_send(memcached_st *ptr,
13 char *key, size_t key_length,
14 char *value, size_t value_length,
15 time_t expiration,
16 uint16_t flags,
17 char *verb)
18 {
19 size_t write_length;
20 ssize_t sent_length;
21 memcached_return rc;
22 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
23 unsigned int server_key;
24
25 assert(value);
26 assert(value_length);
27
28 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
29
30 rc= memcached_connect(ptr);
31 if (rc != MEMCACHED_SUCCESS)
32 return rc;
33
34 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
35
36 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
37 "%s %.*s %x %llu %zu\r\n", verb,
38 (int)key_length, key, flags,
39 (unsigned long long)expiration, value_length);
40 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
41 return MEMCACHED_WRITE_FAILURE;
42 if ((sent_length= send(ptr->hosts[server_key].fd, buffer, write_length, 0)) == -1)
43 return MEMCACHED_WRITE_FAILURE;
44 assert(write_length == sent_length);
45
46 if ((sent_length= send(ptr->hosts[server_key].fd, value, value_length, 0)) == -1)
47 return MEMCACHED_WRITE_FAILURE;
48 assert(value_length == sent_length);
49
50 if ((sent_length= send(ptr->hosts[server_key].fd, "\r\n", 2, 0)) == -1)
51 return MEMCACHED_WRITE_FAILURE;
52 assert(2 == sent_length);
53
54 sent_length= recv(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 0);
55
56 if (sent_length && buffer[0] == 'S') /* STORED */
57 return MEMCACHED_SUCCESS;
58 else if (write_length && buffer[0] == 'N') /* NOT_STORED */
59 return MEMCACHED_NOTSTORED;
60 else
61 return MEMCACHED_READ_FAILURE;
62 }
63
64 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
65 char *value, size_t value_length,
66 time_t expiration,
67 uint16_t flags)
68 {
69 memcached_return rc;
70 LIBMEMCACHED_MEMCACHED_SET_START();
71 rc= memcached_send(ptr, key, key_length, value, value_length,
72 expiration, flags, "set");
73 LIBMEMCACHED_MEMCACHED_SET_END();
74 return rc;
75 }
76
77 memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
78 char *value, size_t value_length,
79 time_t expiration,
80 uint16_t flags)
81 {
82 memcached_return rc;
83 LIBMEMCACHED_MEMCACHED_ADD_START();
84 rc= memcached_send(ptr, key, key_length, value, value_length,
85 expiration, flags, "add");
86 LIBMEMCACHED_MEMCACHED_ADD_END();
87 return rc;
88 }
89
90 memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length,
91 char *value, size_t value_length,
92 time_t expiration,
93 uint16_t flags)
94 {
95 memcached_return rc;
96 LIBMEMCACHED_MEMCACHED_REPLACE_START();
97 rc= memcached_send(ptr, key, key_length, value, value_length,
98 expiration, flags, "replace");
99 LIBMEMCACHED_MEMCACHED_REPLACE_END();
100 return rc;
101 }