Class RDFStorage

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class RDFStorage
    extends Object
    implements Closeable
    This class provides an abstraction of an RDF repository and implements the common low-level operations.
    Author:
    burgetr
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected RDFStorage​(org.eclipse.rdf4j.repository.Repository repo)
      Use the create functions for creating the instances.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(org.eclipse.rdf4j.model.IRI subj, org.eclipse.rdf4j.model.IRI pred, org.eclipse.rdf4j.model.IRI obj, org.eclipse.rdf4j.model.IRI context)
      Adds a new quadruple to the storage.
      void addNamespace​(String prefix, String namespace)  
      void addValue​(org.eclipse.rdf4j.model.IRI subj, org.eclipse.rdf4j.model.IRI pred, Object value, org.eclipse.rdf4j.model.IRI context)
      Adds a new data quadruple to the storage.
      void clear()
      Clears the entire RDF repository.
      void clear​(org.eclipse.rdf4j.model.IRI context)
      Clears the entire context from the RDF repository.
      void clearNamespaces()  
      void close()  
      static RDFStorage create​(org.eclipse.rdf4j.repository.Repository repo)  
      static RDFStorage createHTTP​(String serverUrl, String repositoryId)  
      static RDFStorage createMemory​(String dataDir)  
      static RDFStorage createNative​(String dataDir)  
      void deleteNamespace​(String prefix)  
      void execSparqlUpdate​(String query)  
      org.eclipse.rdf4j.model.Model executeSafeQuery​(String query)
      Executes an internal (safe) SPARQL graph query in a default isolation level.
      org.eclipse.rdf4j.model.Model executeSafeQuery​(String query, org.eclipse.rdf4j.common.transaction.IsolationLevel isolationLevel)
      Executes an internal (safe) SPARQL graph query in a given transaction isolation level.
      List<org.eclipse.rdf4j.query.BindingSet> executeSafeTupleQuery​(String query)
      Executes an internal (safe) tuple query.
      SparqlQueryResult executeSparqlQuery​(String queryString, boolean distinct, long limit, long offset)
      Checks and executes extrenal (possibly unsafe) SPARQL query and returns a result depending on the query type.
      List<org.eclipse.rdf4j.query.BindingSet> executeSparqlTupleQuery​(String queryString, boolean distinct, long limit, long offset)
      Checks and executes extrenal (possibly unsafe) SPARQL SELECT query and returns a result.
      org.eclipse.rdf4j.repository.RepositoryConnection getConnection()  
      org.eclipse.rdf4j.model.Model getContextModel​(Collection<org.eclipse.rdf4j.model.Resource> contexts)
      Obtains a model containing all statements in a given context.
      org.eclipse.rdf4j.model.Model getContextModel​(org.eclipse.rdf4j.model.Resource context)
      Obtains a model containing all statements in a given context.
      List<org.eclipse.rdf4j.model.Resource> getContexts()  
      long getLastSequenceValue​(org.eclipse.rdf4j.model.IRI sequenceIri)
      Obtains the last assigned value of a sequence with the given name.
      static Object getLiteralAsObject​(org.eclipse.rdf4j.model.Literal lval)  
      String getNamespace​(String prefix)  
      List<org.eclipse.rdf4j.model.Namespace> getNamespaces()  
      long getNextSequenceValue​(org.eclipse.rdf4j.model.IRI sequenceIri)  
      org.eclipse.rdf4j.model.Value getPropertyValue​(org.eclipse.rdf4j.model.Resource subject, org.eclipse.rdf4j.model.IRI predicate)
      Obtains the value of the given predicate for the given subject.
      org.eclipse.rdf4j.repository.Repository getRepository()  
      Collection<org.eclipse.rdf4j.model.Resource> getResourcesOfType​(org.eclipse.rdf4j.model.IRI type)
      Finds IRIs of resources of the given type.
      Collection<org.eclipse.rdf4j.model.Resource> getResourcesOfType​(org.eclipse.rdf4j.model.IRI type, org.eclipse.rdf4j.model.Resource context)
      Finds IRIs of resources of the given type.
      org.eclipse.rdf4j.model.Model getSubjectModel​(org.eclipse.rdf4j.model.Resource subject)
      Obtains a model for the specific subject.
      org.eclipse.rdf4j.model.IRI getSubjectType​(org.eclipse.rdf4j.model.Resource subject)
      Determines the type of the subject as determined by the corresponding rdf:type predicate (if present)
      org.eclipse.rdf4j.model.ValueFactory getValueFactory()  
      void importStream​(InputStream stream, org.eclipse.rdf4j.rio.RDFFormat dataFormat)  
      void importStream​(InputStream stream, org.eclipse.rdf4j.rio.RDFFormat dataFormat, String baseURI)  
      void importStream​(InputStream stream, org.eclipse.rdf4j.rio.RDFFormat dataFormat, org.eclipse.rdf4j.model.IRI context)  
      void importStream​(InputStream stream, org.eclipse.rdf4j.rio.RDFFormat dataFormat, org.eclipse.rdf4j.model.IRI context, String baseURI)  
      void importTurtle​(String query)  
      void importTurtle​(String query, org.eclipse.rdf4j.model.IRI context)  
      void importXML​(String query)  
      void importXML​(String query, org.eclipse.rdf4j.model.IRI context)  
      void insertGraph​(org.eclipse.rdf4j.model.Model graph)
      Inserts a new graph to the database.
      void insertGraph​(org.eclipse.rdf4j.model.Model graph, org.eclipse.rdf4j.model.IRI contextIri)
      Inserts a new graph to the database.
      ServiceConfig loadServiceConfig​(org.eclipse.rdf4j.model.IRI iri)  
      void queryExportCSV​(String queryString, OutputStream ostream)  
      void remove​(org.eclipse.rdf4j.model.IRI subj, org.eclipse.rdf4j.model.IRI pred, org.eclipse.rdf4j.model.IRI obj, org.eclipse.rdf4j.model.IRI context)
      Removes a quadruple from the storage.
      void removeStatements​(org.eclipse.rdf4j.model.Resource subj, org.eclipse.rdf4j.model.IRI pred, org.eclipse.rdf4j.model.Value obj, org.eclipse.rdf4j.model.Resource... contexts)  
      void removeValue​(org.eclipse.rdf4j.model.IRI subj, org.eclipse.rdf4j.model.IRI pred, Object value, org.eclipse.rdf4j.model.IRI context)
      Removes a quadruple from the storage.
    • Constructor Detail

      • RDFStorage

        protected RDFStorage​(org.eclipse.rdf4j.repository.Repository repo)
        Use the create functions for creating the instances.
    • Method Detail

      • create

        public static RDFStorage create​(org.eclipse.rdf4j.repository.Repository repo)
      • getRepository

        public org.eclipse.rdf4j.repository.Repository getRepository()
      • getValueFactory

        public org.eclipse.rdf4j.model.ValueFactory getValueFactory()
      • getConnection

        public org.eclipse.rdf4j.repository.RepositoryConnection getConnection()
      • getResourcesOfType

        public Collection<org.eclipse.rdf4j.model.Resource> getResourcesOfType​(org.eclipse.rdf4j.model.IRI type)
        Finds IRIs of resources of the given type.
        Parameters:
        type - The resource type IRI.
        Returns:
        a collection of Resources
      • getResourcesOfType

        public Collection<org.eclipse.rdf4j.model.Resource> getResourcesOfType​(org.eclipse.rdf4j.model.IRI type,
                                                                               org.eclipse.rdf4j.model.Resource context)
        Finds IRIs of resources of the given type.
        Parameters:
        type - The resource type IRI.
        context - Repository context
        Returns:
        a collection of Resources
      • getPropertyValue

        public org.eclipse.rdf4j.model.Value getPropertyValue​(org.eclipse.rdf4j.model.Resource subject,
                                                              org.eclipse.rdf4j.model.IRI predicate)
                                                       throws StorageException
        Obtains the value of the given predicate for the given subject.
        Parameters:
        subject - the subject resource
        predicate - the predicate IRI
        Returns:
        the resulting Value or null when there is no corresponding triplet available.
        Throws:
        StorageException
      • getSubjectModel

        public org.eclipse.rdf4j.model.Model getSubjectModel​(org.eclipse.rdf4j.model.Resource subject)
                                                      throws StorageException
        Obtains a model for the specific subject.
        Parameters:
        subject -
        Returns:
        Throws:
        StorageException
      • getSubjectType

        public org.eclipse.rdf4j.model.IRI getSubjectType​(org.eclipse.rdf4j.model.Resource subject)
                                                   throws StorageException
        Determines the type of the subject as determined by the corresponding rdf:type predicate (if present)
        Parameters:
        subject - the subject IRI
        Returns:
        the type IRI or null when the type is not defined
        Throws:
        StorageException
      • getContextModel

        public org.eclipse.rdf4j.model.Model getContextModel​(org.eclipse.rdf4j.model.Resource context)
                                                      throws StorageException
        Obtains a model containing all statements in a given context.
        Parameters:
        context - the context IRI
        Returns:
        Throws:
        StorageException
      • getContextModel

        public org.eclipse.rdf4j.model.Model getContextModel​(Collection<org.eclipse.rdf4j.model.Resource> contexts)
                                                      throws StorageException
        Obtains a model containing all statements in a given context.
        Parameters:
        contexts - the context IRIs
        Returns:
        Throws:
        StorageException
      • executeSafeQuery

        public org.eclipse.rdf4j.model.Model executeSafeQuery​(String query)
                                                       throws StorageException
        Executes an internal (safe) SPARQL graph query in a default isolation level.
        Parameters:
        query - the SPARQL query
        Returns:
        a the resulting model
        Throws:
        StorageException
      • executeSafeQuery

        public org.eclipse.rdf4j.model.Model executeSafeQuery​(String query,
                                                              org.eclipse.rdf4j.common.transaction.IsolationLevel isolationLevel)
                                                       throws StorageException
        Executes an internal (safe) SPARQL graph query in a given transaction isolation level.
        Parameters:
        query - the SPARQL query
        isolationLevel - the transaction isolation level or null when no transaction is required.
        Returns:
        a the resulting model
        Throws:
        StorageException
      • executeSafeTupleQuery

        public List<org.eclipse.rdf4j.query.BindingSet> executeSafeTupleQuery​(String query)
                                                                       throws StorageException
        Executes an internal (safe) tuple query.
        Parameters:
        query -
        Returns:
        a list of binding sets object representing the result
        Throws:
        StorageException
      • executeSparqlTupleQuery

        public List<org.eclipse.rdf4j.query.BindingSet> executeSparqlTupleQuery​(String queryString,
                                                                                boolean distinct,
                                                                                long limit,
                                                                                long offset)
                                                                         throws StorageException
        Checks and executes extrenal (possibly unsafe) SPARQL SELECT query and returns a result. This is a special version of executeSparqlQuery(String, boolean, long, long) optimized for SELECT queries.
        Parameters:
        queryString - the SPARQL SELECT query
        distinct - true when only distinct results should be returned
        limit - maximal number of returned results
        offset - index of the first result to be returned
        Returns:
        a query result that holds the result type and data
        Throws:
        StorageException - when the query could not be parsed or executed or is not a SELECT query.
      • executeSparqlQuery

        public SparqlQueryResult executeSparqlQuery​(String queryString,
                                                    boolean distinct,
                                                    long limit,
                                                    long offset)
                                             throws StorageException
        Checks and executes extrenal (possibly unsafe) SPARQL query and returns a result depending on the query type.
        Parameters:
        queryString - the SPARQL query
        distinct - true when only distinct results should be returned
        limit - maximal number of returned results
        offset - index of the first result to be returned
        Returns:
        a query result that holds the result type and data
        Throws:
        StorageException - when the query could not be parsed or executed
      • add

        public void add​(org.eclipse.rdf4j.model.IRI subj,
                        org.eclipse.rdf4j.model.IRI pred,
                        org.eclipse.rdf4j.model.IRI obj,
                        org.eclipse.rdf4j.model.IRI context)
        Adds a new quadruple to the storage.
        Parameters:
        subj -
        pred -
        obj -
        context -
      • addValue

        public void addValue​(org.eclipse.rdf4j.model.IRI subj,
                             org.eclipse.rdf4j.model.IRI pred,
                             Object value,
                             org.eclipse.rdf4j.model.IRI context)
        Adds a new data quadruple to the storage.
        Parameters:
        subj -
        pred -
        value -
        context -
      • insertGraph

        public void insertGraph​(org.eclipse.rdf4j.model.Model graph)
                         throws StorageException
        Inserts a new graph to the database.
        Parameters:
        graph -
        Throws:
        StorageException
      • insertGraph

        public void insertGraph​(org.eclipse.rdf4j.model.Model graph,
                                org.eclipse.rdf4j.model.IRI contextIri)
                         throws StorageException
        Inserts a new graph to the database.
        Parameters:
        graph -
        contextIri - the context to be used for the inserted statements
        Throws:
        StorageException
      • clear

        public void clear​(org.eclipse.rdf4j.model.IRI context)
                   throws StorageException
        Clears the entire context from the RDF repository.
        Parameters:
        context - Context IRI
        Throws:
        StorageException
      • removeStatements

        public void removeStatements​(org.eclipse.rdf4j.model.Resource subj,
                                     org.eclipse.rdf4j.model.IRI pred,
                                     org.eclipse.rdf4j.model.Value obj,
                                     org.eclipse.rdf4j.model.Resource... contexts)
      • remove

        public void remove​(org.eclipse.rdf4j.model.IRI subj,
                           org.eclipse.rdf4j.model.IRI pred,
                           org.eclipse.rdf4j.model.IRI obj,
                           org.eclipse.rdf4j.model.IRI context)
        Removes a quadruple from the storage.
        Parameters:
        subj -
        pred -
        obj -
        context -
      • removeValue

        public void removeValue​(org.eclipse.rdf4j.model.IRI subj,
                                org.eclipse.rdf4j.model.IRI pred,
                                Object value,
                                org.eclipse.rdf4j.model.IRI context)
        Removes a quadruple from the storage.
        Parameters:
        subj -
        pred -
        value -
        context -
      • getLiteralAsObject

        public static Object getLiteralAsObject​(org.eclipse.rdf4j.model.Literal lval)
      • getLastSequenceValue

        public long getLastSequenceValue​(org.eclipse.rdf4j.model.IRI sequenceIri)
                                  throws StorageException
        Obtains the last assigned value of a sequence with the given name.
        Parameters:
        sequenceIri - the sequence IRI
        Returns:
        the last assigned value or 0 when the sequence does not exist.
        Throws:
        StorageException
      • getNamespaces

        public List<org.eclipse.rdf4j.model.Namespace> getNamespaces()
      • getNamespace

        public String getNamespace​(String prefix)
      • addNamespace

        public void addNamespace​(String prefix,
                                 String namespace)
      • deleteNamespace

        public void deleteNamespace​(String prefix)
      • clearNamespaces

        public void clearNamespaces()
      • getContexts

        public List<org.eclipse.rdf4j.model.Resource> getContexts()