org.openanzo.server.repository.rdb
Class RepositoryConnection

java.lang.Object
  extended by org.openanzo.server.repository.rdb.RepositoryConnection
All Implemented Interfaces:
IResetListener

public class RepositoryConnection
extends java.lang.Object
implements IResetListener

Provides read access to all the data stored in the repository as a graph of RDF statements. Each RepositoryGraph also provides write access to the database via RDF updates and access to a cache of SQL prepared statements.

Author:
Joe Betz, Matthew Roy ( mroy@cambridgesemantics.com )

Field Summary
 ResourceCache<ACI> aciCache
          Cache of ACI objects
 ResourceCache<NamedGraph> namedGraphCache
          Cache of NamedGraph objects
static int QUERY
          ReadOnly Query type
static int RO
          ReadOnly type
static int RW
          ReadWrite type
static java.lang.String TRANSACTION_SEQUENCE
          Id of sequence for transactions
 
Constructor Summary
RepositoryConnection(org.apache.commons.pool.impl.GenericObjectPool connectionPool, CacheProvider cacheProvider, LiteralIndexer literalIndexer, int type, RepositoryConnectionConfiguration configuration, java.sql.Connection connection)
          Construct a new RepositoryConnection with given configuration properties.
 
Method Summary
 void abort()
          Abort database transaction
 void addInferredObject(org.openrdf.model.URI property, org.openrdf.model.URI obj, org.openrdf.model.URI inferredObj)
          Add an inferred object
 void addInferredProperty(org.openrdf.model.URI property, org.openrdf.model.URI inferredProperty)
          Add an inferred property
 void begin()
          Begin database transaction Note:Database already in transaction
 void beginTempTable()
          Begin database transaction Note:Database already in transaction
 void clearCache()
          Clear the cache objects in the RepositoryConnection
 void close()
          Close the connection
 void commit()
          Commit database transaction
 void endTempTable()
          Commit database transaction
 java.util.Set<java.lang.String> getAllStoredNamedGraphs()
          Get stored NamedGraphs
 java.util.HashMap<java.lang.Long,PrivilegeSet> getCachedACLPrivileges()
          Get the cache of ACL privileges
 RepositoryConnectionConfiguration getConfiguration()
           
 java.sql.Connection getConnection()
          Retrieve the internal JDBC connection
 int getConnectionType()
          Is the JDBC connection set to readOnly
 java.lang.String getContainerName()
          Get the name of the container
 long getDefaultSystemGraphId()
          Get the node ID for the default system graph's URI
 long getDefaultSystemMetaGraphId()
          Get the node ID for the default system metadata graph's URI
 LiteralIndexer getLiteralIndexer()
           
