Volksdata 1.0b10
RDF library
Loading...
Searching...
No Matches
writer_trig.c
Go to the documentation of this file.
2
3
4// Position of a cursor.
12
19 VOLK_Graph * gr;
20 TTLEncodeIterator * gr_it;
22 void * txn;
23};
24
25
26/* * * Codec functions. * * */
27
29static VOLK_rc
30trig_build_prolog (TRIGEncodeIterator *it, char **res_p)
31{
32 char *res = fmt_header ("# ");
33
34 const char ***nsm = VOLK_nsmap_dump ();
35 char *ns_tpl = "@prefix %s: <%s> .\n";
36
37 // Prefix map.
38 for (size_t i = 0; nsm[i]; i++) {
39 const char **ns = nsm[i];
40 size_t old_len = strlen (res);
41 size_t ns_len = strlen (ns[0]) + strlen (ns[1]) + strlen (ns_tpl);
42 char *tmp = realloc (res, old_len + ns_len + 1);
43 if (UNLIKELY (!tmp)) return VOLK_MEM_ERR;
44 res = tmp;
45
46 sprintf (res + old_len, ns_tpl, ns[0], ns[1]);
47 free (ns);
48 }
49 free (nsm);
50
51 *res_p = res;
52 it->pos = TRIG_POS_START_BODY;
53 it->rc = VOLK_OK;
54
55 return VOLK_OK;
56}
57
58
59void *
61{
62 NLNL (ds);
63 TRIGEncodeIterator *it = calloc (1, sizeof (*it));
64 NLNL (it);
65
66 it->dset = ds;
67 it->pos = TRIG_POS_START;
68 it->rc = VOLK_NORESULT;
69 RCNL (VOLK_store_begin (it->dset->store, VOLK_STORE_TXN_RO, &it->txn));
70
71 return it;
72}
73
74
76trig_encode_dset_iter (void *h, char **res_p)
77{
78 TRIGEncodeIterator *it = h;
79
80 // End of overall encoding process.
81 if (it->pos == TRIG_POS_END) return VOLK_END;
82
83 // Beginning of overall encoding process: write prolog.
84 if (it->pos == TRIG_POS_START) return trig_build_prolog (it, res_p);
85 else if (it->pos == TRIG_POS_END_GR) {
86 // End of graph encoding loop: finalize graph codec iterator.
87 ttl_encode_graph_done (it->gr_it);
88 it->gr_it = NULL;
89 }
90 char *gr_uri_trig = NULL;
91
92 // Preallocate closing brace + newline in any case: cheap and simpler.
93 size_t out_size = 3;
94
95 // After writing prolog: populate first graph.
96 // Or: a graph just finished encoding, try to get the next one.
97 // Allocate the boundary symbols.
98reload_graph:
99 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
100 VOLK_Term *gr_uri = VOLK_term_set_pop (it->dset->gr);
101 if (gr_uri) {
102 // The default context URI is the default graph.
103 if (VOLK_term_equals (gr_uri, VOLK_default_ctx)) {
104 gr_uri_trig = NULL;
105 it->gr = VOLK_graph_new (
106 it->dset->store, VOLK_default_ctx->data);
107 out_size += 3;
108 } else {
109 ttl_encode_term (gr_uri, &gr_uri_trig);
110 it->gr = VOLK_graph_new (
111 it->dset->store, gr_uri->data);
112 out_size += strlen (gr_uri_trig) + 4;
113 }
114 VOLK_term_free (gr_uri);
115 TTLEncodeOptions opt = {
117 .txn = it->txn,
118 };
119 it->gr_it = ttl_encode_graph_init (it->gr, &opt);
120 } else {
121 log_info ("End of store encoding.");
122 it->pos = TRIG_POS_END;
123 }
124 }
125 char *res = realloc (*res_p, out_size);
126 NLRCCK (res, VOLK_MEM_ERR);
127 *res_p = res;
128
129 if (it->pos == TRIG_POS_GRAPH) {
130 VOLK_rc gr_rc = ttl_encode_graph_iter (it->gr_it, res_p);
131 if (gr_rc == VOLK_END || gr_rc == VOLK_NORESULT) {
132 ttl_encode_graph_done (it->gr_it);
133 VOLK_graph_free (it->gr);
134 it->pos = TRIG_POS_END_GR;
135 goto reload_graph;
136 }
137 } else {
138 // Write closing and/or opening braces. This uses up the iteration.
139 size_t offset = 0; // Where to start writing graph name.
140 if (it->pos == TRIG_POS_END_GR || it->pos == TRIG_POS_END) {
141 sprintf (res, "}\n");
142 offset = 2;
143 }
144 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
145 if (!gr_uri_trig) sprintf (res + offset, "\n{\n");
146 else {
147 sprintf (res + offset, "\n%s {\n", gr_uri_trig);
148 free (gr_uri_trig);
149 }
150 it->pos = TRIG_POS_GRAPH;
151 it->rc = VOLK_OK;
152 }
153 }
154
155 return it->rc;
156}
157
158
159void
161{
162 if (!h) return;
163 TRIGEncodeIterator *it = h;
164
165 VOLK_store_abort (it->dset->store, it->txn);
166 VOLK_dataset_free (it->dset);
167 free (it);
168}
#define UNLIKELY(x)
Definition core.h:39
void VOLK_dataset_free(VOLK_Dataset *ds)
Free a dataset handle.
Definition dataset.c:56
#define RCNL(exp)
Return NULL if exp returns a nonzero value.
Definition core.h:379
#define NLNL(exp)
Log error and return NULL if exp is NULL.
Definition core.h:401
#define NLRCCK(exp, _rc)
Return rc return code if exp is NULL.
Definition core.h:370
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:156
#define VOLK_NORESULT
No result yielded.
Definition core.h:112
#define VOLK_END
Loop end.
Definition core.h:119
#define VOLK_OK
Generic success return code.
Definition core.h:95
int VOLK_rc
Return code.
Definition core.h:91
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
bool VOLK_term_equals(const VOLK_Term *term1, const VOLK_Term *term2)
Compare two terms.
Definition term.h:379
void VOLK_term_free(VOLK_Term *term)
Definition term.c:392
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:633
char * fmt_header(char *pfx)
Format an informational header.
@ VOLK_CODEC_NO_PROLOG
Do not generate prolog.
@ VOLK_CODEC_INDENT
void VOLK_graph_free(VOLK_Graph *gr)
Free a graph.
Definition graph.c:263
VOLK_Graph * VOLK_graph_new(VOLK_Store *store, const char *uri_str)
Create new graph.
Definition graph.c:45
const char *** VOLK_nsmap_dump(void)
Dump all entries of the namespace map.
Definition namespace.c:191
@ VOLK_STORE_TXN_RO
Start a read-only transaction.
VOLK_rc trig_encode_dset_iter(void *h, char **res_p)
Encode a chunk from a store.
Definition writer_trig.c:76
void trig_encode_dset_done(void *h)
Finalize the store encoding loop.
void * trig_encode_dset_init(VOLK_Dataset *ds)
Initialize a store encoding loop.
Definition writer_trig.c:60
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
RDF Dataset type.
Definition dataset.h:26
RDF term.
Definition term.h:61
char * data
URI, literal value, or BNode label.
Definition term.h:62
TriG encoder iterator.
Definition writer_trig.c:16
void * txn
Store transaction.
Definition writer_trig.c:22
TTLEncodeIterator * gr_it
Current graph iterator.
Definition writer_trig.c:20
VOLK_Graph * gr
Current graph.
Definition writer_trig.c:19
VOLK_rc rc
Internal return code.
Definition writer_trig.c:17
TRIGPos pos
Encoder position.
Definition writer_trig.c:21
VOLK_Dataset * dset
Dataset to encode.
Definition writer_trig.c:18
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