diff options
| author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-05-08 13:18:49 +1000 |
|---|---|---|
| committer | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-05-08 13:18:49 +1000 |
| commit | 37f881ca8eb9e464c57fb905990e569f9e0b4a83 (patch) | |
| tree | fe3ca5ff0e0c22a5e1680828d38f760f38f36242 /examples/berkeleydb_example.py | |
| parent | bb03d810ffd0504c86476f3fd5f9ee31026376f9 (diff) | |
| download | rdflib-37f881ca8eb9e464c57fb905990e569f9e0b4a83.tar.gz | |
Sleepycat -> BerkeleyDBname change; incomplete code update
Diffstat (limited to 'examples/berkeleydb_example.py')
| -rw-r--r-- | examples/berkeleydb_example.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/berkeleydb_example.py b/examples/berkeleydb_example.py new file mode 100644 index 00000000..eec5464e --- /dev/null +++ b/examples/berkeleydb_example.py @@ -0,0 +1,60 @@ +""" +A simple example showing how to use a BerkeleyDB store to do on-disk +persistence. +""" + +from rdflib import ConjunctiveGraph, Namespace, Literal +from rdflib.store import NO_STORE, VALID_STORE + +from tempfile import mktemp + +if __name__ == "__main__": + path = mktemp() + + # Open previously created store, or create it if it doesn't exist yet + graph = ConjunctiveGraph("BerkeleyDB") + + rt = graph.open(path, create=False) + + if rt == NO_STORE: + # There is no underlying BerkeleyDB infrastructure, so create it + graph.open(path, create=True) + else: + assert rt == VALID_STORE, "The underlying store is corrupt" + + print("Triples in graph before add:", len(graph)) + + # Now we'll add some triples to the graph & commit the changes + EG = Namespace("http://example.net/test/") + graph.bind("eg", EG) + + graph.add((EG["pic:1"], EG.name, Literal("Jane & Bob"))) + graph.add((EG["pic:2"], EG.name, Literal("Squirrel in Tree"))) + + graph.commit() + + print("Triples in graph after add:", len(graph)) + + # display the graph in Turtle + print(graph.serialize()) + + # close when done, otherwise BerkeleyDB will leak lock entries. + graph.close() + + graph = None + + # reopen the graph + graph = ConjunctiveGraph("BerkeleyDB") + + graph.open(path, create=False) + + print("Triples still in graph:", len(graph)) + + graph.close() + + # Clean up the temp folder to remove the BerkeleyDB database files... + import os + + for f in os.listdir(path): + os.unlink(path + "/" + f) + os.rmdir(path) |
