File: examples.md

package info (click to toggle)
tracker 3.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,096 kB
  • sloc: ansic: 57,908; javascript: 15,606; python: 6,272; cs: 242; perl: 106; sh: 98; xml: 29; makefile: 20
file content (141 lines) | stat: -rw-r--r-- 4,088 bytes parent folder | download
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
---
title: Examples
short-description: Examples
...

# Examples

This chapters shows some real examples of usage of the Tracker
SPARQL Library.

## Querying a remote endpoint

All SPARQL queries happen on a [](TrackerSparqlConnection), often these
connections represent a remote endpoints maintained by another process or
server.

This example demonstrates the use of these connections on a remote
endpoint. Concretely creating a D-Bus [](TrackerSparqlConnection),
creating a prepared statement from a SPARQL query string, executing
the query, and obtaining the query results from the cursor.

The [](tracker_sparql_connection_query_statement) function can be used
to obtain a [](TrackerSparqlStatement) object holding a prepared SPARQL
query that can then be executed with [](tracker_sparql_statement_execute).
The query string can contain `~name` placeholders which can be replaced with
arbitrary values before query execution with
[](tracker_sparql_statement_bind_string) and similar functions.
This allows parsing the query string only once and to execute it multiple
times with different parameters with potentially significant performance gains.

Multiple functions offer asynchronous variants, so the application
main loop is not blocked while these operations are executed.

Once you end up with the query, remember to call [](tracker_sparql_cursor_close).
The same applies to [](tracker_sparql_connection_close) when no longer needed.

<div class="gi-lang-c">

{{ examples/connection-example.c }}

</div>
<div class="gi-lang-python">

{{ examples/connection-example.py }}

</div>
<div class="gi-lang-javascript">

{{ examples/connection-example.js }}

</div>

## Creating a private database

Applications may create private stores via the [](tracker_sparql_connection_new)
constructor.

This example demonstrates the creation of a private store, for simplicity the
example uses the builtin Nepomuk ontology, but the data structures may be defined
by the application, see the documentation on
[defining ontologies](defining-ontologies.md) for more information about this.

The example also demonstrates the use of [](TrackerResource) and [](TrackerBatch)
for insertion of RDF data. It is also possible the direct use of SPARQL update
strings via [](tracker_sparql_connection_update).

Multiple functions offer asynchronous variants, so the application
main loop is not blocked while these operations are executed.

Once you no longer need the connection, remember to call
[](tracker_sparql_connection_close) on the [](TrackerSparqlConnection).

<div class="gi-lang-c">

{{ examples/private-store-example.c }}

</div>
<div class="gi-lang-python">

{{ examples/private-store-example.py }}

</div>
<div class="gi-lang-javascript">

{{ examples/private-store-example.js }}

</div>

## Creating a SPARQL endpoint

For some applications and services, it might be desirable to export a
SPARQL store as an endpoint. Making it possible for other applications to
query the data they hold.

This example demonstrates the use of [](TrackerEndpoint) subclasses,
concretely the creation of a D-Bus endpoint, that other applications
may query e.g. through a connection created with
[](tracker_sparql_connection_bus_new).

<div class="gi-lang-c">

{{ examples/endpoint-example.c }}

</div>
<div class="gi-lang-python">

{{ examples/endpoint-example.py }}

</div>
<div class="gi-lang-javascript">

{{ examples/endpoint-example.js }}

</div>

## Receiving notification on changes

As an additional feature over SPARQL endpoints, Tracker allows for
users of private and D-Bus SPARQL connections to receive notifications
on changes of certain RDF classes (Those with the
[nrl:notify](nrl-ontology.md#nrl:notify) property, like
[nmm:MusicPiece](nmm-ontology.md#nmm:MusicPiece)).

This example demonstrates the use of [](TrackerNotifier) to receive
notifications on database updates.

<div class="gi-lang-c">

{{ examples/notifier-example.c }}

</div>
<div class="gi-lang-python">

{{ examples/notifier-example.py }}

</div>
<div class="gi-lang-javascript">

{{ examples/notifier-example.js }}

</div>