Volksdata 1.0b12
RDF library and triple store
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 at the start of the body and the data set is empty before
82 // starting popping graphs, quit right away.
83 if (it->pos == TRIG_POS_END || (
84 it->pos == TRIG_POS_START_BODY &&
85 VOLK_term_set_size (it->dset->gr) == 0
86 )) return VOLK_END;
87
88 // Beginning of overall encoding process: write prolog.
89 if (it->pos == TRIG_POS_START) return trig_build_prolog (it, res_p);
90 else if (it->pos == TRIG_POS_END_GR) {
91 // End of graph encoding loop: finalize graph codec iterator.
92 ttl_encode_graph_done (it->gr_it);
93 it->gr_it = NULL;
94 }
95 char *gr_uri_trig = NULL;
96
97 // Preallocate closing brace + newline in any case: cheap and simpler.
98 size_t out_size = 3;
99
100 // After writing prolog: populate first graph.
101 // Or: a graph just finished encoding, try to get the next one.
102 // Allocate the boundary symbols.
103reload_graph:
104 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
105 VOLK_Term *gr_uri = VOLK_term_set_pop (it->dset->gr);
106 if (gr_uri) {
107 log_trace ("New graph to encode: %s", gr_uri->data);
108 // The default context URI is the default graph.
109 if (VOLK_term_equals (gr_uri, VOLK_default_ctx)) {
110 log_trace ("Encoding default context.");
111 gr_uri_trig = NULL;
112 it->gr = VOLK_graph_new (
113 it->dset->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 (
118 it->dset->store, gr_uri->data);
119 out_size += strlen (gr_uri_trig) + 4;
120 }
121 log_trace ("%zu triples to encode.", VOLK_graph_size (it->gr));
122 VOLK_term_free (gr_uri);
123 TTLEncodeOptions opt = {
125 .txn = it->txn,
126 };
127 it->gr_it = ttl_encode_graph_init (it->gr, &opt);
128 } else {
129 log_debug ("End of dataset encoding.");
130 it->pos = TRIG_POS_END;
131 }
132 }
133 char *res = realloc (*res_p, out_size);
134 NLRCCK (res, VOLK_MEM_ERR);
135 *res_p = res;
136
137 if (it->pos == TRIG_POS_GRAPH) {
138 log_trace ("In graph encoding loop.");
139 VOLK_rc gr_rc = ttl_encode_graph_iter (it->gr_it, res_p);
140 if (gr_rc == VOLK_END || gr_rc == VOLK_NORESULT) {
141 ttl_encode_graph_done (it->gr_it);
142 VOLK_graph_free (it->gr);
143 it->pos = TRIG_POS_END_GR;
144 goto reload_graph;
145 }
146 } else {
147 log_trace ("At graph boundary.");
148 // Write closing and/or opening braces. This uses up the iteration.
149 size_t offset; // Where in the out string to start writing graph name.
150 if (it->pos == TRIG_POS_END_GR || it->pos == TRIG_POS_END) {
151 sprintf (res, "}\n");
152 offset = 2;
153 } else offset = 0;
154 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
155 log_trace ("Begin writing graph body.");
156 if (!gr_uri_trig) sprintf (res + offset, "\n{\n");
157 else {
158 sprintf (res + offset, "\n%s {\n", gr_uri_trig);
159 free (gr_uri_trig);
160 }
161 it->pos = TRIG_POS_GRAPH;
162 it->rc = VOLK_OK;
163 }
164 }
165
166 return it->rc;
167}
168
169
170void
172{
173 if (!h) return;
174 TRIGEncodeIterator *it = h;
175
176 VOLK_store_abort (it->dset->store, it->txn);
177 VOLK_data_set_free (it->dset);
178 free (it);
179}
#define UNLIKELY(x)
Definition core.h:65
void VOLK_data_set_free(VOLK_DataSet *ds)
Free a data set handle.
Definition data_set.c:77
#define RCNL(exp)
Return NULL if exp returns a nonzero value.
Definition core.h:405
#define NLNL(exp)
Log error and return NULL if exp is NULL.
Definition core.h:427
#define log_debug(...)
Definition core.h:320
#define NLRCCK(exp, _rc)
Return rc return code if exp is NULL.
Definition core.h:396
#define log_trace(...)
Definition core.h:322
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:182
#define VOLK_NORESULT
No result yielded.
Definition core.h:138
#define VOLK_END
Loop end.
Definition core.h:145
#define VOLK_OK
Generic success return code.
Definition core.h:121
int VOLK_rc
Return code.
Definition core.h:117
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
size_t VOLK_term_set_size(VOLK_TermSet *ts)
Size of a term set.
Definition term.c:646
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:266
#define VOLK_graph_size(...)
Non-transactional version of VOLK_graph_size_txn().
Definition graph.h:293
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 data set.
Definition writer_trig.c:76
void trig_encode_dset_done(void *h)
Finalize the data set encoding loop.
void * trig_encode_dset_init(VOLK_DataSet *ds)
Initialize a data set 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:344
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 data_set.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
VOLK_DataSet * dset
Dataset to encode.
Definition writer_trig.c:18
TRIGPos pos
Encoder position.
Definition writer_trig.c:21
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