Tracker.SparqlConnection¶
- Subclasses:
None
Methods¶
- Inherited:
- Structs:
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
Properties¶
None
Signals¶
- Inherited:
Fields¶
- Inherited:
Name |
Type |
Access |
Description |
|---|---|---|---|
parent_instance |
r |
Class Details¶
- class Tracker.SparqlConnection(**kwargs)¶
- Bases:
- Abstract:
Yes
- Structure:
TrackerSparqlConnectionholds a connection to a RDF triple store.This triple store may be of three types:
Local to the process, created through [ctor`SparqlConnection`.new].
A HTTP SPARQL endpoint over the network, created through [ctor`SparqlConnection`.remote_new]
A DBus SPARQL endpoint owned by another process in the same machine, created through [ctor`SparqlConnection`.bus_new]
When creating a local triple store, it is required to give details about its structure. This is done by passing a location to an ontology, see more on how are ontologies defined. A local database may be stored in a filesystem location, or it may reside in memory.
A
TrackerSparqlConnectionis private to the calling process, it can be exposed to other hosts/processes via a [class`Endpoint`], see [ctor`EndpointDBus`.new] and [ctor`EndpointHttp`.new].When issuing SPARQL queries and updates, it is recommended that these are created through [class`SparqlStatement`] to avoid the SPARQL injection class of bugs, see [method`SparqlConnection`.query_statement] and [method`SparqlConnection`.update_statement]. For SPARQL updates it is also possible to use a “builder” approach to generate RDF data, see [class`Resource`]. It is also possible to create [class`SparqlStatement`] objects for SPARQL queries and updates from SPARQL strings embedded in a [struct`Gio`.Resource], see [method`SparqlConnection`.load_statement_from_gresource].
To get the best performance, it is recommended that SPARQL updates are clustered through [class`Batch`].
TrackerSparqlConnectionalso offers a number of methods for the simple cases, [method`SparqlConnection`.query] may be used when there is a SPARQL query string directly available, and the [method`SparqlConnection`.update] family of functions may be used for one-off updates. All functions have asynchronous variants.When a SPARQL query is executed, a [class`SparqlCursor`] will be obtained to iterate over the query results.
Depending on the ontology definition,
TrackerSparqlConnectionmay emit notifications whenever resources of certain types get insert, modified or deleted from the triple store (see nrl:notify. These notifications can be handled via a [class`Notifier`] obtained with [method`SparqlConnection`.create_notifier].After done with a connection, it is recommended to call [method`SparqlConnection`.close] or [method`SparqlConnection`.close_async] explicitly to cleanly close the connection and prevent consistency checks on future runs. The triple store connection will be implicitly closed when the
TrackerSparqlConnectionobject is disposed.A
TrackerSparqlConnectionmay be used from multiple threads, asynchronous updates are executed sequentially on arrival order, asynchronous queries are dispatched in a thread pool.If you ever have the need to procedurally compose SPARQL query strings, consider the use of [func`sparql_escape_string`] for literal strings and the [func`sparql_escape_uri`] family of functions for URIs.
- classmethod bus_new(service_name, object_path, dbus_connection)¶
- Parameters:
- Raises:
- Returns:
a new
TrackerSparqlConnection.- Return type:
service_name (nullable): The name of the D-Bus service to connect to, or
Noneif not using a message bus. Connects to a database owned by another process on the local machine via DBus.When using a message bus (session/system), the service_name argument will be used to describe the remote endpoint, either by unique or well-known D-Bus names. If not using a message bus (e.g. peer-to-peer D-Bus connections) the service_name may be
None.The D-Bus object path of the remote endpoint will be given through object_path,
Nonemay be used to use the default/org/freedesktop/Tracker3/Endpointpath.The D-Bus connection used to set up the connection may be given through the dbus_connection argument. Using
Nonewill resort to the default session bus.
- classmethod bus_new_async(service_name, object_path, dbus_connection, cancellable, callback, *user_data)¶
- Parameters:
service_name (
str) – The name of the D-Bus service to connect to.object_path (
strorNone) – The path to the object, orNoneto use the default.dbus_connection (
Gio.DBusConnectionorNone) – The [class`Gio`.DBusConnection] to use, orNoneto use the session buscancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Connects asynchronously to a database owned by another process on the local machine via DBus.
New in version 3.1.
- classmethod bus_new_finish(result)¶
- Parameters:
result (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
a new
TrackerSparqlConnection.- Return type:
Finishes the operation started with [func`SparqlConnection`.bus_new_async].
New in version 3.1.
- classmethod new(flags, store, ontology, cancellable)¶
- Parameters:
flags (
Tracker.SparqlConnectionFlags) – Connection flags to define the SPARQL connection behaviorstore (
Gio.FileorNone) – The directory that contains the database as a [iface`Gio`.File], orNoneontology (
Gio.FileorNone) – The directory that contains the database schemas as a [iface`Gio`.File], orNonecancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
a new
TrackerSparqlConnection.- Return type:
Creates or opens a process-local database.
This method should only be used for databases owned by the current process. To connect to databases managed by other processes, use [ctor`SparqlConnection`.bus_new].
If store is
None, the database will be created in memory.If defined, the ontology argument must point to a location containing suitable
.ontologyfiles in Turtle format. These define the structure of the triple store. You can learn more about ontologies, or you can use the stock Nepomuk ontologies by calling [func`sparql_get_ontology_nepomuk`].If opening an existing database, it is possible to pass
Noneas the ontology location, the ontology will be introspected from the database. Passing aNoneontology will raise an error if the database does not exist.If a database is opened without the
READONLY[flags`SparqlConnectionFlags`] flag enabled, and the given ontology holds differences with the current data layout, migration to the new structure will be attempted. This operation may raise an error. In particular, not all migrations are possible without causing data loss and Tracker will refuse to delete data during a migration. The database is always left in a consistent state, either prior or posterior to migration.Operations on a [class`SparqlConnection`] resulting on a
CORRUPT[error`SparqlError`] will have the event recorded persistently through a.meta.corruptedfile alongside the database files. If the database is opened without theREADONLY[flags`SparqlConnectionFlags`] flag enabled and the file is found, this constructor will attempt to repair the database. In that situation, this constructor will either return a valid [class`SparqlConnection`] if the database was repaired successfully, or raise aCORRUPT[error`SparqlError`] error if the database remains corrupted.It is considered a developer error to ship ontologies that contain format errors, or that fail at migrations.
It is encouraged to use
resource:///URI locations for ontology wherever possible, so the triple store structure is tied to the executable binary, and in order to minimize disk seeks duringTrackerSparqlConnectioninitialization.
- classmethod new_async(flags, store, ontology, cancellable, callback, *user_data)¶
- Parameters:
flags (
Tracker.SparqlConnectionFlags) – Connection flags to define the SPARQL connection behaviorstore (
Gio.FileorNone) – The directory that contains the database as a [iface`Gio`.File], orNoneontology (
Gio.FileorNone) – The directory that contains the database schemas as a [iface`Gio`.File], orNonecancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Creates or opens a process-local database asynchronously.
See [ctor`SparqlConnection`.new] for more information.
- classmethod new_finish(result)¶
- Parameters:
result (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Return type:
Finishes the operation started with [func`SparqlConnection`.new_async].
- classmethod remote_new(uri_base)¶
- Parameters:
uri_base (
str) – Base URI of the remote connection- Returns:
a new remote
TrackerSparqlConnection.- Return type:
Creates a connection to a remote HTTP SPARQL endpoint.
The connection is made using the libsoup HTTP library. The connection will normally use the
https://orhttp://protocols.
- close()¶
Closes a SPARQL connection.
No other API calls than
GObject.Object.unref() should happen after this call.This call is blocking. All pending updates will be flushed, and the store databases will be closed orderly. All ongoing SELECT queries will be cancelled. Notifiers will no longer emit events.
- close_async(cancellable, callback, *user_data)¶
- Parameters:
cancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Closes a SPARQL connection asynchronously.
No other API calls than
GObject.Object.unref() should happen after this call.
- close_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
- Return type:
Finishes the operation started with [method`SparqlConnection`.close_async].
- create_batch()¶
- Returns:
(nullable): A new [class`Batch`]
- Return type:
Creates a new [class`Batch`] to store and execute SPARQL updates.
If the connection is readonly or cannot issue SPARQL updates,
Nonewill be returned.
- create_notifier()¶
- Returns:
A newly created notifier.
- Return type:
Creates a new [class`Notifier`] to receive notifications about changes in self.
See [class`Notifier`] documentation for information about how to use this object.
Connections to HTTP endpoints will return
None.
- deserialize_async(flags, format, default_graph, stream, cancellable, callback, *user_data)¶
- Parameters:
flags (
Tracker.DeserializeFlags) – Deserialization flagsformat (
Tracker.RdfFormat) – RDF format of data in streamdefault_graph (
str) – Default graph that will receive the RDF datastream (
Gio.InputStream) – Input stream with RDF datacancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Loads the RDF data contained in stream into the given self.
This is an asynchronous operation, callback will be invoked when the data has been fully inserted to self.
The RDF data will be inserted in the given default_graph if one is provided, or the anonymous graph if default_graph is
None. Any RDF data that has a graph specified (e.g. using theGRAPHclause in the Trig format) will be inserted in the specified graph instead of default_graph.The flags argument is reserved for future expansions, currently
Tracker.DeserializeFlags.NONEmust be passed.New in version 3.4.
- deserialize_finish(result)¶
- Parameters:
result (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
Trueif all data was inserted successfully.- Return type:
Finishes the operation started with [method`SparqlConnection`.deserialize_async].
New in version 3.4.
- get_namespace_manager()¶
- Returns:
a [class`NamespaceManager`] with the prefixes of self.
- Return type:
Returns a [class`NamespaceManager`] that contains all prefixes in the ontology of self.
- load_statement_from_gresource(resource_path, cancellable)¶
- Parameters:
resource_path (
str) – The resource path of the file to parse.cancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
A prepared statement
- Return type:
Prepares a [class`SparqlStatement`] for the SPARQL contained as a [struct`Gio`.Resource] file at resource_path.
SPARQL Query files typically have the .rq extension. This will use [method`SparqlConnection`.query_statement] or [method`SparqlConnection`.update_statement] underneath to indistinctly return SPARQL query or update statements.
New in version 3.3.
- map_connection(handle_name, service_connection)¶
- Parameters:
handle_name (
str) – Handle name for service_connectionservice_connection (
Tracker.SparqlConnection) – aTrackerSparqlConnectionto use from self
Maps a
TrackerSparqlConnectiononto another through aprivate:@handle_nameURI.This can be accessed via the SERVICE SPARQL syntax in queries from self. E.g.:
``c tracker_sparql_connection_map_connection (connection,
“other-connection”, other_connection);
``sparql SELECT ?u {
- SERVICE <private:other-connection> {
?u a rdfs:Resource
}
}¶
This is useful to interrelate data from multiple
TrackerSparqlConnectioninstances maintained by the same process, without creating a public endpoint for service_connection.self may only be a
TrackerSparqlConnectioncreated via [ctor`SparqlConnection`.new] and [func`SparqlConnection`.new_async].New in version 3.3.
- query(sparql, cancellable)¶
- Parameters:
sparql (
str) – String containing the SPARQL querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
a [class`SparqlCursor`] with the results.
- Return type:
Executes a SPARQL query on self.
This method is synchronous and will block until the query is executed. See [method`SparqlConnection`.query_async] for an asynchronous variant.
If the query is partially built from user input or other untrusted sources, special care is required about possible SPARQL injection. In order to avoid it entirely, it is recommended to use [class`SparqlStatement`]. The function [func`sparql_escape_string`] exists as a last resort, but its use is not recommended.
- query_async(sparql, cancellable, callback, *user_data)¶
- Parameters:
sparql (
str) – String containing the SPARQL querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Executes asynchronously a SPARQL query on self
If the query is partially built from user input or other untrusted sources, special care is required about possible SPARQL injection. In order to avoid it entirely, it is recommended to use [class`SparqlStatement`]. The function [func`sparql_escape_string`] exists as a last resort, but its use is not recommended.
- query_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
a [class`SparqlCursor`] with the results.
- Return type:
Finishes the operation started with [method`SparqlConnection`.query_async].
- query_statement(sparql, cancellable)¶
- Parameters:
sparql (
str) – The SPARQL querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
A prepared statement
- Return type:
Prepares the given
SELECT/ASK/DESCRIBE/CONSTRUCTSPARQL query as a [class`SparqlStatement`].This prepared statement can be executed through [method`SparqlStatement`.execute] or [method`SparqlStatement`.serialize_async] families of functions.
- serialize_async(flags, format, query, cancellable, callback, *user_data)¶
- Parameters:
flags (
Tracker.SerializeFlags) – Serialization flagsformat (
Tracker.RdfFormat) – Output RDF formatquery (
str) – SPARQL querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Serializes a
DESCRIBEorCONSTRUCTquery into the specified RDF format.This is an asynchronous operation, callback will be invoked when the data is available for reading.
The SPARQL endpoint may not support the specified format, in that case an error will be raised.
The flags argument is reserved for future expansions, currently
Tracker.SerializeFlags.NONEmust be passed.New in version 3.3.
- serialize_finish(result)¶
- Parameters:
result (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
A [class`Gio`.InputStream] to read RDF content.
- Return type:
Finishes the operation started with [method`SparqlConnection`.serialize_async].
New in version 3.3.
- update(sparql, cancellable)¶
- Parameters:
sparql (
str) – String containing the SPARQL update querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
Executes a SPARQL update on self.
This method is synchronous and will block until the update is finished. See [method`SparqlConnection`.update_async] for an asynchronous variant.
It is recommented to consider the usage of [class`Batch`] to cluster database updates. Frequent isolated SPARQL updates through this method will have a degraded performance in comparison.
If the query is partially built from user input or other untrusted sources, special care is required about possible SPARQL injection. In order to avoid it entirely, it is recommended to use [class`SparqlStatement`], or to build the SPARQL input through [class`Resource`]. The function [func`sparql_escape_string`] exists as a last resort, but its use is not recommended.
- update_array_async(sparql, sparql_length, cancellable, callback, *user_data)¶
- Parameters:
sparql (
str) – An array of strings containing the SPARQL update queriessparql_length (
int) – The amount of strings you pass as sparqlcancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Executes asynchronously an array of SPARQL updates. All updates in the array are handled within a single transaction.
If the query is partially built from user input or other untrusted sources, special care is required about possible SPARQL injection. In order to avoid it entirely, it is recommended to use [class`SparqlStatement`], or to build the SPARQL input through [class`Resource`]. The function [func`sparql_escape_string`] exists as a last resort, but its use is not recommended.
- update_array_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
Trueif there were no errors.- Return type:
Finishes the operation started with [method`SparqlConnection`.update_array_async].
- update_async(sparql, cancellable, callback, *user_data)¶
- Parameters:
sparql (
str) – String containing the SPARQL update querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Executes asynchronously a SPARQL update.
It is recommented to consider the usage of [class`Batch`] to cluster database updates. Frequent isolated SPARQL updates through this method will have a degraded performance in comparison.
If the query is partially built from user input or other untrusted sources, special care is required about possible SPARQL injection. In order to avoid it entirely, it is recommended to use [class`SparqlStatement`], or to build the SPARQL input through [class`Resource`]. The function [func`sparql_escape_string`] exists as a last resort, but its use is not recommended.
- update_blank(sparql, cancellable)¶
- Parameters:
sparql (
str) – String containing the SPARQL update querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
a [type`GLib`.Variant] with the generated URNs.
- Return type:
Executes a SPARQL update and returns the names of the generated blank nodes.
This method is synchronous and will block until the update is finished. See [method`SparqlConnection`.update_blank_async] for an asynchronous variant.
The sparql query should be built with [class`Resource`], or its parts correctly escaped using [func`sparql_escape_string`], otherwise SPARQL injection is possible.
The format string of the
GVariantisaaa{ss}(an array of an array of dictionaries). The first array represents each INSERT that may exist in the SPARQL string. The second array represents each new node for a given WHERE clause. The last array holds a string pair with the blank node name (e.g.foofor the blank node_:foo) and the URN that was generated for it. For most updates the first two outer arrays will only contain one item.Deprecated since version 3.5: This function makes the expectation that blank nodes have a durable name that persist. The SPARQL and RDF specs define a much more reduced scope for blank node labels. This function advises a behavior that goes against that reduced scope, and will directly make the returned values meaningless if the
Tracker.SparqlConnectionFlags.ANONYMOUS_BNODESflag is defined in the connection. Users that want names generated for them, should look for other methods (e.g. IRIs containing UUIDv4 strings).
- update_blank_async(sparql, cancellable, callback, *user_data)¶
- Parameters:
sparql (
str) – String containing the SPARQL update querycancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Executes asynchronously a SPARQL update and returns the names of the generated blank nodes.
See the [method`SparqlConnection`.update_blank] documentation to learn the differences with [method`SparqlConnection`.update].
Deprecated since version 3.5: See [method`SparqlConnection`.update_blank].
- update_blank_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
a [type`GLib`.Variant] with the generated URNs.
- Return type:
Finishes the operation started with [method`SparqlConnection`.update_blank_async].
This method returns the URNs of the generated nodes, if any. See the [method`SparqlConnection`.update_blank] documentation for the interpretation of the returned [type`GLib`.Variant].
Deprecated since version 3.5: See [method`SparqlConnection`.update_blank].
- update_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
Finishes the operation started with [method`SparqlConnection`.update_async].
- update_resource(graph, resource, cancellable)¶
- Parameters:
graph (
strorNone) – RDF graph where the resource should be inserted/updated, orNonefor the default graphresource (
Tracker.Resource) – A [class`Resource`]cancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
Trueif there were no errors.- Return type:
Inserts a resource as described by resource on the given graph.
This method is synchronous and will block until the update is finished. See [method`SparqlConnection`.update_resource_async] for an asynchronous variant.
It is recommented to consider the usage of [class`Batch`] to cluster database updates. Frequent isolated SPARQL updates through this method will have a degraded performance in comparison.
New in version 3.1.
- update_resource_async(graph, resource, cancellable, callback, *user_data)¶
- Parameters:
graph (
strorNone) – RDF graph where the resource should be inserted/updated, orNonefor the default graphresource (
Tracker.Resource) – A [class`Resource`]cancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]callback (
Gio.AsyncReadyCallbackorNone) – User-defined [type`Gio`.AsyncReadyCallback] to be called when the asynchronous operation is finished.user_data (
objectorNone) – User-defined data to be passed to callback
Inserts asynchronously a resource as described by resource on the given graph.
It is recommented to consider the usage of [class`Batch`] to cluster database updates. Frequent isolated SPARQL updates through this method will have a degraded performance in comparison.
New in version 3.1.
- update_resource_finish(res)¶
- Parameters:
res (
Gio.AsyncResult) – A [type`Gio`.AsyncResult] with the result of the operation- Raises:
- Returns:
Trueif there were no errors.- Return type:
Finishes the operation started with [method`SparqlConnection`.update_resource_async].
New in version 3.1.
- update_statement(sparql, cancellable)¶
- Parameters:
sparql (
str) – The SPARQL updatecancellable (
Gio.CancellableorNone) – Optional [type`Gio`.Cancellable]
- Raises:
- Returns:
A prepared statement
- Return type:
Prepares the given
INSERT/DELETESPARQL as a [class`SparqlStatement`].This prepared statement can be executed through the [method`SparqlStatement`.update] family of functions.
New in version 3.5.