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

📄 driverrdb.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			"The database layout cannot be processed by this version of Jena: "
			+ layout);	
		}

	}
	
	private void throwBadFormat ( String prop ) {
		throw new JenaException(
		"The database appears to be unformatted or corrupted - could not find value\n" +
		" for \"" + prop + "\" in Jena system properties table.\n" + 
		"If possible, call IDBConnection.cleanDB(). \n" +
		"Warning: cleanDB will remove all Jena models from the databases.");
	}

	
	/**
	 * Format the database and construct a brand new system specialized graph.
	 */
	protected SpecializedGraph formatAndConstructSystemSpecializedGraph() {
		String errMsg = null;
		if (xactOp(xactIsActive))
			throw new RDFRDBException(
					"Cannot intialize database while transaction is active.\n"
							+ "Commit or abort transaction before intializing database.");

		boolean autoIsOn = xactOp(xactAutoOff);
		try {
			String[] params = getDbInitTablesParams();
			m_sql.runSQLGroup("initDBtables", params);
			m_sql.runSQLGroup("initDBgenerators");//			m_sql.runSQLGroup("initDBprocedures");
		} catch (SQLException e) {
			logger.warn("Problem formatting database", e);
			errMsg = e.toString();
		}

		if (errMsg == null)
			try {
				xactOp(xactCommit);
				xactOp(xactBegin);

				// Construct the system properties
				IPSet pSet = createIPSetInstanceFromName(m_psetClassName,
						SYSTEM_STMT_TABLE);
				m_sysProperties = createLSetInstanceFromName(m_lsetClassName,
						pSet, DEFAULT_ID);

				// The following call constructs a new set of database
				// properties and
				// adds them to the m_sysProperties specialized graph.
                
                // Ugh: m_dbcon.getDatabaseType(), not this.getDatabaseType()
				m_dbProps = new DBPropDatabase(m_sysProperties,
                                               m_dbcon.getDatabaseType(),
                                               VERSION, LAYOUT_VERSION,
                                               String.valueOf(LONG_OBJECT_LENGTH),
                                               String.valueOf(INDEX_KEY_LENGTH),
                                               String.valueOf(IS_XACT_DB),
                                               String.valueOf(URI_COMPRESS), 
                                               String.valueOf(URI_COMPRESS_LENGTH),
                                               TABLE_NAME_PREFIX);

				// Now we also need to construct the parameters that will be the
				// default settings for any graph added to this database
				DBPropGraph def_prop = new DBPropGraph(m_sysProperties,
						DEFAULT_PROPS, "generic");

				def_prop.addGraphId(DEFAULT_ID);

				xactOp(xactCommit);
				if (autoIsOn)
					xactOp(xactAutoOn);
			} catch (Exception e) {
				errMsg = e.toString();
			}

		if (errMsg != null) {
			doCleanDB(false);
			m_sysProperties = null;
			throw new RDFRDBException(errMsg);
		}

		return m_sysProperties;
	}
	
	abstract String[] getDbInitTablesParams();
	
	abstract String[] getCreateTableParams( int graphId, boolean isReif );
	
	abstract public int graphIdAlloc ( String graphName );	
	
	
	
	/**
	 * Construct and return a new specialized graph.
	 */
	public List createSpecializedGraphs(String graphName,
			Graph requestedProperties) {

		/*
		 * create the specialized graphs for the new graph. this includes
		 * updating the database for the new graph (allocating a new graph
		 * identifier, updating the jena system tables and creating tables, if
		 * necessary. this should be done atomically to avoid corrupting the
		 * database but a single transaction is not sufficient because some
		 * database engines (e.g., oracle) require create table statements to
		 * run as a separate transaction, i.e., a create table statement in the
		 * middle of a group of updates will cause an automatic commit of the
		 * updates prior to the create table statement.
		 * 
		 * fortunately, we can run most of the updates in a single transaction.
		 * however, allocation of the graph indentifier must be done prior to
		 * creating the statement tables. so, if any subsequent operation fails,
		 * we must run a compensating transaction to deallocate the graph
		 * identifier.
		 * 
		 * because of the above, we assume that there is no active transaction
		 * when this routine is called.
		 */

		// String graphName = graphProperties.getName();
		String stmtTbl = null;
		String reifTbl = null;
		String dbSchema = STORE_WITH_MODEL;
		boolean didGraphIdAlloc = false;
		boolean didTableCreate = false;
		String errMsg = null;
		DBPropGraph graphProperties = null;

		SpecializedGraph sysGraph = getSystemSpecializedGraph(false);
		// should have already create sys graph.

		if (xactOp(xactIsActive))
			throw new RDFRDBException(
					"Cannot create graph while transaction is active.\n"
							+ "Commit or abort transaction before creating graph");

		boolean autoOn = xactOp(xactAutoOff);
		int graphId = -1; // bogus initialization to make java happy

		try {
			xactOp(xactBegin);
			graphId = graphIdAlloc(graphName);
			didGraphIdAlloc = true;
			xactOp(xactCommit);
			xactOp(xactBegin);
			boolean useDefault = false;

			// dbSchema = graphProperties.getDBSchema();
			// use the default schema if:
			// 1) no schema is specified and we are creating the default
			// (unnamed) graph
			// 2) a schema is specified and it is the default (unnamed) graph
			if (((dbSchema == null) && graphName.equals(GraphRDB.DEFAULT))) {
				useDefault = true;
				dbSchema = DEFAULT_PROPS; // default graph should use default
				// tables
			}
			// else if ( ((dbSchema != null) &&
			// dbSchema.equals(GraphRDB.DEFAULT)) ) {
			// 	useDefault = true;
			//	dbSchema = DEFAULT_PROPS; // default graph should use default
			// tables
			// }
			if (dbSchema != null) {
				DBPropGraph schProp = DBPropGraph.findPropGraphByName(sysGraph,
						dbSchema);
				if (schProp != null) {
					reifTbl = schProp.getReifTable();
					stmtTbl = schProp.getStmtTable();
				}
				if (((reifTbl == null) || (stmtTbl == null))
						&& (useDefault == false))
					// schema not found. this is ok ONLY IF it's the DEFAULT
					// schema
					throw new RDFRDBException("Creating graph " + graphName
							+ ": referenced schema not found: " + dbSchema);
			}
			if ((reifTbl == null) || (stmtTbl == null)) {
				didTableCreate = true;
				reifTbl = createTable(graphId, true);
				stmtTbl = createTable(graphId, false);
				if ((reifTbl == null) || (stmtTbl == null))
					throw new RDFRDBException("Creating graph " + graphName
							+ ": cannot create tables");
			}
			xactOp(xactCommit);  // may not be needed but it doesn't hurt
		} catch (Exception e) {
			errMsg = e.toString();
		}

		// we can now start a new transaction and update the metadata.
		// we should already be committed but we commit again just in case

		if (errMsg == null)
			try {
				xactOp(xactBegin);

				graphProperties = new DBPropGraph(sysGraph, graphName,
						requestedProperties);
				graphProperties.addGraphId(graphId);
				graphProperties.addStmtTable(stmtTbl);
				graphProperties.addReifTable(reifTbl);

				DBPropDatabase dbprop = new DBPropDatabase(
						getSystemSpecializedGraph(true));
				dbprop.addGraph(graphProperties);

				// Add the reifier first
				DBPropPSet pSetReifier = new DBPropPSet(m_sysProperties,
						m_psetReifierClassName, reifTbl);
				DBPropLSet lSetReifier = new DBPropLSet(m_sysProperties,
						"LSET_" + graphProperties.getName() + "_REIFIER",
						m_lsetReifierClassName);
				lSetReifier.setPSet(pSetReifier);
				graphProperties.addLSet(lSetReifier);

				// Now add support for all non-reified triples
				DBPropPSet pSet = new DBPropPSet(m_sysProperties,
						m_psetClassName, stmtTbl);
				DBPropLSet lSet = new DBPropLSet(m_sysProperties, "LSET_"
						+ graphProperties.getName(), m_lsetClassName);
				lSet.setPSet(pSet);
				graphProperties.addLSet(lSet);

				xactOp(xactCommit);
				if (autoOn) xactOp(xactAutoOn);
			} catch (Exception e) {
				errMsg = e.toString();
			}

		if (errMsg == null)
			return recreateSpecializedGraphs(graphProperties);
		else {
			xactOp(xactCommit); // maybe not needed but doesn't hurt
			xactOp(xactBegin);
			try {
			// clean-up
			if (didGraphIdAlloc) {
				graphIdDealloc(graphId);
			}
			} catch ( Exception e ) {
			}
			if (didTableCreate) {
				// make sure the order below matches
				// the order of creation above.
				if (reifTbl != null)
					try { deleteTable(reifTbl); }
					catch ( Exception e ) {};
				if (stmtTbl != null)
					try { deleteTable(stmtTbl); }
					catch ( Exception e ) {};
			}
			xactOp(xactCommit);
			if (autoOn) xactOp(xactAutoOn);
			return null;
		}
	}
	
	/**
	 * Construct and return a list of specialized graphs to match those in the
	 * store.
	 * 
	 * @param graphProperties
	 *            A set of customization properties for the graph.
	 */
	public List recreateSpecializedGraphs(DBPropGraph graphProperties) {
		
		List result = new ArrayList();
		int dbGraphId = graphProperties.getGraphId();

		// to ensure that reifier graphs occur before stmt graphs, make two passes
		String[] lsetTypes = {m_lsetClassName, m_lsetReifierClassName};
		int i;
		for(i=0;i<2;i++) {
			Iterator it = graphProperties.getAllLSets();
			while(it.hasNext() ) {
				DBPropLSet lSetProps = (DBPropLSet)it.next();
				if ( lSetProps.getType().equals(lsetTypes[i]) ) continue;
				DBPropPSet pSetProps = lSetProps.getPset();

				IPSet pSet = createIPSetInstanceFromName(pSetProps.getType(), pSetProps.getTable());		
				result.add( createLSetInstanceFromName( lSetProps.getType(), pSet, dbGraphId));		
			}
		}		
		
		return result;		
	}
	
    /**
     * Create a new IPSet instance of the named implementation class and set the db connection.
     * 
     * @param pName name of a class that implements IPSet.
     * @return an instance of the named class with the db connection set.
     */
	private IPSet createIPSetInstanceFromName(String className, String tblName) {
		IPSet pSet = null;		
		try {
			// get PSet
			pSet = (IPSet) Class.forName(className).newInstance();
			pSet.setDriver(this);
			pSet.setSQLType(ID_SQL_TYPE);
			pSet.setSkipDuplicateCheck(SKIP_DUPLICATE_CHECK);
			pSet.setSQLCache(m_sql);
			pSet.setCachePreparedStatements(CACHE_PREPARED_STATEMENTS);
			pSet.setTblName(tblName);
		} catch (Exception e) {
			logger.warn("Unable to create IPSet instance ", e);
		}
		return pSet;
	}	
		
	private SpecializedGraph createLSetInstanceFromName(String lSetName, IPSet pset, int dbGraphID) {
		SpecializedGraph sg = null;		
		try {
			Class cls = Class.forName(lSetName);
			Class[] params = {IPSet.class, Integer.class};
			java.lang.reflect.Constructor con = cls.getConstructor(params);
			Object[] args = {pset, new Integer(dbGraphID)};
			sg = (SpecializedGraph) con.newInstance(args);
		} catch (Exception e) {
			logger.error("Unable to create instance of SpecializedGraph ", e);
		}
		return sg;
	}

	/**
	 * Remove the specialized graph, erasing all trace of a Graph.
	 * @param graphId The identity of the Graph which these specialized graphs should hold
	 * @param graphProperties The properties for the graph to be removed.
	 */
	public void removeSpecializedGraphs( DBPropGraph graphProperties,
		List specializedGraphs) {
			
		int graphId = graphProperties.getGraphId();
		
		if (xactOp(xactIsActive))
			throw new RDFRDBException(
					"Cannot remove graph while transaction is active.\n"
					+ "Commit or abort transaction before removing graph");

		boolean autoIsOn = xactOp(xactAutoOff);
		xactOp(xactCommit);
		xactOp(xactBegin);
		
		// remove graph metadata from jena sys table in a xact
		String stmtTbl = graphProperties.getStmtTable();
		String reifTbl = graphProperties.getReifTable();
		
		// remove from system properties table
		// It is sufficient just to remove the lSet properties (it will
		// take care of deleting any pset properties automatically).			
		m_dbProps.removeGraph(graphProperties);
		
		if ( graphId != DEFAULT_ID ) graphIdDealloc(graphId);
		
		xactOp(xactCommit);
		xactOp(xactBegin);
		
		/* now remove triples from statement tables.
		*  if the graph is stored in its own tables, we
		*  can simply delete those tables. else, the graph
		*  shares tables with other graphs so we have to
		*  remove each statement. */
		
		// check to see if statement tables for graph are shared
		boolean stInUse = true;
		boolean rtInUse = true;
        
		if ( graphId != DEFAULT_ID ) {
			stInUse = false;
			rtInUse = false;
			Iterator it =  m_dbProps.getAllGraphs();
			while ( it.hasNext() ) {
				DBPropGraph gp = (DBPropGraph) it.next();
				if ( gp.getStmtTable().equals(stmtTbl) ) stInUse = true;
				if ( gp.getReifTable().equals(reifTbl) ) rtInUse = true;
			}
		}
		// now remove the statement tables or else delete all triples.
		if ( stInUse || rtInUse ) {
			Iterator it = specializedGraphs.iterator();
			while (it.hasNext()){
			   SpecializedGraph sg = (SpecializedGraph) it.next();
			   removeSpecializedGraph(sg);
			}
		} else {
			deleteTable(stmtTbl);
			deleteTable(reifTbl);
		}
		xactOp(xactCommit);
		if ( autoIsOn ) xactOp(xactAutoOn);
	}
	
	
	/**
	 * Remove specialized graph from the datastore.
	 * @param graph is the graph to be removed.
	 */

⌨️ 快捷键说明

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