static java.io.InputStream getLocationAsStream(java.lang.String location)
          Convert a string to an InputStream
 org.openrdf.model.URI getMetadataGraphUri(org.openrdf.model.URI namedGraphUri, java.lang.Long lastTransactionTime)
          Get the URI for the NamedGraph's metadata graph
 boolean getNamedGraphRevision(org.openrdf.model.URI namedGraphUri, java.lang.Long revision, IRepositoryHandler handler)
          Get the contents of a NamedGraph at a given revision, and pass the results to an IBocaHandler
 CompositeNodeLayout getNodeLayout()
          Get the NodeLayout for this connection
 long getNumberOfStoredNamedGraphs()
          Get total number of stored NamedGraphs
 long getNumberOfStoredStatements()
          Get total number of stored Statements
 SQLCache getPreparedStatementCache()
          Get the SQLCache for this connection
 ReplicationService getReplicationService()
          Get this RepositoryConnection's ReplicationService
 java.util.Set<org.openrdf.model.URI> getRolesForGraph(org.openrdf.model.URI namedGraphUri)
          Get the set of Roles that have permission to read the given graph
 java.util.Set<org.openrdf.model.URI> getRolesForUser(org.openrdf.model.URI user)
          Get the set of Roles to which the given user is a member
 Sequence getSequence()
          Get Sequence provider for this connection
 java.lang.Long getServerId()
          Get the server ID for the server
 org.openrdf.model.URI getServerUri()
          Get the unique id of this server instance
 long getSize(org.openrdf.model.URI namedGraphUri)
          Get the size of a NamedGraph
 java.util.Set<org.openrdf.model.URI> getStoredNamedGraphs(java.lang.Long authenticatedUserId)
          Get stored NamedGraphs that user can read
 java.lang.Long getUserId()
          Get the current userId that this connection is bound
 java.util.Set<org.openrdf.model.URI> getUsersForGraph(java.lang.Long authenticatedUserId, org.openrdf.model.URI graphUri)
          Get the set of userIds that have read permission for the given NamedGraph
 boolean isClosed()
          Is the JDBC connection closed
 boolean isLocked()
          Is this connection currently locked
 boolean isReseting()
          Is the database in the process of being reset
 boolean isReturned()
          Has this connection been returned to the ContainerConnectionPool
 boolean isUserInRole(long userId, org.openrdf.model.URI role)
          Test to determine if a user is in a role
 void passivate()
          Return this connection to its parent pool
 void reset(java.lang.Long id)
          Server was reset
 void resetDatabase(boolean hardReset, java.util.Collection<org.openrdf.model.Statement> statements)
          Reset the database contents with the system graph data contained in the graph
 void resetDatabase(boolean hardReset, java.io.InputStream stream)
          Reset the database contents with the system graph data contained in the InputStream
 boolean runAskQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs, java.util.Set<org.openrdf.model.URI> graphs, java.lang.String query, boolean current)
          Run a Sparql ASK query
 java.util.Collection<StatementWrapper> runIndexQuery(java.lang.Long userId, java.lang.String query, int offset, int limit)
          Query the text indexer for a set of statements that match a text indexer query
 QueryResult runQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs, java.util.Set<org.openrdf.model.URI> graphs, java.lang.String query, org.openrdf.model.URI baseUri)
          Run a query on the server's data
 int runQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs, java.util.Set<org.openrdf.model.URI> graphs, java.lang.String query, org.openrdf.model.URI baseUri, java.io.OutputStream outputStream)
          Run a query on the server's data
 void setReseting(boolean reseting)
          Set if the database is in the process of being reset
 void setReturned(boolean returned)
          Set if connection has been returned to the ContainerConnectionPool
 void setUserId(java.lang.Long userId)
          Bind the connection to the given userId for all subsequent operations until userId is set to a new value, or set to null.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TRANSACTION_SEQUENCE

public static final java.lang.String TRANSACTION_SEQUENCE
Id of sequence for transactions

See Also:
Constant Field Values

aciCache

public ResourceCache<ACI> aciCache
Cache of ACI objects


namedGraphCache

public ResourceCache<NamedGraph> namedGraphCache
Cache of NamedGraph objects


RO

public static final int RO
ReadOnly type

See Also:
Constant Field Values

QUERY

public static final int QUERY
ReadOnly Query type

See Also:
Constant Field Values

RW

public static final int RW
ReadWrite type

See Also:
Constant Field Values
Constructor Detail

RepositoryConnection

public RepositoryConnection(org.apache.commons.pool.impl.GenericObjectPool connectionPool,
                            CacheProvider cacheProvider,
                            LiteralIndexer literalIndexer,
                            int type,
                            RepositoryConnectionConfiguration configuration,
                            java.sql.Connection connection)
Construct a new RepositoryConnection with given configuration properties.

Parameters:
connectionPool - pool that this connection was taken from
cacheProvider - shared cache of node to id caches
literalIndexer - literal indexer for queries
type - type of connection, QUERY vs WRITE
configuration - configuration data for this connection
connection - underlying jdbc connection
Method Detail

setUserId

