com.hp.hpl.mesa.rdf.jena.rdb
Class ModelRDB

java.lang.Object
  |
  +--com.hp.hpl.mesa.rdf.jena.common.ModelCom
        |
        +--com.hp.hpl.mesa.rdf.jena.rdb.ModelRDB
All Implemented Interfaces:
Model, ModelCon, ModelI, RDFReaderF, RDFWriterF

public class ModelRDB
extends ModelCom
implements Model, ModelI

This implementation of the Model interface uses a relational database to hold the model statements. This provides persistence, transactions and scaling at the expense of performance. It doesn't provide query support since the jena query interface is currently procedural rather than declarative.

Different SQL dialects and different table layout structures are supported by delegating all database access to a separate IRDBSpec implementation.

To reopen an existing database use the open() static factory methods.
To create a database for the first time use the create() static factory methods.

Version:
$Revision: 1.12 $ on $Date: 2002/09/04 08:47:38 $
Author:
Dave Reynolds

Constructor Summary
ModelRDB(IDBConnection dbcon)
          Return the default model from the specified database.
 
Method Summary
 Model abort()
          Abort the current transaction and abandon any changes in progress.
 Model add(Statement s)
          Add a statement to the model
 Model begin()
          Begin a new transation.
 void clear()
          Remove all the statements from the database which are associated with just this model.
 void close()
          Close the Model and free up the resources held.
 Model commit()
          Commit the current transaction.
 boolean contains(Resource s, Property p, RDFNode o)
          Test if the model contains a triple
 Property convert(Property p)
          Convert a property to a database implementation
 RDFNode convert(RDFNode n)
          Convert a node (property, resource or literal) to a database implementation
 Resource convert(Resource r)
          Convert a resource to a database implementation
static ModelRDB create(IDBConnection dbcon, java.lang.String databaseType)
          Create a new database suitable for storing RDF data.
static ModelRDB create(IDBConnection dbcon, java.lang.String layoutType, java.lang.String databaseType)
          Create a new database suitable for storing RDF data.
 IConstraints createConstraints()
          Create a constraint object appropriate for this type of database layout
static ModelRDB createModel(IDBConnection dbcon, java.lang.String name)
          Create a new model on an existing, preformatted database.
 Statement createStatement(Resource r, Property p, RDFNode o)
          create a new statement, not yet added to the modl
static void deleteModel(IDBConnection dbcon, java.lang.String name)
          Create a named model from an existing multi-model database.
 Model difference(Model model)
          Create a new model containing all the statements in this model which are not in another.
 IDBID getDBID()
          Return the database index id for this model.
 StoreRDB getStore()
          Return the store which underpins this model
 boolean independent()
          Determine whether this model is independent.
 Model intersection(Model model)
          Create a new model containing all the statements which are in both this model and another.
static java.util.Iterator listModels(IDBConnection dbcon)
          List the name of all models stored in the database
 StmtIterator listReifiedStatements()
          list all statements which are present in reified form
 StmtIterator listStatements(IConstraints constraints)
          List all the statements in the given moddel which make the given set of constraints (a constraint set can be obtained using createConstraints()).
 Model loadAll()
          Loads all the statements for this model into an in-memory model.
static ModelRDB open(IDBConnection dbcon)
          Open an existing rdf database.
