Volksdata 1.0b12
RDF library and triple store
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#ifndef VOLK_CORE_H
2#define VOLK_CORE_H
3
4#ifndef NOCOLOR
5#define LOG_USE_COLOR
6#endif
7
8#include <ctype.h>
9#include <dirent.h>
10#include <inttypes.h>
11#include <limits.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/stat.h>
19#include <uuid/uuid.h>
20
21#include "log.h"
22#include "xxhash.h"
23
24
25// Logging and debugging.
26
27#ifdef DEBUG
28#include <signal.h>
30#define BREAKPOINT raise (SIGUSR1)
31
32#ifdef __GLIBC__
33#include <execinfo.h>
40#define STRACE(...) do { \
41 void *array[10]; \
42 int size = backtrace (array, 10); \
43 char **strings = backtrace_symbols (array, size); \
44 if (strings != NULL) { \
45 log_debug (__VA_ARGS__); \
46 log_debug ("[%d stack frames]", size); \
47 for (int i = 0; i < size; i++) \
48 log_debug ("%s", strings[i]); \
49 } \
50 free (strings); \
51} while (0);
52#else
53#define STRACE log_debug
54#endif
55
56#else
57
58#define BREAKPOINT
59#define STRACE log_debug
60
61#endif
62
63
64#define LIKELY(x) __builtin_expect(!!(x), true)
65#define UNLIKELY(x) __builtin_expect(!!(x), false)
66
67// TODO Cross-platform ramdisk path.
68#define TMPDIR "/tmp"
69
73
79#define VOLK_VERSION "1.0b12"
80
81#define VOLK_NS "info:volksdata:"
82
83#define KLEN sizeof(VOLK_Key)
84#define DBL_KLEN sizeof(VOLK_DoubleKey)
85#define TRP_KLEN sizeof(VOLK_TripleKey)
86#define QUAD_KLEN sizeof(VOLK_QuadKey)
87
88# define UUIDSTR_SIZE 37
89
92#define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
94
95
96/* * * RETURN CODES * * */
97
103
117typedef int VOLK_rc;
118
121#define VOLK_OK 0
122
131#define VOLK_NOACTION 88801
132
138#define VOLK_NORESULT 88802
139
145#define VOLK_END 88803
146
158#define VOLK_CONFLICT 88804
159
161#define VOLK_ERROR -88899
162
164#define VOLK_PARSE_ERR -88898
165
167#define VOLK_VALUE_ERR -88897
168
170#define VOLK_TXN_ERR -88896
171
173#define VOLK_DB_ERR -88895
174
176#define VOLK_NOT_IMPL_ERR -88894
177
179#define VOLK_IO_ERR -88893
180
182#define VOLK_MEM_ERR -88892
183
193#define VOLK_CONFLICT_ERR -88891
194
196#define VOLK_ENV_ERR -88890
197
202const char *
204
206
207
212
213#ifndef VOLK_HASH_SEED
215#define VOLK_HASH_SEED 0
216#endif
217
219#define VOLK_HASH32(buf, size, seed) XXH32 (buf, size, seed)
221#define VOLK_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
223#define VOLK_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
225#if INTPTR_MAX == INT64_MAX
226#define VOLK_HASH(...) VOLK_HASH64 (__VA_ARGS__)
227#else
228#define VOLK_HASH(...) VOLK_HASH32 (__VA_ARGS__)
229#endif
230
231
232extern char *warning_msg[], *error_msg[];
233
234extern char *VOLK_root_path;
235
241extern bool VOLK_env_is_init;
242
244typedef XXH32_hash_t VOLK_Hash32;
246typedef XXH64_hash_t VOLK_Hash64;
248typedef XXH128_hash_t VOLK_Hash128;
253#if INTPTR_MAX == INT64_MAX
255#else
256typedef VOLK_Hash32 VOLK_Hash;
257#endif
259
267
268
270typedef size_t VOLK_Key;
277
280
282
283
287
293
294
296#define VOLK_MIN_ERROR VOLK_ERROR
297
301#define VOLK_MAX_ERROR -88000
302
304#define VOLK_MIN_WARNING VOLK_NOACTION
305
310#define VOLK_MAX_WARNING 88899
311
312
313/*
314 * Only compile debug logging in debug mode. Parsing variables for debug and
315 * trace messages takes unnecessary cycles for messages that are not displayed
316 * in non-debug mode.
317 */
318#ifndef DEBUG
319#undef log_debug
320#define log_debug(...) do {} while(0)
321#undef log_trace
322#define log_trace(...) do {} while(0)
323#endif
324
331#define LOG_RC(rc) do { \
332 if ((rc) < 0) log_error (VOLK_strerror (rc)); \
333 else if ((rc) > 0) log_warn (VOLK_strerror (rc)); \
334} while (0)
335
337#define CHECK(exp, marker) do { \
338 VOLK_rc _rc = (exp); \
339 LOG_RC(_rc); \
340 if (UNLIKELY (_rc != VOLK_OK)) { \
341 log_error ( \
342 "*** PREMATURE EXIT due to error: %s", \
343 VOLK_strerror (_rc)); \
344 BREAKPOINT; \
345 goto marker; \
346 } \
347} while (0)
348
350#define PCHECK(exp, marker) do { \
351 VOLK_rc _rc = (exp); \
352 if (UNLIKELY (_rc < VOLK_OK)) { \
353 log_error ( \
354 "*** PREMATURE EXIT due to error: %s", \
355 VOLK_strerror (_rc)); \
356 LOG_RC(_rc); \
357 BREAKPOINT; \
358 goto marker; \
359 } \
360} while (0)
361
363#define NLCHECK(exp, marker) do { \
364 if (UNLIKELY ((exp) == NULL)) { \
365 log_error ("*** PREMATURE EXIT due to NULL result."); \
366 BREAKPOINT; \
367 goto marker; \
368 } \
369} while (0);
370
372#define RCCK(exp) do { \
373 VOLK_rc _rc = (exp); \
374 if (UNLIKELY (_rc != VOLK_OK)) { \
375 log_error ( \
376 "*** PREMATURE EXIT due to non-zero exit: %s (%d)", \
377 VOLK_strerror (_rc), _rc); \
378 BREAKPOINT; \
379 return _rc; \
380 } \
381} while (0)
382
384#define PRCCK(exp) do { \
385 VOLK_rc _rc = (exp); \
386 if (UNLIKELY (_rc < VOLK_OK)) { \
387 log_error ( \
388 "*** PREMATURE EXIT due to error: %s (%d)", \
389 VOLK_strerror (_rc), _rc); \
390 BREAKPOINT; \
391 return _rc; \
392 } \
393} while (0)
394
396#define NLRCCK(exp, _rc) do { \
397 if (UNLIKELY ((exp) == NULL)) { \
398 log_error ("*** PREMATURE EXIT due to NULL result."); \
399 BREAKPOINT; \
400 return _rc; \
401 } \
402} while (0)
403
405#define RCNL(exp) do { \
406 VOLK_rc _rc = (exp); \
407 if (UNLIKELY (_rc != VOLK_OK)) { \
408 log_error ( \
409 "*** PREMATURE EXIT due to non-zero exit: %s (%d)", \
410 VOLK_strerror (_rc), _rc); \
411 return NULL; \
412 } \
413} while (0);
414
416#define PRCNL(exp) do { \
417 VOLK_rc _rc = (exp); \
418 if (UNLIKELY (_rc < VOLK_OK)) { \
419 log_error ( \
420 "*** PREMATURE EXIT due to error: %s (%d)", \
421 VOLK_strerror (_rc), _rc); \
422 return NULL; \
423 } \
424} while (0)
425
427#define NLNL(exp) do { \
428 if (UNLIKELY ((exp) == NULL)) { \
429 log_error ("*** PREMATURE EXIT due to NULL result."); \
430 return NULL; \
431 } \
432} while (0)
433
435#define MALLOC_GUARD(var, rc) do { \
436 (var) = malloc (sizeof *(var)); \
437 if (UNLIKELY (var == NULL)) return (rc); \
438} while (0)
439
441#define CALLOC_GUARD(var, rc) do { \
442 (var) = calloc (1, sizeof *(var)); \
443 if (UNLIKELY (var == NULL)) return (rc); \
444} while (0)
445
446/*
447#define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), VOLK_MEM_ERR) \
448#define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), VOLK_MEM_ERR) \
449#define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
450#define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
451*/
452
453
455
461
471char *strndup (const char *src, size_t max);
472
473
480char *strdup (const char *src);
481
482
489mkdir_p (const char *path, mode_t mode);
490
491
497rm_r (const char *path);
498
499
513inline int utf8_encode (const uint32_t utf, unsigned char *out)
514{
515 if (utf <= 0x7F) {
516 // Plain ASCII
517 out[0] = (char) utf;
518 out[1] = 0;
519 return 1;
520 }
521 else if (utf <= 0x07FF) {
522 // 2-byte unicode
523 out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
524 out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
525 out[2] = 0;
526 return 2;
527 }
528 else if (utf <= 0xFFFF) {
529 // 3-byte unicode
530 out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
531 out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
532 out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
533 out[3] = 0;
534 return 3;
535 }
536 else if (utf <= 0x10FFFF) {
537 // 4-byte unicode
538 out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
539 out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
540 out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
541 out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
542 out[4] = 0;
543 return 4;
544 }
545 else {
546 // error - use replacement character
547 out[0] = (char) 0xEF;
548 out[1] = (char) 0xBF;
549 out[2] = (char) 0xBD;
550 out[3] = 0;
551 return 0;
552 }
553}
554
557
558#endif /* VOLK_CORE_H */
#define UUIDSTR_SIZE
Definition core.h:88
char * VOLK_root_path
Definition core.c:46
XXH64_hash_t VOLK_Hash64
64-bit hash data type.
Definition core.h:246
VOLK_Hash64 VOLK_Hash
Default hash data type.
Definition core.h:254
XXH128_hash_t VOLK_Hash128
128-bit hash data type.
Definition core.h:248
char * error_msg[]
Definition core.h:232
bool VOLK_env_is_init
Whether the environment is initialized.
Definition core.c:11
char * warning_msg[]
Warning messages.
Definition core.c:18
XXH32_hash_t VOLK_Hash32
32-bit hash data type.
Definition core.h:244
char * strdup(const char *src)
Replacement for GNU strdup.
Definition core.c:109
int utf8_encode(const uint32_t utf, unsigned char *out)
Encode a code point using UTF-8.
Definition core.h:513
VOLK_rc rm_r(const char *path)
Remove a directory recursively, as in Unix "rm -r".
Definition core.c:124
char * strndup(const char *src, size_t max)
Replacement for GNU strndup.
Definition core.c:92
VOLK_rc mkdir_p(const char *path, mode_t mode)
Make recursive directories.
Definition core.c:50
int VOLK_rc
Return code.
Definition core.h:117
const char * VOLK_strerror(VOLK_rc rc)
Return an error message for a return code.
Definition core.c:196
size_t VOLK_Key
Term key, i.e., hash of a serialized term.
Definition core.h:270
char uuid_str_t[37]
UUID string tpe.
Definition core.h:279
VOLK_Key VOLK_TripleKey[3]
Array of three VOLK_Key values, representing a triple.
Definition core.h:274
VOLK_bool_op
Boolean operations that can be performed on a graph.
Definition core.h:261
VOLK_Key VOLK_DoubleKey[2]
Array of two VOLK_Key values.
Definition core.h:272
VOLK_Key VOLK_QuadKey[4]
Array of three VOLK_Key values, representing a triple with context.
Definition core.h:276
@ VOLK_BOOL_XOR
Boolean XOR.
Definition core.h:265
@ VOLK_BOOL_UNION
Boolean union.
Definition core.h:262
@ VOLK_BOOL_SUBTRACTION
Boolean subtraction.
Definition core.h:263
@ VOLK_BOOL_INTERSECTION
Boolean intersection.
Definition core.h:264