Volksdata 1.0b7
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 (SIGINT)
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.0b7"
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
79typedef int VOLK_rc;
80
83#define VOLK_OK 0
84
93#define VOLK_NOACTION 88801
94
100#define VOLK_NORESULT 88802
101
107#define VOLK_END 88803
108
120#define VOLK_CONFLICT 88804
121
123#define VOLK_ERROR -88899
124
126#define VOLK_PARSE_ERR -88898
127
129#define VOLK_VALUE_ERR -88897
130
132#define VOLK_TXN_ERR -88896
133
135#define VOLK_DB_ERR -88895
136
138#define VOLK_NOT_IMPL_ERR -88894
139
141#define VOLK_IO_ERR -88893
142
144#define VOLK_MEM_ERR -88892
145
155#define VOLK_CONFLICT_ERR -88891
156
158#define VOLK_ENV_ERR -88890
159
162const char *
164
166
167
172
173#ifndef VOLK_HASH_SEED
175#define VOLK_HASH_SEED 0
176#endif
177
179#define VOLK_HASH32(buf, size, seed) XXH32 (buf, size, seed)
181#define VOLK_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
183#define VOLK_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
185#if INTPTR_MAX == INT64_MAX
186#define VOLK_HASH(...) VOLK_HASH64 (__VA_ARGS__)
187#else
188#define VOLK_HASH(...) VOLK_HASH32 (__VA_ARGS__)
189#endif
190
191
192extern char *warning_msg[], *error_msg[];
193
194extern char *VOLK_root_path;
195
201extern bool VOLK_env_is_init;
202
204typedef XXH32_hash_t VOLK_Hash32;
206typedef XXH64_hash_t VOLK_Hash64;
208typedef XXH128_hash_t VOLK_Hash128;
213#if INTPTR_MAX == INT64_MAX
215#else
216typedef VOLK_Hash32 VOLK_Hash;
217#endif
219
227
228
230typedef size_t VOLK_Key;
237
240
242
243
247
249#define VOLK_MIN_ERROR -88899
250
254#define VOLK_MAX_ERROR -88000
255
257#define VOLK_MIN_WARNING 88800
258
263#define VOLK_MAX_WARNING 88899
264
265
266/*
267 * Only compile debug code in debug mode. Parsing variables for debug and
268 * trace messages takes unnecessary cycles for messages that are not displayed
269 * in non-debug mode.
270 */
271#ifdef DEBUG
272#define LOG_DEBUG(...) log_debug(__VA_ARGS__)
273#define LOG_TRACE(...) log_trace(__VA_ARGS__)
274#else
275#define LOG_DEBUG(...)
276#define LOG_TRACE(...)
277#endif
278
285#define LOG_RC(rc) do { \
286 if ((rc) < 0) log_error (VOLK_strerror (rc)); \
287 else if ((rc) > 0) log_warn (VOLK_strerror (rc)); \
288} while (0)
289
291#define CHECK(exp, marker) do { \
292 VOLK_rc _rc = (exp); \
293 LOG_RC(_rc); \
294 if (UNLIKELY (_rc != VOLK_OK)) { \
295 log_error ( \
296 "*** PREMATURE EXIT due to error: %s", \
297 VOLK_strerror (_rc)); \
298 goto marker; \
299 } \
300} while (0)
301
303#define PCHECK(exp, marker) do { \
304 VOLK_rc _rc = (exp); \
305 if (UNLIKELY (_rc < VOLK_OK)) { \
306 log_error ( \
307 "*** PREMATURE EXIT due to error: %s", \
308 VOLK_strerror (_rc)); \
309 LOG_RC(_rc); \
310 goto marker; \
311 } \
312} while (0)
313
315#define NLCHECK(exp, marker) do { \
316 if (UNLIKELY ((exp) == NULL)) { \
317 log_error ("*** PREMATURE EXIT due to NULL result."); \
318 goto marker; \
319 } \
320} while (0);
321
323#define RCCK(exp) do { \
324 VOLK_rc _rc = (exp); \
325 if (UNLIKELY (_rc != VOLK_OK)) { \
326 log_error ( \
327 "*** PREMATURE EXIT due to error: %s", \
328 VOLK_strerror (_rc)); \
329 return _rc; \
330 } \
331} while (0)
332
334#define PRCCK(exp) do { \
335 VOLK_rc _rc = (exp); \
336 if (UNLIKELY (_rc < VOLK_OK)) { \
337 log_error ( \
338 "*** PREMATURE EXIT due to error: %s", \
339 VOLK_strerror (_rc)); \
340 return _rc; \
341 } \
342} while (0)
343
345#define RCNL(exp) do { \
346 VOLK_rc _rc = (exp); \
347 if (UNLIKELY (_rc != VOLK_OK)) { \
348 log_error ( \
349 "*** PREMATURE EXIT due to error: %s", \
350 VOLK_strerror (_rc)); \
351 return NULL; \
352 } \
353} while (0);
354
356#define PRCNL(exp) do { \
357 VOLK_rc _rc = (exp); \
358 if (UNLIKELY (_rc < VOLK_OK)) { \
359 log_error ( \
360 "*** PREMATURE EXIT due to error: %s", \
361 VOLK_strerror (_rc)); \
362 return NULL; \
363 } \
364} while (0)
365
367#define NLNL(exp) do { \
368 if (UNLIKELY ((exp) == NULL)) { \
369 log_error ("*** PREMATURE EXIT due to NULL result."); \
370 return NULL; \
371 } \
372} while (0)
373
375#define MALLOC_GUARD(var, rc) do { \
376 (var) = malloc (sizeof *(var)); \
377 if (UNLIKELY (var == NULL)) return (rc); \
378} while (0)
379
381#define CALLOC_GUARD(var, rc) do { \
382 (var) = calloc (1, sizeof *(var)); \
383 if (UNLIKELY (var == NULL)) return (rc); \
384} while (0)
385
386/*
387#define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), VOLK_MEM_ERR) \
388#define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), VOLK_MEM_ERR) \
389#define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
390#define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
391*/
392
393
403char *strndup (const char *src, size_t max);
404
405
412char *strdup (const char *src);
413
414
421mkdir_p (const char *path, mode_t mode);
422
423
429rm_r (const char *path);
430
431
445inline int utf8_encode (const uint32_t utf, unsigned char *out)
446{
447 if (utf <= 0x7F) {
448 // Plain ASCII
449 out[0] = (char) utf;
450 out[1] = 0;
451 return 1;
452 }
453 else if (utf <= 0x07FF) {
454 // 2-byte unicode
455 out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
456 out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
457 out[2] = 0;
458 return 2;
459 }
460 else if (utf <= 0xFFFF) {
461 // 3-byte unicode
462 out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
463 out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
464 out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
465 out[3] = 0;
466 return 3;
467 }
468 else if (utf <= 0x10FFFF) {
469 // 4-byte unicode
470 out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
471 out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
472 out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
473 out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
474 out[4] = 0;
475 return 4;
476 }
477 else {
478 // error - use replacement character
479 out[0] = (char) 0xEF;
480 out[1] = (char) 0xBF;
481 out[2] = (char) 0xBD;
482 out[3] = 0;
483 return 0;
484 }
485}
486
487
489
490#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:206
VOLK_Hash64 VOLK_Hash
Default hash data type.
Definition core.h:214
XXH128_hash_t VOLK_Hash128
128-bit hash data type.
Definition core.h:208
char * error_msg[]
Definition core.h:192
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:204
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:445
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
size_t VOLK_Key
Term key, i.e., hash of a serialized term.
Definition core.h:230
VOLK_Key VOLK_TripleKey[3]
Array of three VOLK_Key values, representing a triple.
Definition core.h:234
char uuid_str_t[UUIDSTR_SIZE]
UUID string tpe.
Definition core.h:239
VOLK_bool_op
Boolean operations that can be performed on a graph.
Definition core.h:221
VOLK_Key VOLK_DoubleKey[2]
Array of two VOLK_Key values.
Definition core.h:232
VOLK_Key VOLK_QuadKey[4]
Array of three VOLK_Key values, representing a triple with context.
Definition core.h:236
@ VOLK_BOOL_XOR
Boolean XOR.
Definition core.h:225
@ VOLK_BOOL_UNION
Boolean union.
Definition core.h:222
@ VOLK_BOOL_SUBTRACTION
Boolean subtraction.
Definition core.h:223
@ VOLK_BOOL_INTERSECTION
Boolean intersection.
Definition core.h:224
int VOLK_rc
Definition core.h:79
const char * VOLK_strerror(VOLK_rc rc)
Return an error message for a return code.
Definition core.c:195