Restructuring for new memcached_result_st.
[m6w6/libmemcached] / include / memcached.h
1 /*
2 * Summary: interface for memcached server
3 * Description: main include file for libmemcached
4 *
5 * Copy: See Copyright for the status of this software.
6 *
7 * Author: Brian Aker
8 */
9
10 #ifndef __MEMCACHED_H__
11 #define __MEMCACHED_H__
12
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <sys/types.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 typedef struct memcached_st memcached_st;
22 typedef struct memcached_stat_st memcached_stat_st;
23 typedef struct memcached_result_st memcached_result_st;
24 typedef struct memcached_string_st memcached_string_st;
25 typedef struct memcached_server_st memcached_server_st;
26
27 #define MEMCACHED_DEFAULT_PORT 11211
28 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
29 #define SMALL_STRING_LEN 1024
30 #define HUGE_STRING_LEN 8196
31 #define MEMCACHED_MAX_KEY 251 /* We add one to have it null terminated */
32 #define MEMCACHED_MAX_BUFFER HUGE_STRING_LEN
33
34 typedef enum {
35 MEMCACHED_SUCCESS,
36 MEMCACHED_FAILURE,
37 MEMCACHED_HOST_LOCKUP_FAILURE,
38 MEMCACHED_CONNECTION_FAILURE,
39 MEMCACHED_CONNECTION_BIND_FAILURE,
40 MEMCACHED_WRITE_FAILURE,
41 MEMCACHED_READ_FAILURE,
42 MEMCACHED_UNKNOWN_READ_FAILURE,
43 MEMCACHED_PROTOCOL_ERROR,
44 MEMCACHED_CLIENT_ERROR,
45 MEMCACHED_SERVER_ERROR,
46 MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE,
47 MEMCACHED_DATA_EXISTS,
48 MEMCACHED_DATA_DOES_NOT_EXIST,
49 MEMCACHED_NOTSTORED,
50 MEMCACHED_STORED,
51 MEMCACHED_NOTFOUND,
52 MEMCACHED_MEMORY_ALLOCATION_FAILURE,
53 MEMCACHED_PARTIAL_READ,
54 MEMCACHED_SOME_ERRORS,
55 MEMCACHED_NO_SERVERS,
56 MEMCACHED_END,
57 MEMCACHED_DELETED,
58 MEMCACHED_VALUE,
59 MEMCACHED_STAT,
60 MEMCACHED_ERRNO,
61 MEMCACHED_FAIL_UNIX_SOCKET,
62 MEMCACHED_NOT_SUPPORTED,
63 MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */
64 } memcached_return;
65
66 typedef enum {
67 MEMCACHED_BEHAVIOR_NO_BLOCK,
68 MEMCACHED_BEHAVIOR_TCP_NODELAY,
69 MEMCACHED_BEHAVIOR_HASH,
70 MEMCACHED_BEHAVIOR_KETAMA,
71 MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE,
72 MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE,
73 } memcached_behavior;
74
75 typedef enum {
76 MEMCACHED_HASH_DEFAULT= 0,
77 MEMCACHED_HASH_MD5,
78 MEMCACHED_HASH_CRC,
79 MEMCACHED_HASH_FNV1_64,
80 MEMCACHED_HASH_FNV1A_64,
81 MEMCACHED_HASH_FNV1_32,
82 MEMCACHED_HASH_FNV1A_32,
83 MEMCACHED_HASH_KETAMA,
84 } memcached_hash;
85
86 typedef enum {
87 MEMCACHED_CONNECTION_UNKNOWN,
88 MEMCACHED_CONNECTION_TCP,
89 MEMCACHED_CONNECTION_UDP,
90 MEMCACHED_CONNECTION_UNIX_SOCKET,
91 } memcached_connection;
92
93 typedef enum {
94 MEMCACHED_NOT_ALLOCATED,
95 MEMCACHED_ALLOCATED,
96 } memcached_allocated;
97
98 struct memcached_server_st {
99 char *hostname;
100 unsigned int port;
101 int fd;
102 unsigned int stack_responses;
103 unsigned int cursor_active;
104 memcached_connection type;
105 };
106
107 struct memcached_stat_st {
108 unsigned int pid;
109 unsigned int uptime;
110 unsigned int threads;
111 time_t time;
112 char version[8];
113 unsigned int pointer_size;
114 unsigned int rusage_user;
115 unsigned int rusage_system;
116 unsigned int rusage_user_seconds;
117 unsigned int rusage_user_microseconds;
118 unsigned int rusage_system_seconds;
119 unsigned int rusage_system_microseconds;
120 unsigned int curr_items;
121 unsigned int total_items;
122 unsigned long long bytes;
123 unsigned int curr_connections;
124 unsigned int total_connections;
125 unsigned int connection_structures;
126 unsigned long long cmd_get;
127 unsigned long long cmd_set;
128 unsigned long long get_hits;
129 unsigned long long get_misses;
130 unsigned long long evictions;
131 unsigned long long bytes_read;
132 unsigned long long bytes_written;
133 unsigned int limit_maxbytes;
134 };
135
136 struct memcached_string_st {
137 memcached_st *root;
138 memcached_allocated is_allocated;
139 char *string;
140 char *end;
141 size_t current_size;
142 size_t block_size;
143 };
144
145 struct memcached_result_st {
146 memcached_allocated is_allocated;
147 memcached_st *root;
148 memcached_string_st key;
149 memcached_string_st value;
150 uint16_t flags;
151 uint64_t cas;
152 };
153
154 struct memcached_st {
155 memcached_allocated is_allocated;
156 memcached_server_st *hosts;
157 unsigned int number_of_hosts;
158 unsigned int cursor_server;
159 char read_buffer[MEMCACHED_MAX_BUFFER];
160 size_t read_buffer_length;
161 char *read_ptr;
162 char write_buffer[MEMCACHED_MAX_BUFFER];
163 size_t write_buffer_offset;
164 char connected;
165 int cached_errno;
166 unsigned long long flags;
167 int send_size;
168 int recv_size;
169 memcached_string_st result_buffer;
170 memcached_hash hash;
171 memcached_return warning; /* Future Use */
172 };
173
174 /* Public API */
175 memcached_st *memcached_create(memcached_st *ptr);
176 void memcached_free(memcached_st *ptr);
177 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr);
178
179 memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
180 time_t expiration);
181 memcached_return memcached_increment(memcached_st *ptr,
182 char *key, size_t key_length,
183 unsigned int offset,
184 unsigned int *value);
185 memcached_return memcached_decrement(memcached_st *ptr,
186 char *key, size_t key_length,
187 unsigned int offset,
188 unsigned int *value);
189 void memcached_stat_free(memcached_st *, memcached_stat_st *);
190 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error);
191 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
192 char *hostname, unsigned int port);
193 memcached_return memcached_flush(memcached_st *ptr, time_t expiration);
194 memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity);
195 void memcached_quit(memcached_st *ptr);
196 char *memcached_strerror(memcached_st *ptr, memcached_return rc);
197 memcached_return memcached_behavior_set(memcached_st *ptr, memcached_behavior flag, void *data);
198 unsigned long long memcached_behavior_get(memcached_st *ptr, memcached_behavior flag);
199
200 /* All of the functions for adding data to the server */
201 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
202 char *value, size_t value_length,
203 time_t expiration,
204 uint16_t flags);
205 memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
206 char *value, size_t value_length,
207 time_t expiration,
208 uint16_t flags);
209 memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length,
210 char *value, size_t value_length,
211 time_t expiration,
212 uint16_t flags);
213
214 /* Get functions */
215 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
216 size_t *value_length,
217 uint16_t *flags,
218 memcached_return *error);
219 memcached_return memcached_mget(memcached_st *ptr,
220 char **keys, size_t *key_length,
221 unsigned int number_of_keys);
222 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
223 size_t *value_length, uint16_t *flags,
224 memcached_return *error);
225 memcached_result_st *memcached_fetch_object(memcached_st *ptr, memcached_return *error);
226
227 /* Server Public functions */
228 #define memcached_server_count(A) A->number_of_hosts
229 #define memcached_server_name(A,B) B.hostname
230 #define memcached_server_port(A,B) B.port
231 #define memcached_server_list(A) A->hosts
232 #define memcached_server_response_count(A,B) A->hosts[B].stack_responses
233
234 memcached_return memcached_server_add_unix_socket(memcached_st *ptr,
235 char *filename);
236 memcached_return memcached_server_add(memcached_st *ptr, char *hostname,
237 unsigned int port);
238 void memcached_server_list_free(memcached_server_st *ptr);
239 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list);
240
241 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
242 char *hostname, unsigned int port,
243 memcached_return *error);
244 unsigned int memcached_server_list_count(memcached_server_st *ptr);
245 memcached_server_st *memcached_servers_parse(char *server_strings);
246
247 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
248 char *key, memcached_return *error);
249 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
250 memcached_return *error);
251
252 /* Result Struct */
253 #define memcache_result_key_value(A) memcached_string_value(A->key)
254 #define memcache_result_key_length(A) memcached_string_length(A->key)
255 #define memcache_result_result_value(A) memcached_string_value(A->value)
256 #define memcache_result_result_length(A) memcached_string_length(A->value)
257 #define memcache_result_flags(A) A->flags
258 #define memcache_result_cas(A) A->cas
259
260 /* Some personal debugging functions */
261 #ifdef HAVE_DEBUG
262 #define WATCHPOINT fprintf(stderr, "\nWATCHPOINT %s:%d (%s)\n", __FILE__, __LINE__,__func__);fflush(stdout);
263 #define WATCHPOINT_ERROR(A) fprintf(stderr, "\nWATCHPOINT %s:%d %s\n", __FILE__, __LINE__, memcached_strerror(NULL, A));fflush(stdout);
264 #define WATCHPOINT_STRING(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__,A);fflush(stdout);
265 #define WATCHPOINT_NUMBER(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %zu\n", __FILE__, __LINE__,__func__,(size_t)(A));fflush(stdout);
266 #define WATCHPOINT_ERRNO(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__, strerror(A));A= 0;fflush(stdout);
267 #define WATCHPOINT_ASSERT(A) assert((A));
268 #else
269 /*
270 #define WATCHPOINT { 1; };
271 #define WATCHPOINT_ERROR(A) { 1; };
272 #define WATCHPOINT_STRING(A) { 1; };
273 #define WATCHPOINT_NUMBER(A) { 1; };
274 #define WATCHPOINT_ERRNO(A) { 1; };
275 #define WATCHPOINT_ASSERT(A) { 1; };
276 */
277 #define WATCHPOINT
278 #define WATCHPOINT_ERROR(A)
279 #define WATCHPOINT_STRING(A)
280 #define WATCHPOINT_NUMBER(A)
281 #define WATCHPOINT_ERRNO(A)
282 #define WATCHPOINT_ASSERT(A)
283 #endif
284
285
286 #ifdef __cplusplus
287 }
288 #endif
289
290 #endif /* __MEMCACHED_H__ */