public void setUserId(java.lang.Long userId)
Bind the connection to the given userId for all subsequent operations until userId is set to a new value, or set to null. This userId is the userId that all database operations are bound to in order to establish identity in the system.

Parameters:
userId - UserId to which connection is bound

getUserId

public java.lang.Long getUserId()
Get the current userId that this connection is bound

Returns:
Current userId that this connection is bound or null if no userId set

reset

public void reset(java.lang.Long id)
Description copied from interface: IResetListener
Server was reset

Specified by:
reset in interface IResetListener
Parameters:
id - Server's id which was reset

getPreparedStatementCache

public SQLCache getPreparedStatementCache()
Get the SQLCache for this connection

Returns:
SQLCache for this connection

getNodeLayout

public CompositeNodeLayout getNodeLayout()
Get the NodeLayout for this connection

Returns:
NodeLayout for this connection

getConnection

public java.sql.Connection getConnection()
Retrieve the internal JDBC connection

Returns:
The internal JDBC connection

close

public void close()
           throws java.sql.SQLException
Close the connection

Throws:
java.sql.SQLException

isLocked

public boolean isLocked()
Is this connection currently locked

Returns:
true if this connection currently locked

begin

public void begin()
Begin database transaction Note:Database already in transaction


abort

public void abort()
Abort database transaction


commit

public void commit()
Commit database transaction


beginTempTable

public void beginTempTable()
Begin database transaction Note:Database already in transaction


endTempTable

public void endTempTable()
Commit database transaction


getSize

public long getSize(org.openrdf.model.URI namedGraphUri)
             throws AnzoException
Get the size of a NamedGraph

Parameters:
namedGraphUri - URI of NamedGraph
Returns:
Number of triples in NamedGraph
Throws:
AnzoException - Throws an exception if graph not found or user does not have permission to read graph. Note: If user doesn't have permission to see graph, unknown graph error is still thrown in order to prevent probing queries to determine existing of graphs

getMetadataGraphUri

public org.openrdf.model.URI getMetadataGraphUri(org.openrdf.model.URI namedGraphUri,
                                                 java.lang.Long lastTransactionTime)
                                          throws AnzoException
Get the URI for the NamedGraph's metadata graph

Parameters:
namedGraphUri - URI of NamedGraph
lastTransactionTime - Timestamp of last committed transaction, if null, query is run to find last transaction time
Returns:
URI of metadata graph
Throws:
AnzoException - Throws an exception if graph not found or user does not have permission to read graph. Note: If user doesn't have permission to see graph, unknown graph error is still thrown in order to prevent probing queries to determine existing of graphs

isUserInRole

public boolean isUserInRole(long userId,
                            org.openrdf.model.URI role)
                     throws AnzoException
Test to determine if a user is in a role

Parameters:
userId - ID of user
role - Role to determine membership
Returns:
User's membership in given role
Throws:
AnzoException

addInferredProperty

public void addInferredProperty(org.openrdf.model.URI property,
                                org.openrdf.model.URI inferredProperty)
                         throws AnzoException
Add an inferred property

Parameters:
property - property to add
inferredProperty - inferred property to add
Throws:
AnzoException

addInferredObject

public void addInferredObject(org.openrdf.model.URI property,
                              org.openrdf.model.URI obj,
                              org.openrdf.model.URI inferredObj)
                       throws AnzoException
Add an inferred object

Parameters:
property - property to add
obj - object to add
inferredObj - inferred object to add
Throws:
AnzoException

getUsersForGraph

public java.util.Set<org.openrdf.model.URI> getUsersForGraph(java.lang.Long authenticatedUserId,
                                                             org.openrdf.model.URI graphUri)
                                                      throws AnzoException
Get the set of userIds that have read permission for the given NamedGraph

Parameters:
authenticatedUserId - userId of authenticated user for this session
graphUri - URI of NamedGraph to get users
Returns:
Set of userIds that have read permission for the given NamedGraph
Throws:
AnzoException

getRolesForUser

