The TripleStore class

class ferenda.TripleStore(location, repository, **kwargs)

Presents a limited but uniform interface to different triple stores. It supports both standalone servers accessed over HTTP (Fuseki and Sesame, right now) as well as RDFLib-based persistant stores (The SQLite and Sleepycat/BerkeleyDB backends are supported).

Note

This class does not implement the RDFlib store interface. Instead, it provides a small list of operations that is generally useful for the kinds of things that ferenda-based applications need to do.

This class is an abstract base class, and is not directly instantiated. Instead, call connect(), which returns an initialized object of the appropriate subclass. All subclasses implements the following API.

static connect(storetype, location, repository, **kwargs)

Returns a initialized object, the exact type depending on the storetype parameter.

Parameters:
  • storetype – The type of store to connect to ("FUSEKI", "SESAME", "SLEEPYCAT" or "SQLITE")
  • location – The URL or file path where the main repository is stored
  • repository – The name of the repository to use with the main repository storage
  • **kwargs – Any other named parameters are passed to the appropriate class constructor (see “Store-specific parameters” below).

Example:

>>> # creates a new SQLite db at /tmp/test.sqlite if not already present
>>> sqlitestore = TripleStore.connect("SQLITE", "/tmp/test.sqlite", "myrepo")
>>> sqlitestore.triple_count()
0
>>> sqlitestore.close()
>>> # connect to same db, but store all triples in memory (read-only)
>>> sqlitestore = TripleStore.connect("SQLITE", "/tmp/test.sqlite", "myrepo", inmemory=True)
>>> # connect to a remote Fuseki store over HTTP, using the command-line
>>> # tool curl for faster batch up/downloads
>>> fusekistore = TripleStore.connect("FUSEKI", "http://localhost:3030/", "ds", curl=True)

Store-specific parameters:

When using storetypes SQLITE or SLEEPYCAT, the select() and construct() methods can be sped up (around 150%) by loading the entire content of the triple store into memory, by setting the inmemory parameter to True

When using storetypes FUSEKI or SESAME, storage and retrieval of a large number of triples (particularly the add_serialized_file() and get_serialized_file() methods) can be sped up by setting the curl parameter to True, if the command-line tool curl is available.

add_serialized(data, format, context=None)

Add the serialized RDF statements in the string data directly to the repository.

add_serialized_file(filename, format, context=None)

Add the serialized RDF statements contained in the file filename directly to the repository.

get_serialized(format=u'nt', context=None)

Returns a string containing all statements in the store, serialized in the selected format. Returns byte string, not unicode array!

get_serialized_file(filename, format=u'nt', context=None)

Saves all statements in the store to filename.

select(query, format=u'sparql')

Run a SPARQL SELECT query against the triple store and returns the results.

Parameters:
  • query (str) – A SPARQL query with all neccessary prefixes defined.
  • format (str) – Either one of the standard formats for queries ("sparql", "json" or "binary") – returns whatever requests.get().content returns – or the special value "python" which returns a python list of dicts representing rows and columns.
construct(query)

Run a SPARQL CONSTRUCT query against the triple store and returns the results as a RDFLib graph

Parameters:query (str) – A SPARQL query with all neccessary prefixes defined.
update(query)

Run a SPARQL UPDATE (or DELETE/DROP/CLEAR) against the triplestore. Returns nothing but may raise an exception if something went wrong.

Parameters:query (str) – A SPARQL query with all neccessary prefixes defined.
triple_count(context=None)

Returns the number of triples in the repository.

clear(context=None)

Removes all statements from the repository (without removing the repository as such).

close()

Close all connections to the triplestore. Needed if using RDFLib-based triple store, a no-op if using HTTP based stores.