The TripleStore
class¶
-
class
ferenda.
TripleStore
(location, repository, **kwargs)[source]¶ 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)[source]¶ 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
orSLEEPYCAT
, theselect()
andconstruct()
methods can be sped up (around 150%) by loading the entire content of the triple store into memory, by setting theinmemory
parameter toTrue
When using storetypes
FUSEKI
orSESAME
, storage and retrieval of a large number of triples (particularly theadd_serialized_file()
andget_serialized_file()
methods) can be sped up by setting thecurl
parameter toTrue
, if the command-line tool curl is available.- storetype – The type of store to connect to (
-
re_fromgraph
= re.compile('\\sFROM <(?P<graphuri>[^>]+)>\\s')¶ Internal utility regex to determine wether a query specifies a particular graph to select against.
-
add_serialized
(data, format, context=None)[source]¶ Add the serialized RDF statements in the string data directly to the repository.
-
add_serialized_file
(filename, format, context=None)[source]¶ Add the serialized RDF statements contained in the file filename directly to the repository.
-
get_serialized
(format='nt', context=None)[source]¶ 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='nt', context=None)[source]¶ Saves all statements in the store to filename.
-
select
(query, format='sparql')[source]¶ 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 whateverrequests.get().content
returns – or the special value"python"
which returns a python list of dicts representing rows and columns.
-
construct
(query)[source]¶ 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)[source]¶ 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.
-
static