public java.util.Set<org.openrdf.model.URI> getRolesForUser(org.openrdf.model.URI user)
                                                     throws AnzoException
Get the set of Roles to which the given user is a member

Parameters:
user - URI of user to fetch roles
Returns:
the set of Roles to which the given user is a member
Throws:
AnzoException

getRolesForGraph

public java.util.Set<org.openrdf.model.URI> getRolesForGraph(org.openrdf.model.URI namedGraphUri)
                                                      throws AnzoException
Get the set of Roles that have permission to read the given graph

Parameters:
namedGraphUri - URI of graph to fetch roles
Returns:
the set of Roles that have permission to read the given graph
Throws:
AnzoException

getStoredNamedGraphs

public java.util.Set<org.openrdf.model.URI> getStoredNamedGraphs(java.lang.Long authenticatedUserId)
                                                          throws AnzoException
Get stored NamedGraphs that user can read

Parameters:
authenticatedUserId - userId of authenticated user for this session
Returns:
Set of NamedGraph URIs for NamedGraphs that user can read
Throws:
AnzoException

getNumberOfStoredNamedGraphs

public long getNumberOfStoredNamedGraphs()
                                  throws AnzoException
Get total number of stored NamedGraphs

Returns:
Total number of stored NamedGraphs
Throws:
AnzoException

getAllStoredNamedGraphs

public java.util.Set<java.lang.String> getAllStoredNamedGraphs()
                                                        throws AnzoException
Get stored NamedGraphs

Returns:
Set of stored NamedGraphs on server
Throws:
AnzoException

getNumberOfStoredStatements

public long getNumberOfStoredStatements()
                                 throws AnzoException
Get total number of stored Statements

Returns:
Total number of stored Statements
Throws:
AnzoException

getSequence

public Sequence getSequence()
Get Sequence provider for this connection

Returns:
Sequence provider for this connection

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

isReturned

public boolean isReturned()
Has this connection been returned to the ContainerConnectionPool

Returns:
Connection has been returned

setReturned

public void setReturned(boolean returned)
Set if connection has been returned to the ContainerConnectionPool

Parameters:
returned -

getConnectionType

public int getConnectionType()
Is the JDBC connection set to readOnly

Returns:
JDBC connection set to readOnly

isClosed

public boolean isClosed()
Is the JDBC connection closed

Returns:
JDBC connection is closed

getReplicationService

public ReplicationService getReplicationService()
Get this RepositoryConnection's ReplicationService

Returns:
RepositoryConnection's ReplicationService

clearCache

public void clearCache()
Clear the cache objects in the RepositoryConnection


resetDatabase

public void resetDatabase(boolean hardReset,
                          java.io.InputStream stream)
                   throws AnzoException
Reset the database contents with the system graph data contained in the InputStream

Parameters:
hardReset - if true, all tables will be dropped and recreated, instead of just clearing the contents of the existing tables
stream - Stream containing the data to initialize the system graph
Throws:
AnzoException

resetDatabase

public void resetDatabase(boolean hardReset,
                          java.util.Collection<org.openrdf.model.Statement> statements)
Reset the database contents with the system graph data contained in the graph

Parameters:
hardReset - if true, all tables will be dropped and recreated, instead of just clearing the contents of the existing tables
statements - statements containing the data to initialize the system graph

isReseting

public boolean isReseting()
Is the database in the process of being reset

Returns:
Database is being reset

setReseting

public void setReseting(boolean reseting)
Set if the database is in the process of being reset

Parameters:
reseting - Is datbase being reset

getNamedGraphRevision

public boolean getNamedGraphRevision(org.openrdf.model.URI namedGraphUri,
                                     java.lang.Long revision,
                                     IRepositoryHandler handler)
                              throws AnzoException
Get the contents of a NamedGraph at a given revision, and pass the results to an IBocaHandler

Parameters:
namedGraphUri - URI of NamedGraph
revision - Revision to retrieve
handler - Handler to which results are written
Returns:
true unless there was an exception
Throws:
AnzoException

