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
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 (store);
63 if (!(VOLK_store_features (store) & VOLK_STORE_CTX)) {
64 log_error ("Store is not context-aware.");
65 return NULL;
66 }
67 TRIGEncodeIterator *it;
68 CALLOC_GUARD (it, NULL);
69
70 it->store = store;
71 VOLK_rc rc = VOLK_store_begin (it->store, VOLK_STORE_TXN_RO, &it->txn);
72 // TODO RCCK (rc);
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 // 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 // After writing prolog: populate first graph.
98 // Or: a graph just finished encoding, try to get the next one.
99 // Allocate the boundary symbols.
100 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
101 // Preallocate closing brace + newline in any case.
102 size_t out_size = 3;
103
104 VOLK_Term *gr_uri = VOLK_term_set_pop (it->gr_uri_ts);
105 if (gr_uri) {
106 ttl_encode_term (gr_uri, &gr_uri_trig);
107 VOLK_Graph *gr = VOLK_graph_new (it->store, gr_uri->data);
108 VOLK_term_free (gr_uri);
109 out_size += strlen (gr_uri_trig) + 4;
111 } else {
112 log_debug ("End of store encoding.");
113 it->pos = TRIG_POS_END;
114 it->rc = VOLK_END;
115 }
116
117 char *tmp = realloc (*res_p, out_size); // Only one realloc.
118 NLRCCK (tmp, VOLK_MEM_ERR);
119 *res_p = tmp;
120 }
121
122 if (it->pos == TRIG_POS_GRAPH) {
123 VOLK_rc gr_rc = ttl_encode_graph_iter (it->gr_it, res_p);
124 if (gr_rc == VOLK_END || gr_rc == VOLK_NORESULT)
125 it->pos = TRIG_POS_END_GR;
126 } else {
127 // Write closing and/or opening braces. This uses up the iteration.
128 size_t offset = 0; // Where to start writing graph name.
129 if (it->pos == TRIG_POS_END_GR || it->pos == TRIG_POS_END) {
130 sprintf (*res_p, "}\n");
131 *res_p[2] = '0';
132 offset = 2;
133 }
134 if (it->pos == TRIG_POS_START_BODY || it->pos == TRIG_POS_END_GR) {
135 sprintf (*res_p + offset, "\n%s {\n", gr_uri_trig);
136 offset += strlen (gr_uri_trig) + 3;
137 free (gr_uri_trig);
138 it->pos = TRIG_POS_GRAPH;
139 it->rc = VOLK_OK;
140 *res_p[offset] = '0';
141 }
142 }
143
144 return it->rc;
145}
146
147
148void
150{
151 if (!h) return;
152 TRIGEncodeIterator *it = h;
153
154 VOLK_store_abort (it->store, it->txn);
155 VOLK_term_set_free (it->gr_uri_ts);
156 free (it);
157}
#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_TermSet * VOLK_graph_list_txn(void *txn, VOLK_Store *store)
List all graph URIs in a store.
Definition graph.c:634
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 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 log_debug(...)
Definition core.h:273
#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
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_term_set_pop(VOLK_TermSet *ts)
Pop a term from a term set.
Definition term.c:612
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:22
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:20
VOLK_Store * store
Store pointer for store encoding.
Definition writer_trig.c:18
VOLK_rc rc
Internal return code.
Definition writer_trig.c:17
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
void * trig_encode_store_init(VOLK_Store *store)
Initialize a store encoding loop.
Definition writer_trig.c:60
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)
Definition writer_ttl.c:29
void ttl_encode_graph_done(void *h)
Definition writer_ttl.c:303
VOLK_rc ttl_encode_graph_iter(void *h, char **res_p)
Definition writer_ttl.c:236
void * ttl_encode_graph_init(const VOLK_Graph *gr, VOLK_CodecFlags flags)
Definition writer_ttl.c:173