src/bin: apply clang-format
[awesomized/libmemcached] / src / bin / common / execute.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17 #include "execute.h"
18
19 unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of) {
20 uint32_t count = 0;
21 for (; count < number_of; ++count) {
22 memcached_return_t rc = memcached_set(memc, pairs[count].key, pairs[count].key_length,
23 pairs[count].value, pairs[count].value_length, 0, 0);
24 if (memcached_failed(rc)) {
25 fprintf(stderr, "%s:%d Failure on %u insert (%s) of %.*s\n", __FILE__, __LINE__, count,
26 memcached_last_error_message(memc), (unsigned int) pairs[count].key_length,
27 pairs[count].key);
28
29 // We will try to reconnect and see if that fixes the issue
30 memcached_quit(memc);
31
32 return count;
33 }
34 }
35
36 return count;
37 }
38
39 /*
40 Execute a memcached_get() on a set of pairs.
41 Return the number of rows retrieved.
42 */
43 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of) {
44 unsigned int x;
45 unsigned int retrieved;
46
47 for (retrieved = 0, x = 0; x < number_of; x++) {
48 size_t value_length;
49 uint32_t flags;
50
51 unsigned int fetch_key = (unsigned int) ((unsigned int) random() % number_of);
52
53 memcached_return_t rc;
54 char *value = memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
55 &value_length, &flags, &rc);
56
57 if (memcached_failed(rc)) {
58 fprintf(stderr, "%s:%d Failure on read(%s) of %.*s\n", __FILE__, __LINE__,
59 memcached_last_error_message(memc), (unsigned int) pairs[fetch_key].key_length,
60 pairs[fetch_key].key);
61 } else {
62 retrieved++;
63 }
64
65 ::free(value);
66 }
67
68 return retrieved;
69 }
70
71 /**
72 * Callback function to count the number of results
73 */
74 static memcached_return_t callback_counter(const memcached_st *ptr, memcached_result_st *result,
75 void *context) {
76 (void) ptr;
77 (void) result;
78 unsigned int *counter = (unsigned int *) context;
79 *counter = *counter + 1;
80
81 return MEMCACHED_SUCCESS;
82 }
83
84 /**
85 * Try to run a large mget to get all of the keys
86 * @param memc memcached handle
87 * @param keys the keys to get
88 * @param key_length the length of the keys
89 * @param number_of the number of keys to try to get
90 * @return the number of keys received
91 */
92 unsigned int execute_mget(memcached_st *memc, const char *const *keys, size_t *key_length,
93 unsigned int number_of) {
94 unsigned int retrieved = 0;
95 memcached_execute_fn callbacks[] = {callback_counter};
96 memcached_return_t rc;
97 rc = memcached_mget_execute(memc, keys, key_length, (size_t) number_of, callbacks, &retrieved, 1);
98
99 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_BUFFERED
100 || rc == MEMCACHED_END)
101 {
102 rc = memcached_fetch_execute(memc, callbacks, (void *) &retrieved, 1);
103 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END) {
104 fprintf(stderr, "%s:%d Failed to execute mget: %s\n", __FILE__, __LINE__,
105 memcached_strerror(memc, rc));
106 memcached_quit(memc);
107 return 0;
108 }
109 } else {
110 fprintf(stderr, "%s:%d Failed to execute mget: %s\n", __FILE__, __LINE__,
111 memcached_strerror(memc, rc));
112 memcached_quit(memc);
113 return 0;
114 }
115
116 return retrieved;
117 }