core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef _LSUP_CORE_H
  2. #define _LSUP_CORE_H
  3. #ifndef NOCOLOR
  4. #define LOG_USE_COLOR
  5. #endif
  6. #include <ctype.h>
  7. #include <dirent.h>
  8. #include <inttypes.h>
  9. #include <limits.h>
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <uuid/uuid.h>
  18. #include "log.h"
  19. #include "xxhash.h"
  20. #define LSUP_VERSION "1.0a3"
  21. // Logging and debugging.
  22. #ifdef DEBUG
  23. // GDB breakpoints.
  24. #include <signal.h>
  25. #define BREAKPOINT raise (SIGINT)
  26. #else
  27. #define BREAKPOINT
  28. #endif
  29. #define LIKELY(x) __builtin_expect(!!(x), true)
  30. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  31. // TODO Cross-platform ramdisk path.
  32. #define TMPDIR "/tmp"
  33. /** @defgroup public LSUP Public API
  34. * @{
  35. */
  36. #define LSUP_NS "urn:lsup:" /// Default LS namespace.
  37. #define KLEN sizeof(LSUP_Key) /// Key length.
  38. #define DBL_KLEN sizeof(LSUP_DoubleKey) /// Double key length.
  39. #define TRP_KLEN sizeof(LSUP_TripleKey) /// Triple key length.
  40. #define QUAD_KLEN sizeof(LSUP_QuadKey) /// Quad key length.
  41. # define UUIDSTR_SIZE 37
  42. /** @brief "NULL" triple, a value that is never user-provided.
  43. */
  44. #define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
  45. /* * * RETURN CODES * * */
  46. /**@defgroup rc Return codes
  47. * 0 is success, positive integers (>88800) are warnings, and negative integers
  48. * (>-88900) are errors.
  49. *
  50. * @ingroup public
  51. * @{
  52. */
  53. typedef int LSUP_rc;
  54. /** @brief Generic success return code.
  55. */
  56. #define LSUP_OK 0
  57. /** @brief No action taken.
  58. *
  59. * An attempt to create or update a resource was made, but the resource already
  60. * existed substantially in the same form, so no action took place. The caller
  61. * is expected to find the resource as though the action actually took place.
  62. * If this is returned from the iteration of multiple updates, it means that
  63. * none of the iterations produced a change in state.
  64. */
  65. #define LSUP_NOACTION 88801
  66. /** @brief No result yielded.
  67. *
  68. * A read operation returned no results. If an iterator is expected to be
  69. * created, it may be created empty.
  70. */
  71. #define LSUP_NORESULT 88802
  72. /** @brief Loop end.
  73. *
  74. * End of a loop was reached. This can be used in a while() or for()
  75. * loop as a terminating condition.
  76. */
  77. #define LSUP_END 88803
  78. /** @brief Conflict warning.
  79. *
  80. * An attempt to create or update a resource was made, but the resource existed
  81. * with a different form or value. The caller should find the value of the
  82. * existing resource to be different than the one that was attempted to store.
  83. * If this is returned from the iteration of multiple updates, it means that
  84. * other resources in the loop may have changed state and the operation as a
  85. * whole completed successfully.
  86. *
  87. * The error-level counterpart to this is #LSUP_CONFLICT_ERR.
  88. */
  89. #define LSUP_CONFLICT 88804
  90. #define LSUP_MIN_WARNING LSUP_NOACTION
  91. /**brief Last warning value. Used internally.
  92. *
  93. * @note When adding new warning codes, use a value larger than the last one
  94. * in the list. Also change LSUP_MAX_WARNING.
  95. */
  96. #define LSUP_MAX_WARNING LSUP_CONFLICT
  97. /// Generic error return code.
  98. #define LSUP_ERROR -88899
  99. /// Codec parser error.
  100. #define LSUP_PARSE_ERR -88898
  101. /// An invalid input value was provided.
  102. #define LSUP_VALUE_ERR -88897
  103. /// Error handling a store transaction.
  104. #define LSUP_TXN_ERR -88896
  105. /// Low-level database error.
  106. #define LSUP_DB_ERR -88895
  107. /// Functionality is not implemented.
  108. #define LSUP_NOT_IMPL_ERR -88894
  109. /// I/O error.
  110. #define LSUP_IO_ERR -88893
  111. /// Memory allocation error.
  112. #define LSUP_MEM_ERR -88892
  113. /** @brief Conflict error.
  114. *
  115. * A critical resource conflict happened and no resources were updated. If this
  116. * is returned from the iteration of multiple updates, it means that the
  117. * operation has been interrupted and any state change within the loop prior to
  118. * the error has been rolled back.
  119. *
  120. * The warning-level counterpart to this is #LSUP_CONFLICT.
  121. */
  122. #define LSUP_CONFLICT_ERR -88891
  123. /// Error while handling environment setup; or environment not initialized.
  124. #define LSUP_ENV_ERR -88890
  125. ///@} END defgroup return
  126. /** @defgroup hashing Hashing stuff
  127. * @ingroup public
  128. * @{
  129. */
  130. #ifndef LSUP_HASH_SEED
  131. /// Seed used for all hashing. Compile-time configurable.
  132. #define LSUP_HASH_SEED 0
  133. #endif
  134. /// Default 32-bit hashing function.
  135. #define LSUP_HASH32(buf, size, seed) XXH32 (buf, size, seed)
  136. /// Default 64-bit hashing function.
  137. #define LSUP_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
  138. /// Default 128-bit hashing function.
  139. #define LSUP_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
  140. /// Default hashing function. Depends on architecture.
  141. #if INTPTR_MAX == INT64_MAX
  142. #define LSUP_HASH(...) LSUP_HASH64 (__VA_ARGS__)
  143. #else
  144. #define LSUP_HASH(...) LSUP_HASH32 (__VA_ARGS__)
  145. #endif
  146. extern char *warning_msg[], *error_msg[];
  147. extern char *LSUP_root_path;
  148. /// 32-bit hash data type.
  149. typedef XXH32_hash_t LSUP_Hash32;
  150. /// 64-bit hash data type.
  151. typedef XXH64_hash_t LSUP_Hash64;
  152. /// 128-bit hash data type.
  153. typedef XXH128_hash_t LSUP_Hash128;
  154. /** @brief Default hash data type.
  155. *
  156. * This is 64 bit long for 64-bit systems.
  157. */
  158. #if INTPTR_MAX == INT64_MAX
  159. typedef LSUP_Hash64 LSUP_Hash;
  160. #else
  161. typedef LSUP_Hash32 LSUP_Hash;
  162. #endif
  163. ///@} END defgroup hashing
  164. /// Boolean operations that can be performed on a graph.
  165. typedef enum {
  166. LSUP_BOOL_UNION, ///< Boolean union.
  167. LSUP_BOOL_SUBTRACTION, ///< Boolean subtraction.
  168. LSUP_BOOL_INTERSECTION, ///< Boolean intersection.
  169. LSUP_BOOL_XOR, ///< Boolean XOR.
  170. } LSUP_bool_op;
  171. /// Term key, i.e., hash of a serialized term.
  172. typedef size_t LSUP_Key;
  173. /// Array of two #LSUP_Key values.
  174. typedef LSUP_Key LSUP_DoubleKey[2];
  175. /// Array of three #LSUP_Key values, representing a triple.
  176. typedef LSUP_Key LSUP_TripleKey[3];
  177. /// Array of three #LSUP_Key values, representing a triple with context.
  178. typedef LSUP_Key LSUP_QuadKey[4];
  179. /// UUID string tpe.
  180. typedef char uuid_str_t[UUIDSTR_SIZE];
  181. /** @brief Make recursive directories.
  182. *
  183. * Modified from
  184. * https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  185. */
  186. LSUP_rc
  187. mkdir_p (const char *path, mode_t mode);
  188. /** @brief Remove a directory recursively, as in Unix "rm -r".
  189. *
  190. * @param[in] path Path of directory to remove.
  191. */
  192. LSUP_rc
  193. rm_r (const char *path);
  194. /** @brief Return an error message for a return code.
  195. */
  196. const char *
  197. LSUP_strerror (LSUP_rc rc);
  198. /// @} END defgroup public
  199. /** @defgroup private Private LSUP API
  200. * @{
  201. */
  202. /// minimum error value.
  203. #define LSUP_MIN_ERROR LSUP_ERROR
  204. /**brief minimum error value.
  205. * @note When adding new error codes, use a value larger than the last one
  206. * in the list. Also change LSUP_MAX_ERROR.
  207. */
  208. #define LSUP_MAX_ERROR LSUP_ENV_ERR
  209. /** @brief Encode a code point using UTF-8.
  210. *
  211. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  212. *
  213. * @author Ondřej Hruška <ondra@ondrovo.com>
  214. *
  215. * @copyright MIT
  216. *
  217. * @param out - output buffer (min 5 characters), will be 0-terminated
  218. * @param utf - code point 0-0x10FFFF
  219. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  220. * which uses 3 bytes)
  221. */
  222. inline int utf8_encode (const uint32_t utf, unsigned char *out)
  223. {
  224. if (utf <= 0x7F) {
  225. // Plain ASCII
  226. out[0] = (char) utf;
  227. out[1] = 0;
  228. return 1;
  229. }
  230. else if (utf <= 0x07FF) {
  231. // 2-byte unicode
  232. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  233. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  234. out[2] = 0;
  235. return 2;
  236. }
  237. else if (utf <= 0xFFFF) {
  238. // 3-byte unicode
  239. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  240. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  241. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  242. out[3] = 0;
  243. return 3;
  244. }
  245. else if (utf <= 0x10FFFF) {
  246. // 4-byte unicode
  247. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  248. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  249. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  250. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  251. out[4] = 0;
  252. return 4;
  253. }
  254. else {
  255. // error - use replacement character
  256. out[0] = (char) 0xEF;
  257. out[1] = (char) 0xBF;
  258. out[2] = (char) 0xBD;
  259. out[3] = 0;
  260. return 0;
  261. }
  262. }
  263. /*
  264. * Only compile debug code in debug mode. Parsing variables for debug and
  265. * trace messages takes unnecessary cycles for messages that are not displayed
  266. * in non-debug mode.
  267. */
  268. #ifdef DEBUG
  269. #define LOG_DEBUG(...) log_debug(__VA_ARGS__)
  270. #define LOG_TRACE(...) log_trace(__VA_ARGS__)
  271. #else
  272. #define LOG_DEBUG(...)
  273. #define LOG_TRACE(...)
  274. #endif
  275. /** @brief Log an error or warning for return codes that are not LSUP_OK.
  276. *
  277. * Note that, if used outside of the other macros below, care must be taken
  278. * to pass it an actual return code rather than an expression, otherwise the
  279. * expression will be evaluated multiple times.
  280. */
  281. #define LOG_RC(rc) do { \
  282. if ((rc) < 0) log_error (LSUP_strerror (rc)); \
  283. else if ((rc) > 0) LOG_DEBUG(LSUP_strerror (rc)); \
  284. } while (0);
  285. /// Jump to `marker` if `exp` does not return `LSUP_OK`.
  286. #define CHECK(exp, marker) do { \
  287. LSUP_rc _rc = (exp); \
  288. LOG_RC(_rc); \
  289. if (UNLIKELY (_rc != LSUP_OK)) goto marker; \
  290. } while (0);
  291. /// Jump to `marker` if `exp` returns a negative value (skip warnings).
  292. #define PCHECK(exp, marker) do { \
  293. LSUP_rc _rc = (exp); \
  294. LOG_RC(_rc); \
  295. if (UNLIKELY (_rc < LSUP_OK)) goto marker; \
  296. } while (0);
  297. /// Return `exp` return value if it is of `LSUP_rc` type and nonzero.
  298. #define RCCK(exp) do { \
  299. LSUP_rc _rc = (exp); \
  300. LOG_RC(_rc); \
  301. if (UNLIKELY (_rc != LSUP_OK)) return _rc; \
  302. } while (0);
  303. /// Return `exp` return value if it is of `LSUP_rc` type and negative (=error)
  304. #define PRCCK(exp) do { \
  305. LSUP_rc _rc = (exp); \
  306. LOG_RC(_rc); \
  307. if (UNLIKELY (_rc < LSUP_OK)) return _rc; \
  308. } while (0);
  309. /// Return `NULL` if `exp` returns a nonzero value.
  310. #define RCNL(exp) do { \
  311. LSUP_rc _rc = (exp); \
  312. LOG_RC(_rc); \
  313. if (UNLIKELY (_rc != LSUP_OK)) return NULL; \
  314. } while (0);
  315. /// Return NULL if `exp` returns a negative value (=error)
  316. #define PRCNL(exp) do { \
  317. LSUP_rc _rc = (exp); \
  318. LOG_RC(_rc); \
  319. if (UNLIKELY (_rc < LSUP_OK)) return NULL; \
  320. } while (0);
  321. /// Allocate one pointer with malloc and return rc if it fails.
  322. #define MALLOC_GUARD(var, rc) do { \
  323. (var) = malloc (sizeof *(var)); \
  324. if (UNLIKELY (var == NULL)) return (rc); \
  325. } while (0);
  326. /// Allocate one pointer with calloc and return rc if it fails.
  327. #define CALLOC_GUARD(var, rc) do { \
  328. (var) = calloc (1, sizeof *(var)); \
  329. if (UNLIKELY (var == NULL)) return (rc); \
  330. } while (0);
  331. /*
  332. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  333. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  334. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  335. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  336. */
  337. /// @} END defgroup private
  338. #endif /* _LSUP_CORE_H */