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
41/* TODO
42static int store_reg_comp_fn (const void *a, const void *b, void *_unused)
43{
44 (void) _unused;
45 const StoreRegEntry *ela = a;
46 const StoreRegEntry *elb = b;
47
48 return strcmp (ela->id, elb->id);
49}
50
51static uint64_t store_reg_hash_fn (
52 const void *item, uint64_t seed0, uint64_t _unused)
53{
54 (void) _unused;
55
56 const StoreRegEntry *el = item;
57 return (uint64_t) VOLK_HASH64 (el->id, strlen (el->id), seed0);
58}
59*/
60
61
62/*
63 * API
64 */
65
68{
69 if (VOLK_env_is_init) {
71 return VOLK_NOACTION;
72 }
73
74 char *_loglevel = getenv ("VOLK_LOGLEVEL");
75 log_set_level ((_loglevel == NULL) ? LOG_INFO : atoi (_loglevel));
76
77 // Default namespace map.
78 VOLK_default_nsm = hashmap_new (
79 sizeof (NSEntry), 0, VOLK_HASH_SEED, 0,
80 nsmap_hash_fn, nsmap_comp_fn, nsmap_free_fn, NULL);
81 if (hashmap_oom (VOLK_default_nsm)) return VOLK_MEM_ERR;
82
83
85 for (int i = 0; init_nsmap[i][0] != NULL; i++)
87
88 // Default context URI.
89 const char *default_ctx_str = getenv ("VOLK_DEFAULT_CTX");
90 if (!default_ctx_str) default_ctx_str = DEFAULT_CTX_LABEL;
91 VOLK_default_ctx = VOLK_iriref_new (default_ctx_str);
95
96 // Initialize term cache.
99
100 // Create and cache default literal datatype key.
101 // This will be done only once in the program, so no need to check for
102 // duplicates.
106 PRCCK (rc);
107
108 // Create hashmap for store registry.
109 /* TODO
110 VOLK_store_reg = hashmap_new (
111 sizeof (StoreRegEntry), 1, VOLK_HASH_SEED, 0,
112 store_reg_hash_fn, store_reg_comp_fn, NULL, NULL);
113 if (hashmap_oom (VOLK_store_reg)) return VOLK_MEM_ERR;
114 */
115
116 VOLK_env_is_init = true;
117 log_info ("Volksdata environment initialized.");
118
119 // Set automatic teardown TODO Is this a good idea?
120 atexit (VOLK_done);
121
122 return VOLK_OK;
123}
124
125
126void
128{
129 if (!VOLK_env_is_init) return;
130
131 // Free default NS map and context.
134 hashmap_free (VOLK_default_nsm);
135
136 // Free ID cache, including default literal datatype.
137 hashmap_free (VOLK_term_cache);
139 VOLK_env_is_init = false;
140
141 // Free store registry.
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:43
VOLK_rc VOLK_init(void)
Initialize the default environment.
Definition environment.c:67
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_RC(rc)
Log an error or warning for return codes that are not VOLK_OK.
Definition core.h:284
#define PRCCK(exp)
Return exp return value if it is of VOLK_rc type and negative (=error).
Definition core.h:333
#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