Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
environment.c
Go to the documentation of this file.
3
4
8
9typedef struct store_reg_entry {
10 char *id;
11 void *env;
13
14
18static int nsmap_comp_fn (const void *a, const void *b, void *_unused)
19{
20 (void) _unused;
21
22 const NSEntry *nsa = a;
23 const NSEntry *nsb = b;
24
25 return strncmp (nsa->pfx, nsb->pfx, PFX_LEN);
26}
27
28static uint64_t nsmap_hash_fn (
29 const void *item, uint64_t seed0, uint64_t _unused)
30{
31 (void) _unused;
32 const NSEntry *nse = item;
33
34 return (uint64_t) VOLK_HASH64 (nse->pfx, strlen (nse->pfx), seed0);
35}
36
37static void nsmap_free_fn (void *item)
38{ free (((NSEntry *) item)->ns); }
39
40
41static int store_reg_comp_fn (const void *a, const void *b, void *_unused)
42{
43 (void) _unused;
44 const StoreRegEntry *ela = a;
45 const StoreRegEntry *elb = b;
46
47 return strcmp (ela->id, elb->id);
48}
49
50static uint64_t store_reg_hash_fn (
51 const void *item, uint64_t seed0, uint64_t _unused)
52{
53 (void) _unused;
54
55 const StoreRegEntry *el = item;
56 return (uint64_t) VOLK_HASH64 (el->id, strlen (el->id), seed0);
57}
58
59
60/*
61 * API
62 */
63
66{
67 if (VOLK_env_is_init) {
69 return VOLK_NOACTION;
70 }
71
72#ifdef DEBUG
73 // In debug mode, always use max logging.
74 int loglevel = LOG_TRACE;
75#else
76 char *_loglevel = getenv ("VOLK_LOGLEVEL");
77 int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
78#endif
79 log_set_level (loglevel);
80
81 // Default namespace map.
82 VOLK_default_nsm = hashmap_new (
83 sizeof (NSEntry), 0, VOLK_HASH_SEED, 0,
84 nsmap_hash_fn, nsmap_comp_fn, nsmap_free_fn, NULL);
85 if (hashmap_oom (VOLK_default_nsm)) return VOLK_MEM_ERR;
86
87
89 for (int i = 0; init_nsmap[i][0] != NULL; i++)
91
92 // Default context URI.
93 const char *default_ctx_str = getenv ("VOLK_DEFAULT_CTX");
94 if (!default_ctx_str) default_ctx_str = DEFAULT_CTX_LABEL;
95 VOLK_default_ctx = VOLK_iriref_new (default_ctx_str);
99
100 // Initialize term cache.
103
104 // Create and cache default literal datatype key.
105 // This will be done only once in the program, so no need to check for
106 // duplicates.
110 PRCCK (rc);
111
112 // Create hashmap for store registry.
113 VOLK_store_reg = hashmap_new (
114 sizeof (StoreRegEntry), 1, VOLK_HASH_SEED, 0,
115 store_reg_hash_fn, store_reg_comp_fn, NULL, NULL);
116 if (hashmap_oom (VOLK_store_reg)) return VOLK_MEM_ERR;
117
118 VOLK_env_is_init = true;
119 log_info ("Volksdata environment initialized.");
120
121 // Set automatic teardown TODO Is this a good idea?
122 atexit (VOLK_done);
123
124 return VOLK_OK;
125}
126
127
128void
130{
131 if (!VOLK_env_is_init) return;
132
133 // Free default NS map and context.
136 hashmap_free (VOLK_default_nsm);
137
138 // Free ID cache, including default literal datatype.
139 hashmap_free (VOLK_term_cache);
141 VOLK_env_is_init = false;
142
143 log_info ("Volksdata environment torn down.");
144}
145
146
147/*
148 * Externals.
149 */
150
151struct hashmap *VOLK_store_reg = NULL;
const char * init_nsmap[][2]
Initial namespace map.
Definition bootstrap.h:6
#define UNLIKELY(x)
Definition core.h:39
Handle LSUP environment initialization and teardown.
VOLK_Buffer * VOLK_default_ctx_buf
Serialized default context.
Definition buffer.c:5
void VOLK_buffer_free(VOLK_Buffer *buf)
Free a buffer.
Definition buffer.c:97
void VOLK_done(void)
Close the default environment.
#define DEFAULT_CTX_LABEL
Default context.
Definition environment.h:41
VOLK_rc VOLK_init(void)
Initialize the default environment.
Definition environment.c:65
struct hashmap * VOLK_store_reg
Internal registry of store handles.
#define VOLK_HASH_SEED
Seed used for all hashing. Compile-time configurable.
Definition core.h:175
#define VOLK_HASH64(buf, size, seed)
Default 64-bit hashing function.
Definition core.h:181
bool VOLK_env_is_init
Whether the environment is initialized.
Definition core.c:11
struct hashmap * VOLK_default_nsm
Default namespace prefix map.
Definition namespace.c:38
VOLK_rc VOLK_nsmap_add(const char *pfx, const char *nsstr)
Add a prefix -> namespace pair to the map or update it.
Definition namespace.c:42
#define PFX_LEN
Namespace prefix length, including terminator.
Definition namespace.h:16
#define LOG_TRACE(...)
Definition core.h:276
#define LOG_RC(rc)
Log an error or warning for return codes that are not VOLK_OK.
Definition core.h:285
#define PRCCK(exp)
Return exp return value if it is of VOLK_rc type and negative (=error).
Definition core.h:334
#define VOLK_ERROR
Generic error return code.
Definition core.h:123
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:144
#define VOLK_OK
Generic success return code.
Definition core.h:83
#define VOLK_NOACTION
No action taken.
Definition core.h:93
int VOLK_rc
Definition core.h:79
VOLK_rc VOLK_term_set_add(VOLK_TermSet *ts, VOLK_Term *term, VOLK_Term **existing)
Add term to a term set.
Definition term.c:562
VOLK_Term * VOLK_iriref_new(const char *data)
Create an IRI reference.
Definition term.h:192
VOLK_Term * VOLK_default_datatype
Default literal data type URI.
Definition term.c:60
VOLK_TermSet * VOLK_term_set_new()
Create a new term set.
Definition term.c:549
void VOLK_term_free(VOLK_Term *term)
Definition term.c:387
VOLK_Buffer * VOLK_term_serialize(const VOLK_Term *term)
Serialize a term into a buffer.
Definition term.c:293
#define DEFAULT_DTYPE
Default data type for untyped literals (prefixed IRI).
Definition term.h:20
VOLK_Term * VOLK_default_ctx
Default context.
Definition term.c:59
VOLK_TermSet * VOLK_term_cache
Global term cache.
Definition term.c:61
Prefix / Namespace pair.
Definition namespace.h:26
VOLK_ns_pfx pfx
Definition namespace.h:27