⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdfrepository.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*  Sesame - Storage and Querying architecture for RDF and RDF Schema *  Copyright (C) 2001-2005 Aduna * *  Contact: *  	Aduna *  	Prinses Julianaplein 14 b *  	3817 CS Amersfoort *  	The Netherlands *  	tel. +33 (0)33 465 99 87 *  	fax. +33 (0)33 465 99 87 * *  	http://aduna.biz/ *  	http://www.openrdf.org/ * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2.1 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package org.openrdf.sesame.sailimpl.rdbms;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import org.openrdf.util.log.ThreadLog;import org.openrdf.model.BNode;import org.openrdf.model.Literal;import org.openrdf.model.Resource;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.sesame.sail.Namespace;import org.openrdf.sesame.sail.SailChangedListener;import org.openrdf.sesame.sail.SailInitializationException;import org.openrdf.sesame.sail.SailInternalException;import org.openrdf.sesame.sail.SailUpdateException;import org.openrdf.sesame.sail.util.SailChangedEventImpl;/** * A portable implementation of the RdfRepository interface for relational * databases, based on (a subset of) the SQL2/SQL92 standard. This class * defines the update methods. The superclass RdfSource defines the * read-only methods. * * @author Arjohn Kampman **/public class RdfRepository extends RdfSource implements		org.openrdf.sesame.sail.RdfRepository, TableNames{	/*----------+	 | Variables |	 +----------*/	protected List _sailChangedListeners;	protected SailChangedEventImpl _sailChangedEvent;	/** 	 * Key used to specify how often to commit during transaction, in number of triples.	 */	protected static final String COMMIT_INTERVAL_KEY = "commitInterval";	/**	 * The ID for the next resource that will be added. This variable starts with	 * value 1 and is increased everytime a resource is added.	 **/	protected int _nextResourceId;	/**	 * The ID for the next literal that will be added. This variable starts with	 * value -1 and is decreased everytime a literal is added.	 **/	protected int _nextLiteralId;	/**	 * The ID for the next statement that will be added. This variable starts with	 * value 1 and is increased everytime a statement is added.	 **/	protected int _nextStatementId;	/** Flag indicating whether a transaction is currently being performed. **/	protected boolean _transactionStarted;	/** Flag indicating whether statements were added during a transaction. **/	protected boolean _statementsAdded;	/** Flag indicating whether statements were removed during a transaction. **/	protected boolean _statementsRemoved;	/**	 * The number of triples added via {@link#addStatement(Resource, URI, Value}	 * at which the database transaction will be commited. The user can specify 	 * this value by setting the <code>commitInterval</code> parameter in the 	 * repository configuration.  Defaults to 1000 if unspecified.	 * @see #COMMIT_INTERVAL_KEY	 * @see #addStatement(Resource,URI,Value)	 */	protected int _triplesCommitInterval = 1000;	// Connection and PreparedStatement used during a transaction.	protected Connection _addRawTriplesConn;	protected PreparedStatement _addRawTriplesSt;	protected int _rawTriplesCount;	/*----------------------------------------+	 | Constructors                            |	 +----------------------------------------*/	public RdfRepository() {		super();		_nextResourceId = 1;		_nextLiteralId = -1;		_nextStatementId = 1;		_sailChangedListeners = new ArrayList(0);		_transactionStarted = false;	}	/*----------------------------------------+	 | overriden methods from RdfSource        |	 +----------------------------------------*/	/**	 * @see org.openrdf.sesame.sail.Sail#initialize(java.util.Map)	 */	public void initialize(Map configParams)		throws SailInitializationException	{		String commitIntervalStr = (String)configParams.get(COMMIT_INTERVAL_KEY);		if (commitIntervalStr != null) {			try {				_triplesCommitInterval = Integer.parseInt(commitIntervalStr);			}			catch (NumberFormatException e) {				throw new SailInitializationException(e);			}		}		super.initialize(configParams);	}	/*------------------------+	 | Database initialization |	 +------------------------*/	/**	 * Initializes the database. _initDatabase() creates tables, indexes and	 * inserts default values into the database.	 **/	protected void _initDatabase()		throws SailInitializationException	{		super._initDatabase();		try {			// Initialize _nextResourceId, _nextLiteralId and _nextStatementId;			_setNextResourceId();			_setNextLiteralId();			_setNextStatementId();		}		catch (SQLException e) {			throw new SailInitializationException(e);		}	}	protected void _createDbSchema()		throws SQLException	{		super._createDbSchema();		// Tables used to add new statements:		_createAddedTriplesTable();		_createNewTriplesTable();		_createRawTriplesTable();		_createExpiredTriplesTable();		_createExpiredResourcesTable();		_createExpiredLiteralsTable();	}	protected void _createAddedTriplesTable()		throws SQLException	{		if (_schemaVersion == -1) {			_createTriplesTable(ADDED_TRIPLES_TABLE, false);			// Add an index over (subj, pred, obj)			_rdbms.createIndex(ADDED_TRIPLES_TABLE, new String[] {					"subj", "pred", "obj" }, false);		}		else if (_schemaVersion < 4) {			_rdbms.renameTableColumn(ADDED_TRIPLES_TABLE, "subject", "subj",					_rdbms.ID_INT + " NOT NULL");			_rdbms.renameTableColumn(ADDED_TRIPLES_TABLE, "predicate", "pred",					_rdbms.ID_INT + " NOT NULL");			_rdbms.renameTableColumn(ADDED_TRIPLES_TABLE, "object", "obj",					_rdbms.ID_INT + " NOT NULL");		}	}	protected void _createNewTriplesTable()		throws SQLException	{		if (_schemaVersion == -1) {			_createTriplesTable(NEW_TRIPLES_TABLE, false);		}		else if (_schemaVersion < 4) {			_rdbms.renameTableColumn(NEW_TRIPLES_TABLE, "subject", "subj",					_rdbms.ID_INT + " NOT NULL");			_rdbms.renameTableColumn(NEW_TRIPLES_TABLE, "predicate", "pred",					_rdbms.ID_INT + " NOT NULL");			_rdbms.renameTableColumn(NEW_TRIPLES_TABLE, "object", "obj",					_rdbms.ID_INT + " NOT NULL");		}	}	protected void _createRawTriplesTable()		throws SQLException	{		if (_schemaVersion < 2) {			// Old or non-existent table			if (_schemaVersion != -1) {				// Drop old table				_rdbms.dropTable(RAW_TRIPLES_TABLE);			}			_rdbms.executeUpdate("CREATE TABLE " + RAW_TRIPLES_TABLE + " (" +			// statement ID					"id " + _rdbms.ID_INT + " NOT NULL, " +					// subject					"subjNs " + _rdbms.ID_INT + " NOT NULL, " + "subjLname "					+ _rdbms.LOCALNAME + NN_ON_TEXT + ", " +					// predicate					"predNs " + _rdbms.ID_INT + " NOT NULL, " + "predLname "					+ _rdbms.LOCALNAME + NN_ON_TEXT + ", " +					// object (resource, or datatype of literal)					"objNs " + _rdbms.ID_INT + " NOT NULL, " + "objLname "					+ _rdbms.LOCALNAME + NN_ON_TEXT + ", " +					// object (literal)					"objLabelHash " + _rdbms.LABEL_HASH + ", " + "objLang "					+ _rdbms.LANGUAGE + ", " + "objLabel " + _rdbms.LABEL + ", "					+ "objIsLiteral " + _rdbms.BOOLEAN + ")");		}	}	protected void _createExpiredTriplesTable()		throws SQLException	{		if (_schemaVersion == -1) {			_rdbms.executeUpdate("CREATE TABLE " + EXPIRED_TRIPLES_TABLE + " (id "					+ _rdbms.ID_INT + " NOT NULL)");		}		else if (_schemaVersion < 5) {			// EXPIRED_VALUES_TABLE has been replaced by two new tables:			// EXPIRED_RESOURCES_TABLE and EXPIRED_LITERALS_TABLE			_rdbms.dropTable(EXPIRED_VALUES_TABLE);		}	}	protected void _createExpiredResourcesTable()		throws SQLException	{		if (_schemaVersion < 5) {			_rdbms.executeUpdate("CREATE TABLE " + EXPIRED_RESOURCES_TABLE					+ " (id " + _rdbms.ID_INT + " NOT NULL)");		}	}	protected void _createExpiredLiteralsTable()		throws SQLException	{		if (_schemaVersion < 5) {			_rdbms.executeUpdate("CREATE TABLE " + EXPIRED_LITERALS_TABLE					+ " (id " + _rdbms.ID_INT + " NOT NULL)");		}	}	/*----------------------------------------+	 | Initialization of local variables       |	 +----------------------------------------*/	protected void _setNextResourceId()		throws SQLException	{		Connection con = _rdbms.getConnection();		java.sql.Statement st = con.createStatement();		ResultSet rs = st.executeQuery("SELECT MAX(id) FROM " + RESOURCES_TABLE);		if (rs.next()) {			// next resource ID is maximum + 1			_nextResourceId = rs.getInt(1) + 1;		}		else {			// start with ID 1			_nextResourceId = 1;		}		rs.close();		st.close();		con.close();	}	protected int _getNextResourceId() {		return _nextResourceId++;	}	protected void _setNextLiteralId()		throws SQLException	{		Connection con = _rdbms.getConnection();		java.sql.Statement st = con.createStatement();		ResultSet rs = st.executeQuery("SELECT MIN(id) FROM " + LITERALS_TABLE);		if (rs.next()) {			// next literal ID is minimum - 1			_nextLiteralId = rs.getInt(1) - 1;		}		else {			// start with ID -1			_nextLiteralId = -1;		}		rs.close();		st.close();		con.close();	}	protected int _getNextLiteralId() {		return _nextLiteralId--;	}	protected void _setNextStatementId()		throws SQLException	{		Connection con = _rdbms.getConnection();		java.sql.Statement st = con.createStatement();		ResultSet rs = st.executeQuery("SELECT MAX(id) FROM " + TRIPLES_TABLE);		if (rs.next()) {			// next statement ID is maximum + 1			_nextStatementId = rs.getInt(1) + 1;		}		else {			// start with ID 1			_nextStatementId = 1;		}		rs.close();		st.close();		con.close();	}	protected int _getNextStatementId() {		return _nextStatementId++;	}	/*----------------------------------------+	 | methods from RdfRepository              |	 +----------------------------------------*/	protected void _prepareUploadConnection()		throws SQLException	{		_addRawTriplesConn = _rdbms.getConnection();		_addRawTriplesConn.setAutoCommit(false);		_addRawTriplesSt = _addRawTriplesConn.prepareStatement("INSERT INTO "				+ RAW_TRIPLES_TABLE + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");	}	protected void _closeUploadConnection()		throws SQLException	{		if (_rawTriplesCount > 0) {			_addRawTriplesConn.commit();			_rawTriplesCount = 0;		}		_addRawTriplesSt.close();		_addRawTriplesConn.close();	}	public void startTransaction() {		if (_transactionStarted) {			throw new SailInternalException(					"A transaction has already been started");		}		try {			_prepareUploadConnection();			_rawTriplesCount = 0;			_transactionStarted = true;			_sailChangedEvent = new SailChangedEventImpl();		}		catch (SQLException e) {			throw new SailInternalException(e);		}	}	public boolean transactionStarted() {		return _transactionStarted;	}	public void addStatement(Resource subj, URI pred, Value obj)		throws SailUpdateException	{		if (!_transactionStarted) {			throw new SailUpdateException("No transaction was started");		}		try {			// statement ID			_addRawTriplesSt.setInt(1, _getNextStatementId());			int nsId;			// subject			if (subj instanceof BNode) {				BNode bNode = (BNode)subj;				String nodeId = bNode.getID();				_addRawTriplesSt.setInt(2, 0); // subject namespace				_addRawTriplesSt.setString(3, nodeId);			}			else {				URI uri = (URI)subj;				nsId = _createIdForNamespace(uri.getNamespace());				_addRawTriplesSt.setInt(2, nsId);				_addRawTriplesSt.setString(3, uri.getLocalName());			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -