de.fuberlin.wiwiss.ng4j.db
Class NamedGraphSetDB

java.lang.Object
  extended by de.fuberlin.wiwiss.ng4j.impl.NamedGraphSetIO
      extended by de.fuberlin.wiwiss.ng4j.db.NamedGraphSetDB
All Implemented Interfaces:
NamedGraphSet

public class NamedGraphSetDB
extends NamedGraphSetIO
implements NamedGraphSet

A NamedGraphSet implementation backed by a relational database.

The real work is done by a QuadDB instance. This class provides a NamedGraphSet view onto the QuadDB.

Author:
Richard Cyganiak (richard@cyganiak.de)

Constructor Summary
NamedGraphSetDB(Connection connection)
          Creates a persistent NamedGraphSet from a database connection.
NamedGraphSetDB(Connection connection, String tablePrefix)
          Creates a persistent NamedGraphSet from a database connection using a table prefix.
NamedGraphSetDB(QuadDB db)
          Creates a new NamedGraphSet.
 
Method Summary
 void addGraph(NamedGraph graph)
          Adds a NamedGraph to the set.
 void addQuad(Quad quad)
          Adds a quad to the NamedGraphSet.
 Graph asJenaGraph(Node defaultGraphForAdding)
          Returns the union graph of the NamedGraphSet.
 NamedGraphModel asJenaModel(String defaultGraphForAdding)
          Returns a Jena Model view on the NamedGraphSet, equivalent to the union graph of all graphs in the graph set.
 void clear()
          Deletes all NamedGraphs from the set.
 void close()
          Closes the NamedGraphSet and frees up resources held.
 boolean containsGraph(Node graphName)
          Tells wether the NamedGraphSet contains a NamedGraph.
 boolean containsGraph(String graphNameURI)
          Tells wether the NamedGraphSet contains a NamedGraph.
 boolean containsQuad(Quad pattern)
          Tells wether the NamedGraphSet contains a quad or quads matching a pattern.
 long countGraphs()
          Returns the number of NamedGraphs in the set.
 int countQuads()
          Counts the Quads in the NamedGraphSet.
 NamedGraph createGraph(Node graphName)
          Creates a new NamedGraph and adds it to the set.
 NamedGraph createGraph(String graphNameURI)
          Creates a new NamedGraph and adds it to the set.
static void delete(Connection connection)
          Drops the persistent NamedGraphSet from the database.
static void delete(Connection connection, String tablePrefix)
          Drops a persistent NamedGraphSet from a database.
 Iterator findQuads(Node graphName, Node subject, Node predicate, Node object)
          Finds Quads that match a pattern.
 Iterator findQuads(Quad pattern)
          Finds Quads that match a quad pattern.
 NamedGraph getGraph(Node graphName)
          Returns the NamedGraph with a specific name from the GraphSet.
 NamedGraph getGraph(String graphNameURI)
          Returns the NamedGraph with a specific name from the GraphSet.
 boolean isEmpty()
          Tells wether the set contains any NamedGraphs.
 Iterator listGraphs()
          Returns an iterator over all NamedGraphs in the set.
 void removeGraph(Node graphName)
          Removes a NamedGraph from the set.
 void removeGraph(String graphNameURI)
          Removes the NamedGraph with a specific name.
 void removeQuad(Quad pattern)
          Deletes Quads from the NamedGraphSet.
 
Methods inherited from class de.fuberlin.wiwiss.ng4j.impl.NamedGraphSetIO
read, read, read, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.fuberlin.wiwiss.ng4j.NamedGraphSet
read, read, read, write, write
 

Constructor Detail

NamedGraphSetDB

public NamedGraphSetDB(QuadDB db)
Creates a new NamedGraphSet. The necessary tables will be created automatically if they don't exist.

Parameters:
db - A QuadDB instance which provides persistence for this NamedGraphSet.

NamedGraphSetDB

public NamedGraphSetDB(Connection connection)
Creates a persistent NamedGraphSet from a database connection. The necessary tables will be created automatically if they don't exist.

Parameters:
connection - A connection to an SQL database

NamedGraphSetDB

public NamedGraphSetDB(Connection connection,
                       String tablePrefix)
Creates a persistent NamedGraphSet from a database connection using a table prefix. The necessary tables will be created if they don't exist. The table prefix allows storing multiple NamedGraphSets in a single database.

