2 Copyright 2011-2016 David Robillard <http://drobilla.net>
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.
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.
18 @file sord.h API for Sord, a lightweight RDF model library.
28 #include "serd/serd.h"
32 # define SORD_LIB_IMPORT __declspec(dllimport)
33 # define SORD_LIB_EXPORT __declspec(dllexport)
35 # define SORD_LIB_IMPORT __attribute__((visibility("default")))
36 # define SORD_LIB_EXPORT __attribute__((visibility("default")))
39 # define SORD_API SORD_LIB_EXPORT
41 # define SORD_API SORD_LIB_IMPORT
55 A lightweight RDF model library.
57 Sord stores RDF (subject object predicate context) quads, where the context
58 may be omitted (to represent triples in the default graph).
64 The World represents all library state, including interned strings.
66 typedef struct SordWorldImpl SordWorld
;
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
75 typedef struct SordModelImpl SordModel
;
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.
84 typedef struct SordInserterImpl SordInserter
;
89 typedef struct SordIterImpl SordIter
;
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).
97 typedef struct SordNodeImpl SordNode
;
100 Quad of nodes (a statement), or a quad pattern.
102 Nodes are ordered (S P O G). The ID of the default graph is 0.
104 typedef const SordNode
* SordQuad
[4];
107 Index into a SordQuad.
110 SORD_SUBJECT
= 0, /**< Subject */
111 SORD_PREDICATE
= 1, /**< Predicate ("key") */
112 SORD_OBJECT
= 2, /**< Object ("value") */
113 SORD_GRAPH
= 3 /**< Graph ("context") */
120 SORD_URI
= 1, /**< URI */
121 SORD_BLANK
= 2, /**< Blank node identifier */
122 SORD_LITERAL
= 3 /**< Literal (string with optional lang or datatype) */
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 */
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.
150 sord_world_new(void);
157 sord_world_free(SordWorld
* world
);
160 Set a function to be called when errors occur.
162 The `error_sink` will be called with `handle` as its first argument. If
163 no error function is set, errors are printed to stderr.
167 sord_world_set_error_sink(SordWorld
* world
,
168 SerdErrorSink error_sink
,
178 Get a URI node from a string.
180 Note this function measures `str`, which is a common bottleneck.
181 Use sord_node_from_serd_node() instead if `str` is already measured.
185 sord_new_uri(SordWorld
* world
, const uint8_t* uri
);
188 Get a URI node from a relative URI string.
192 sord_new_relative_uri(SordWorld
* world
,
194 const uint8_t* base_uri
);
197 Get a blank node from a string.
199 Note this function measures `str`, which is a common bottleneck.
200 Use sord_node_from_serd_node() instead if `str` is already measured.
204 sord_new_blank(SordWorld
* world
, const uint8_t* str
);
207 Get a literal node from a string.
209 Note this function measures `str`, which is a common bottleneck.
210 Use sord_node_from_serd_node() instead if `str` is already measured.
214 sord_new_literal(SordWorld
* world
,
220 Copy a node (obtain a reference).
222 Node that since nodes are interned and reference counted, this does not
223 actually create a deep copy of `node`.
227 sord_node_copy(const SordNode
* node
);
230 Free a node (drop a reference).
234 sord_node_free(SordWorld
* world
, SordNode
* node
);
237 Return the type of a node (SORD_URI, SORD_BLANK, or SORD_LITERAL).
241 sord_node_get_type(const SordNode
* node
);
244 Return the string value of a node.
248 sord_node_get_string(const SordNode
* node
);
251 Return the string value of a node, and set `bytes` to its length in bytes.
255 sord_node_get_string_counted(const SordNode
* node
, size_t* bytes
);
258 Return the string value of a node, and set `bytes` to its length in bytes,
259 and `count` to its length in characters.
263 sord_node_get_string_measured(const SordNode
* node
,
268 Return the language of a literal node (or NULL).
272 sord_node_get_language(const SordNode
* node
);
275 Return the datatype URI of a literal node (or NULL).
279 sord_node_get_datatype(const SordNode
* node
);
282 Return the flags (string attributes) of a node.
286 sord_node_get_flags(const SordNode
* node
);
289 Return true iff node can be serialised as an inline object.
291 More specifically, this returns true iff the node is the object field
292 of exactly one statement, and therefore can be inlined since it needn't
293 be referred to by name.
297 sord_node_is_inline_object(const SordNode
* node
);
300 Return true iff `a` is equal to `b`.
302 Note this is much faster than comparing the node's strings.
306 sord_node_equals(const SordNode
* a
,
310 Return a SordNode as a SerdNode.
312 The returned node is shared and must not be freed or modified.
316 sord_node_to_serd_node(const SordNode
* node
);
319 Create a new SordNode from a SerdNode.
321 The returned node must be freed using sord_node_free().
325 sord_node_from_serd_node(SordWorld
* world
,
327 const SerdNode
* node
,
328 const SerdNode
* datatype
,
329 const SerdNode
* lang
);
340 @param world The world in which to make this model.
342 @param indices SordIndexOption flags (e.g. SORD_SPO|SORD_OPS). Be sure to
343 enable an index where the most significant node(s) are not variables in your
344 queries (e.g. to make (? P O) queries, enable either SORD_OPS or SORD_POS).
346 @param graphs If true, store (and index) graph contexts.
350 sord_new(SordWorld
* world
,
355 Close and free `model`.
359 sord_free(SordModel
* model
);
362 Get the world associated with `model`.
366 sord_get_world(SordModel
* model
);
369 Return the number of nodes stored in `world`.
371 Nodes are included in this count iff they are a part of a quad in `world`.
375 sord_num_nodes(const SordWorld
* world
);
378 Return the number of quads stored in `model`.
382 sord_num_quads(const SordModel
* model
);
385 Return an iterator to the start of `model`.
389 sord_begin(const SordModel
* model
);
392 Search for statements by a quad pattern.
393 @return an iterator to the first match, or NULL if no matches found.
397 sord_find(SordModel
* model
, const SordQuad pat
);
400 Search for statements by nodes.
401 @return an iterator to the first match, or NULL if no matches found.
405 sord_search(SordModel
* model
,
411 Search for a single node that matches a pattern.
412 Exactly one of `s`, `p`, `o` must be NULL.
413 This function is mainly useful for predicates that only have one value.
414 The returned node must be freed using sord_node_free().
415 @return the first matching node, or NULL if no matches are found.
419 sord_get(SordModel
* model
,
426 Return true iff a statement exists.
430 sord_ask(SordModel
* model
,
437 Return the number of matching statements.
441 sord_count(SordModel
* model
,
448 Check if `model` contains a triple pattern.
450 @return true if `model` contains a match for `pat`, otherwise false.
454 sord_contains(SordModel
* model
, const SordQuad pat
);
457 Add a quad to a model.
459 Calling this function invalidates all iterators on `model`.
461 @return true on success, false, on error.
465 sord_add(SordModel
* model
, const SordQuad quad
);
468 Remove a quad from a model.
470 Calling this function invalidates all iterators on `model`. To remove quads
471 while iterating, use sord_erase() instead.
475 sord_remove(SordModel
* model
, const SordQuad quad
);
478 Remove a quad from a model via an iterator.
480 Calling this function invalidates all iterators on `model` except `iter`.
482 @param model The model which `iter` points to.
483 @param iter Iterator to the element to erase, which is incremented to the
484 next value on return.
488 sord_erase(SordModel
* model
, SordIter
* iter
);
497 Create an inserter for writing statements to a model.
501 sord_inserter_new(SordModel
* model
,
509 sord_inserter_free(SordInserter
* inserter
);
512 Set the current base URI for writing to the model.
514 Note this function can be safely casted to SerdBaseSink.
518 sord_inserter_set_base_uri(SordInserter
* inserter
,
519 const SerdNode
* uri
);
522 Set a namespace prefix for writing to the model.
524 Note this function can be safely casted to SerdPrefixSink.
528 sord_inserter_set_prefix(SordInserter
* inserter
,
529 const SerdNode
* name
,
530 const SerdNode
* uri
);
533 Write a statement to the model.
535 Note this function can be safely casted to SerdStatementSink.
539 sord_inserter_write_statement(SordInserter
* inserter
,
540 SerdStatementFlags flags
,
541 const SerdNode
* graph
,
542 const SerdNode
* subject
,
543 const SerdNode
* predicate
,
544 const SerdNode
* object
,
545 const SerdNode
* object_datatype
,
546 const SerdNode
* object_lang
);
555 Set `quad` to the quad pointed to by `iter`.
559 sord_iter_get(const SordIter
* iter
, SordQuad quad
);
562 Return a field of the quad pointed to by `iter`.
564 Returns NULL if `iter` is NULL or is at the end.
568 sord_iter_get_node(const SordIter
* iter
, SordQuadIndex index
);
571 Return the store pointed to by `iter`.
575 sord_iter_get_model(SordIter
* iter
);
578 Increment `iter` to point to the next statement.
582 sord_iter_next(SordIter
* iter
);
585 Return true iff `iter` is at the end of its range.
589 sord_iter_end(const SordIter
* iter
);
596 sord_iter_free(SordIter
* iter
);
605 Match two quads (using ID comparison only).
607 This function is a straightforward and fast equivalence match with wildcard
608 support (ID 0 is a wildcard). It does not actually read node data.
609 @return true iff `x` and `y` match.
613 sord_quad_match(const SordQuad x
, const SordQuad y
);
622 Return a reader that will read into `model`.
626 sord_new_reader(SordModel
* model
,
632 Write a model to a writer.
636 sord_write(SordModel
* model
,
641 Write a range to a writer.
643 This increments `iter` to its end, then frees it.
647 sord_write_iter(SordIter
* iter
,
659 #endif /* SORD_SORD_H */