Implement memcached_behavior_noreply
[awesomized/libmemcached] / libmemcached / common.h
1 /*
2 Common include file for libmemached
3 */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <ctype.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 #include <unistd.h>
19 #include <limits.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sys/un.h>
23 #include <netinet/tcp.h>
24
25 #ifdef TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 # else
32 # include <time.h>
33 # endif
34 #endif
35
36
37
38 #include <memcached.h>
39 #include "memcached_io.h"
40
41 #include "memcached/protocol_binary.h"
42 #include "libmemcached_config.h"
43
44 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
45
46 #define likely(x) if((x))
47 #define unlikely(x) if((x))
48
49 #else
50
51 #define likely(x) if(__builtin_expect(!!(x), 1))
52 #define unlikely(x) if(__builtin_expect((x), 0))
53 #endif
54
55 #include "libmemcached_probes.h"
56
57 #define MEMCACHED_BLOCK_SIZE 1024
58 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
59 #define SMALL_STRING_LEN 1024
60 #define HUGE_STRING_LEN 8196
61
62
63 typedef enum {
64 MEM_NO_BLOCK= (1 << 0),
65 MEM_TCP_NODELAY= (1 << 1),
66 MEM_REUSE_MEMORY= (1 << 2),
67 MEM_USE_MD5= (1 << 3),
68 /* 4 was once Ketama */
69 MEM_USE_CRC= (1 << 5),
70 MEM_USE_CACHE_LOOKUPS= (1 << 6),
71 MEM_SUPPORT_CAS= (1 << 7),
72 MEM_BUFFER_REQUESTS= (1 << 8),
73 MEM_USE_SORT_HOSTS= (1 << 9),
74 MEM_VERIFY_KEY= (1 << 10),
75 /* 11 used for weighted ketama */
76 MEM_KETAMA_WEIGHTED= (1 << 11),
77 MEM_BINARY_PROTOCOL= (1 << 12),
78 MEM_HASH_WITH_PREFIX_KEY= (1 << 13),
79 MEM_NOREPLY= (1 << 14)
80 } memcached_flags;
81
82 /* Hashing algo */
83 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
84 uint32_t hash_crc32(const char *data,
85 size_t data_len);
86 uint32_t hsieh_hash(const char *key, size_t key_length);
87 uint32_t murmur_hash(const char *key, size_t key_length);
88 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
89
90 memcached_return memcached_connect(memcached_server_st *ptr);
91 memcached_return memcached_response(memcached_server_st *ptr,
92 char *buffer, size_t buffer_length,
93 memcached_result_st *result);
94 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length);
95 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death);
96
97 #define memcached_server_response_increment(A) (A)->cursor_active++
98 #define memcached_server_response_decrement(A) (A)->cursor_active--
99 #define memcached_server_response_reset(A) (A)->cursor_active=0
100
101 memcached_return memcached_do(memcached_server_st *ptr, const void *commmand,
102 size_t command_length, uint8_t with_flush);
103 memcached_return memcached_version(memcached_st *ptr);
104 memcached_return value_fetch(memcached_server_st *ptr,
105 char *buffer,
106 memcached_result_st *result);
107 void server_list_free(memcached_st *ptr, memcached_server_st *servers);
108
109 memcached_return memcachd_key_test(char **keys, size_t *key_length,
110 unsigned int number_of_keys);
111
112 memcached_return run_distribution(memcached_st *ptr);
113
114 uint32_t generate_hash_value(const char *key, size_t key_length, memcached_hash hash_algorithm);
115
116 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
117 memcached_return memcached_server_remove(memcached_server_st *st_ptr);
118
119 extern uint64_t ntohll(uint64_t);
120 extern uint64_t htonll(uint64_t);
121
122 memcached_return memcached_purge(memcached_server_st *ptr);
123
124 static inline memcached_return memcached_validate_key_length(size_t key_length,
125 bool binary) {
126 unlikely (key_length == 0)
127 return MEMCACHED_BAD_KEY_PROVIDED;
128
129 if (binary)
130 {
131 unlikely (key_length > 0xffff)
132 return MEMCACHED_BAD_KEY_PROVIDED;
133 }
134 else
135 {
136 unlikely (key_length > 250)
137 return MEMCACHED_BAD_KEY_PROVIDED;
138 }
139
140 return MEMCACHED_SUCCESS;
141 }
142
143 #endif /* __COMMON_H__ */