📄 versioningrdbmssail.java
字号:
* @throws SailUpdateException */ public void addStatement(Resource subj, URI pred, Value obj) throws SailUpdateException { /** * In INCEMENTAL_MODE ONLY * 1. if such a statement exist - mark it in the 'expected' table */ if (SessionContext.getContext().updateMode != SessionContext.INCREMENTAL_MODE) { int subjId = _getResourceId(subj); int predId = _getURIId(pred); int objId = _getValueId(obj); int statementId = 0; boolean hasRow = false; boolean bExplicit = false; try { Connection con = RDBMS().getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "SELECT id, explicit "+ " FROM "+TRIPLES_TABLE+ " WHERE subject="+subjId+ " AND predicate="+predId+ " AND object="+objId ); if (rs.next()) { statementId = rs.getInt(1); bExplicit = rs.getString(2).equalsIgnoreCase(RDBMS().convertBoolean(true)); hasRow = true; } rs.close(); st.close(); con.close(); if (hasRow == true) { // here we mark that there is such a triple in the table RDBMS().executeUpdate("UPDATE "+EXPECTED_STATEMENTS_TABLE+ " SET found=1 WHERE sid = "+statementId); } } catch (SQLException e) { throw new SailInternalException(e); } } // ent !INCREMENTAL_MODE super.addStatement(subj, pred, obj); } // addStatement()/** * Clears the repository. After the repository was cleared, it will * be in the same state as after initialization. * Our Sail implementation clears all additional tables an all currently available * branches that are not permanent. * @exception SailInternalException To indicate an internal error. * */ public void clearRepository() throws SailUpdateException { super.clearRepository(); try { RDBMS().executeUpdate("DROP TABLE " + UPDATES_TABLE); RDBMS().executeUpdate("DROP TABLE " + TRIPLES_HIST_TABLE); RDBMS().executeUpdate("DROP TABLE " + VERSION_TABLE); RDBMS().executeUpdate("DROP TABLE " + BASEURL_TABLE); RDBMS().executeUpdate("DROP TABLE " + EXPECTED_STATEMENTS_TABLE); dropBranchedVersions(); } catch (SQLException e) { throw new SailInternalException(e); }} /** * Starts a transaction. A transaction needs to be started before * data can be added to or removed from the repository. * @exception SailInternalException To indicate an internal error. **/ public void startTransaction() { super.startTransaction(); baseUrlIndex = 0; SessionContext sc = SessionContext.getContext(); if (sc.updateMode != SessionContext.INCREMENTAL_MODE) { String url = sc.baseUrl; if (url.length() > 0) { try { Connection con = RDBMS().getConnection(); java.sql.Statement st = con.createStatement(); // try to find the BASE URL in the cache System.out.println("CP="+1); ResultSet rs = null; try { rs = st.executeQuery("SELECT urlID FROM "+BASEURL_TABLE+ " WHERE url = '"+RDBMS().escapeString(url)+"'"); if (rs.next()) baseUrlIndex = rs.getInt(1); rs.close(); } catch (SQLException e) { throw new SailInternalException(e); } System.out.println("baseUrlIndex="+baseUrlIndex); if (baseUrlIndex == 0) { // try to add it in the cache int max = 0; rs = st.executeQuery("SELECT max(urlID) FROM "+BASEURL_TABLE); if (rs.next()) max = rs.getInt(1); rs.close(); baseUrlIndex = max+1; RDBMS().executeUpdate("INSERT INTO "+BASEURL_TABLE+ " VALUES ("+baseUrlIndex+", '"+RDBMS().escapeString(url)+"')"); System.out.println("max="+baseUrlIndex); } // build expected_resources_table to be able to mark the appeared ones // so to remove the 'obsoleted' at commit_transaction // how we can build that table // 1. first we collect all updateIDS created when that base url was // used on startTransaction(the code above); // 2. add the id's of the still 'Live' explicit triples // that were submited to the repository during these the updates (when // the 'BASEURL' was used) // 3. afterwards on each addStatement() of particular explicit statement // we mark the valid ones (using the flag from EXPECTED_STATEMENTS_TABLE) // 4. on commitTransaction() // a). add the UpdateID with the provided BASEURLIndex // b). remove any unused statements mentioned in the EXPECTED_STATEMENTS_TABLE // without the flag set. // 5. Enjoy the ADAPTIVE Upload // 6. WE shall implement RE-INIT mode doing remove of the statements with // ids collected on step 2 and then just continuing as usual; // step.1 RDBMS().executeUpdate(" TRUNCATE "+EXPECTED_STATEMENTS_TABLE); RDBMS().executeUpdate(" INSERT INTO "+EXPECTED_STATEMENTS_TABLE+ " SELECT th.id, 0 FROM "+TRIPLES_HIST_TABLE+" th "+ " INNER JOIN "+UPDATES_TABLE+" u on th.BornAt = u.uid "+ " WHERE u.urlID = "+baseUrlIndex+" AND th.DiedAt = 0 AND th.explicit = 1"); /* if (sc.updateMode == SessionContext.REINIT_MODE) { } */ st.close(); con.close(); } catch (SQLException e) { throw new SailInternalException(e); } } // an empty url } VersionUid = -1; VersionUid = getStateUid(); } /** * Commits a started transaction. A transaction needs to be commited * to be guaranteed that data has been added to, or removed from the * repository. * @exception SailInternalException To indicate an internal error. **/ public void commitTransaction() { SessionContext sc = SessionContext.getContext(); try { if (sc.updateMode == SessionContext.ADAPTIVE_MODE) { // here we remove the 'obsolete' statements from repository // these statements do not appear in the data under the baseURL Connection con = null; java.sql.Statement st = null; ResultSet rs = null; try { con = RDBMS().getConnection(); st = con.createStatement(); rs = st.executeQuery("SELECT sid FROM "+EXPECTED_STATEMENTS_TABLE+ " WHERE found = 0" ); while (rs.next()) { int sid = rs.getInt(1); if (0 < RDBMS().executeUpdate("UPDATE "+TRIPLES_TABLE+ " SET explicit="+RDBMS().FALSE+" WHERE id="+sid)) { _statementsRemoved = true; } } rs.close(); st.close(); con.close(); } catch(SQLException e) { throw new SailInternalException(e); } RDBMS().executeUpdate("TRUNCATE "+EXPECTED_STATEMENTS_TABLE); } } catch (SQLException ex) { throw new SailInternalException(ex); } super.commitTransaction(); VersionUid = -1; } /** * Helper to retrive the user ID of the current user (that user that starts the * operation). See the SessionContext * @return the id of the current user */ protected int getCurrentUserID() { SessionContext o = SessionContext.getContext(); if (o == null) throw new SailInternalException("null session context in getCurrentUserID()"); if (o.userID == 0) o.userID = 2; // set the user id to the 'anonymous' account return o.userID; } /** * Return current value of teh Update counter. If it is not cased we compute it * on the fly. * @return curent value of the update counter */ protected int getStateUid() { SessionContext o = SessionContext.getContext(); if (o == null) throw new SailInternalException("null session context in getStateUid()"); VersionUid = o.VersionState; if (VersionUid <= 0) { try { Connection con = RDBMS().getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "SELECT max(uid) FROM " + UPDATES_TABLE); if (rs.next()) VersionUid = rs.getInt(1); else VersionUid = 0; rs.close(); st.close(); con.close(); } catch (SQLException ex) { throw new SailInternalException(ex); } } return VersionUid; } /** * Return a list of all Update states of the current repository as Strings. * Mainly used in the HTML User interface. * @return Iterator throuh the values of the Update Counter */ public Iterator getUpdateIds() { return new Iterator() { // initialize Connection con = null; java.sql.Statement st = null; ResultSet rs = null; boolean hn =false; { try { con = RDBMS().getConnection(); st = con.createStatement(); rs = st.executeQuery( "SELECT uid FROM " + UPDATES_TABLE); hn = rs.next(); if (!hn) { rs.close(); st.close(); con.close(); } } catch (SQLException ex) { throw new SailInternalException(ex); } } public boolean hasNext() { return hn; } public Object next() { Object res = null; if (hn) { try { res = rs.getString(1); hn = rs.next(); if (!hn) { rs.close(); st.close(); con.close(); } } catch (SQLException ex) { throw new SailInternalException(ex); } } return res; } public void remove(){} }; } // getUpdateIds() /** * Return a list of all Version Ids of the current repository as Strings. * @return Iterator over all version ids */ public Iterator getVersionIds() { return new Iterator() { // initialize Connection con = null; java.sql.Statement st = null; ResultSet rs = null; boolean hn =false; { try { con = RDBMS().getConnection(); st = con.createStatement(); rs = st.executeQuery( "SELECT vid FROM " + VERSION_TABLE); hn = rs.next(); if (!hn) { rs.close(); st.close(); con.close(); } } catch (SQLException ex) { throw new SailInternalException(ex); } } public boolean hasNext() { return hn; } public Object next() { Object res = null; if (hn) { try { res = rs.getString(1); hn = rs.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -