yrmcds.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /** @file yrmcds.h
  2. * libyrmcds public API.
  3. * (C) 2013-2016 Cybozu.
  4. */
  5. #pragma once
  6. #ifndef YRMCDS_H_INCLUDED
  7. #define YRMCDS_H_INCLUDED
  8. /// Library version string such as "1.3.0".
  9. #define LIBYRMCDS_VERSION "1.3.0"
  10. /// Library version number such as 10201.
  11. #define LIBYRMCDS_VERSION_NUMBER 10300
  12. #include <pthread.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * Data structure of a connection to memcached/yrmcds server.
  20. */
  21. typedef struct {
  22. int sock; ///< the socket file descriptor.
  23. /* for sending */
  24. pthread_mutex_t lock; ///< guard lock to serialize sends.
  25. uint32_t serial; ///< last issued serial number.
  26. size_t compress_size; ///< threshold data size for LZ4 compression.
  27. /* for receiving */
  28. char* recvbuf; ///< received data buffer.
  29. size_t capacity; ///< buffer capacity
  30. size_t used; ///< used bytes.
  31. size_t last_size; ///< size of the last response.
  32. char* decompressed; ///< decompressed data.
  33. int invalid; ///< invalid flag.
  34. /* for text mode */
  35. int text_mode; ///< text mode flag.
  36. uint32_t rserial; ///< serial emulation.
  37. } yrmcds;
  38. /**
  39. * Server status codes.
  40. */
  41. typedef enum {
  42. YRMCDS_STATUS_OK = 0,
  43. YRMCDS_STATUS_NOTFOUND = 0x0001,
  44. YRMCDS_STATUS_EXISTS = 0x0002,
  45. YRMCDS_STATUS_TOOLARGEVALUE = 0x0003,
  46. YRMCDS_STATUS_INVALID = 0x0004,
  47. YRMCDS_STATUS_NOTSTORED = 0x0005,
  48. YRMCDS_STATUS_NONNUMERIC = 0x0006,
  49. YRMCDS_STATUS_LOCKED = 0x0010,
  50. YRMCDS_STATUS_NOTLOCKED = 0x0011,
  51. YRMCDS_STATUS_UNKNOWNCOMMAND = 0x0081,
  52. YRMCDS_STATUS_OUTOFMEMORY = 0x0082,
  53. YRMCDS_STATUS_OTHER = 0xffff, ///< unknown error in text protocol.
  54. } yrmcds_status;
  55. /**
  56. * Binary commands.
  57. */
  58. typedef enum {
  59. YRMCDS_CMD_GET = '\x00',
  60. YRMCDS_CMD_SET = '\x01',
  61. YRMCDS_CMD_ADD = '\x02',
  62. YRMCDS_CMD_REPLACE = '\x03',
  63. YRMCDS_CMD_DELETE = '\x04',
  64. YRMCDS_CMD_INCREMENT = '\x05',
  65. YRMCDS_CMD_DECREMENT = '\x06',
  66. YRMCDS_CMD_QUIT = '\x07',
  67. YRMCDS_CMD_FLUSH = '\x08',
  68. YRMCDS_CMD_GETQ = '\x09',
  69. YRMCDS_CMD_NOOP = '\x0a',
  70. YRMCDS_CMD_VERSION = '\x0b',
  71. YRMCDS_CMD_GETK = '\x0c',
  72. YRMCDS_CMD_GETKQ = '\x0d',
  73. YRMCDS_CMD_APPEND = '\x0e',
  74. YRMCDS_CMD_PREPEND = '\x0f',
  75. YRMCDS_CMD_STAT = '\x10',
  76. YRMCDS_CMD_SETQ = '\x11',
  77. YRMCDS_CMD_ADDQ = '\x12',
  78. YRMCDS_CMD_REPLACEQ = '\x13',
  79. YRMCDS_CMD_DELETEQ = '\x14',
  80. YRMCDS_CMD_INCREMENTQ = '\x15',
  81. YRMCDS_CMD_DECREMENTQ = '\x16',
  82. YRMCDS_CMD_QUITQ = '\x17',
  83. YRMCDS_CMD_FLUSHQ = '\x18',
  84. YRMCDS_CMD_APPENDQ = '\x19',
  85. YRMCDS_CMD_PREPENDQ = '\x1a',
  86. YRMCDS_CMD_TOUCH = '\x1c',
  87. YRMCDS_CMD_GAT = '\x1d',
  88. YRMCDS_CMD_GATQ = '\x1e',
  89. YRMCDS_CMD_GATK = '\x23',
  90. YRMCDS_CMD_GATKQ = '\x24',
  91. YRMCDS_CMD_LOCK = '\x40',
  92. YRMCDS_CMD_LOCKQ = '\x41',
  93. YRMCDS_CMD_UNLOCK = '\x42',
  94. YRMCDS_CMD_UNLOCKQ = '\x43',
  95. YRMCDS_CMD_UNLOCKALL = '\x44',
  96. YRMCDS_CMD_UNLOCKALLQ = '\x45',
  97. YRMCDS_CMD_LAG = '\x46',
  98. YRMCDS_CMD_LAGQ = '\x47',
  99. YRMCDS_CMD_LAGK = '\x48',
  100. YRMCDS_CMD_LAGKQ = '\x49',
  101. YRMCDS_CMD_RAU = '\x4a',
  102. YRMCDS_CMD_RAUQ = '\x4b',
  103. YRMCDS_CMD_KEYS = '\x50',
  104. YRMCDS_CMD_BOTTOM // place this at the bottom.
  105. } yrmcds_command;
  106. /**
  107. * Data structure to store a response packet.
  108. */
  109. typedef struct {
  110. uint32_t serial; ///< serial number of the corresponding request.
  111. size_t length; ///< the length of the response packet.
  112. yrmcds_status status; ///< the response status.
  113. yrmcds_command command; ///< the request command.
  114. uint64_t cas_unique; ///< CAS unique value.
  115. uint32_t flags; ///< The object's flags.
  116. const char* key; ///< Returned key for GetK/GaTK/LaGK.
  117. size_t key_len; ///< The length of \p key.
  118. const char* data; ///< Returned data for Get commands.
  119. size_t data_len; ///< The length of \p data.
  120. uint64_t value; ///< The new value after Increment or Decrement.
  121. } yrmcds_response;
  122. /**
  123. * Library error numbers.
  124. */
  125. typedef enum {
  126. YRMCDS_OK = 0,
  127. YRMCDS_SYSTEM_ERROR, ///< check \p errno for details.
  128. YRMCDS_BAD_ARGUMENT, ///< bad arguments.
  129. YRMCDS_NOT_RESOLVED, ///< host name cannot be resolved.
  130. YRMCDS_TIMEOUT, ///< time out for some network operations.
  131. YRMCDS_DISCONNECTED, ///< connection was reset unexpectedly.
  132. YRMCDS_OUT_OF_MEMORY, ///< malloc/realloc failed.
  133. YRMCDS_COMPRESS_FAILED, ///< LZ4 compression failed.
  134. YRMCDS_PROTOCOL_ERROR, ///< received malformed packet.
  135. YRMCDS_NOT_IMPLEMENTED, ///< the function is not available.
  136. YRMCDS_IN_BINARY, ///< connection is fixed for binary protocol.
  137. YRMCDS_BAD_KEY, ///< bad key.
  138. } yrmcds_error;
  139. /**
  140. * Reserved flag bits.
  141. */
  142. typedef enum {
  143. YRMCDS_FLAG_COMPRESS = 1 << 30, ///< transparent LZ4 compression.
  144. } yrmcds_flags;
  145. /**
  146. * @defgroup yrmcds_functions Public functions
  147. * @{
  148. */
  149. /**
  150. * Return a string to describe a library error.
  151. * @param e An error number returned from a library function.
  152. * @return A pointer to a constant string.
  153. */
  154. const char* yrmcds_strerror(yrmcds_error e);
  155. /**
  156. * Connecct to a memcached/yrmcds server.
  157. * @param c A pointer to ::yrmcds.
  158. * @param node The server name.
  159. * @param port TCP port number of the server (normally 11211).
  160. * @return 0 if connected successfully. Other values indicate an error.
  161. *
  162. * This function connects to a memcached/yrmcds server and initializes
  163. * \p c. TCP_NODELAY flag will be set for the returned socket.
  164. */
  165. yrmcds_error yrmcds_connect(yrmcds* c, const char* node, uint16_t port);
  166. /**
  167. * Close the connection.
  168. * @param c A pointer to ::yrmcds.
  169. * @return 0 if \p c is valid. Other values indicate an error.
  170. *
  171. * This function closes the connection and frees buffers in \p c.
  172. */
  173. yrmcds_error yrmcds_close(yrmcds* c);
  174. /**
  175. * Shutdown the receiving end of the socket.
  176. * @param c A pointer to ::yrmcds.
  177. * @return 0 if \p c is valid. Other values indicate an error.
  178. *
  179. * This function simply calls \p shutdown system call with \p SHUT_RD .
  180. * This can be used to interrupt a thread waiting in ::yrmcds_recv.
  181. *
  182. * Note that interrupted ::yrmcds_recv will return ::YRMCDS_DISCONNECTED.
  183. *
  184. * \see https://github.com/cybozu/libyrmcds/issues/8
  185. */
  186. yrmcds_error yrmcds_shutdown(yrmcds* c);
  187. /**
  188. * Turn on text protocol mode.
  189. * @param c A pointer to ::yrmcds.
  190. * @return 0 if succeeded. Other values indicate an error.
  191. *
  192. * This function puts the connection into text protocol mode.
  193. * \p c should be a newly connected object; if any (binary) request
  194. * has been sent, this function will return an error.
  195. *
  196. * Text protocol mode has overheads and limitations; most notably,
  197. * \p quiet option for command sending functions cannot be enabled.
  198. */
  199. yrmcds_error yrmcds_text_mode(yrmcds* c);
  200. /**
  201. * Enable/disable (de)compression for large objects.
  202. * @param c A pointer to ::yrmcds.
  203. * @param threshold The threshold for compression.
  204. * @return 0 if arguments are valid. Other values indicate an error.
  205. *
  206. * This function enables transparent compression using LZ4 if \p threshold
  207. * is greater than 0. If \p threshold is 0, then compression is disabled.
  208. *
  209. * The compression is disabled by default.
  210. *
  211. * If the library is built without LZ4, this function always return
  212. * ::YRMCDS_NOT_IMPLEMENTED.
  213. *
  214. * Note that ::YRMCDS_FLAG_COMPRESS bit in the flags of compressed objects
  215. * will be used by the library iff the compression is enabled.
  216. */
  217. yrmcds_error yrmcds_set_compression(yrmcds* c, size_t threshold);
  218. /**
  219. * Return the underlying socket in ::yrmcds.
  220. * @param c A pointer to ::yrmcds.
  221. * @return A UNIX file descriptor of a socket.
  222. */
  223. int yrmcds_fileno(yrmcds* c);
  224. /**
  225. * Set timeout seconds for send/recv operations.
  226. * @param c A pointer to ::yrmcds.
  227. * @param timeout Seconds before network operations time out.
  228. * @return 0 if arguments are valid. Other values indicate an error.
  229. */
  230. yrmcds_error yrmcds_set_timeout(yrmcds* c, int timeout);
  231. /**
  232. * Receives a response packet.
  233. * @param c A pointer to ::yrmcds.
  234. * @param r A pointer to ::yrmcds_response.
  235. * @return 0 if succeeded. Other values indicate an error.
  236. *
  237. * This function receives a response packet. If no response is available,
  238. * the function will be blocked. For each \p c, only one thread can use
  239. * this function, though command sending functions can be used in parallel.
  240. *
  241. * The response data stored in \p r keep valid until the next call of this
  242. * function or until ::yrmcds_close is called. \p r can be reused for the
  243. * next call of ::yrmcds_recv.
  244. *
  245. * This will return ::YRMCDS_DISCONNECTED when the socket is closed or
  246. * ::yrmcds_shutdown is called from another thread.
  247. */
  248. yrmcds_error yrmcds_recv(yrmcds* c, yrmcds_response* r);
  249. /**
  250. * Send Noop command.
  251. * @param c A pointer to ::yrmcds.
  252. * @param serial A pointer to \p uint32_t, or \p NULL.
  253. * @return 0 if succeeded. Other values indicate an error.
  254. *
  255. * This function sends Noop command to the server.
  256. * If \p serial is not \p NULL, the serial number of the request will be
  257. * stored if the command was sent successfully.
  258. */
  259. yrmcds_error yrmcds_noop(yrmcds* c, uint32_t* serial);
  260. /**
  261. * Send Get/GetQ command.
  262. * @param c A pointer to ::yrmcds.
  263. * @param key Key data.
  264. * @param key_len Length of \p key.
  265. * @param quiet 0 to send Get, other values to send GetQ.
  266. * @param serial A pointer to \p uint32_t, or \p NULL.
  267. * @return 0 if succeeded. Other values indicate an error.
  268. *
  269. * This function sends Get/GetQ command to the server.
  270. * If \p serial is not \p NULL, the serial number of the request will be
  271. * stored if the command was sent successfully.
  272. */
  273. yrmcds_error yrmcds_get(yrmcds* c, const char* key, size_t key_len,
  274. int quiet, uint32_t* serial);
  275. /**
  276. * Send GetK/GetKQ command.
  277. * @param c A pointer to ::yrmcds.
  278. * @param key Key data.
  279. * @param key_len Length of \p key.
  280. * @param quiet 0 to send GetK, other values to send GetKQ.
  281. * @param serial A pointer to \p uint32_t, or \p NULL.
  282. * @return 0 if succeeded. Other values indicate an error.
  283. *
  284. * This function sends GetK/GetKQ command to the server.
  285. * Unlike yrmcds_get(), the response to this request brings the key.
  286. *
  287. * If \p serial is not \p NULL, the serial number of the request will be
  288. * stored if the command was sent successfully.
  289. */
  290. yrmcds_error yrmcds_getk(yrmcds* c, const char* key, size_t key_len,
  291. int quiet, uint32_t* serial);
  292. /**
  293. * Send GaT/GaTQ command.
  294. * @param c A pointer to ::yrmcds.
  295. * @param key Key data.
  296. * @param key_len Length of \p key.
  297. * @param expire Expiration time. 0 disables expiration.
  298. * @param quiet 0 to send GaT, other values to send GaTQ.
  299. * @param serial A pointer to \p uint32_t, or \p NULL.
  300. * @return 0 if succeeded. Other values indicate an error.
  301. *
  302. * This function sends GaT/GaTQ (get and touch) command to the server.
  303. * If \p serial is not \p NULL, the serial number of the request will be
  304. * stored if the command was sent successfully.
  305. */
  306. yrmcds_error yrmcds_get_touch(yrmcds* c, const char* key, size_t key_len,
  307. uint32_t expire, int quiet, uint32_t* serial);
  308. /**
  309. * Send GaTK/GaTKQ command.
  310. * @param c A pointer to ::yrmcds.
  311. * @param key Key data.
  312. * @param key_len Length of \p key.
  313. * @param expire Expiration time. 0 disables expiration.
  314. * @param quiet 0 to send GaTK, other values to send GaTKQ.
  315. * @param serial A pointer to \p uint32_t, or \p NULL.
  316. * @return 0 if succeeded. Other values indicate an error.
  317. *
  318. * This function sends GaTK/GaTKQ (get and touch) command to the server.
  319. * Unlike yrmcds_get_touch(), the response to this request brings the key.
  320. *
  321. * If \p serial is not \p NULL, the serial number of the request will be
  322. * stored if the command was sent successfully.
  323. */
  324. yrmcds_error yrmcds_getk_touch(yrmcds* c, const char* key, size_t key_len,
  325. uint32_t expire, int quiet, uint32_t* serial);
  326. /**
  327. * Send LaG/LaGQ command.
  328. * @param c A pointer to ::yrmcds.
  329. * @param key Key data.
  330. * @param key_len Length of \p key.
  331. * @param quiet 0 to send LaG, other values to send LaGQ.
  332. * @param serial A pointer to \p uint32_t, or \p NULL.
  333. * @return 0 if succeeded. Other values indicate an error.
  334. *
  335. * This function sends LaG/LaGQ command to the server.
  336. * If \p serial is not \p NULL, the serial number of the request will be
  337. * stored if the command was sent successfully.
  338. */
  339. yrmcds_error yrmcds_lock_get(yrmcds* c, const char* key, size_t key_len,
  340. int quiet, uint32_t* serial);
  341. /**
  342. * Send LaGK/LaGKQ command.
  343. * @param c A pointer to ::yrmcds.
  344. * @param key Key data.
  345. * @param key_len Length of \p key.
  346. * @param quiet 0 to send LaGK, other values to send LaGKQ.
  347. * @param serial A pointer to \p uint32_t, or \p NULL.
  348. * @return 0 if succeeded. Other values indicate an error.
  349. *
  350. * This function sends LaGK/LaGKQ command to the server.
  351. * Unlike yrmcds_lock_get(), the response to this request brings the key.
  352. *
  353. * If \p serial is not \p NULL, the serial number of the request will be
  354. * stored if the command was sent successfully.
  355. */
  356. yrmcds_error yrmcds_lock_getk(yrmcds* c, const char* key, size_t key_len,
  357. int quiet, uint32_t* serial);
  358. /**
  359. * Send Touch command.
  360. * @param c A pointer to ::yrmcds.
  361. * @param key Key data.
  362. * @param key_len Length of \p key.
  363. * @param expire Expiration time. 0 disables expiration.
  364. * @param quiet Reserved for future enhancement.
  365. * @param serial A pointer to \p uint32_t, or \p NULL.
  366. * @return 0 if succeeded. Other values indicate an error.
  367. *
  368. * This function sends Touch command to the server.
  369. * If \p serial is not \p NULL, the serial number of the request will be
  370. * stored if the command was sent successfully.
  371. */
  372. yrmcds_error yrmcds_touch(yrmcds* c, const char* key, size_t key_len,
  373. uint32_t expire, int quiet, uint32_t* serial);
  374. /**
  375. * Send Set/SetQ command.
  376. * @param c A pointer to ::yrmcds.
  377. * @param key Key data.
  378. * @param key_len Length of \p key.
  379. * @param data Data to be stored.
  380. * @param data_len Length of \p data.
  381. * @param flags Flags stored along with the data.
  382. * @param expire Expiration time. 0 disables expiration.
  383. * @param cas Try compare-and-swap. 0 disables CAS.
  384. * @param quiet 0 to send Set, other values to send SetQ.
  385. * @param serial A pointer to \p uint32_t, or \p NULL.
  386. * @return 0 if succeeded. Other values indicate an error.
  387. *
  388. * This function sends Set/SetQ command to the server.
  389. * If \p serial is not \p NULL, the serial number of the request will be
  390. * stored if the command was sent successfully.
  391. */
  392. yrmcds_error yrmcds_set(yrmcds* c, const char* key, size_t key_len,
  393. const char* data, size_t data_len,
  394. uint32_t flags, uint32_t expire, uint64_t cas,
  395. int quiet, uint32_t* serial);
  396. /**
  397. * Send Replace/ReplaceQ command.
  398. * @param c A pointer to ::yrmcds.
  399. * @param key Key data.
  400. * @param key_len Length of \p key.
  401. * @param data Data to be stored.
  402. * @param data_len Length of \p data.
  403. * @param flags Flags stored along with the data.
  404. * @param expire Expiration time. 0 disables expiration.
  405. * @param cas Try compare-and-swap. 0 disables CAS.
  406. * @param quiet 0 to send Replace, other values to send ReplaceQ.
  407. * @param serial A pointer to \p uint32_t, or \p NULL.
  408. * @return 0 if succeeded. Other values indicate an error.
  409. *
  410. * This function sends Replace/ReplaceQ command to the server.
  411. * If \p serial is not \p NULL, the serial number of the request will be
  412. * stored if the command was sent successfully.
  413. */
  414. yrmcds_error yrmcds_replace(yrmcds* c, const char* key, size_t key_len,
  415. const char* data, size_t data_len,
  416. uint32_t flags, uint32_t expire, uint64_t cas,
  417. int quiet, uint32_t* serial);
  418. /**
  419. * Send Add/AddQ command.
  420. * @param c A pointer to ::yrmcds.
  421. * @param key Key data.
  422. * @param key_len Length of \p key.
  423. * @param data Data to be stored.
  424. * @param data_len Length of \p data.
  425. * @param flags Flags stored along with the data.
  426. * @param expire Expiration time. 0 disables expiration.
  427. * @param cas Try compare-and-swap. 0 disables CAS.
  428. * @param quiet 0 to send Add, other values to send AddQ.
  429. * @param serial A pointer to \p uint32_t, or \p NULL.
  430. * @return 0 if succeeded. Other values indicate an error.
  431. *
  432. * This function sends Add/AddQ command to the server.
  433. * If \p serial is not \p NULL, the serial number of the request will be
  434. * stored if the command was sent successfully.
  435. */
  436. yrmcds_error yrmcds_add(yrmcds* c, const char* key, size_t key_len,
  437. const char* data, size_t data_len,
  438. uint32_t flags, uint32_t expire, uint64_t cas,
  439. int quiet, uint32_t* serial);
  440. /**
  441. * Send RaU/RaUQ command.
  442. * @param c A pointer to ::yrmcds.
  443. * @param key Key data.
  444. * @param key_len Length of \p key.
  445. * @param data Data to be stored.
  446. * @param data_len Length of \p data.
  447. * @param flags Flags stored along with the data.
  448. * @param expire Expiration time. 0 disables expiration.
  449. * @param quiet 0 to send RaU, other values to send RaUQ.
  450. * @param serial A pointer to \p uint32_t, or \p NULL.
  451. * @return 0 if succeeded. Other values indicate an error.
  452. *
  453. * This function sends RaU/RaUQ (replace and unlock) command to the server.
  454. * The command will fail unless the object is locked by the same session.
  455. *
  456. * If \p serial is not \p NULL, the serial number of the request will be
  457. * stored if the command was sent successfully.
  458. */
  459. yrmcds_error yrmcds_replace_unlock(yrmcds* c, const char* key, size_t key_len,
  460. const char* data, size_t data_len,
  461. uint32_t flags, uint32_t expire,
  462. int quiet, uint32_t* serial);
  463. /**
  464. * Send Increment/IncrementQ command.
  465. * @param c A pointer to ::yrmcds.
  466. * @param key Key data.
  467. * @param key_len Length of \p key.
  468. * @param value Amount to add.
  469. * @param quiet 0 to send Increment, other values to send IncrementQ.
  470. * @param serial A pointer to \p uint32_t, or \p NULL.
  471. * @return 0 if succeeded. Other values indicate an error.
  472. *
  473. * This function sends Increment/IncrementQ command to the server.
  474. * If \p key is not found, the command will fail.
  475. *
  476. * If \p serial is not \p NULL, the serial number of the request will be
  477. * stored if the command was sent successfully.
  478. */
  479. yrmcds_error yrmcds_incr(yrmcds* c, const char* key, size_t key_len,
  480. uint64_t value, int quiet, uint32_t* serial);
  481. /**
  482. * Send Increment/IncrementQ command.
  483. * @param c A pointer to ::yrmcds.
  484. * @param key Key data.
  485. * @param key_len Length of \p key.
  486. * @param value Amount to add.
  487. * @param initial Initial value used when \p key does not exist.
  488. * @param expire Expiration time. 0 disables expiration.
  489. * @param quiet 0 to send Increment, other values to send IncrementQ.
  490. * @param serial A pointer to \p uint32_t, or \p NULL.
  491. * @return 0 if succeeded. Other values indicate an error.
  492. *
  493. * This function sends Increment/IncrementQ command to the server.
  494. * Unlike yrmcds_incr(), this function creates a new object with
  495. * \p initial and \p expire when \p key is not found.
  496. *
  497. * If \p serial is not \p NULL, the serial number of the request will be
  498. * stored if the command was sent successfully.
  499. */
  500. yrmcds_error yrmcds_incr2(yrmcds* c, const char* key, size_t key_len,
  501. uint64_t value, uint64_t initial, uint32_t expire,
  502. int quiet, uint32_t* serial);
  503. /**
  504. * Send Decrement/DecrementQ command.
  505. * @param c A pointer to ::yrmcds.
  506. * @param key Key data.
  507. * @param key_len Length of \p key.
  508. * @param value Amount to add.
  509. * @param quiet 0 to send Decrement, other values to send DecrementQ.
  510. * @param serial A pointer to \p uint32_t, or \p NULL.
  511. * @return 0 if succeeded. Other values indicate an error.
  512. *
  513. * This function sends Decrement/DecrementQ command to the server.
  514. * If \p key is not found, the command will fail.
  515. *
  516. * If \p serial is not \p NULL, the serial number of the request will be
  517. * stored if the command was sent successfully.
  518. */
  519. yrmcds_error yrmcds_decr(yrmcds* c, const char* key, size_t key_len,
  520. uint64_t value, int quiet, uint32_t* serial);
  521. /**
  522. * Send Decrement/DecrementQ command.
  523. * @param c A pointer to ::yrmcds.
  524. * @param key Key data.
  525. * @param key_len Length of \p key.
  526. * @param value Amount to add.
  527. * @param initial Initial value used when \p key does not exist.
  528. * @param expire Expiration time. 0 disables expiration.
  529. * @param quiet 0 to send Decrement, other values to send DecrementQ.
  530. * @param serial A pointer to \p uint32_t, or \p NULL.
  531. * @return 0 if succeeded. Other values indicate an error.
  532. *
  533. * This function sends Decrement/DecrementQ command to the server.
  534. * Unlike yrmcds_decr(), this function creates a new object with
  535. * \p initial and \p expire when \p key is not found.
  536. *
  537. * If \p serial is not \p NULL, the serial number of the request will be
  538. * stored if the command was sent successfully.
  539. */
  540. yrmcds_error yrmcds_decr2(yrmcds* c, const char* key, size_t key_len,
  541. uint64_t value, uint64_t initial, uint32_t expire,
  542. int quiet, uint32_t* serial);
  543. /**
  544. * Send Append/AppendQ command.
  545. * @param c A pointer to ::yrmcds.
  546. * @param key Key data.
  547. * @param key_len Length of \p key.
  548. * @param data Data.
  549. * @param data_len Length of \p data.
  550. * @param quiet 0 to send Append, other values to send AppendQ.
  551. * @param serial A pointer to \p uint32_t, or \p NULL.
  552. * @return 0 if succeeded. Other values indicate an error.
  553. *
  554. * This function sends Append/AppendQ command to the server.
  555. * If \p serial is not \p NULL, the serial number of the request will be
  556. * stored if the command was sent successfully.
  557. *
  558. * \b WARNING: if compression is enabled, this may collapse the data!
  559. */
  560. yrmcds_error yrmcds_append(yrmcds* c, const char* key, size_t key_len,
  561. const char* data, size_t data_len,
  562. int quiet, uint32_t* serial);
  563. /**
  564. * Send Prepend/PrependQ command.
  565. * @param c A pointer to ::yrmcds.
  566. * @param key Key data.
  567. * @param key_len Length of \p key.
  568. * @param data Data.
  569. * @param data_len Length of \p data.
  570. * @param quiet 0 to send Prepend, other values to send PrependQ.
  571. * @param serial A pointer to \p uint32_t, or \p NULL.
  572. * @return 0 if succeeded. Other values indicate an error.
  573. *
  574. * This function sends Prepend/PrependQ command to the server.
  575. * If \p serial is not \p NULL, the serial number of the request will be
  576. * stored if the command was sent successfully.
  577. *
  578. * \b WARNING: if compression is enabled, this may collapse the data!
  579. */
  580. yrmcds_error yrmcds_prepend(yrmcds* c, const char* key, size_t key_len,
  581. const char* data, size_t data_len,
  582. int quiet, uint32_t* serial);
  583. /**
  584. * Send Delete/DeleteQ command.
  585. * @param c A pointer to ::yrmcds.
  586. * @param key Key data.
  587. * @param key_len Length of \p key.
  588. * @param quiet 0 to send Delete, other values to send DeleteQ.
  589. * @param serial A pointer to \p uint32_t, or \p NULL.
  590. * @return 0 if succeeded. Other values indicate an error.
  591. *
  592. * This function sends Delete/DeleteQ command to the server.
  593. * If \p serial is not \p NULL, the serial number of the request will be
  594. * stored if the command was sent successfully.
  595. */
  596. yrmcds_error yrmcds_remove(yrmcds* c, const char* key, size_t key_len,
  597. int quiet, uint32_t* serial);
  598. /**
  599. * Send Lock/LockQ command.
  600. * @param c A pointer to ::yrmcds.
  601. * @param key Key data.
  602. * @param key_len Length of \p key.
  603. * @param quiet 0 to send Lock, other values to send LockQ.
  604. * @param serial A pointer to \p uint32_t, or \p NULL.
  605. * @return 0 if succeeded. Other values indicate an error.
  606. *
  607. * This function sends Lock/LockQ command to the server.
  608. * If \p serial is not \p NULL, the serial number of the request will be
  609. * stored if the command was sent successfully.
  610. */
  611. yrmcds_error yrmcds_lock(yrmcds* c, const char* key, size_t key_len,
  612. int quiet, uint32_t* serial);
  613. /**
  614. * Send Unlock/UnlockQ command.
  615. * @param c A pointer to ::yrmcds.
  616. * @param key Key data.
  617. * @param key_len Length of \p key.
  618. * @param quiet 0 to send Unlock, other values to send UnlockQ.
  619. * @param serial A pointer to \p uint32_t, or \p NULL.
  620. * @return 0 if succeeded. Other values indicate an error.
  621. *
  622. * This function sends Unlock/UnlockQ command to the server.
  623. * If \p serial is not \p NULL, the serial number of the request will be
  624. * stored if the command was sent successfully.
  625. */
  626. yrmcds_error yrmcds_unlock(yrmcds* c, const char* key, size_t key_len,
  627. int quiet, uint32_t* serial);
  628. /**
  629. * Send UnlockAll/UnlockAllQ command.
  630. * @param c A pointer to ::yrmcds.
  631. * @param quiet 0 to send UnlockAll, other values to send UnlockAllQ.
  632. * @param serial A pointer to \p uint32_t, or \p NULL.
  633. * @return 0 if succeeded. Other values indicate an error.
  634. *
  635. * This function sends UnlockAll/UnlockAllQ command to the server.
  636. * If \p serial is not \p NULL, the serial number of the request will be
  637. * stored if the command was sent successfully.
  638. */
  639. yrmcds_error yrmcds_unlockall(yrmcds* c, int quiet, uint32_t* serial);
  640. /**
  641. * Send Flush/FlushQ command.
  642. * @param c A pointer to ::yrmcds.
  643. * @param delay delay seconds before flush.
  644. * @param quiet 0 to send Flush, other values to send FlushQ.
  645. * @param serial A pointer to \p uint32_t, or \p NULL.
  646. * @return 0 if succeeded. Other values indicate an error.
  647. *
  648. * This function sends Flush/FlushQ command to the server.
  649. * If \p serial is not \p NULL, the serial number of the request will be
  650. * stored if the command was sent successfully.
  651. */
  652. yrmcds_error yrmcds_flush(yrmcds* c, uint32_t delay,
  653. int quiet, uint32_t* serial);
  654. /**
  655. * Send Stat command to obtain general statistics.
  656. * @param c A pointer to ::yrmcds.
  657. * @param serial A pointer to \p uint32_t, or \p NULL.
  658. * @return 0 if succeeded. Other values indicate an error.
  659. */
  660. yrmcds_error yrmcds_stat_general(yrmcds* c, uint32_t* serial);
  661. /**
  662. * Send Stat command to obtain setting statistics.
  663. * @param c A pointer to ::yrmcds.
  664. * @param serial A pointer to \p uint32_t, or \p NULL.
  665. * @return 0 if succeeded. Other values indicate an error.
  666. */
  667. yrmcds_error yrmcds_stat_settings(yrmcds* c, uint32_t* serial);
  668. /**
  669. * Send Stat command to obtain item statistics.
  670. * @param c A pointer to ::yrmcds.
  671. * @param serial A pointer to \p uint32_t, or \p NULL.
  672. * @return 0 if succeeded. Other values indicate an error.
  673. */
  674. yrmcds_error yrmcds_stat_items(yrmcds* c, uint32_t* serial);
  675. /**
  676. * Send Stat command to obtain size statistics.
  677. * @param c A pointer to ::yrmcds.
  678. * @param serial A pointer to \p uint32_t, or \p NULL.
  679. * @return 0 if succeeded. Other values indicate an error.
  680. */
  681. yrmcds_error yrmcds_stat_sizes(yrmcds* c, uint32_t* serial);
  682. /**
  683. * Send Keys command to list all keys matching the given prefix.
  684. * To retrieve all keys, pass \p NULL and 0 as \p prefix and \p prefix_len.
  685. * @param c A pointer to ::yrmcds.
  686. * @param prefix Prefix data.
  687. * @param prefix_len Length of \p prefix.
  688. * @param serial A pointer to \p uint32_t, or \p NULL.
  689. * @return 0 if succeeded. Other values indicate an error.
  690. */
  691. yrmcds_error yrmcds_keys(yrmcds* c, const char* prefix, size_t prefix_len,
  692. uint32_t* serial);
  693. /**
  694. * Send Version command.
  695. * @param c A pointer to ::yrmcds.
  696. * @param serial A pointer to \p uint32_t, or \p NULL.
  697. * @return 0 if succeeded. Other values indicate an error.
  698. */
  699. yrmcds_error yrmcds_version(yrmcds* c, uint32_t* serial);
  700. /**
  701. * Send Quit/QuitQ command.
  702. * @param c A pointer to ::yrmcds.
  703. * @param quiet 0 to send Quit, other values to send QuitQ.
  704. * @param serial A pointer to \p uint32_t, or \p NULL.
  705. * @return 0 if succeeded. Other values indicate an error.
  706. */
  707. yrmcds_error yrmcds_quit(yrmcds* c, int quiet, uint32_t* serial);
  708. /**
  709. * @}
  710. * @mainpage A memcached/yrmcds client library for C.
  711. *
  712. * - \ref yrmcds_functions
  713. * - \ref yrmcds_cnt_functions
  714. */
  715. /**
  716. * Data structure to store the reference to a statistic data for the counter extension.
  717. */
  718. typedef struct {
  719. const char* name; ///< the name of a statistic item
  720. size_t name_length; ///< the length of \p name
  721. const char* value; ///< the ASCII text information
  722. size_t value_length; ///< the length of \p value
  723. } yrmcds_cnt_stat;
  724. /**
  725. * Data structure to store statistics for the counter extension.
  726. */
  727. typedef struct {
  728. yrmcds_cnt_stat* records; ///< the array of the statistic information
  729. size_t count; ///< the number of statistics
  730. size_t capacity; ///< the maximum number of records that \p records can hold.
  731. } yrmcds_cnt_statistics;
  732. /**
  733. * Data structure of a connection to yrmcds counter server.
  734. */
  735. typedef struct {
  736. pthread_mutex_t lock; ///< guard lock to serialize sends.
  737. yrmcds_cnt_statistics stats; ///< the result of `stats` command.
  738. char* recvbuf; ///< received data buffer.
  739. size_t capacity; ///< buffer capacity.
  740. size_t used; ///< used bytes.
  741. size_t last_size; ///< size of the last response.
  742. int sock; ///< the socket file descriptor.
  743. int invalid; ///< invalid flag.
  744. uint32_t serial; ///< last issued serial number.
  745. } yrmcds_cnt;
  746. /**
  747. * Server status codes for the counter extension.
  748. */
  749. typedef enum {
  750. YRMCDS_CNT_STATUS_OK = 0x00,
  751. YRMCDS_CNT_STATUS_NOT_FOUND = 0x01,
  752. YRMCDS_CNT_STATUS_INVALID = 0x04,
  753. YRMCDS_CNT_STATUS_RESOURCE_NOT_AVAILABLE = 0x21,
  754. YRMCDS_CNT_STATUS_NOT_ACQUIRED = 0x22,
  755. YRMCDS_CNT_STATUS_UNKNOWN_COMMAND = 0x81,
  756. YRMCDS_CNT_STATUS_OUT_OF_MEMORY = 0x82,
  757. } yrmcds_cnt_status;
  758. /**
  759. * Binary commands for the counter extension.
  760. */
  761. typedef enum {
  762. YRMCDS_CNT_CMD_NOOP = 0x00,
  763. YRMCDS_CNT_CMD_GET = 0x01,
  764. YRMCDS_CNT_CMD_ACQUIRE = 0x02,
  765. YRMCDS_CNT_CMD_RELEASE = 0x03,
  766. YRMCDS_CNT_CMD_STATS = 0x10,
  767. YRMCDS_CNT_CMD_DUMP = 0x11,
  768. } yrmcds_cnt_command;
  769. /**
  770. * Data structure to store a response packet for the counter extension.
  771. */
  772. typedef struct {
  773. yrmcds_cnt_statistics* stats; ///< the result of `stats` command.
  774. const char* body; ///< the pointer to the response body.
  775. size_t body_length; ///< the body length of the response packet.
  776. const char* name; ///< the name of the semaphore (only for Dump command).
  777. size_t name_length; ///< the length of \p name (only for Dump command).
  778. uint32_t serial; ///< serial number of the corresponding request.
  779. uint32_t resources; ///< the number of acquired resources.
  780. uint32_t current_consumption; ///< the current consumption of resources.
  781. uint32_t max_consumption; ///< maximum consumption (only for Dump command).
  782. uint8_t status; ///< the response status.
  783. uint8_t command; ///< the request command.
  784. } yrmcds_cnt_response;
  785. /**
  786. * @defgroup yrmcds_cnt_functions Public functions for the counter extension
  787. * @{
  788. */
  789. /**
  790. * Connecct to a yrmcds server.
  791. * @param c A pointer to ::yrmcds_cnt.
  792. * @param node The server name.
  793. * @param port TCP port number of the server (normally 11215).
  794. * @return 0 if connected successfully. Other values indicate an error.
  795. *
  796. * This function connects to a yrmcds server and initializes
  797. * \p c. TCP_NODELAY flag will be set for the returned socket.
  798. */
  799. yrmcds_error
  800. yrmcds_cnt_connect(yrmcds_cnt* c, const char* node, uint16_t port);
  801. /**
  802. * Close the connection.
  803. * @param c A pointer to ::yrmcds_cnt.
  804. * @return 0 if \p c is valid. Other values indicate an error.
  805. *
  806. * This function closes the connection and frees buffers in \p c.
  807. */
  808. yrmcds_error
  809. yrmcds_cnt_close(yrmcds_cnt* c);
  810. /**
  811. * Shutdown the receiving end of the socket.
  812. * @param c A pointer to ::yrmcds_cnt.
  813. * @return 0 if \p c is valid. Other values indicate an error.
  814. *
  815. * This function simply calls \p shutdown system call with \p SHUT_RD .
  816. * This can be used to interrupt a thread waiting in ::yrmcds_cnt_recv.
  817. *
  818. * Note that interrupted ::yrmcds_cnt_recv will return ::YRMCDS_DISCONNECTED.
  819. *
  820. * \see https://github.com/cybozu/libyrmcds/issues/8
  821. */
  822. yrmcds_error
  823. yrmcds_cnt_shutdown(yrmcds_cnt* c);
  824. /**
  825. * Return the underlying socket in ::yrmcds_cnt.
  826. * @param c A pointer to ::yrmcds_cnt.
  827. * @return A UNIX file descriptor of a socket.
  828. */
  829. int
  830. yrmcds_cnt_fileno(yrmcds_cnt* c);
  831. /**
  832. * Set timeout seconds for send/recv operations.
  833. * @param c A pointer to ::yrmcds_cnt.
  834. * @param timeout Seconds before network operations time out.
  835. * @return 0 if arguments are valid. Other values indicate an error.
  836. */
  837. yrmcds_error
  838. yrmcds_cnt_set_timeout(yrmcds_cnt* c, int timeout);
  839. /**
  840. * Receives a response packet.
  841. * @param c A pointer to ::yrmcds_cnt.
  842. * @param r A pointer to ::yrmcds_cnt_response.
  843. * @return 0 if succeeded. Other values indicate an error.
  844. *
  845. * This function receives a response packet. If no response is available,
  846. * the function will be blocked. For each \p c, only one thread can use
  847. * this function, though command sending functions can be used in parallel.
  848. *
  849. * The response data stored in \p r keep valid until the next call of this
  850. * function or until yrmcds_cnt_close() is called. \p r can be reused for the
  851. * next call of yrmcds_cnt_recv().
  852. */
  853. yrmcds_error
  854. yrmcds_cnt_recv(yrmcds_cnt* c, yrmcds_cnt_response* r);
  855. /**
  856. * Send Noop command.
  857. * @param c A pointer to ::yrmcds_cnt.
  858. * @param serial A pointer to \p uint32_t, or \p NULL.
  859. * @return 0 if succeeded. Other values indicate an error.
  860. *
  861. * This function sends Noop command to the server.
  862. * If \p serial is not \p NULL, the serial number of the request will be
  863. * stored if the command was sent successfully.
  864. */
  865. yrmcds_error
  866. yrmcds_cnt_noop(yrmcds_cnt* c, uint32_t* serial);
  867. /**
  868. * Send Get command.
  869. * @param c A pointer to ::yrmcds_cnt.
  870. * @param name Name.
  871. * @param name_len Length of \p name.
  872. * @param serial A pointer to \p uint32_t, or \p NULL.
  873. * @return 0 if succeeded. Other values indicate an error.
  874. *
  875. * This function sends Get command to the server.
  876. * If \p serial is not \p NULL, the serial number of the request will be
  877. * stored if the command was sent successfully.
  878. */
  879. yrmcds_error
  880. yrmcds_cnt_get(yrmcds_cnt* c,
  881. const char* name, size_t name_len, uint32_t* serial);
  882. /**
  883. * Send Acquire command.
  884. * @param c A pointer to ::yrmcds_cnt.
  885. * @param name Name.
  886. * @param name_len Length of \p name.
  887. * @param resources The number of resources to acquire.
  888. * @param maximum The maximum number of resources.
  889. * @param serial A pointer to \p uint32_t, or \p NULL.
  890. * @return 0 if succeeded. Other values indicate an error.
  891. *
  892. * This function sends Acquire command to the server.
  893. * If \p serial is not \p NULL, the serial number of the request will be
  894. * stored if the command was sent successfully.
  895. */
  896. yrmcds_error
  897. yrmcds_cnt_acquire(yrmcds_cnt* c, const char* name, size_t name_len,
  898. uint32_t resources, uint32_t maximum, uint32_t* serial);
  899. /**
  900. * Send Release command.
  901. * @param c A pointer to ::yrmcds_cnt.
  902. * @param name Name.
  903. * @param name_len Length of \p name.
  904. * @param resources The number of resources to release.
  905. * @param serial A pointer to \p uint32_t, or \p NULL.
  906. * @return 0 if succeeded. Other values indicate an error.
  907. *
  908. * This function sends Release command to the server.
  909. * If \p serial is not \p NULL, the serial number of the request will be
  910. * stored if the command was sent successfully.
  911. */
  912. yrmcds_error
  913. yrmcds_cnt_release(yrmcds_cnt* c, const char* name, size_t name_len,
  914. uint32_t resources, uint32_t* serial);
  915. /**
  916. * Send Stats command.
  917. * @param c A pointer to ::yrmcds_cnt.
  918. * @param serial A pointer to \p uint32_t, or \p NULL.
  919. * @return 0 if succeeded. Other values indicate an error.
  920. *
  921. * This function sends Stats command to the server.
  922. * If \p serial is not \p NULL, the serial number of the request will be
  923. * stored if the command was sent successfully.
  924. */
  925. yrmcds_error
  926. yrmcds_cnt_stats(yrmcds_cnt* c, uint32_t* serial);
  927. /**
  928. * Send Dump command.
  929. * @param c A pointer to ::yrmcds_cnt.
  930. * @param serial A pointer to \p uint32_t, or \p NULL.
  931. * @return 0 if succeeded. Other values indicate an error.
  932. *
  933. * This function sends Dump command to the server.
  934. * If \p serial is not \p NULL, the serial number of the request will be
  935. * stored if the command was sent successfully.
  936. */
  937. yrmcds_error
  938. yrmcds_cnt_dump(yrmcds_cnt* c, uint32_t* serial);
  939. /**
  940. * @}
  941. */
  942. #ifdef __cplusplus
  943. } // extern "C"
  944. #endif
  945. #endif // YRMCDS_H_INCLUDED