ZODB with rdflib

Note that you must have ZODB3 installed. Also newer versions (3.7 and up) now use the import transaction command and everywhere you would have used get_transaction().commit() now you use transaction.commit() command. As you browse the web looking at sample code, remember this difference!

If run with FileStorage you can only access the Database file (like Data.fs in Zope) can only be opened by one process at a time. This is a severe limitation. The Sleepycat Rdflib backend has the same difficulty.

If you use the same File store but run a ZEO server then you can run the same code with only 2 lines of code changed (one import statement and the fs = (Client or File)Storage command)

Run with FileStorage

Run with ZODB data file called Rdflib.fs

import ZODB
import transaction
from ZODB.FileStorage import FileStorage

# Where is the file
fs=FileStorage('Rdflib.fs')

# get the Zope Database
zdb=ZODB.DB(fs)
# open it
conn=zdb.open()
#get the root
root=conn.root()

# get the Conjunctive Graph or create it the first time
if 'rdflib' not in root:
    from rdflib import ConjunctiveGraph
    root['rdflib'] = ConjunctiveGraph('ZODB')
db=root['rdflib']

# some rdflib stuff like
# c=db.triples((None ...
# db.add((rdf.something,prefix.predicate...
# followed by

transaction.commit()

Run with ZEO

Start the ZEO Server

python /your_path_to/python2.x/site-packages/maybe-a-ZODB3-eggdir/ZEO/runzeo.py -a localhost:8672 -f ./Rdflib.fs

Start a ZEO client

import ZODB
import transaction
from ZEO.ClientStorage import ClientStorage

# Where is the server
fs=ClientStorage(('localhost',8672))

# get the Zope Database
zdb=ZODB.DB(fs)
# open it
conn=zdb.open()
#get the root
root=conn.root()

# get the Conjunctive Graph
# see sample above to create it if it's not in the file
db=root['rdflib'] 

# some rdflib stuff like
# c=db.triples((None ...
# db.add((rdf.something,prefix.predicate...
# followed by

transaction.commit()

In the distributed ZEO environment, use a transaction.commit() at the end AND at the begining of a transaction to catch any previous changes.