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 VOLK_ERROR
250
254#define VOLK_MAX_ERROR -88000
255
257#define VOLK_MIN_WARNING VOLK_NOACTION
258
263#define VOLK_MAX_WARNING 88899
264
265
266/*
267 * Only compile debug logging 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#ifndef DEBUG
272#undef log_debug
273#define log_debug(...) do {} while(0)
274#undef log_trace
275#define log_trace(...) do {} while(0)
276#endif
277
284#define LOG_RC(rc) do { \
285 if ((rc) < 0) log_error (VOLK_strerror (rc)); \
286 else if ((rc) > 0) log_warn (VOLK_strerror (rc)); \
287} while (0)
288
290#define CHECK(exp, marker) do { \
291 VOLK_rc _rc = (exp); \
292 LOG_RC(_rc); \
293 if (UNLIKELY (_rc != VOLK_OK)) { \
294 log_error ( \
295 "*** PREMATURE EXIT due to error: %s", \
296 VOLK_strerror (_rc)); \
297 goto marker; \
298 } \
299} while (0)
300
302#define PCHECK(exp, marker) do { \
303 VOLK_rc _rc = (exp); \
304 if (UNLIKELY (_rc < VOLK_OK)) { \
305 log_error ( \
306 "*** PREMATURE EXIT due to error: %s", \
307 VOLK_strerror (_rc)); \
308 LOG_RC(_rc); \
309 goto marker; \
310 } \
311} while (0)
312
314#define NLCHECK(exp, marker) do { \
315 if (UNLIKELY ((exp) == NULL)) { \
316 log_error ("*** PREMATURE EXIT due to NULL result."); \
317 goto marker; \
318 } \
319} while (0);
320
322#define RCCK(exp) do { \
323 VOLK_rc _rc = (exp); \
324 if (UNLIKELY (_rc != VOLK_OK)) { \
325 log_error ( \
326 "*** PREMATURE EXIT due to error: %s (%d)", \
327 VOLK_strerror (_rc), _rc); \
328 return _rc; \
329 } \
330} while (0)
331
333#define PRCCK(exp) do { \
334 VOLK_rc _rc = (exp); \
335 if (UNLIKELY (_rc < VOLK_OK)) { \
336 log_error ( \
337 "*** PREMATURE EXIT due to error: %s (%d)", \
338 VOLK_strerror (_rc), _rc); \
339 return _rc; \
340 } \
341} while (0)
342
344#define NLRCCK(exp, _rc) do { \
345 if (UNLIKELY ((exp) == NULL)) { \
346 log_error ("*** PREMATURE EXIT due to NULL result."); \
347 return _rc; \
348 } \
349} while (0)
350
352#define RCNL(exp) do { \
353 VOLK_rc _rc = (exp); \
354 if (UNLIKELY (_rc != VOLK_OK)) { \
355 log_error ( \
356 "*** PREMATURE EXIT due to error: %s (%d)", \
357 VOLK_strerror (_rc), _rc); \
358 return NULL; \
359 } \
360} while (0);
361
363#define PRCNL(exp) do { \
364 VOLK_rc _rc = (exp); \
365 if (UNLIKELY (_rc < VOLK_OK)) { \
366 log_error ( \
367 "*** PREMATURE EXIT due to error: %s (%d)", \
368 VOLK_strerror (_rc), _rc); \
369 return NULL; \
370 } \
371} while (0)
372
374#define NLNL(exp) do { \
375 if (UNLIKELY ((exp) == NULL)) { \
376 log_error ("*** PREMATURE EXIT due to NULL result."); \
377 return NULL; \
378 } \
379} while (0)
380
382#define MALLOC_GUARD(var, rc) do { \
383 (var) = malloc (sizeof *(var)); \
384 if (UNLIKELY (var == NULL)) return (rc); \
385} while (0)
386
388#define CALLOC_GUARD(var, rc) do { \
389 (var) = calloc (1, sizeof *(var)); \
390 if (UNLIKELY (var == NULL)) return (rc); \
391} while (0)
392
393/*
394#define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), VOLK_MEM_ERR) \
395#define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), VOLK_MEM_ERR) \
396#define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
397#define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
398*/
399
400
410char *strndup (const char *src, size_t max);
411
412
419char *strdup (const char *src);
420
421
428mkdir_p (const char *path, mode_t mode);
429
430
436rm_r (const char *path);
437
438
452inline int utf8_encode (const uint32_t utf, unsigned char *out)
453{
454 if (utf <= 0x7F) {
455 // Plain ASCII
456 out[0] = (char) utf;
457 out[1] = 0;
458 return 1;
459 }
460 else if (utf <= 0x07FF) {
461 // 2-byte unicode
462 out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
463 out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
464 out[2] = 0;
465 return 2;
466 }
467 else if (utf <= 0xFFFF) {
468 // 3-byte unicode
469 out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
470 out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
471 out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
472 out[3] = 0;
473 return 3;
474 }
475 else if (utf <= 0x10FFFF) {
476 // 4-byte unicode
477 out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
478 out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
479 out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
480 out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
481 out[4] = 0;
482 return 4;
483 }
484 else {
485 // error - use replacement character
486 out[0] = (char) 0xEF;
487 out[1] = (char) 0xBF;
488 out[2] = (char) 0xBD;
489 out[3] = 0;
490 return 0;
491 }
492}
493
494
496
497#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:452
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:196