core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * An attempt to create or update a resource was made, but the resource existed
  80. * with a different form or value. The caller should find the value of the
  81. * existing resource to be different than the one that was attempted to store.
  82. * If this is returned from the iteration of multiple updates, it means that
  83. * other resources in the loop may have changed state and the operation as a
  84. * whole completed successfully.
  85. */
  86. #define LSUP_CONFLICT 88804
  87. #define LSUP_MIN_WARNING LSUP_NOACTION
  88. /**brief Last warning value. Used internally.
  89. *
  90. * @note When adding new warning codes, use a value larger than the last one
  91. * in the list. Also change LSUP_MAX_WARNING.
  92. */
  93. #define LSUP_MAX_WARNING LSUP_CONFLICT
  94. /// Generic error return code.
  95. #define LSUP_ERROR -88899
  96. /// Codec parser error.
  97. #define LSUP_PARSE_ERR -88898
  98. /// An invalid input value was provided.
  99. #define LSUP_VALUE_ERR -88897
  100. /// Error handling a store transaction.
  101. #define LSUP_TXN_ERR -88896
  102. /// Low-level database error.
  103. #define LSUP_DB_ERR -88895
  104. /// Functionality is not implemented.
  105. #define LSUP_NOT_IMPL_ERR -88894
  106. /// I/O error.
  107. #define LSUP_IO_ERR -88893
  108. /// Memory allocation error.
  109. #define LSUP_MEM_ERR -88892
  110. /** @brief Conflict error.
  111. *
  112. * A critical resource conflict happened and no resources were updated. If this
  113. * is returned from the iteration of multiple updates, it means that the
  114. * operation has been interrupted and any state change within the loop prior to
  115. * the error has been rolled back.
  116. */
  117. #define LSUP_CONFLICT_ERR -88891
  118. /// Error while handling environment setup.
  119. #define LSUP_ENV_ERR -88890
  120. ///@} END defgroup return
  121. /** @defgroup hashing Hashing stuff
  122. * @ingroup public
  123. * @{
  124. */
  125. #ifndef LSUP_HASH_SEED
  126. /// Seed used for all hashing. Compile-time configurable.
  127. #define LSUP_HASH_SEED 0
  128. #endif
  129. /// Default 32-bit hashing function.
  130. #define LSUP_HASH32(buf, size, seed) XXH32 (buf, size, seed)
  131. /// Default 64-bit hashing function.
  132. #define LSUP_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
  133. /// Default 128-bit hashing function.
  134. #define LSUP_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
  135. /// Default hashing function. Depends on architecture.
  136. #if INTPTR_MAX == INT64_MAX
  137. #define LSUP_HASH(...) LSUP_HASH64 (__VA_ARGS__)
  138. #else
  139. #define LSUP_HASH(...) LSUP_HASH32 (__VA_ARGS__)
  140. #endif
  141. extern char *warning_msg[], *error_msg[];
  142. extern char *LSUP_root_path;
  143. /// 32-bit hash data type.
  144. typedef XXH32_hash_t LSUP_Hash32;
  145. /// 64-bit hash data type.
  146. typedef XXH64_hash_t LSUP_Hash64;
  147. /// 128-bit hash data type.
  148. typedef XXH128_hash_t LSUP_Hash128;
  149. /** @brief Default hash data type.
  150. *
  151. * This is 64 bit long for 64-bit systems.
  152. */
  153. #if INTPTR_MAX == INT64_MAX
  154. typedef LSUP_Hash64 LSUP_Hash;
  155. #else
  156. typedef LSUP_Hash32 LSUP_Hash;
  157. #endif
  158. ///@} END defgroup hashing
  159. /// Boolean operations that can be performed on a graph.
  160. typedef enum {
  161. LSUP_BOOL_UNION, ///< Boolean union.
  162. LSUP_BOOL_SUBTRACTION, ///< Boolean subtraction.
  163. LSUP_BOOL_INTERSECTION, ///< Boolean intersection.
  164. LSUP_BOOL_XOR, ///< Boolean XOR.
  165. } LSUP_bool_op;
  166. /// Term key, i.e., hash of a serialized term.
  167. typedef size_t LSUP_Key;
  168. /// Array of two #LSUP_Key values.
  169. typedef LSUP_Key LSUP_DoubleKey[2];
  170. /// Array of three #LSUP_Key values, representing a triple.
  171. typedef LSUP_Key LSUP_TripleKey[3];
  172. /// Array of three #LSUP_Key values, representing a triple with context.
  173. typedef LSUP_Key LSUP_QuadKey[4];
  174. /// UUID string tpe.
  175. typedef char uuid_str_t[UUIDSTR_SIZE];
  176. /** @brief Make recursive directories.
  177. *
  178. * Modified from
  179. * https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  180. */
  181. LSUP_rc
  182. mkdir_p (const char *path, mode_t mode);
  183. /** @brief Remove a directory recursively, as in Unix "rm -r".
  184. *
  185. * @param[in] path Path of directory to remove.
  186. */
  187. LSUP_rc
  188. rm_r (const char *path);
  189. /** @brief Return an error message for a return code.
  190. */
  191. const char *
  192. LSUP_strerror (LSUP_rc rc);
  193. /// @} END defgroup public
  194. /** @defgroup private Private LSUP API
  195. * @{
  196. */
  197. /// minimum error value.
  198. #define LSUP_MIN_ERROR LSUP_ERROR
  199. /**brief minimum error value.
  200. * @note When adding new error codes, use a value larger than the last one
  201. * in the list. Also change LSUP_MAX_ERROR.
  202. */
  203. #define LSUP_MAX_ERROR LSUP_ENV_ERR
  204. /** @brief Encode a code point using UTF-8.
  205. *
  206. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  207. *
  208. * @author Ondřej Hruška <ondra@ondrovo.com>
  209. *
  210. * @copyright MIT
  211. *
  212. * @param out - output buffer (min 5 characters), will be 0-terminated
  213. * @param utf - code point 0-0x10FFFF
  214. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  215. * which uses 3 bytes)
  216. */
  217. inline int utf8_encode (const uint32_t utf, unsigned char *out)
  218. {
  219. if (utf <= 0x7F) {
  220. // Plain ASCII
  221. out[0] = (char) utf;
  222. out[1] = 0;
  223. return 1;
  224. }
  225. else if (utf <= 0x07FF) {
  226. // 2-byte unicode
  227. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  228. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  229. out[2] = 0;
  230. return 2;
  231. }
  232. else if (utf <= 0xFFFF) {
  233. // 3-byte unicode
  234. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  235. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  236. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  237. out[3] = 0;
  238. return 3;
  239. }
  240. else if (utf <= 0x10FFFF) {
  241. // 4-byte unicode
  242. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  243. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  244. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  245. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  246. out[4] = 0;
  247. return 4;
  248. }
  249. else {
  250. // error - use replacement character
  251. out[0] = (char) 0xEF;
  252. out[1] = (char) 0xBF;
  253. out[2] = (char) 0xBD;
  254. out[3] = 0;
  255. return 0;
  256. }
  257. }
  258. /*
  259. * Only compile debug code in debug mode. Parsing variables for debug and
  260. * trace messages takes unnecessary cycles for messages that are not displayed
  261. * in non-debug mode.
  262. */
  263. #ifdef DEBUG
  264. #define LOG_DEBUG(...) log_debug(__VA_ARGS__)
  265. #define LOG_TRACE(...) log_trace(__VA_ARGS__)
  266. #else
  267. #define LOG_DEBUG(...)
  268. #define LOG_TRACE(...)
  269. #endif
  270. /** @brief Log an error or warning for return codes that are not LSUP_OK.
  271. *
  272. * Note that, if used outside of the other macros below, care must be taken
  273. * to pass it an actual return code rather than an expression, otherwise the
  274. * expression will be evaluated multiple times.
  275. */
  276. #define LOG_RC(rc) do { \
  277. if ((rc) < 0) log_error (LSUP_strerror (rc)); \
  278. else if ((rc) > 0) LOG_DEBUG(LSUP_strerror (rc)); \
  279. } while (0);
  280. /// Jump to `marker` if `exp` does not return `LSUP_OK`.
  281. #define CHECK(exp, marker) do { \
  282. LSUP_rc _rc = (exp); \
  283. LOG_RC(_rc); \
  284. if (UNLIKELY (_rc != LSUP_OK)) goto marker; \
  285. } while (0);
  286. /// Jump to `marker` if `exp` returns a negative value (skip warnings).
  287. #define PCHECK(exp, marker) do { \
  288. LSUP_rc _rc = (exp); \
  289. LOG_RC(_rc); \
  290. if (UNLIKELY (_rc < LSUP_OK)) goto marker; \
  291. } while (0);
  292. /// Return `exp` return value if it is of `LSUP_rc` type and nonzero.
  293. #define RCCK(exp) do { \
  294. LSUP_rc _rc = (exp); \
  295. LOG_RC(_rc); \
  296. if (UNLIKELY (_rc != LSUP_OK)) return _rc; \
  297. } while (0);
  298. /// Return `exp` return value if it is of `LSUP_rc` type and negative (=error)
  299. #define PRCCK(exp) do { \
  300. LSUP_rc _rc = (exp); \
  301. LOG_RC(_rc); \
  302. if (UNLIKELY (_rc < LSUP_OK)) return _rc; \
  303. } while (0);
  304. /// Return `NULL` if `exp` returns a nonzero value.
  305. #define RCNL(exp) do { \
  306. LSUP_rc _rc = (exp); \
  307. LOG_RC(_rc); \
  308. if (UNLIKELY (_rc != LSUP_OK)) return NULL; \
  309. } while (0);
  310. /// Return NULL if `exp` returns a negative value (=error)
  311. #define PRCNL(exp) do { \
  312. LSUP_rc _rc = (exp); \
  313. LOG_RC(_rc); \
  314. if (UNLIKELY (_rc < LSUP_OK)) return NULL; \
  315. } while (0);
  316. /// Allocate one pointer with malloc and return rc if it fails.
  317. #define MALLOC_GUARD(var, rc) do { \
  318. (var) = malloc (sizeof *(var)); \
  319. if (UNLIKELY (var == NULL)) return (rc); \
  320. } while (0);
  321. /// Allocate one pointer with calloc and return rc if it fails.
  322. #define CALLOC_GUARD(var, rc) do { \
  323. (var) = calloc (1, sizeof *(var)); \
  324. if (UNLIKELY (var == NULL)) return (rc); \
  325. } while (0);
  326. /*
  327. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  328. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  329. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  330. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  331. */
  332. /// @} END defgroup private
  333. #endif /* _LSUP_CORE_H */