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

java.lang.Object
  |
  +--com.hp.hpl.mesa.rdf.jena.rdb.DriverGenericGeneric
All Implemented Interfaces:
IRDBDriver
Direct Known Subclasses:
DriverGenericAttribute, DriverGenericGenericProc, DriverGenericHash, DriverGenericMMGeneric, DriverGenericProc

public class DriverGenericGeneric
extends java.lang.Object
implements IRDBDriver

Base database driver for implementing ModelRDB and StoreRDB. Different drivers are needed for different databases and different layout schemes.

This driver is a base implemention from which database-specific drivers can inherit. It is not generic in the sense that it will work on any minimal SQL store and so should be treated as if it were an abstract class.

The SQL statements which implement each of the functions are loaded in a separate file etc/[layout]_[database].sql from the classpath. See SQLCache documentation for more information on the format of this file.

Version:
$Revision: 1.16 $ on $Date: 2002/10/16 16:19:26 $
Author:
Dave Reynolds

Field Summary
static java.lang.String ANON_NAMESPACE
          Namesapce used to flag anonymous resources
 
Constructor Summary
DriverGenericGeneric()
          Create a bare instance of the driver.
 
Method Summary
 void addStatement(Statement s, ModelRDB m)
          Register a statement in the database.
 IDBID allocateModelID(java.lang.String modelName)
          Allocate a new databaseID for a new model in the same database.
 void attachStore(StoreRDB store)
          Register a store as a user of this driver.
 void cleanDB()
          Remove all RDF information from a database.
 void clear(ModelRDB m)
          Remove all the statements from the given model.
 void close()
          Close the database connection.
 IConstraints createConstraints(ModelRDB model)
          Create a constraint object appropriate for this type of database layout
 void deleteModel(java.lang.String modelName)
          Delete the named from from within the database.
 boolean deleteStatement(IDBID statementID, ModelRDB m)
          Delete the statement in the model corresponding to the given statement ID
 boolean deleteStatement(Resource subject, Property predicate, RDFNode object, ModelRDB m)
          Delete the statements in the model which match the given subject, predicate object.
 void detachStore(StoreRDB store)
          Notify that a store is no longer using this driver.
 void formatDB()
          Initialise a database ready to store RDF tables.
 StatementImplRDB getBareStatementResource(IDBID id, ModelRDB m)
          Fetch a reiefied statement resource just knowing its resource rdb-id.
 java.sql.Connection getConnection()
          Return the jdbc connection to the database, opening it if necessary
 Literal getLiteral(IDBID id)
          Fetch a literal just knowing its literal rdb-id.
 Literal getLiteralFromCache(IDBID id)
          Fetch a literal from the cache just knowing its literal rdb-id.
 IDBID getLiteralID(Literal l)
          Return the database ID for the literal, if it exists
 IDBID getModelID(java.lang.String modelName)
          Return the databaseID for an existing named model within the database.
 java.lang.String getNamespace(IDBID nsid)
          Return the namespace string corresponding to a given namespace id.
 Property getProperty(IDBID id, ModelRDB m)
          Fetch a property just knowing its resource rdb-id.
 Property getProperty(java.lang.String uri, ModelRDB m)
          Fetch a property, knowing its uri.
 Property getPropertyFromCache(IDBID id, ModelRDB m)
          Fetch a property from the cache just knowing its resource rdb-id.
 Resource getResource(IDBID id, ModelRDB m)
          Fetch a resource just knowing its resource rdb-id.
 Resource getResource(java.lang.String uri, ModelRDB m)
          Fetch a resource, knowing its uri.
 Resource getResourceFromCache(IDBID id, ModelRDB m)
          Fetch a resource from the cache just knowing its resource rdb-id.
 IDBID getResourceID(Resource resource)
          Lookup the unique ID for a resource.
 SQLCache getSQLCache()
          Return the SQLCache used for database commands.
 void hintPropertyTable(Property p)
          Hint to the store that the given property could be stored as an attribute table.
 boolean isDBFormatOK()
          Check that the database has a legal set of RDF tables.
 boolean isStatementPresent(Resource subject, Property predicate, RDFNode object, ModelRDB m)
          Check if a statement is in the database.
 java.util.Iterator listModels()
          List the names of all models in this database
 ResultSetIterator listNamespaces(ModelRDB m)
          List all namespaces referenced in the model
 ResultSetIterator listReifiedStatements(ModelRDB m)
          Return an iterator over all statements which are have been added as reified statements.
 ResultSetIterator listStatements(IConstraints constraints, ModelRDB m)
          List all the statements in the given moddel which make the given set of constraints (a constraint set can be obtained using createConstraints(com.hp.hpl.mesa.rdf.jena.rdb.ModelRDB)).
 ResultSetIterator listStatements(Resource subject, Property predicate, RDFNode object, ModelRDB m)
          List all the statements in the model which match the given subject, predicate object - each of which can be null (meaning wildcard).
 ResultSetIterator listSubjects(ModelRDB m)
          List all the subject resources in the model
 void loadDatabaseDriver()
          Force a load of the jdbc driver.
 void loadProperties(java.util.Properties props)
          Load a set of configuration parameters into the driver.
 Property makeProperty(IDBID id, java.lang.String localname, IDBID nsid, ModelRDB m)
          Re-create a property from its local name and namespace identifier
 Resource makeResource(IDBID id, java.lang.String localname, IDBID nsid, ModelRDB m)
          Re-create a resource from its local name and namespace identifier
 Resource makeResource(IDBID id, java.lang.String localname, IDBID nsid, ModelRDB m, IDBID stmtID)
          Re-create a resource from its local name and namespace identifier.
 void preloadCaches(ModelRDB m)
          Preload the resource and literal caches with all data in the DB.
 void saveProperties()
          Store the configuration parameters onto the database layout table in the attached database
 void setCacheLimits(int literalLimit, int resourceLimit)
          Set cache limits for caching resources and literals.
 void setConnection(IDBConnection dbcon)
          Link an existing instance of the driver to a specific jdbc connection
