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

📄 versioningrdbmssail.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            if (!hn)            {              rs.close();              st.close();              con.close();            }          } catch (SQLException ex) {            throw new SailInternalException(ex);          }        }        return res;      }      public void remove(){}    };  } // getVersionIds()  /**   * Create a labeled version for a statte of the repository assigning the   * necessary mata-information about thet operation.   * @param stateUID of a particular repository state   * @param label associated with the state   *   * NOTE: This method can throw a security exception if the request is made on   * behalf of the user with insufficent rights to create versions   */  public void labelState(long stateUID, String label)  {    int usid = getCurrentUserID();    try {      Connection con = RDBMS().getConnection();      java.sql.Statement st = con.createStatement();      ResultSet rs = st.executeQuery("select max(vid) from "+VERSION_TABLE);      int maxvid = 0;      if (rs.next())        maxvid = rs.getInt(1);      rs.close();      // ad a system default label      if (label == null || label.length() ==0)        label = "New label "+(maxvid+1);      st.executeUpdate("insert into "+VERSION_TABLE+                       " VALUES ("+(maxvid+1)+" , "+usid+", "+stateUID+" ,"+                       " '"+label+"')" );      st.close();      con.close();    } catch (SQLException e) {      throw new SailInternalException(e);    }  } //labelState  /**   * Create a labeled version of the curent repository state.   * @param label to be associated with the state   * NOTE: This method can throw a security exception if the request is made on   * behalf of the user with insufficent rights to create versions   */  public void labelCurrentState(String label)  {    int saveState = VersionUid;    VersionUid = -1;    int stateUID = getStateUid();    VersionUid = saveState;    labelState(stateUID, label);  }  /**   * Restore the repository to previous state removing all statements added   * after the value of the update counter and revive all removed ones.   * @param stateUID - the update counter of a particular repository state   * NOTE: This method can throw a security exception if the request is made on   * behalf of the user with insufficent rights to revert the repository   */  public void revertToState(long stateUID)  {    throw new RuntimeException( "revertToState is not implemented" );  }  /**   * Which Sail class should be used to access the intermediate branched versions   * of the repository - to speed up the work with the RO version we use the most   * basic sail implementation   * @return the name of sail class.   */  protected String alias()  {    return super.getClass().getSuperclass().getName();  }  /**   * Sets the repository to given statete for further read operations.   * @param stateUID - the update counter of a particular repository state   */  public void workWithState(long stateUID)  {    //  FIXME: should check if it is possible    SessionContext sc = SessionContext.getContext();    int vs = sc.VersionState;    if (vs > 0)    {      try {        Connection con = RDBMS().getConnection();        java.sql.Statement st = con.createStatement();        ResultSet rs = st.executeQuery("SELECT refs FROM "+ACTIVE_STATES_TABLE+                                       " WHERE uid ="+vs);        boolean toRemove = false;        if (rs.next())        {          int refs = rs.getInt(1);          if (refs == 1)            toRemove = true;        }        rs.close();        st.close();        con.close();        if (!toRemove)          RDBMS().executeUpdate("UPDATE "+ACTIVE_STATES_TABLE+                                " SET refs=refs-1 WHERE uid="+vs);        else {          RDBMS().executeUpdate("DELETE FROM "+ACTIVE_STATES_TABLE+                                " WHERE uid="+vs);        } // remove or not      } catch (SQLException e) {        throw new SailInternalException(e);      }    } // if (vs > 0)    if (stateUID > 0)    {      try {        Connection con = RDBMS().getConnection();        java.sql.Statement st = con.createStatement();        ResultSet rs = st.executeQuery("SELECT refs FROM "+ACTIVE_STATES_TABLE+                                       " WHERE uid ="+stateUID);        boolean toInsert = true;        if (rs.next())        {          int refs = rs.getInt(1);          toInsert = false;        }        rs.close();        if (!toInsert)          RDBMS().executeUpdate("UPDATE "+ACTIVE_STATES_TABLE+                                " SET refs=refs+1 WHERE uid="+stateUID);        else {          RDBMS().executeUpdate("INSERT INTO "+ACTIVE_STATES_TABLE+                                " VALUES ( "+stateUID+" , "+1+")");          branchState(stateUID);        } // insert or not        st.close();        con.close();      } catch (SQLException e) {        throw new SailInternalException(e);      }    } // if (stateUID > 0)  }  public String branchState(long stateUID)  {    String result = "";    SessionContext sc = SessionContext.getContext();    try {      SystemConfig sysConfig = SesameServer.getSystemConfig();      String branchId = sc.repository + "branch_" + getSuffix((int)stateUID);      RepositoryConfig rc = sysConfig.cloneRepository(sc.repository, branchId);      rc.setTitle("branch of " + getSuffix((int)stateUID) + " " + rc.getTitle());      sc.repository = branchId;      boolean b_do_not_append_data = false;      boolean b_do_not_create_data = true;      Iterator iter = rc.getSailList().iterator();      while (iter.hasNext())      {        SailConfig sailConfig = (SailConfig)iter.next();        String param = sailConfig.getSailClass();        if (param.equals(getClass().getName())) {          sailConfig.setSailClass(alias());        }        String url = sailConfig.getParameter("jdbcUrl");        if (url != null)        {          int pos = url.lastIndexOf('/');          String repdb = url.substring(pos+1);          repdb += getSuffix((int)stateUID)+"branch";          url = url.substring(0,pos+1);          sailConfig.setParameter("jdbcUrl",url+repdb);          String user = sailConfig.getParameter("user");          String pass = sailConfig.getParameter("password");          Connection c1 = null;          try {            c1 = java.sql.DriverManager.getConnection(url+repdb, user, pass);          }          catch (SQLException ignore) {}          if (c1 != null)          {            c1.close();            b_do_not_append_data = true && b_do_not_create_data;            break;          }          if (b_do_not_create_data)          {            if (adminConnection == null) {              adminConnection = java.sql.DriverManager.getConnection(url, user, pass);            }            java.sql.Statement st2 = adminConnection.createStatement();            try {              st2.execute("DROP DATABASE "+repdb);            } catch(SQLException e) { }            st2.execute("CREATE DATABASE "+repdb);            createdRepositories.add(repdb);            st2.close();            b_do_not_create_data = false;          }        } // not have url      } //while      // Give admin user access to new repository      UserInfo uiAdmin = sysConfig.getUserInfo(1);      uiAdmin.addReadWriteAccess(rc);      if (b_do_not_append_data == false)      {        ThreadLog.trace("branch begin");                LocalService service = SesameServer.getLocalService();                service.login(uiAdmin.getLogin(), uiAdmin.getPassword());                LocalRepository rep = (LocalRepository)service.getRepository(rc.getRepositoryId());                Sail otherSail = rep.getSail();                ThreadLog.trace("branch end");        Connection con = RDBMS().getConnection();        java.sql.Statement st = con.createStatement();        ResultSet rs = null;        if (otherSail != null)        {          int old_id = sc.userID;          sc.userID = 1; // as admin          rs = st.executeQuery(              "SELECT n1.name, r1.localname,n2.name, r2.localname, n3.name, r3.localname FROM "+                TRIPLES_HIST_TABLE+" th ,"+                RESOURCES_TABLE+" r1, "+ // was HIST_                NAMESPACES_TABLE+" n1, "+                RESOURCES_TABLE+" r2, "+ // was HIST_                NAMESPACES_TABLE+" n2, "+                RESOURCES_TABLE+" r3, "+ // was HIST_                NAMESPACES_TABLE+" n3 "+              " WHERE th.explicit="+RDBMS().TRUE+" AND th.BornAt <= "+stateUID+              " AND (th.DiedAt > "+stateUID+" OR th.DiedAt = 0)"+              " AND r1.id=th.subject"+              " AND r2.id=th.predicate"+              " AND r3.id=th.object"+              " AND r1.namespace =n1.id"+              " AND r2.namespace =n2.id"+              " AND r3.namespace =n3.id"              );          ((RdfRepository)otherSail).startTransaction();          while (rs.next())          {            /**@todo: instead of resource use the new Impl objects import triples with Resource objects */            String nsString = rs.getString(1);            Resource subj = null;            if (nsString == null || 0==nsString.compareToIgnoreCase("NULL"))              subj = new org.openrdf.model.impl.BNodeImpl(rs.getString(2));            else              subj = new org.openrdf.model.impl.URIImpl(nsString, rs.getString(2));            URI pred = new org.openrdf.model.impl.URIImpl(rs.getString(3), rs.getString(4));            Value obj = null;            String valString = rs.getString(5);            if (valString == null || 0==valString.compareToIgnoreCase("NULL"))              obj = new org.openrdf.model.impl.BNodeImpl(rs.getString(6));            else              obj = new org.openrdf.model.impl.URIImpl(valString, rs.getString(6));            try {              ((RdfRepository)otherSail).addStatement(subj,pred, obj);            } catch (SailUpdateException e) {              throw new SailInternalException(e);            }          }          rs.close();          rs = st.executeQuery(              "SELECT n1.name, r1.localname,n2.name, r2.localname, l1.label, l1.language FROM "+              TRIPLES_HIST_TABLE+" th ,"+              RESOURCES_TABLE+" r1, "+ // was HIST_              NAMESPACES_TABLE+" n1, "+              RESOURCES_TABLE+" r2, "+ // was HIST_              NAMESPACES_TABLE+" n2, "+              LITERALS_TABLE+" l1 "+ // was HIST_              " WHERE th.explicit="+RDBMS().TRUE+" AND th.BornAt <= "+stateUID+              " AND (th.DiedAt > "+stateUID+" OR th.DiedAt = 0)"+              " AND r1.id = th.subject"+              " AND r2.id = th.predicate"+              " AND l1.id = th.object"+              " AND r1.namespace =n1.id"+              " AND r2.namespace =n2.id"              );          while (rs.next())          {            /**@todo: instead of resource use the new Impl objects import triples with Literal objects*/            String nsString = rs.getString(1);            Resource subj = null;            if (nsString == null || 0==nsString.compareToIgnoreCase("NULL"))              subj = new org.openrdf.model.impl.BNodeImpl(rs.getString(2));            else              subj = new org.openrdf.model.impl.URIImpl(nsString, rs.getString(2));            URI pred = new org.openrdf.model.impl.URIImpl(rs.getString(3), rs.getString(4));            String val = rs.getString(5);            String lang = rs.getString(6);            Value obj = new org.openrdf.model.impl.LiteralImpl(val, lang);            try {              ((RdfRepository)otherSail).addStatement(subj,pred, obj);            } catch (SailUpdateException e) {              throw new SailInternalException(e);            }          }          rs.close();          ((RdfRepository)otherSail).commitTransaction();          // we should update the namespaces          NamespaceIterator nsIter = getNamespaces();          while (nsIter.hasNext()) {            nsIter.next();            try {              ((RdfRepository)otherSail).changeNamespacePrefix(nsIter.getName(), nsIter.getPrefix());            }            catch (SailUpdateException e) {              ThreadLog.warning(                  "Unable to set namespace prefix '" + nsIter.getPrefix() +                  "' for namespace '" + nsIter.getName() + "': " +                  e.getMessage());            }          }          sc.userID = old_id; // as current user        } // have a Sail        st.close();        con.close();      }

⌨️ 快捷键说明

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