comparison win32-mingw/include/sord/sord.h @ 16:7f77decdb26d

Move serd/sord includes
author Chris Cannam <chris.cannam@eecs.qmul.ac.uk>
date Mon, 25 Mar 2013 12:24:18 +0000
parents
children
comparison
equal deleted inserted replaced
14:48209aa7aa51 16:7f77decdb26d
1 /*
2 Copyright 2011-2013 David Robillard <http://drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /**
18 @file sord.h API for Sord, a lightweight RDF model library.
19 */
20
21 #ifndef SORD_SORD_H
22 #define SORD_SORD_H
23
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdio.h>
27
28 #include "serd/serd.h"
29
30 #ifdef SORD_SHARED
31 # ifdef _WIN32
32 # define SORD_LIB_IMPORT __declspec(dllimport)
33 # define SORD_LIB_EXPORT __declspec(dllexport)
34 # else
35 # define SORD_LIB_IMPORT __attribute__((visibility("default")))
36 # define SORD_LIB_EXPORT __attribute__((visibility("default")))
37 # endif
38 # ifdef SORD_INTERNAL
39 # define SORD_API SORD_LIB_EXPORT
40 # else
41 # define SORD_API SORD_LIB_IMPORT
42 # endif
43 #else
44 # define SORD_API
45 #endif
46
47 #ifdef __cplusplus
48 extern "C" {
49 #else
50 # include <stdbool.h>
51 #endif
52
53 /**
54 @defgroup sord Sord
55 A lightweight RDF model library.
56
57 Sord stores RDF (subject object predicate context) quads, where the context
58 may be omitted (to represent triples in the default graph).
59 @{
60 */
61
62 /**
63 Sord World.
64 The World represents all library state, including interned strings.
65 */
66 typedef struct SordWorldImpl SordWorld;
67
68 /**
69 Sord Model.
70
71 A model is an indexed set of Quads (i.e. it can contain several RDF
72 graphs). It may be searched using various patterns depending on which
73 indices are enabled.
74 */
75 typedef struct SordModelImpl SordModel;
76
77 /**
78 Model Inserter.
79
80 An inserter is used for writing statements to a model using the Serd sink
81 interface. This makes it simple to write to a model directly using a
82 SerdReader, or any other code that writes statements to a SerdStatementSink.
83 */
84 typedef struct SordInserterImpl SordInserter;
85
86 /**
87 Model Iterator.
88 */
89 typedef struct SordIterImpl SordIter;
90
91 /**
92 RDF Node.
93 A Node is a component of a Quad. Nodes may be URIs, blank nodes, or
94 (in the case of quad objects only) string literals. Literal nodes may
95 have an associate language or datatype (but not both).
96 */
97 typedef struct SordNodeImpl SordNode;
98
99 /**
100 Quad of nodes (a statement), or a quad pattern.
101
102 Nodes are ordered (S P O G). The ID of the default graph is 0.
103 */
104 typedef const SordNode* SordQuad[4];
105
106 /**
107 Index into a SordQuad.
108 */
109 typedef enum {
110 SORD_SUBJECT = 0, /**< Subject */
111 SORD_PREDICATE = 1, /**< Predicate (a.k.a. "key") */
112 SORD_OBJECT = 2, /**< Object (a.k.a. "value") */
113 SORD_GRAPH = 3 /**< Graph (a.k.a. "context") */
114 } SordQuadIndex;
115
116 /**
117 Type of a node.
118 */
119 typedef enum {
120 SORD_URI = 1, /**< URI */
121 SORD_BLANK = 2, /**< Blank node identifier */
122 SORD_LITERAL = 3 /**< Literal (string with optional lang or datatype) */
123 } SordNodeType;
124
125 /**
126 Indexing option.
127 */
128 typedef enum {
129 SORD_SPO = 1, /**< Subject, Predicate, Object */
130 SORD_SOP = 1 << 1, /**< Subject, Object, Predicate */
131 SORD_OPS = 1 << 2, /**< Object, Predicate, Subject */
132 SORD_OSP = 1 << 3, /**< Object, Subject, Predicate */
133 SORD_PSO = 1 << 4, /**< Predicate, Subject, Object */
134 SORD_POS = 1 << 5 /**< Predicate, Object, Subject */
135 } SordIndexOption;
136
137 /**
138 @name World
139 @{
140 */
141
142 /**
143 Create a new Sord World.
144 It is safe to use multiple worlds in one process, though no data
145 (e.g. nodes) can be shared between worlds, and this should be avoided if
146 possible for performance reasons.
147 */
148 SORD_API
149 SordWorld*
150 sord_world_new(void);
151
152 /**
153 Free @c world.
154 */
155 SORD_API
156 void
157 sord_world_free(SordWorld* world);
158
159 /**
160 Set a function to be called when errors occur.
161
162 The @p error_sink will be called with @p handle as its first argument. If
163 no error function is set, errors are printed to stderr.
164 */
165 SORD_API
166 void
167 sord_world_set_error_sink(SordWorld* world,
168 SerdErrorSink error_sink,
169 void* handle);
170
171 /**
172 @}
173 @name Node
174 @{
175 */
176
177 /**
178 Get a URI node from a string.
179
180 Note this function measures @c str, which is a common bottleneck.
181 Use sord_node_from_serd_node instead if @c str is already measured.
182 */
183 SORD_API
184 SordNode*
185 sord_new_uri(SordWorld* world, const uint8_t* uri);
186
187 /**
188 Get a URI node from a relative URI string.
189 */
190 SORD_API
191 SordNode*
192 sord_new_relative_uri(SordWorld* world,
193 const uint8_t* str,
194 const uint8_t* base_uri);
195
196 /**
197 Get a blank node from a string.
198
199 Note this function measures @c str, which is a common bottleneck.
200 Use sord_node_from_serd_node instead if @c str is already measured.
201 */
202 SORD_API
203 SordNode*
204 sord_new_blank(SordWorld* world, const uint8_t* str);
205
206 /**
207 Get a literal node from a string.
208
209 Note this function measures @c str, which is a common bottleneck.
210 Use sord_node_from_serd_node instead if @c str is already measured.
211 */
212 SORD_API
213 SordNode*
214 sord_new_literal(SordWorld* world,
215 SordNode* datatype,
216 const uint8_t* str,
217 const char* lang);
218
219 /**
220 Copy a node (obtain a reference).
221
222 Node that since nodes are interned and reference counted, this does not
223 actually create a deep copy of @c node.
224 */
225 SORD_API
226 SordNode*
227 sord_node_copy(const SordNode* node);
228
229 /**
230 Free a node (drop a reference).
231 */
232 SORD_API
233 void
234 sord_node_free(SordWorld* world, SordNode* node);
235
236 /**
237 Return the type of a node (SORD_URI, SORD_BLANK, or SORD_LITERAL).
238 */
239 SORD_API
240 SordNodeType
241 sord_node_get_type(const SordNode* node);
242
243 /**
244 Return the string value of a node.
245 */
246 SORD_API
247 const uint8_t*
248 sord_node_get_string(const SordNode* node);
249
250 /**
251 Return the string value of a node, and set @c len to its length.
252 */
253 SORD_API
254 const uint8_t*
255 sord_node_get_string_counted(const SordNode* node, size_t* len);
256
257 /**
258 Return the language of a literal node (or NULL).
259 */
260 SORD_API
261 const char*
262 sord_node_get_language(const SordNode* node);
263
264 /**
265 Return the datatype URI of a literal node (or NULL).
266 */
267 SORD_API
268 SordNode*
269 sord_node_get_datatype(const SordNode* node);
270
271 /**
272 Return the flags (string attributes) of a node.
273 */
274 SORD_API
275 SerdNodeFlags
276 sord_node_get_flags(const SordNode* node);
277
278 /**
279 Return true iff node can be serialised as an inline object.
280
281 More specifically, this returns true iff the node is the object field
282 of exactly one statement, and therefore can be inlined since it needn't
283 be referred to by name.
284 */
285 SORD_API
286 bool
287 sord_node_is_inline_object(const SordNode* node);
288
289 /**
290 Return true iff @c a is equal to @c b.
291
292 Note this is much faster than comparing the node's strings.
293 */
294 SORD_API
295 bool
296 sord_node_equals(const SordNode* a,
297 const SordNode* b);
298
299 /**
300 Return a SordNode as a SerdNode.
301
302 The returned node is shared and must not be freed or modified.
303 */
304 SORD_API
305 const SerdNode*
306 sord_node_to_serd_node(const SordNode* node);
307
308 /**
309 Create a new SordNode from a SerdNode.
310
311 The returned node must be freed using sord_node_free.
312 */
313 SORD_API
314 SordNode*
315 sord_node_from_serd_node(SordWorld* world,
316 SerdEnv* env,
317 const SerdNode* node,
318 const SerdNode* datatype,
319 const SerdNode* lang);
320
321 /**
322 @}
323 @name Model
324 @{
325 */
326
327 /**
328 Create a new model.
329
330 @param world The world in which to make this model.
331
332 @param indices SordIndexOption flags (e.g. SORD_SPO|SORD_OPS). Be sure to
333 enable an index where the most significant node(s) are not variables in your
334 queries (e.g. to make (? P O) queries, enable either SORD_OPS or SORD_POS).
335
336 @param graphs If true, store (and index) graph contexts.
337 */
338 SORD_API
339 SordModel*
340 sord_new(SordWorld* world,
341 unsigned indices,
342 bool graphs);
343
344 /**
345 Close and free @c model.
346 */
347 SORD_API
348 void
349 sord_free(SordModel* model);
350
351 /**
352 Get the world associated with @c model.
353 */
354 SORD_API
355 SordWorld*
356 sord_get_world(SordModel* model);
357
358 /**
359 Return the number of nodes stored in @c world.
360
361 Nodes are included in this count iff they are a part of a quad in @c world.
362 */
363 SORD_API
364 size_t
365 sord_num_nodes(const SordWorld* world);
366
367 /**
368 Return the number of quads stored in @c model.
369 */
370 SORD_API
371 size_t
372 sord_num_quads(const SordModel* model);
373
374 /**
375 Return an iterator to the start of @c model.
376 */
377 SORD_API
378 SordIter*
379 sord_begin(const SordModel* model);
380
381 /**
382 Search for statements by a quad pattern.
383 @return an iterator to the first match, or NULL if no matches found.
384 */
385 SORD_API
386 SordIter*
387 sord_find(SordModel* model, const SordQuad pat);
388
389 /**
390 Search for statements by nodes.
391 @return an iterator to the first match, or NULL if no matches found.
392 */
393 SORD_API
394 SordIter*
395 sord_search(SordModel* model,
396 const SordNode* s,
397 const SordNode* p,
398 const SordNode* o,
399 const SordNode* g);
400 /**
401 Search for a single node that matches a pattern.
402 Exactly one of @p s, @p p, @p o must be NULL.
403 This function is mainly useful for predicates that only have one value.
404 The returned node must be freed using sord_node_free.
405 @return the first matching node, or NULL if no matches are found.
406 */
407 SORD_API
408 SordNode*
409 sord_get(SordModel* model,
410 const SordNode* s,
411 const SordNode* p,
412 const SordNode* o,
413 const SordNode* g);
414
415 /**
416 Return true iff a statement exists.
417 */
418 SORD_API
419 bool
420 sord_ask(SordModel* model,
421 const SordNode* s,
422 const SordNode* p,
423 const SordNode* o,
424 const SordNode* g);
425
426 /**
427 Return the number of matching statements.
428 */
429 SORD_API
430 uint64_t
431 sord_count(SordModel* model,
432 const SordNode* s,
433 const SordNode* p,
434 const SordNode* o,
435 const SordNode* g);
436
437 /**
438 Check if @a model contains a triple pattern.
439 */
440 SORD_API
441 bool
442 sord_contains(SordModel* model, const SordQuad pat);
443
444 /**
445 Add a quad to a model.
446 */
447 SORD_API
448 bool
449 sord_add(SordModel* model, const SordQuad quad);
450
451 /**
452 Remove a quad from a model.
453
454 Note that is it illegal to remove while iterating over @c model.
455 */
456 SORD_API
457 void
458 sord_remove(SordModel* model, const SordQuad quad);
459
460 /**
461 @}
462 @name Inserter
463 @{
464 */
465
466 /**
467 Create an inserter for writing statements to a model.
468 */
469 SORD_API
470 SordInserter*
471 sord_inserter_new(SordModel* model,
472 SerdEnv* env);
473
474 /**
475 Free an inserter.
476 */
477 SORD_API
478 void
479 sord_inserter_free(SordInserter* inserter);
480
481 /**
482 Set the current base URI for writing to the model.
483
484 Note this function can be safely casted to SerdBaseSink.
485 */
486 SORD_API
487 SerdStatus
488 sord_inserter_set_base_uri(SordInserter* inserter,
489 const SerdNode* uri);
490
491 /**
492 Set a namespace prefix for writing to the model.
493
494 Note this function can be safely casted to SerdPrefixSink.
495 */
496 SORD_API
497 SerdStatus
498 sord_inserter_set_prefix(SordInserter* inserter,
499 const SerdNode* name,
500 const SerdNode* uri);
501
502 /**
503 Write a statement to the model.
504
505 Note this function can be safely casted to SerdStatementSink.
506 */
507 SORD_API
508 SerdStatus
509 sord_inserter_write_statement(SordInserter* inserter,
510 SerdStatementFlags flags,
511 const SerdNode* graph,
512 const SerdNode* subject,
513 const SerdNode* predicate,
514 const SerdNode* object,
515 const SerdNode* object_datatype,
516 const SerdNode* object_lang);
517
518 /**
519 @}
520 @name Iteration
521 @{
522 */
523
524 /**
525 Set @c quad to the quad pointed to by @c iter.
526 */
527 SORD_API
528 void
529 sord_iter_get(const SordIter* iter, SordQuad quad);
530
531 /**
532 Return a field of the quad pointed to by @c iter.
533 */
534 SORD_API
535 const SordNode*
536 sord_iter_get_node(const SordIter* iter, SordQuadIndex index);
537
538 /**
539 Return the store pointed to by @c iter.
540 */
541 SORD_API
542 const SordModel*
543 sord_iter_get_model(SordIter* iter);
544
545 /**
546 Increment @c iter to point to the next statement.
547 */
548 SORD_API
549 bool
550 sord_iter_next(SordIter* iter);
551
552 /**
553 Return true iff @c iter is at the end of its range.
554 */
555 SORD_API
556 bool
557 sord_iter_end(const SordIter* iter);
558
559 /**
560 Free @c iter.
561 */
562 SORD_API
563 void
564 sord_iter_free(SordIter* iter);
565
566 /**
567 @}
568 @name Utilities
569 @{
570 */
571
572 /**
573 Match two quads (using ID comparison only).
574
575 This function is a straightforward and fast equivalence match with wildcard
576 support (ID 0 is a wildcard). It does not actually read node data.
577 @return true iff @c x and @c y match.
578 */
579 SORD_API
580 bool
581 sord_quad_match(const SordQuad x, const SordQuad y);
582
583 /**
584 @}
585 @name Serialisation
586 @{
587 */
588
589 /**
590 Return a reader that will read into @c model.
591 */
592 SORD_API
593 SerdReader*
594 sord_new_reader(SordModel* model,
595 SerdEnv* env,
596 SerdSyntax syntax,
597 SordNode* graph);
598
599 /**
600 Write a model to a writer.
601 */
602 SORD_API
603 bool
604 sord_write(SordModel* model,
605 SerdWriter* writer,
606 SordNode* graph);
607
608 /**
609 Write a range to a writer.
610
611 This increments @c iter to its end, then frees it.
612 */
613 SORD_API
614 bool
615 sord_write_iter(SordIter* iter,
616 SerdWriter* writer);
617
618 /**
619 @}
620 @}
621 */
622
623 #ifdef __cplusplus
624 } /* extern "C" */
625 #endif
626
627 #endif /* SORD_SORD_H */