static int splitNamespace(java.lang.String uri)
          Given a URI, determine the split point between the namespace part and the localname part.
 boolean supportsJenaReification()
          Returns true if the database layout supports implicit reification of statements (i.e.
 boolean supportsMultipleModels()
          Returns true of the database layout supports multiple RDF models in the same database.
 java.lang.String toString()
          Printable name for the driver configuration
 IDBID wrapDBID(java.lang.Object id)
          Convert the raw SQL object used to store a database identifier into a java object which meets the IDBID interface
 java.lang.Object wrapFlag(boolean flag)
          Wrap up a boolean flag as a object which the jdbc driver can assert into a boolean/short column
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ANON_NAMESPACE

public static final java.lang.String ANON_NAMESPACE
Namesapce used to flag anonymous resources

See Also:
Constant Field Values
Constructor Detail

DriverGenericGeneric

public DriverGenericGeneric()
Create a bare instance of the driver. It is not functional until a database connection has been supplied via setConnection.

Method Detail

setConnection

public void setConnection(IDBConnection dbcon)
                   throws RDFRDBException
Link an existing instance of the driver to a specific jdbc connection

Specified by:
setConnection in interface IRDBDriver
RDFRDBException

close

public void close()
           throws RDFRDBException
Close the database connection.

This closes all the prepared statements but will leave the jdbc connection itself alone.

Specified by:
close in interface IRDBDriver
Throws:
RDFRDBException - if there is an access problem

getConnection

public java.sql.Connection getConnection()
                                  throws RDFRDBException
Return the jdbc connection to the database, opening it if necessary

Specified by:
getConnection in interface IRDBDriver
RDFRDBException

getSQLCache

public SQLCache getSQLCache()
Return the SQLCache used for database commands.

Specified by:
getSQLCache in interface IRDBDriver

isDBFormatOK

public boolean isDBFormatOK()
                     throws RDFRDBException
Check that the database has a legal set of RDF tables. This is not a full validation check simply a test for the existence of some key tables as evidence of at least attempted formatting.

Specified by:
isDBFormatOK in interface IRDBDriver
Returns:
true if the database seems to be legally formated.
Throws:
RDFRDBException - typtically due to internal programming errors, might trap and change to logging operations in the future.

formatDB

public void formatDB()
              throws RDFRDBException
Initialise a database ready to store RDF tables. This version uses a simple generic representation based a statement table and a resource table, equivalent to the edge-based method of XML storage.

Specified by:
formatDB in interface IRDBDriver
Throws:
RDFRDBException - typtically due to internal programming errors, might trap and change to logging operations in the future.

cleanDB

public void cleanDB()
Remove all RDF information from a database. This will only work if there are no other clients using the DB so is more to simplify testing than a practical tool. Partial DB config or cleaning can leave the DB in a strange state hence it will continue with each cleanup phase even if earlier phases have generated errors and erros are only logged, not propagated as exceptions.

Specified by:
cleanDB in interface IRDBDriver

loadProperties

public void loadProperties(java.util.Properties props)
Load a set of configuration parameters into the driver.

Specified by:
loadProperties in interface IRDBDriver

saveProperties

public void saveProperties()
                    throws java.sql.SQLException
Store the configuration parameters onto the database layout table in the attached database

java.sql.SQLException

toString

public java.lang.String toString()
Printable name for the driver configuration

Overrides:
toString in class java.lang.Object

setCacheLimits

public void setCacheLimits(int literalLimit,
                           int resourceLimit)
Set cache limits for caching resources and literals. In each case a limit of 0 indicates no caching and a limit of -1 indicates unlimited caching.

Specified by:
setCacheLimits in interface IRDBDriver
Parameters:
literalLimit - limit on number of literals to cache

preloadCaches

public void preloadCaches(ModelRDB m)
                   throws RDFException
Preload the resource and literal caches with all data in the DB. Used for higher performance bulk loading in situations where the DB is just used for persistance, not for volume scaling.

There is an issue with reification support in here (isn't there always?!). To avoid having to revisit the database for every reified statement we create dummy StatementImplRDB's for all resources that are actually reified statements and then fix up the content of these dummies when the referenced statements are actually loaded. This is fine in the context of a "loadAll" command but if you try to use preloadCaches in any other situation this behaviour could leave the dangling reification references unless you are careful.

Specified by:
preloadCaches in interface IRDBDriver
RDFException

attachStore

public void attachStore(StoreRDB store)
Register a store as a user of this driver. One driver can support multiple store instances.

Specified by:
attachStore in interface IRDBDriver

detachStore

public void detachStore(StoreRDB store)
Notify that a store is no longer using this driver.

Specified by:
detachStore in interface IRDBDriver

addStatement

public void addStatement(Statement s,
                         ModelRDB m)
                  throws RDFException
Register a statement in the database.

Specified by:
addStatement in interface IRDBDriver
RDFException

listSubjects

public ResultSetIterator listSubjects(ModelRDB m)
                               throws RDFRDBException
List all the subject resources in the model

Specified by:
listSubjects in interface IRDBDriver
Parameters:
m - the model whose subjects are to be listed
Returns:
iterator over Resource objects
RDFRDBException

listNamespaces

public ResultSetIterator listNamespaces(ModelRDB m)
                                 throws RDFRDBException
List all namespaces referenced in the model

Specified by:
listNamespaces in interface IRDBDriver
Returns:
iterator over Strings
RDFRDBException

listStatements

public ResultSetIterator listStatements(Resource subject,
                                        Property predicate,
                                        RDFNode object,
                                        ModelRDB m)
                                 throws RDFException
List all the statements in the model which match the given subject, predicate object - each of which can be null (meaning wildcard).

Specified by:
listStatements in interface IRDBDriver
RDFException

isStatementPresent

public boolean isStatementPresent(Resource subject,
                                  Property predicate,
                                  RDFNode object,
                                  ModelRDB m)
                           throws RDFException
Check if a statement is in the database. Somewhat weird interface so that to reduce the number of db accesses needed during reification work.

Specified by:
isStatementPresent in interface IRDBDriver
Returns:
Boolean.FALSE if the statement is not present at all, Boolean.TRUE if the statement is present and assert, else the DBID of the reified but not asserted statement.
RDFException

listStatements

public ResultSetIterator listStatements(IConstraints constraints,
                                        ModelRDB m)
                                 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(com.hp.hpl.mesa.rdf.jena.rdb.ModelRDB)).

Specified by:
listStatements in interface IRDBDriver
RDFException

createConstraints

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

Specified by:
createConstraints in interface IRDBDriver

deleteStatement

public boolean deleteStatement(Resource subject,
                               Property predicate,
                               RDFNode object,
                               ModelRDB m)
                        throws RDFException
Delete the statements in the model which match the given subject, predicate object.

Specified by:
deleteStatement in interface IRDBDriver
Returns:
true if the statement was successfully deleted
RDFException

deleteStatement

public boolean deleteStatement(IDBID statementID,
                               ModelRDB m)
                        throws RDFException
Delete the statement in the model corresponding to the given statement ID

Specified by:
deleteStatement in interface IRDBDriver
Returns:
true if the statement was successfully deleted
RDFException

clear

public void clear(ModelRDB m)
           throws RDFRDBException
Remove all the statements from the given model. N.B. The implementation is not guaranteed to garbage collect resources and literal tables but this fact is not detectable from the API.

Specified by:
clear in interface IRDBDriver
RDFRDBException

getResource

public Resource getResource(java.lang.String uri,
                            ModelRDB m)
                     throws RDFException
Fetch a resource, knowing its uri.

Specified by:
getResource in interface IRDBDriver
RDFException

getProperty

public Property getProperty(java.lang.String uri,
                            ModelRDB m)
                     throws RDFException
Fetch a property, knowing its uri.

Specified by:
getProperty in interface IRDBDriver
RDFException

supportsJenaReification

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

Specified by:
supportsJenaReification in interface IRDBDriver

listReifiedStatements

public ResultSetIterator listReifiedStatements(ModelRDB m)
                                        throws RDFException
Return an iterator over all statements which are have been added as reified statements. Defect: The current implementation will not notice if all references to a reified statement have been removed.

Specified by:
listReifiedStatements in interface IRDBDriver
RDFException

supportsMultipleModels

public boolean supportsMultipleModels()
Returns true of the database layout supports multiple RDF models in the same database.

Specified by:
supportsMultipleModels in interface IRDBDriver

allocateModelID

public IDBID allocateModelID(java.lang.String modelName)
                      throws RDFRDBException
Allocate a new databaseID for a new model in the same database.

Specified by:
allocateModelID in interface IRDBDriver
RDFRDBException

getModelID

public IDBID getModelID(java.lang.String modelName)
                 throws RDFRDBException
Return the databaseID for an existing named model within the database. Throws RDFRDBException if the driver does not support multiple models per db.

Specified by:
getModelID in interface IRDBDriver
RDFRDBException

deleteModel

public void deleteModel(java.lang.String modelName)
                 throws RDFRDBException
Delete the named from from within the database. This will remove any statements associated with the model and remove the model from the table of known models. Throws RDFRDBException if the driver does not support multiple models per db.

Specified by:
deleteModel in interface IRDBDriver
RDFRDBException

listModels

public java.util.Iterator listModels()
                              throws RDFRDBException
List the names of all models in this database

Specified by:
listModels in interface IRDBDriver
RDFRDBException

getNamespace

public java.lang.String getNamespace(IDBID nsid)
                              throws RDFRDBException
Return the namespace string corresponding to a given namespace id. Can be null if the ID can from a hash function and namespace isn't registered yet.

Specified by:
getNamespace in interface IRDBDriver
RDFRDBException

getLiteral

public Literal getLiteral(IDBID id)
                   throws RDFException
Fetch a literal just knowing its literal rdb-id. Can be null if the ID can from a hash function and literal isn't registered yet.

Specified by:
getLiteral in interface IRDBDriver
RDFException

getLiteralFromCache

public Literal getLiteralFromCache(IDBID id)
Fetch a literal from the cache just knowing its literal rdb-id. If it is not in the cache, do not attempt to retrieve it from the database.

Specified by:
getLiteralFromCache in interface IRDBDriver

getLiteralID

public IDBID getLiteralID(Literal l)
                   throws RDFRDBException
Return the database ID for the literal, if it exists

Specified by:
getLiteralID in interface IRDBDriver
RDFRDBException

getResourceID

public IDBID getResourceID(Resource resource)
                    throws RDFException
Lookup the unique ID for a resource. Return null if it does not have one yet.

Specified by:
getResourceID in interface IRDBDriver
RDFException

getResource

public Resource getResource(IDBID id,
                            ModelRDB m)
                     throws RDFException
Fetch a resource just knowing its resource rdb-id. Can be null if the ID can from a hash function and resource isn't registered yet.

Specified by:
getResource in interface IRDBDriver
RDFException

getResourceFromCache

public Resource getResourceFromCache(IDBID id,
                                     ModelRDB m)
                              throws RDFException
Fetch a resource from the cache just knowing its resource rdb-id. If the resource is not cached, does not attempt to load it from the database.

Specified by:
getResourceFromCache in interface IRDBDriver
RDFException

getPropertyFromCache

public Property getPropertyFromCache(IDBID id,
                                     ModelRDB m)
                              throws RDFException
Fetch a property from the cache just knowing its resource rdb-id. If the resource is not cached, does not attempt to load it from the database.

Specified by:
getPropertyFromCache in interface IRDBDriver
RDFException

getBareStatementResource

public StatementImplRDB getBareStatementResource(IDBID id,
                                                 ModelRDB m)
                                          throws RDFException
Fetch a reiefied statement resource just knowing its resource rdb-id. Can be null if the ID can from a hash function and resource isn't registered yet. This variant will never attempt to instantiate the contents of the Statement (as does getResource) - if the statement is not cached then an empty StatementImplRDB will be returned.

Specified by:
getBareStatementResource in interface IRDBDriver
RDFException

getProperty

public Property getProperty(IDBID id,
                            ModelRDB m)
                     throws RDFException
Fetch a property just knowing its resource rdb-id. Can be null if the ID can from a hash function and resource isn't registered yet.

Specified by:
getProperty in interface IRDBDriver
RDFException

wrapDBID

public IDBID wrapDBID(java.lang.Object id)
               throws RDFRDBException
Convert the raw SQL object used to store a database identifier into a java object which meets the IDBID interface

Specified by:
wrapDBID in interface IRDBDriver
RDFRDBException

wrapFlag

public java.lang.Object wrapFlag(boolean flag)
Wrap up a boolean flag as a object which the jdbc driver can assert into a boolean/short column


makeProperty

public Property makeProperty(IDBID id,
                             java.lang.String localname,
                             IDBID nsid,
                             ModelRDB m)
                      throws RDFException
Re-create a property from its local name and namespace identifier

Specified by:
makeProperty in interface IRDBDriver
RDFException

makeResource

public Resource makeResource(IDBID id,
                             java.lang.String localname,
                             IDBID nsid,
                             ModelRDB m)
                      throws RDFException
Re-create a resource from its local name and namespace identifier

Specified by:
makeResource in interface IRDBDriver
RDFException

makeResource

public Resource makeResource(IDBID id,
                             java.lang.String localname,
                             IDBID nsid,
                             ModelRDB m,
                             IDBID stmtID)
                      throws RDFException
Re-create a resource from its local name and namespace identifier. If stmtID is not null then the resource is actually a reified statement and with that as its id.

Specified by:
makeResource in interface IRDBDriver
RDFException

loadDatabaseDriver

public void loadDatabaseDriver()
                        throws RDFRDBException
Force a load of the jdbc driver.

Throws:
RDFRDBException - if the driver fails to load

hintPropertyTable

public void hintPropertyTable(Property p)
                       throws RDFException
Hint to the store that the given property could be stored as an attribute table.

Specified by:
hintPropertyTable in interface IRDBDriver
RDFException

splitNamespace

public static int splitNamespace(java.lang.String uri)
Given a URI, determine the split point between the namespace part and the localname part. This is the old pre jena 1.6.0 algorithm.

Parameters:
uri -
Returns:
the index of the first character of the localname


Copyright © 2001 Hewlett-Packard. All Rights Reserved.