Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
writer_trig.c
Go to the documentation of this file.
2
3
4// Position of a cursor.
12
20 VOLK_Graph * gr;
21 TTLEncodeIterator * gr_it;
23 void * txn;
24};
25
26
27/* * * Codec functions. * * */
28
30static VOLK_rc
31trig_build_prolog (TRIGEncodeIterator *it, char **res_p)
32{
33 char *res = fmt_header ("# ");
34
35 const char ***nsm = VOLK_nsmap_dump ();
36 char *ns_tpl = "@prefix %s: <%s> .\n";
37
38 // Prefix map.
39 for (size_t i = 0; nsm[i]; i++) {
40 const char **ns = nsm[i];
41 size_t old_len = strlen (res);
42 size_t ns_len = strlen (ns[0]) + strlen (ns[1]) + strlen (ns_tpl);
43 char *tmp = realloc (res, old_len + ns_len + 1);
44 if (UNLIKELY (!tmp)) return VOLK_MEM_ERR;
45 res = tmp;
46
47 sprintf (res + old_len, ns_tpl, ns[0], ns[1]);
48 free (ns);
49 }
50 free (nsm);
51
52 *res_p = res;
53 it->pos = TRIG_POS_START_BODY;
54 it->rc = VOLK_OK;
55
56 return VOLK_OK;
57}
58
59
60void *
62{
63 NLNL (store);
64 if (!(VOLK_store_features (store) & VOLK_STORE_CTX)) {
65 log_error ("Store is not context-aware.");
66 return NULL;
67 }
68 TRIGEncodeIterator *it;
69 CALLOC_GUARD (it, NULL);
70
71 it->store = store;
72 RCNL (VOLK_store_begin (it->store, VOLK_STORE_TXN_RO, &it->txn));
73 it->gr_uri_ts = VOLK_graph_list_txn (it->txn, it->store);
74 //it->gr_it = NULL;
75 it->pos = TRIG_POS_START;
76
77 it->rc = VOLK_NORESULT;
78
79 return it;
80}
81
82
84trig_encode_store_iter (void *h, char **res_p)
85{
86 TRIGEncodeIterator *it = h;
87
88 if (it->pos == TRIG_POS_END) return VOLK_END;
89
90 // Beginning of overall encoding process: write prolog.
91 if (it->pos == TRIG_POS_START) return trig_build_prolog (it, res_p);
92 else if (it->pos == TRIG_POS_END_GR) {
93 // End of graph encoding loop: finalize graph codec iterator.
94 ttl_encode_graph_done (it->gr_it);
95 it->gr_it = NULL;
96 }
97 char *gr_uri_trig = NULL;
98
99 // Preallocate closing brace + newline in any case.
100 size_t out_size = 3;
101
102 // After writing prolog: populate first graph.
103 // Or: a graph just finished encoding, try to get the next one.
104 // Allocate the boundary symbols.
105reload_graph:
106 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
107
108 VOLK_Term *gr_uri = VOLK_term_set_pop (it->gr_uri_ts);
109 if (gr_uri) {
110 // The default context URI is the default graph.
111 if (VOLK_term_equals (gr_uri, VOLK_default_ctx)) {
112 gr_uri_trig = NULL;
113 it->gr = VOLK_graph_new (it->store, VOLK_default_ctx->data);
114 out_size += 3;
115 } else {
116 ttl_encode_term (gr_uri, &gr_uri_trig);
117 it->gr = VOLK_graph_new (it->store, gr_uri->data);
118 VOLK_term_free (gr_uri);
119 out_size += strlen (gr_uri_trig) + 4;
120 }
121 TTLEncodeOptions opt = {
123 .txn = it->txn,
124 };
125 it->gr_it = ttl_encode_graph_init (it->gr, &opt);
126 } else {
127 log_info ("End of store encoding.");
128 it->pos = TRIG_POS_END;
129 }
130 }
131 char *res = realloc (*res_p, out_size);
132 NLRCCK (res, VOLK_MEM_ERR);
133 *res_p = res;
134
135 if (it->pos == TRIG_POS_GRAPH) {
136 VOLK_rc gr_rc = ttl_encode_graph_iter (it->gr_it, res_p);
137 if (gr_rc == VOLK_END || gr_rc == VOLK_NORESULT) {
138 ttl_encode_graph_done (it->gr_it);
139 VOLK_graph_free (it->gr);
140 it->pos = TRIG_POS_END_GR;
141 goto reload_graph;
142 }
143 } else {
144 // Write closing and/or opening braces. This uses up the iteration.
145 size_t offset = 0; // Where to start writing graph name.
146 if (it->pos == TRIG_POS_END_GR || it->pos == TRIG_POS_END) {
147 sprintf (res, "}\n");
148 offset = 2;
149 }
150 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
151 if (!gr_uri_trig) sprintf (res + offset, "\n{\n");
152 else {
153 sprintf (res + offset, "\n%s {\n", gr_uri_trig);
154 free (gr_uri_trig);
155 }
156 it->pos = TRIG_POS_GRAPH;
157 it->rc = VOLK_OK;
158 }
159 }
160
161 return it->rc;
162}
163
164
165void
167{
168 if (!h) return;
169 TRIGEncodeIterator *it = h;
170
171 VOLK_store_abort (it->store, it->txn);
172 VOLK_term_set_free (it->gr_uri_ts);
173 free (it);
174}
#define UNLIKELY(x)
Definition core.h:39
char * fmt_header(char *pfx)
Format an informational header.
@ VOLK_CODEC_NO_PROLOG
Do not generate prolog.
@ VOLK_CODEC_INDENT
VOLK_TermSet * VOLK_graph_list_txn(void *txn, VOLK_Store *store)
List all graph URIs in a store.
Definition graph.c:634
void VOLK_graph_free(VOLK_Graph *gr)
Free a graph.
Definition graph.c:246
VOLK_Graph * VOLK_graph_new(VOLK_Store *store, const char *uri_str)
Create new graph.
Definition graph.c:44
const char *** VOLK_nsmap_dump(void)
Dump all entries of the namespace map.
Definition namespace.c:191
#define RCNL(exp)
Return NULL if exp returns a nonzero value.
Definition core.h:352
#define NLNL(exp)
Log error and return NULL if exp is NULL.
Definition core.h:374
#define CALLOC_GUARD(var, rc)
Allocate one pointer with calloc and return rc if it fails.
Definition core.h:388
#define NLRCCK(exp, _rc)
Return rc return code if exp is NULL.
Definition core.h:344
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:144
#define VOLK_NORESULT
No result yielded.
Definition core.h:100
#define VOLK_END
Loop end.
Definition core.h:107
#define VOLK_OK
Generic success return code.
Definition core.h:83
int VOLK_rc
Definition core.h:79
@ VOLK_STORE_TXN_RO
Start a read-only transaction.
@ VOLK_STORE_CTX
VOLK_StoreFeature VOLK_store_features(const VOLK_Store *store)
Feature flags belonging to the store interface.
Definition store.c:86
VOLK_rc VOLK_store_begin(VOLK_Store *store, VOLK_StoreFlags flags, void **txn)
Begin a transaction.
Definition store.c:96
void VOLK_store_abort(VOLK_Store *store, void *txn)
Abort (roll back) a transaction.
Definition store.c:115
struct hashmap VOLK_TermSet
a set of unique terms.
Definition term.h:124
bool VOLK_term_equals(const VOLK_Term *term1, const VOLK_Term *term2)
Compare two terms.
Definition term.h:377
void VOLK_term_set_free(VOLK_TermSet *ts)
Free a term set.
Definition term.c:604
void VOLK_term_free(VOLK_Term *term)
Definition term.c:387
VOLK_Term * VOLK_default_ctx
Default context.
Definition term.c:59
VOLK_Term * VOLK_term_set_pop(VOLK_TermSet *ts)
Pop a term from a term set.
Definition term.c:612
void * trig_encode_store_init(VOLK_Store *store)
Initialize a store encoding loop.
Definition writer_trig.c:61
void trig_encode_store_done(void *h)
Finalize the store encoding loop.
VOLK_rc trig_encode_store_iter(void *h, char **res_p)
Encode a chunk from a store.
Definition writer_trig.c:84
VOLK_rc ttl_encode_term(const VOLK_Term *term, char **out_p)
Encode a single term as Turtle.
Definition writer_ttl.c:32
void ttl_encode_graph_done(void *h)
Finalize a TTL graph iteration.
Definition writer_ttl.c:347
VOLK_rc ttl_encode_graph_iter(void *h, char **res_p)
Run one encoding iteration.
Definition writer_ttl.c:252
void * ttl_encode_graph_init(const VOLK_Graph *gr, void *oh)
Initialize a TTL graph iteration.
Definition writer_ttl.c:178
Options passed to ttl_encode_graph_init().
Definition writer_ttl.h:15
Store structure.
Definition store.h:59
RDF term.
Definition term.h:62
char * data
URI, literal value, or BNode label.
Definition term.h:63
TriG encoder iterator.
Definition writer_trig.c:16
void * txn
Store transaction.
Definition writer_trig.c:23
VOLK_TermSet * gr_uri_ts
Term set of named graph URIs.
Definition writer_trig.c:19
TTLEncodeIterator * gr_it
Current graph iterator.
Definition writer_trig.c:21
VOLK_Store * store
Store pointer for store encoding.
Definition writer_trig.c:18
VOLK_Graph * gr
Current graph.
Definition writer_trig.c:20
VOLK_rc rc
Internal return code.
Definition writer_trig.c:17
TRIGPos pos
Encoder position.
Definition writer_trig.c:22
TRIGPos
Definition writer_trig.c:5
@ TRIG_POS_END
End of overall encoding.
Definition writer_trig.c:10
@ TRIG_POS_END_GR
Boundary between graphs.
Definition writer_trig.c:9
@ TRIG_POS_START
Start of overall encoding.
Definition writer_trig.c:6
@ TRIG_POS_START_BODY
After writing prolog.
Definition writer_trig.c:7
@ TRIG_POS_GRAPH
Inside a graph.
Definition writer_trig.c:8