You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Zachary M Jablons <zm...@us.ibm.com> on 2014/06/18 01:50:25 UTC
Named models disappearing into TDB
Hey all,
I've been working with TDB for a bit now processing data into it, but I've
run into an issue where after adding a named model, I find that the named
model simply disappears, and is inaccessible. Here's some example code:
Model prodModel = null;
dset.begin(ReadWrite.WRITE);
Lock lck = dset.getLock();
try {
lck.enterCriticalSection(Lock.WRITE);
if (dset.containsNamedModel("product_info")) {
prodModel = dset.getNamedModel("product_info");
System.out.println("Using existing model");
} else {
System.out.println("Making new model");
dset.addNamedModel("product_info",
ModelFactory.createDefaultModel());
prodModel = dset.getNamedModel("product_info");
}
dset.commit();
} finally {lck.leaveCriticalSection(); dset.end();}
TDB.sync(dset);
dset.begin(ReadWrite.READ);
Iterator<String> itr = dset.listNames();
while (itr.hasNext()) {
System.out.println(itr.next());
}
dset.end();
In this case dset is a class attribute set by TDBFactory.createDataset
("path/to/dataset/") at construction. Is it possible that this is causing
an issue? If I run this multiple times, containsNamedModel returns false
each time, even though manually inspecting nodes.dat shows it.
I would expect it to print the named graph ("product_info") but instead
nothing is printed. I might be using Jena's TDB interface incorrectly --
it's hard to tell -- but I've been able to do this before with no issue.
I'm using jena-tdb-0.10.1, jena-core-2.10.1, jena-arq-2.10.1.
Thanks,
- Zach Jablons
Re: Named models disappearing into TDB
Posted by Andy Seaborne <an...@apache.org>.
Hi Zach,
A named model in TDB only "exists" as far as contains() and listNames()
are concerned only if it has any triples in it. There is no separate
graph management.
Named graphs are stored in the quads table and listNames() is, in
effect, a projection of the named graph column (made distinct).
dset.addNamedModel("product_info", ModelFactory.createDefaultModel());
adds an empty graph.
In TDB, adding a graph is copying it in ... but it's empty. No triples
added so it does not appear in listNames(). It is different in a memory
dataset.
For TDB, you don't need to check existence, just do:
prodModel = dset.getNamedModel("product_info");
You will always get a model which you can update. The default memory
dataset is the same here.
A few unrelated points:
Use a proper URI for the graph name else you will run into the problems
if you ever try to write out a named graph format.
TDB.sync isn't necessary if you use transactions.
Lock lck isn't necessary if you use transactions unless you have
multiple threads inside one transaction. Multiple transactions are
already thread safe.
nodes.dat is not referenced counted - just because there is a node in
it, does not mean it is still active in the database.
Andy
On 18/06/14 00:50, Zachary M Jablons wrote:
>
>
> Hey all,
>
> I've been working with TDB for a bit now processing data into it, but I've
> run into an issue where after adding a named model, I find that the named
> model simply disappears, and is inaccessible. Here's some example code:
>
> Model prodModel = null;
> dset.begin(ReadWrite.WRITE);
> Lock lck = dset.getLock();
> try {
> lck.enterCriticalSection(Lock.WRITE);
> if (dset.containsNamedModel("product_info")) {
> prodModel = dset.getNamedModel("product_info");
> System.out.println("Using existing model");
> } else {
> System.out.println("Making new model");
> dset.addNamedModel("product_info",
> ModelFactory.createDefaultModel());
> prodModel = dset.getNamedModel("product_info");
> }
> dset.commit();
> } finally {lck.leaveCriticalSection(); dset.end();}
>
> TDB.sync(dset);
> dset.begin(ReadWrite.READ);
> Iterator<String> itr = dset.listNames();
> while (itr.hasNext()) {
> System.out.println(itr.next());
> }
> dset.end();
> In this case dset is a class attribute set by TDBFactory.createDataset
> ("path/to/dataset/") at construction. Is it possible that this is causing
> an issue? If I run this multiple times, containsNamedModel returns false
> each time, even though manually inspecting nodes.dat shows it.
> I would expect it to print the named graph ("product_info") but instead
> nothing is printed. I might be using Jena's TDB interface incorrectly --
> it's hard to tell -- but I've been able to do this before with no issue.
>
> I'm using jena-tdb-0.10.1, jena-core-2.10.1, jena-arq-2.10.1.
>
> Thanks,
> - Zach Jablons
>
Re: Named models disappearing into TDB
Posted by Rob Vesse <rv...@dotnetrdf.org>.
Graphs are named with URIs so by using the name "product_info" what you
have used is a relative URI and will not be the complete name
When given a relative URI Jena's default behaviour is to resolve it
against the current working directory. So your graph is actually getting
named something like the following:
file:///your/working/dir/product_info
Depending on where your code runs the Base URI will change and so the
graph you are trying to access will change hence the apparent
disappearance of graphs. You can use the dset.listNames() method to get
an iterator over the graph names in a store.
It is also worth noting that TDB does not store empty graphs (which is
what your code appears to do) so adding an empty named model has no effect
other than to add the graph name to the node table. Therefore asking if
an empty graph is contained in TDB will always return false.
Btw you are several versions behind current stable release which is now
Jena/ARQ 2.11.2 and TDB 1.0.2 (Not that this is likely to make any
difference in this case)
Rob
On 18/06/2014 00:50, "Zachary M Jablons" <zm...@us.ibm.com> wrote:
>
>
>Hey all,
>
>I've been working with TDB for a bit now processing data into it, but I've
>run into an issue where after adding a named model, I find that the named
>model simply disappears, and is inaccessible. Here's some example code:
>
> Model prodModel = null;
> dset.begin(ReadWrite.WRITE);
> Lock lck = dset.getLock();
> try {
> lck.enterCriticalSection(Lock.WRITE);
> if (dset.containsNamedModel("product_info")) {
> prodModel = dset.getNamedModel("product_info");
> System.out.println("Using existing model");
> } else {
> System.out.println("Making new model");
> dset.addNamedModel("product_info",
>ModelFactory.createDefaultModel());
> prodModel = dset.getNamedModel("product_info");
> }
> dset.commit();
> } finally {lck.leaveCriticalSection(); dset.end();}
>
> TDB.sync(dset);
> dset.begin(ReadWrite.READ);
> Iterator<String> itr = dset.listNames();
> while (itr.hasNext()) {
> System.out.println(itr.next());
> }
> dset.end();
>In this case dset is a class attribute set by TDBFactory.createDataset
>("path/to/dataset/") at construction. Is it possible that this is causing
>an issue? If I run this multiple times, containsNamedModel returns false
>each time, even though manually inspecting nodes.dat shows it.
>I would expect it to print the named graph ("product_info") but instead
>nothing is printed. I might be using Jena's TDB interface incorrectly --
>it's hard to tell -- but I've been able to do this before with no issue.
>
>I'm using jena-tdb-0.10.1, jena-core-2.10.1, jena-arq-2.10.1.
>
>Thanks,
>- Zach Jablons