static ModelRDB open(IDBConnection dbcon, java.lang.String name)
          Open an existing rdf database.
 Model query(Selector selector)
          Create a new model containing the statements matching a query.
 boolean supportsJenaReification()
          Returns true if the database layout supports implicit reification of statements (i.e.
 boolean supportsSetOperations()
          Determine whether this model supports set operations.
 boolean supportsTransactions()
          Determine whether this model supports transactions.
 Model union(Model model)
          Create a new model containing all the statements in this model together with all of those in another given model.
 
Methods inherited from class com.hp.hpl.mesa.rdf.jena.common.ModelCom
add, add, add, add, add, add, add, add, add, add, add, add, add, add, containerContains, containerIndexOf, containerSize, contains, contains, contains, contains, contains, contains, contains, contains, contains, contains, containsAll, containsAll, containsAny, containsAny, createAlt, createAlt, createBag, createBag, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createProperty, createProperty, createResource, createResource, createResource, createResource, createResource, createResource, createSeq, createSeq, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, equals, getAlt, getAlt, getBag, getBag, getProperty, getProperty, getProperty, getReader, getReader, getResource, getResource, getSeq, getSeq, getWriter, getWriter, isReified, listContainerMembers, listNameSpaces, listObjects, listObjectsOfProperty, listObjectsOfProperty, listStatements, listStatements, listSubjects, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, read, read, read, read, remove, remove, remove, setReaderClassName, setWriterClassName, size, write, write, write
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.hp.hpl.mesa.rdf.jena.model.Model
add, add, contains, contains, containsAll, containsAll, containsAny, containsAny, createLiteral, createLiteral, createProperty, createResource, createResource, equals, getProperty, getProperty, getResource, isReified, listNameSpaces, listObjects, listObjectsOfProperty, listObjectsOfProperty, listStatements, listStatements, listSubjects, listSubjectsWithProperty, listSubjectsWithProperty, read, read, read, read, remove, size, write, write, write
 
Methods inherited from interface com.hp.hpl.mesa.rdf.jena.model.ModelCon
add, add, add, add, add, add, add, add, add, add, add, add, contains, contains, contains, contains, contains, contains, contains, contains, createAlt, createAlt, createBag, createBag, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createLiteral, createProperty, createResource, createResource, createResource, createResource, createSeq, createSeq, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, createStatement, getAlt, getAlt, getBag, getBag, getProperty, getResource, getSeq, getSeq, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, listSubjectsWithProperty, remove, remove
 
Methods inherited from interface com.hp.hpl.mesa.rdf.jena.model.RDFReaderF
getReader, getReader, setReaderClassName
 
Methods inherited from interface com.hp.hpl.mesa.rdf.jena.model.RDFWriterF
getWriter, getWriter, setWriterClassName
 
Methods inherited from interface com.hp.hpl.mesa.rdf.jena.common.ModelI
containerContains, containerIndexOf, containerSize, listContainerMembers
 

Constructor Detail

ModelRDB

public ModelRDB(IDBConnection dbcon)
         throws RDFRDBException
Return the default model from the specified database. If the database does not appear to be formated it formats it and creates an empty default model.

Parameters:
dbcon - a DBConnectionI specifying the database connection
Throws:
RDFRDBException - if the database cannot be opened
Method Detail

open

public static ModelRDB open(IDBConnection dbcon,
                            java.lang.String name)
                     throws RDFRDBException
Open an existing rdf database. The layout and datatype type information will be dynamically loaded from the database. Will throw an RDFRDBException if the database layout does not support multiple models or if the database does not seem to formated.

Parameters:
dbcon - a DBConnectionI specifying the database connection
name - the name of the RDF model to open, some database layouts support multiple models in a single database
RDFRDBException

open

public static ModelRDB open(IDBConnection dbcon)
                     throws RDFRDBException
Open an existing rdf database. The layout and datatype type information will be dynamically loaded from the database. If the database supports more than one model then the "default" model will be opended.

Parameters:
dbcon - a DBConnectionI specifying the database connection
RDFRDBException

createModel

public static ModelRDB createModel(IDBConnection dbcon,
                                   java.lang.String name)
                            throws RDFRDBException
Create a new model on an existing, preformatted database. Will throw an RDFRDBException if the database layout does not support multiple models or if the database does not seem to formated.

Parameters:
dbcon - a DBConnectionI specifying the database connection
name - the name to give the newly created model
RDFRDBException

deleteModel

public static void deleteModel(IDBConnection dbcon,
                               java.lang.String name)
                        throws RDFRDBException
Create a named model from an existing multi-model database. Will throw an RDFRDBException if the database layout does not support multiple models or if the database does not seem to formated.

Parameters:
dbcon - a DBConnectionI specifying the database connection
name - the name to give the newly created model
RDFRDBException

listModels

public static java.util.Iterator listModels(IDBConnection dbcon)
                                     throws RDFRDBException
List the name of all models stored in the database

RDFRDBException

create

public static ModelRDB create(IDBConnection dbcon,
                              java.lang.String layoutType,
                              java.lang.String databaseType)
                       throws RDFRDBException
Create a new database suitable for storing RDF data. In fact the database has to exist since jdbc can't create an empty database from a vacuum but it can be empty and this call will format it with appropriate tables and stored procedures.

The appropriate RDF-RDB driver to use is assumed to be the class Driver. If that can't be found it defaults to looking for a property file in /etc/Driver.config and uses that to determine the driver class and parameters.

Parameters:
dbcon - a DBConnectionI specifying the database connection
layoutType - the name of the layout style to use. Currently one of: "Generic", "Hash", "MMGeneric", "MMHash", "Proc", "ThinProc".
databaseType - the name of the database type. Currently one of: "Interbase" "Postgresql" "Mysql" "Oracle". This may seem a little redundant given that the jdbc uri implicitly contains this information but there is no standard way of extracting this (esp. if the user connects via a bridge).
RDFRDBException

create

public static ModelRDB create(IDBConnection dbcon,
                              java.lang.String databaseType)
                       throws RDFRDBException
Create a new database suitable for storing RDF data. In fact the database has to exist since jdbc can't create an empty database from a vacuum but it can be empty and this call will format it with appropriate tables and stored procedures.

Uses a default layout format which is able to support multiple models in a single database.

Parameters:
databaseType - the name of the database type. Currently one of: "Interbase" "Postgresql" "Mysql" "Oracle". This may seem a little redundant given that the jdbc uri implicitly contains this information but there is no standard way of extracting this (esp. if the user connects via a bridge).
RDFRDBException

close

public void close()
Close the Model and free up the resources held.

This will discard the underlying StoreRDB object which in turn. Multiple models can share a single underlying DBConnection object (when the database layout is type "MMxxx") so it is only when the last ModelRDB associated with a driver is closed that the driver itself will be closed which will close.

In earlier releases the final driver close resulted in closing the DBConnection which in turn closed the jdbc connection. This proved unpopular and has been removed. The close will free up associated prepared statements but the DBConnection will remain open so you should close that explicitly if you wish to shut down the jdbc connection.

Specified by:
close in interface Model
Overrides:
close in class ModelCom

getStore

public StoreRDB getStore()
Return the store which underpins this model


getDBID

public IDBID getDBID()
Return the database index id for this model. This will be 0 unless the underlying database layout supports multiple models per database.


clear

public void clear()
           throws RDFRDBException
Remove all the statements from the database which are associated with just this model. This no longer reformats the database (which makes it safer and useful for multi-model databases) but means that it is not guaranteed to garbage collect the resource table.

RDFRDBException

loadAll

public Model loadAll()
              throws RDFException
Loads all the statements for this model into an in-memory model.

Returns:
a ModelMem containing the whole of the RDB model
RDFException

listStatements

public StmtIterator listStatements(IConstraints constraints)
                            throws RDFException
List all the statements in the given moddel which make the given set of constraints (a constraint set can be obtained using createConstraints()).

RDFException

createConstraints

public IConstraints createConstraints()
Create a constraint object appropriate for this type of database layout


convert

public Resource convert(Resource r)
                 throws RDFException
Convert a resource to a database implementation

Specified by:
convert in interface ModelI
Overrides:
convert in class ModelCom
Parameters:
r -
Returns:
the converted resource
Throws:
RDFException

convert

public Property convert(Property p)
                 throws RDFException
Convert a property to a database implementation

Specified by:
convert in interface ModelI
Overrides:
convert in class ModelCom
Parameters:
p -
Returns:
the converted property
Throws:
RDFException

convert

public RDFNode convert(RDFNode n)
                throws RDFException
Convert a node (property, resource or literal) to a database implementation

Specified by:
convert in interface ModelI
Overrides:
convert in class ModelCom
Parameters:
n -
Returns:
the converted node
Throws:
RDFException

supportsJenaReification

public boolean supportsJenaReification()
Returns true if the database layout supports implicit reification of statements (i.e. statements can be treated as resources).


listReifiedStatements

public StmtIterator listReifiedStatements()
                                   throws RDFException
list all statements which are present in reified form

Specified by:
listReifiedStatements in interface Model
Overrides:
listReifiedStatements in class ModelCom
Returns:
an iterator over the set of all reified statements in the model
Throws:
RDFException - Generic RDF Exception

add

public Model add(Statement s)
          throws RDFException
Add a statement to the model

Specified by:
add in interface Model
Overrides:
add in class ModelCom
Parameters:
s - The statement to be added.
Returns:
This model.
Throws:
RDFException - Generic RDF Exception

contains

public boolean contains(Resource s,
                        Property p,
                        RDFNode o)
                 throws RDFException
Test if the model contains a triple

Specified by:
contains in interface Model
Overrides:
contains in class ModelCom
Parameters:
s - The subject of the statment tested.
p - The predicate of the statement tested.
o - The object of the statement tested.
Returns:
true if the statement with subject s, property p and object o is in the model, false otherwise
Throws:
RDFException - Generic RDF Exception

createStatement

public Statement createStatement(Resource r,
                                 Property p,
                                 RDFNode o)
                          throws RDFException
create a new statement, not yet added to the modl

Specified by:
createStatement in interface Model
Overrides:
createStatement in class ModelCom
Parameters:
r - the subject of the statement
p - the predicate of the statement
o - the object of the statement
Returns:
the new statement
Throws:
RDFException - generic RDF exception

begin

public Model begin()
            throws RDFException
Begin a new transation.

All changes made to a model within a transaction, will either be made, or none of them will be made.

Specified by:
begin in interface Model
Overrides:
begin in class ModelCom
Returns:
this model to enable cascading.
Throws:
RDFException - Generic RDF Exception

abort

public Model abort()
            throws RDFException
Abort the current transaction and abandon any changes in progress.

Specified by:
abort in interface Model
Overrides:
abort in class ModelCom
Returns:
this model to enable cascading.
Throws:
RDFException - Generic RDF Exception

commit

public Model commit()
             throws RDFException
Commit the current transaction.

Specified by:
commit in interface Model
Overrides:
commit in class ModelCom
Returns:
this model to enable cascading.
Throws:
RDFException - Generic RDF Exception

independent

public boolean independent()
Determine whether this model is independent.

For efficiency reasons, some implementations may create models which which are dependent on others, i.e. a change in one model may cause a change in another. If this is the case this method will return false, otherwise it will return true.

Specified by:
independent in interface Model
Overrides:
independent in class ModelCom
Returns:
true if this model is indepdent of others

supportsTransactions

public boolean supportsTransactions()
Determine whether this model supports transactions.

Specified by:
supportsTransactions in interface Model
Returns:
true if this model supports transactions.

supportsSetOperations

public boolean supportsSetOperations()
Description copied from interface: Model
Determine whether this model supports set operations.

Specified by:
supportsSetOperations in interface Model
Returns:
true if this model supports set operations.

query

public Model query(Selector selector)
            throws RDFException
Description copied from interface: Model
Create a new model containing the statements matching a query.

A statment is considered to match if the test method of s returns true when called on s.

Specified by:
query in interface Model
Parameters:
selector - A selector object.
Returns:
an iterator over the matching statements
Throws:
RDFException - Generic RDF exception.

union

public Model union(Model model)
            throws RDFException
Description copied from interface: Model
Create a new model containing all the statements in this model together with all of those in another given model.

Specified by:
union in interface Model
Parameters:
model - The other model whose statements are to be included.
Returns:
A new model containing all the statements that are in either model
Throws:
RDFException - Generic RDF Exception

intersection

public Model intersection(Model model)
                   throws RDFException
Description copied from interface: Model
Create a new model containing all the statements which are in both this model and another. As models are sets of statements, a statement contained in both models will only appear once in the resulting model.

Specified by:
intersection in interface Model
Parameters:
model - The other model.
Returns:
A new model containing all the statements that are in both models.
Throws:
RDFException - Generic RDF Exception

difference

public Model difference(Model model)
                 throws RDFException
Description copied from interface: Model
Create a new model containing all the statements in this model which are not in another.

Specified by:
difference in interface Model
Parameters:
model - the other model whose statements are to be excluded.
Returns:
a new model containing all the statements in this model that are not in the given model.
Throws:
RDFException - Generic RDF Exception


Copyright © 2001 Hewlett-Packard. All Rights Reserved.