Search code examples
javardfsparqlsemantic-websesame

How can I load and query RDF at the same time


Can you think of any way to load and query RDF triples at the same time?

I just know that in Sesame you have to first initialize a repository (like loading rdf file) and then you are able to query through SPARQL.

Imagine we initialize an RDF repository(input a file) and do not want for loading to be finished, but at the same time of loading RDF data, want to query data through SPARQL for validation purposes.

For the below initialization, one should wait until the loading of RDF file in repository is finished (my assumption), just then you can query RDF data in repository:

File rdfFile; // parameter, RDF file.
repo = new SailRepository(new MemoryStore());
repo.initialize();

        try {
            RepositoryConnection con = repo.getConnection();
            try {
                con.add(rdfFile, null, rdfFormat);
            } finally {
                con.close();
            }
        }

In the above code, RepositoryConnection.add gets a File as argument. Would giving a FileInputStream as parameter resolve the problem? (Initialization of repository does not wait for loading the file and it will be possible to query before loading is finished).


Solution

  • Using a FileInputStream instead of a File won't make a difference in how Sesame processes the file: the add method will still not return until it has completely added the data.

    However, you can achieve what you want by executing the query in a separate thread from the loading of the file, and then using a transaction with isolation level READ_UNCOMMITTED. Of course, there are few guarantees of completeness or consistency of the query result in such a scenario, but it's technically possible.

    Something like this (from the top of my head, so untested):

       File rdfFile; // parameter, RDF file.
       repo = new SailRepository(new MemoryStore());
       repo.initialize();
    
       Thead loader = new Thread() {
          public void run() {
           try (RepositoryConnection con = repo.getConnection()) {
                con.add(rdfFile, null, rdfFormat);
           }
          }
       };
       Thead query = new Thread() {
          public void run() {
           try (RepositoryConnection con = repo.getConnection()) {
            conn.begin(IsolationLevels.READ_UNCOMMITTED);
            String query = "SELECT * WHERE ... "; 
            try(TupleQueryResult result = conn.prepareTupleQuery(query).evaluate()) {
               while (result.hasNext()) {
                    ...
               }
            }
            conn.commit();
          }
       };
       loader.start(); 
       query.start();