ordf / ordf / handler /

This module implements some triplestores. Most basic but least 
scalable is the storage that comes with `RDFLib`_. More advanced
and scalable but more complicated to set up is `4store`_.

RDFLib Basic Storage -- :class:`RDFLib`
.. autoclass:: RDFLib

4Store Quad-Store -- :class:`FourStore`
.. autoclass:: FourStore

.. _RDFLib:
.. _4store:

__all__ = ["RDFLib", "FourStore"]

from rdflib import plugin
from import Store
from ordf.graph import Graph, _Graph, ConjunctiveGraph
from ordf.utils import get_identifier
from ordf.handler import HandlerPlugin
from logging import getLogger

class RDFLib(HandlerPlugin):
    The RDFLib handler can use any of the back-ends that are supported
    by rdflib itself. The constructor takes a *store* keyword argument
    that specifies which to use. This defaults to *"IOMemory"* which is
    not terribly useful. Initialisation arguments for the store should
    be passed as positional arguments, for example::

        RDFLib("/some/where/data", store="Sleepycat")

    or the equivalent in the configuration file,::

        rdflib.args = /some/where/data = Sleepycat
    def __init__(self, *av, **kw):
        kw = kw.copy()
        store = kw.setdefault("store", "IOMemory")
        del kw["store"]
        self.log = getLogger(__name__ + "." + self.__class__.__name__)
        if isinstance(store, basestring):
            store_cls = plugin.get(store, Store)
  "Initialising %s storage" % store_cls.__name__)
            store = store_cls(*av, **kw) = store
    def __getitem__(self, k):
        ## Careful, the graph in the store is to be treated 
        ## as read-only unless a set operation is made.
        ## Unfortunately this means we have to copy it
        ident = get_identifier(k)
        orig = Graph(, identifier=ident)
        new = Graph(identifier=ident)
        new += orig
        return new
    def __setitem__(self, k, g):
        assert isinstance(g, _Graph)
        old = Graph(, identifier=get_identifier(k))
        old.remove((None, None, None))
        for statement in g.triples((None, None, None)):
        if hasattr(, "sync"):
    def __delitem__(self, k):
        old = Graph(, identifier=get_identifier(k))
        old.remove((None, None, None))
        if hasattr(, "sync"):

    def append(self, g):
        old = Graph(, identifier=g.identifier)
        old += g
        if hasattr(, "sync"):
    def remove(self, g):
        del self[g]

    def query(self, *av, **kw):
        if hasattr(, "query"):
            return*av, **kw)
            g = ConjunctiveGraph(
            return g.query(*av, **kw)

    def commit(self):
    def rollback(self):
class FourStore(RDFLib):
    Use of this back-end requires the `py4s`_ bindings to `4store`_.
    It also requires 4store to be built from the `multiclient branch`_
    to support multiple simultaneous client connections.

    Initialisation takes a comma-separated string of arguments,
    only the first, the name of the *kb* is required. For example::


    or equivalently in the configuration file::

        fourstore.args = kbname
        fourstore.args = kbname,soft_limit=-1

    .. _py4s:
    .. _multiclient branch:
    def __init__(self, config):
        from py4s import LazyFourStore = LazyFourStore(config)
    def __setitem__(self, k, g):
        assert isinstance(g, _Graph)
        ident = get_identifier(k)
        cursor =
        cursor.add_graph(g, replace=True)
    def __delitem__(self, k):
        ident = get_identifier(k)
        cursor =
    def append(self, frag):
        ident = get_identifier(frag)
        cursor =
        cursor.add_graph(frag, replace=False)
    def query(self, *av, **kw):
        return*av, **kw)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.