Parameters:
connection - A connection to an SQL database
tablePrefix - a prefix for all tables used by the new NamedGraphSet
Method Detail

delete

public static void delete(Connection connection)
Drops the persistent NamedGraphSet from the database.

Parameters:
connection - A connection to an SQL database

delete

public static void delete(Connection connection,
                          String tablePrefix)
Drops a persistent NamedGraphSet from a database. Must be used to delete persistent NamedGraphSets that were created with a table prefix.

Parameters:
connection - A connection to an SQL database
tablePrefix - The prefix of the tables used by the NamedGraphSet

addGraph

public void addGraph(NamedGraph graph)
Description copied from interface: NamedGraphSet
Adds a NamedGraph to the set. Will replace a NamedGraph with the same name that is already in the set.

Specified by:
addGraph in interface NamedGraphSet
Parameters:
graph - The NamedGraph to be added

removeGraph

public void removeGraph(Node graphName)
Description copied from interface: NamedGraphSet
Removes a NamedGraph from the set. Nothing happens if no graph with that name is contained in the set. Node.ANY will remove all graphs from the set.

Specified by:
removeGraph in interface NamedGraphSet
Parameters:
graphName - The name of the NamedGraph to be removed

removeGraph

public void removeGraph(String graphNameURI)
Description copied from interface: NamedGraphSet
Removes the NamedGraph with a specific name. Nothing happens if no graph with that name is contained in the set. Node.ANY will remove all graphs from the set.

Specified by:
removeGraph in interface NamedGraphSet
Parameters:
graphNameURI - The name of the NamedGraph to be removed

containsGraph

public boolean containsGraph(Node graphName)
Description copied from interface: NamedGraphSet
Tells wether the NamedGraphSet contains a NamedGraph.

Specified by:
containsGraph in interface NamedGraphSet
Parameters:
graphName - The name of a NamedGraph
Returns:
True if the set contains a graph with that name, false otherwise.

containsGraph

public boolean containsGraph(String graphNameURI)
Description copied from interface: NamedGraphSet
Tells wether the NamedGraphSet contains a NamedGraph.

Specified by:
containsGraph in interface NamedGraphSet
Parameters:
graphNameURI - The name of a NamedGraph
Returns:
True if the set contains a graph with that name, false otherwise.

getGraph

public NamedGraph getGraph(Node graphName)
Description copied from interface: NamedGraphSet
Returns the NamedGraph with a specific name from the GraphSet. Changes to the graph will be reflected in the set.

Specified by:
getGraph in interface NamedGraphSet
Parameters:
graphName - The name of the NamedGraph to be returned
Returns:
The graph with that name, or null if no graph with that name is contained in the set

getGraph

public NamedGraph getGraph(String graphNameURI)
Description copied from interface: NamedGraphSet
Returns the NamedGraph with a specific name from the GraphSet. Changes to the graph will be reflected in the set.

Specified by:
getGraph in interface NamedGraphSet
Parameters:
graphNameURI - The name of the NamedGraph to be returned
Returns:
The graph with that name, or null if no graph with that name is contained in the set

createGraph

public NamedGraph createGraph(Node graphName)
Description copied from interface: NamedGraphSet
Creates a new NamedGraph and adds it to the set. An existing graph with the same name will be replaced.

Specified by:
createGraph in interface NamedGraphSet
Parameters:
graphName - The name of the NamedGraph to be created; must be an URI node
Returns:
The newly created NamedGraph instance

createGraph

public NamedGraph createGraph(String graphNameURI)
Description copied from interface: NamedGraphSet
Creates a new NamedGraph and adds it to the set. An existing graph with the same name will be replaced.

Specified by:
createGraph in interface NamedGraphSet
Parameters:
graphNameURI - The name of the NamedGraph to be created; must be an URI
Returns:
The newly created NamedGraph instance

listGraphs

public Iterator listGraphs()
Description copied from interface: NamedGraphSet
Returns an iterator over all NamedGraphs in the set.

Specified by:
listGraphs in interface NamedGraphSet
Returns:
An iterator over all NamedGraphs in the set

countGraphs

public long countGraphs()
Description copied from interface: NamedGraphSet
Returns the number of NamedGraphs in the set. Empty graphs are counted.

Specified by:
countGraphs in interface NamedGraphSet
Returns:
The number of NamedGraphs in the set.

