core.h 11 KB

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