You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by "Andy Seaborne (JIRA)" <ji...@apache.org> on 2015/08/05 22:10:05 UTC

[jira] [Commented] (JENA-1006) How read and add triples to `Graph` at the same time?

    [ https://issues.apache.org/jira/browse/JENA-1006?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14658801#comment-14658801 ] 

Andy Seaborne commented on JENA-1006:
-------------------------------------

Iterators read from the database when {{hasNext}}/{{next}} are called.  As with Java iterators on collections, if the storage changes, the iterator is inconsistent.  TDB happens to explicitly check.

You need to complete the iterator, keeping the triples returned. e.g. {{ExtendedIterator.toList()}}

 Sketch:

{noformat}
Iterator<Triple> iter = graph.find(...).toList().iterator() ;
{noformat}
or
{noformat}
for ( Triple t : graph.find(...).toList() ) {
   ...
}
{noformat}

Consider using transactions.

Consider using SPARQL Update.

Use absolute URIs.

> How read and add triples to `Graph` at the same time?
> -----------------------------------------------------
>
>                 Key: JENA-1006
>                 URL: https://issues.apache.org/jira/browse/JENA-1006
>             Project: Apache Jena
>          Issue Type: Question
>          Components: Core, TDB
>    Affects Versions: Jena 2.13.0
>            Reporter: Eugene Tenkaev
>
> Here is my code:
> {code:java}
> public class App {
>     public static void main(String[] args) {
>         DatasetGraph datasetGraph = TDBFactory.createDatasetGraph();
>         Graph graph = datasetGraph.getDefaultGraph();
>         // Fill graph.
>         graph.add(
>             new Triple(
>                 NodeFactory.createURI("www.test.org/unit13"),
>                 NodeFactory.createURI("name"),
>                 NodeFactory.createLiteral("Unit 13", "en")
>             )
>         );
>         graph.add(
>             new Triple(
>                 NodeFactory.createURI("www.test.org/unit13"),
>                 NodeFactory.createURI("type"),
>                 NodeFactory.createURI("robot")
>             )
>         );
>         graph.add(
>             new Triple(
>                 NodeFactory.createURI("www.test.org/unit13"),
>                 NodeFactory.createURI("creationYear"),
>                 NodeFactory.createURI("2015")
>             )
>         );
>         // Test
>         ExtendedIterator<Triple> iter = graph.find(
>             Node.ANY,
>             NodeFactory.createURI("creationYear"),
>             NodeFactory.createURI("2015")
>         );
>         while (iter.hasNext()) {
>             Triple triple = iter.next();
>             Node subject = triple.getSubject();
>             // Exception here.
>             graph.add(
>                 new Triple(
>                     subject, NodeFactory.createURI("status"), NodeFactory.createURI("available")
>                 )
>             );
>         }
>     }
> }
> {code}
> This code gives me exception:
> {noformat}
> Exception in thread "main" java.util.ConcurrentModificationException: Iterator: started at 8, now 9
>     at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW.policyError(DatasetControlMRSW.java:157)
>     at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW.access$000(DatasetControlMRSW.java:32)
>     at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.checkCourrentModification(DatasetControlMRSW.java:110)
>     at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.hasNext(DatasetControlMRSW.java:118)
>     at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:318)
>     at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:318)
>     at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:318)
>     at org.apache.jena.atlas.iterator.Iter.hasNext(Iter.java:942)
>     at com.hp.hpl.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
>     at com.github.hronom.test.jena.App.main(App.java:48)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:606)
>     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
> {noformat}
> I accept this.
> But how I must change code, to add meta info {{<status> <available>}} to every {{<www.test.org/unit13>}} that has {{<creationYear> <2015>}}?
> Test project on [GitHub|https://github.com/Hronom/test-jena]
> Please help me:P



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)