isEmpty

public boolean isEmpty()
Description copied from interface: NamedGraphSet
Tells wether the set contains any NamedGraphs.

Specified by:
isEmpty in interface NamedGraphSet
Returns:
True if the set contains any NamedGraphs, false otherwise

clear

public void clear()
Description copied from interface: NamedGraphSet
Deletes all NamedGraphs from the set.

Specified by:
clear in interface NamedGraphSet

addQuad

public void addQuad(Quad quad)
Description copied from interface: NamedGraphSet
Adds a quad to the NamedGraphSet. The argument must not contain any wildcards. If the quad is already present, nothing happens. A new named graph will automatically be created if necessary.

Specified by:
addQuad in interface NamedGraphSet
Parameters:
quad - A quad to be added to the NamedGraphSet

containsQuad

public boolean containsQuad(Quad pattern)
Description copied from interface: NamedGraphSet
Tells wether the NamedGraphSet contains a quad or quads matching a pattern. Quad patterns are Quad instances with Node.ANY in one or more positions.

Specified by:
containsQuad in interface NamedGraphSet
Parameters:
pattern - A quad or quad pattern
Returns:
True if The NamedGraphSet contains matching quads, false otherwise

removeQuad

public void removeQuad(Quad pattern)
Description copied from interface: NamedGraphSet
Deletes Quads from the NamedGraphSet. The argument may contain wildcards (Node.ANY). All matching Quads will be deleted. If no Quads match, nothing happens. This operation will not delete any NamedGraphs from the set. Empty NamedGraphs will be retained.

Specified by:
removeQuad in interface NamedGraphSet
Parameters:
pattern - A quad or quad pattern to be deleted

countQuads

public int countQuads()
Description copied from interface: NamedGraphSet
Counts the Quads in the NamedGraphSet. Identical Triples in different NamedGraphs are counted individually.

Specified by:
countQuads in interface NamedGraphSet
Returns:
The number of Quads in the set

findQuads

public Iterator findQuads(Quad pattern)
Description copied from interface: NamedGraphSet
Finds Quads that match a quad pattern. The argument may contain wildcards (Node.ANY).

Specified by:
findQuads in interface NamedGraphSet
Parameters:
pattern - A quad or quad pattern
Returns:
An iterator over all Quads that match the pattern

findQuads

public Iterator findQuads(Node graphName,
                          Node subject,
                          Node predicate,
                          Node object)
Description copied from interface: NamedGraphSet
Finds Quads that match a pattern. All arguments may be Node.ANY to match everything in that position.

Specified by:
findQuads in interface NamedGraphSet
Parameters:
graphName - The graph to find triples from
subject - The subject to be matched
predicate - The predicate to be matched
object - The object to be matched
Returns:
An iterator over all Quads that match the pattern

asJenaGraph

public Graph asJenaGraph(Node defaultGraphForAdding)
Description copied from interface: NamedGraphSet
Returns the union graph of the NamedGraphSet. The graph is backed by the NamedGraphSet: Subsequent changes to one are reflected in the other.

Add operations to the union graph are all written to the default graph specified as the argument.

Delete operations remove the triple from all NamedGraphs.

Specified by:
asJenaGraph in interface NamedGraphSet
Parameters:
defaultGraphForAdding - The name of the default graph used for adding triples; must be an URI
Returns:
A Graph view on the NamedGraphSet

asJenaModel

public NamedGraphModel asJenaModel(String defaultGraphForAdding)
Description copied from interface: NamedGraphSet
Returns a Jena Model view on the NamedGraphSet, equivalent to the union graph of all graphs in the graph set.

Add operations on the returned model are all written to the default graph.

Read and write operations one the returned model have the behaviour of NamedGraphSet.read(String, String) and NamedGraphSet.write(OutputStream, String, String).

All Statements returned by the NamedGraphModel can be casted to NamedGraphStatement to access information about the graphs they are contained in.

Specified by:
asJenaModel in interface NamedGraphSet
Parameters:
defaultGraphForAdding - The name of the default graph used for adding triples; must be an URI
Returns:
A Model view on the NamedGraphSet

close

public void close()
Description copied from interface: NamedGraphSet
Closes the NamedGraphSet and frees up resources held. Any subsequent calls to methods of the object have undefined results.

Specified by:
close in interface NamedGraphSet