Volksdata 1.0b10
RDF library
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// GDB breakpoints.
29#include <signal.h>
30#define BREAKPOINT raise (SIGUSR1)
31
32#else
33#define BREAKPOINT
34
35#endif
36
37
38#define LIKELY(x) __builtin_expect(!!(x), true)
39#define UNLIKELY(x) __builtin_expect(!!(x), false)
40
41// TODO Cross-platform ramdisk path.
42#define TMPDIR "/tmp"
43
47
53#define VOLK_VERSION "1.0b10"
54
55#define VOLK_NS "info:volksdata:"
56
57#define KLEN sizeof(VOLK_Key)
58#define DBL_KLEN sizeof(VOLK_DoubleKey)
59#define TRP_KLEN sizeof(VOLK_TripleKey)
60#define QUAD_KLEN sizeof(VOLK_QuadKey)
61
62# define UUIDSTR_SIZE 37
63
66#define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
68
69
70/* * * RETURN CODES * * */
71
77
91typedef int VOLK_rc;
92
95#define VOLK_OK 0
96
105#define VOLK_NOACTION 88801
106
112#define VOLK_NORESULT 88802
113
119#define VOLK_END 88803
120
132#define VOLK_CONFLICT 88804
133
135#define VOLK_ERROR -88899
136
138#define VOLK_PARSE_ERR -88898
139
141#define VOLK_VALUE_ERR -88897
142
144#define VOLK_TXN_ERR -88896
145
147#define VOLK_DB_ERR -88895
148
150#define VOLK_NOT_IMPL_ERR -88894
151
153#define VOLK_IO_ERR -88893
154
156#define VOLK_MEM_ERR -88892
157
167#define VOLK_CONFLICT_ERR -88891
168
170#define VOLK_ENV_ERR -88890
171
176const char *
178
180
181
186
187#ifndef VOLK_HASH_SEED
189#define VOLK_HASH_SEED 0
190#endif
191
193#define VOLK_HASH32(buf, size, seed) XXH32 (buf, size, seed)
195#define VOLK_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
197#define VOLK_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
199#if INTPTR_MAX == INT64_MAX
200#define VOLK_HASH(...) VOLK_HASH64 (__VA_ARGS__)
201#else
202#define VOLK_HASH(...) VOLK_HASH32 (__VA_ARGS__)
203#endif
204
205
206extern char *warning_msg[], *error_msg[];
207
208extern char *VOLK_root_path;
209
215extern bool VOLK_env_is_init;
216
218typedef XXH32_hash_t VOLK_Hash32;
220typedef XXH64_hash_t VOLK_Hash64;
222typedef XXH128_hash_t VOLK_Hash128;
227#if INTPTR_MAX == INT64_MAX
229#else
230typedef VOLK_Hash32 VOLK_Hash;
231#endif
233
241
242
244typedef size_t VOLK_Key;
251
254
256
257
261
267
268
270#define VOLK_MIN_ERROR VOLK_ERROR
271
275#define VOLK_MAX_ERROR -88000
276
278#define VOLK_MIN_WARNING VOLK_NOACTION
279
284#define VOLK_MAX_WARNING 88899
285
286
287/*
288 * Only compile debug logging in debug mode. Parsing variables for debug and
289 * trace messages takes unnecessary cycles for messages that are not displayed
290 * in non-debug mode.
291 */
292#ifndef DEBUG
293#undef log_debug
294#define log_debug(...) do {} while(0)
295#undef log_trace
296#define log_trace(...) do {} while(0)
297#endif
298
305#define LOG_RC(rc) do { \
306 if ((rc) < 0) log_error (VOLK_strerror (rc)); \
307 else if ((rc) > 0) log_warn (VOLK_strerror (rc)); \
308} while (0)
309
311#define CHECK(exp, marker) do { \
312 VOLK_rc _rc = (exp); \
313 LOG_RC(_rc); \
314 if (UNLIKELY (_rc != VOLK_OK)) { \
315 log_error ( \
316 "*** PREMATURE EXIT due to error: %s", \
317 VOLK_strerror (_rc)); \
318 BREAKPOINT; \
319 goto marker; \
320 } \
321} while (0)
322
324#define PCHECK(exp, marker) do { \
325 VOLK_rc _rc = (exp); \
326 if (UNLIKELY (_rc < VOLK_OK)) { \
327 log_error ( \
328 "*** PREMATURE EXIT due to error: %s", \
329 VOLK_strerror (_rc)); \
330 LOG_RC(_rc); \
331 BREAKPOINT; \
332 goto marker; \
333 } \
334} while (0)
335
337#define NLCHECK(exp, marker) do { \
338 if (UNLIKELY ((exp) == NULL)) { \
339 log_error ("*** PREMATURE EXIT due to NULL result."); \
340 BREAKPOINT; \
341 goto marker; \
342 } \
343} while (0);
344
346#define RCCK(exp) do { \
347 VOLK_rc _rc = (exp); \
348 if (UNLIKELY (_rc != VOLK_OK)) { \
349 log_error ( \
350 "*** PREMATURE EXIT due to non-zero exit: %s (%d)", \
351 VOLK_strerror (_rc), _rc); \
352 BREAKPOINT; \
353 return _rc; \
354 } \
355} while (0)
356
358#define PRCCK(exp) do { \
359 VOLK_rc _rc = (exp); \
360 if (UNLIKELY (_rc < VOLK_OK)) { \
361 log_error ( \
362 "*** PREMATURE EXIT due to error: %s (%d)", \
363 VOLK_strerror (_rc), _rc); \
364 BREAKPOINT; \
365 return _rc; \
366 } \
367} while (0)
368
370#define NLRCCK(exp, _rc) do { \
371 if (UNLIKELY ((exp) == NULL)) { \
372 log_error ("*** PREMATURE EXIT due to NULL result."); \
373 BREAKPOINT; \
374 return _rc; \
375 } \
376} while (0)
377
379#define RCNL(exp) do { \
380 VOLK_rc _rc = (exp); \
381 if (UNLIKELY (_rc != VOLK_OK)) { \
382 log_error ( \
383 "*** PREMATURE EXIT due to non-zero exit: %s (%d)", \
384 VOLK_strerror (_rc), _rc); \
385 return NULL; \
386 } \
387} while (0);
388
390#define PRCNL(exp) do { \
391 VOLK_rc _rc = (exp); \
392 if (UNLIKELY (_rc < VOLK_OK)) { \
393 log_error ( \
394 "*** PREMATURE EXIT due to error: %s (%d)", \
395 VOLK_strerror (_rc), _rc); \
396 return NULL; \
397 } \
398} while (0)
399
401#define NLNL(exp) do { \
402 if (UNLIKELY ((exp) == NULL)) { \
403 log_error ("*** PREMATURE EXIT due to NULL result."); \
404 return NULL; \
405 } \
406} while (0)
407
409#define MALLOC_GUARD(var, rc) do { \
410 (var) = malloc (sizeof *(var)); \
411 if (UNLIKELY (var == NULL)) return (rc); \
412} while (0)
413
415#define CALLOC_GUARD(var, rc) do { \
416 (var) = calloc (1, sizeof *(var)); \
417 if (UNLIKELY (var == NULL)) return (rc); \
418} while (0)
419
420/*
421#define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), VOLK_MEM_ERR) \
422#define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), VOLK_MEM_ERR) \
423#define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
424#define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
425*/
426
427
429
435
445char *strndup (const char *src, size_t max);
446
447
454char *strdup (const char *src);
455
456
463mkdir_p (const char *path, mode_t mode);
464
465
471rm_r (const char *path);
472
473
487inline int utf8_encode (const uint32_t utf, unsigned char *out)
488{
489 if (utf <= 0x7F) {
490 // Plain ASCII
491 out[0] = (char) utf;
492 out[1] = 0;
493 return 1;
494 }
495 else if (utf <= 0x07FF) {
496 // 2-byte unicode
497 out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
498 out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
499 out[2] = 0;
500 return 2;
501 }
502 else if (utf <= 0xFFFF) {
503 // 3-byte unicode
504 out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
505 out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
506 out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
507 out[3] = 0;
508 return 3;
509 }
510 else if (utf <= 0x10FFFF) {
511 // 4-byte unicode
512 out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
513 out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
514 out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
515 out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
516 out[4] = 0;
517 return 4;
518 }
519 else {
520 // error - use replacement character
521 out[0] = (char) 0xEF;
522 out[1] = (char) 0xBF;
523 out[2] = (char) 0xBD;
524 out[3] = 0;
525 return 0;
526 }
527}
528
531
532#endif /* VOLK_CORE_H */
#define UUIDSTR_SIZE
Definition core.h:62
char * VOLK_root_path
Definition core.c:46
XXH64_hash_t VOLK_Hash64
64-bit hash data type.
Definition core.h:220
VOLK_Hash64 VOLK_Hash
Default hash data type.
Definition core.h:228
XXH128_hash_t VOLK_Hash128
128-bit hash data type.
Definition core.h:222
char * error_msg[]
Definition core.h:206
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:218
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:487
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:91
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:244
char uuid_str_t[37]
UUID string tpe.
Definition core.h:253
VOLK_Key VOLK_TripleKey[3]
Array of three VOLK_Key values, representing a triple.
Definition core.h:248
VOLK_bool_op
Boolean operations that can be performed on a graph.
Definition core.h:235
VOLK_Key VOLK_DoubleKey[2]
Array of two VOLK_Key values.
Definition core.h:246
VOLK_Key VOLK_QuadKey[4]
Array of three VOLK_Key values, representing a triple with context.
Definition core.h:250
@ VOLK_BOOL_XOR
Boolean XOR.
Definition core.h:239
@ VOLK_BOOL_UNION
Boolean union.
Definition core.h:236
@ VOLK_BOOL_SUBTRACTION
Boolean subtraction.
Definition core.h:237
@ VOLK_BOOL_INTERSECTION
Boolean intersection.
Definition core.h:238