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