getServerUri

public org.openrdf.model.URI getServerUri()
Get the unique id of this server instance

Returns:
Unique id of this server instance

getServerId

public java.lang.Long getServerId()
Get the server ID for the server

Returns:
the server ID for the server

getCachedACLPrivileges

public java.util.HashMap<java.lang.Long,PrivilegeSet> getCachedACLPrivileges()
Get the cache of ACL privileges

Returns:
The cached ACLPrivilages

passivate

public void passivate()
               throws java.lang.Exception
Return this connection to its parent pool

Throws:
java.lang.Exception

getConfiguration

public RepositoryConnectionConfiguration getConfiguration()
Returns:
the configuration

runQuery

public int runQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs,
                    java.util.Set<org.openrdf.model.URI> graphs,
                    java.lang.String query,
                    org.openrdf.model.URI baseUri,
                    java.io.OutputStream outputStream)
             throws AnzoException
Run a query on the server's data

Parameters:
defaultNamedGraphs - Set of NamedGraph URIs that are used as the default graph
graphs - Set of NamedGraph URIs that make up dataset's NamedGraph set
query - Text of query
baseUri - Base URI for resolving relative URIs in the query. May be null.
outputStream - Stream on to which the results are written
Returns:
the type of query results
Throws:
AnzoException

runQuery

public QueryResult runQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs,
                            java.util.Set<org.openrdf.model.URI> graphs,
                            java.lang.String query,
                            org.openrdf.model.URI baseUri)
                     throws AnzoException
Run a query on the server's data

Parameters:
defaultNamedGraphs - Set of NamedGraph URIs that are used as the default graph
graphs - Set of NamedGraph URIs that make up dataset's NamedGraph set
query - Text of query
baseUri - Base URI for resolving relative URIs in the query. May be null.
Returns:
the results of the query
Throws:
AnzoException

runAskQuery

public boolean runAskQuery(java.util.Set<org.openrdf.model.URI> defaultNamedGraphs,
                           java.util.Set<org.openrdf.model.URI> graphs,
                           java.lang.String query,
                           boolean current)
                    throws AnzoException
Run a Sparql ASK query

Parameters:
defaultNamedGraphs - Set of NamedGraph URIs that are used as the default graph
graphs - Set of NamedGraph URIs that make up dataset's NamedGraph set
query - Text of query
current - Run against live data, or against last committed transaction
Returns:
results of the Sparql ASK query
Throws:
AnzoException

runIndexQuery

public java.util.Collection<StatementWrapper> runIndexQuery(java.lang.Long userId,
                                                            java.lang.String query,
                                                            int offset,
                                                            int limit)
                                                     throws AnzoException
Query the text indexer for a set of statements that match a text indexer query

Parameters:
userId - user id for the user that the query is run on behalf
query - query string
offset - index of first result to return
limit - maximum number of results to return
Returns:
set of statements that match a text indexer query
Throws:
AnzoException

getLiteralIndexer

public LiteralIndexer getLiteralIndexer()
Returns:
the literalIndexer

getDefaultSystemMetaGraphId

public long getDefaultSystemMetaGraphId()
Get the node ID for the default system metadata graph's URI

Returns:
the node ID for the default system metadata graph's URI

getDefaultSystemGraphId

public long getDefaultSystemGraphId()
Get the node ID for the default system graph's URI

Returns:
the node ID for the default system graph's URI

getContainerName

public java.lang.String getContainerName()
Get the name of the container

Returns:
the name of the container

getLocationAsStream

public static java.io.InputStream getLocationAsStream(java.lang.String location)
                                               throws AnzoException
Convert a string to an InputStream

Parameters:
location - Location of data to convert to an InputStream
Returns:
the location converted to an InputStream
Throws:
AnzoException


Copyright © 2007 Cambridge Semantics Inc.. All Rights Reserved.