3 static char *memcached_value_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
6 memcached_return
*error
,
8 unsigned int server_key
)
10 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
18 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
22 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
23 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
25 if (*error
== MEMCACHED_SUCCESS
)
30 string_ptr
+= 6; /* "VALUE " */
35 memset(key
, 0, MEMCACHED_MAX_KEY
);
36 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++)
42 else /* Skip characters */
43 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
45 if (end_ptr
== string_ptr
)
48 /* Flags fetch move past space */
50 if (end_ptr
== string_ptr
)
53 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
54 *flags
= (uint16_t)strtol(next_ptr
, &string_ptr
, 10);
56 if (end_ptr
== string_ptr
)
59 /* Length fetch move past space*/
61 if (end_ptr
== string_ptr
)
64 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
65 *value_length
= (size_t)strtoll(next_ptr
, &string_ptr
, 10);
67 if (end_ptr
== string_ptr
)
70 /* Skip past the \r\n */
73 if (end_ptr
< string_ptr
)
81 /* We add two bytes so that we can walk the \r\n */
82 value
= (char *)malloc(((*value_length
) +2) * sizeof(char));
87 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
91 read_length
= read(ptr
->hosts
[server_key
].fd
, value
, (*value_length
)+2);
93 if (read_length
!= (size_t)(*value_length
+ 2))
105 *error
= MEMCACHED_PARTIAL_READ
;
109 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
110 size_t *value_length
,
112 memcached_return
*error
)
115 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
116 unsigned int server_key
;
119 *error
= memcached_connect(ptr
);
121 if (*error
!= MEMCACHED_SUCCESS
)
124 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
126 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
127 (int)key_length
, key
);
128 if (*error
!= MEMCACHED_SUCCESS
)
131 if ((write(ptr
->hosts
[server_key
].fd
, buffer
, send_length
) == -1))
133 *error
= MEMCACHED_WRITE_FAILURE
;
137 return memcached_value_fetch(ptr
, key
, &key_length
, value_length
, flags
,
138 error
, 0, server_key
);
141 memcached_return
memcached_mget(memcached_st
*ptr
,
142 char **keys
, size_t *key_length
,
143 unsigned int number_of_keys
)
145 char buffer
[HUGE_STRING_LEN
];
148 memcached_string_st
**cursor_key_exec
;
150 ptr
->cursor_server
= 0;
151 memset(buffer
, 0, HUGE_STRING_LEN
);
153 rc
= memcached_connect(ptr
);
155 if (rc
!= MEMCACHED_SUCCESS
)
158 cursor_key_exec
= (memcached_string_st
**)malloc(sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
159 memset(cursor_key_exec
, 0, sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
162 for (x
= 0; x
< number_of_keys
; x
++)
164 unsigned int server_key
;
166 server_key
= memcached_generate_hash(keys
[x
], key_length
[x
]) % ptr
->number_of_hosts
;
168 if (cursor_key_exec
[server_key
])
170 memcached_string_st
*string
= cursor_key_exec
[server_key
];
172 memcached_string_append_character(ptr
, string
, ' ');
173 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
177 memcached_string_st
*string
= memcached_string_init(ptr
, SMALL_STRING_LEN
);
182 memcached_string_append(ptr
, string
, "get ", 4);
183 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
185 cursor_key_exec
[server_key
]= string
;
191 Should we muddle on if some servers are dead?
193 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
195 if (cursor_key_exec
[x
])
197 memcached_string_st
*string
= cursor_key_exec
[x
];
198 memcached_string_append(ptr
, string
, "\r\n", 2);
200 if ((write(ptr
->hosts
[x
].fd
, string
->string
,
201 memcached_string_length(ptr
, string
)) == -1))
204 rc
= MEMCACHED_SOME_ERRORS
;
206 memcached_string_free(ptr
, string
);
207 cursor_key_exec
[x
]= NULL
; /* Remove warning */
211 free(cursor_key_exec
);
216 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
217 size_t *value_length
,
219 memcached_return
*error
)
223 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
225 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
226 error
, 1, ptr
->cursor_server
);
228 if (*error
== MEMCACHED_NOTFOUND
)
229 ptr
->cursor_server
++;