//    public StructuralSummaryIndex(String indexName,int numOfKeys)
 //    {
 //        //this.qNameList=qNamelist;
 //        //this.nodeList=nodeList;
 //        this.indexName=indexName;
 //        this.numOfKeys=numOfKeys;
 //        logicFileMgr = LogicalFileManager.Instance;
 //        bufMngr = BufferManager.getInstance();
 //        XBufferChannel=new XBufferChannel();//vasl bshe b buf mgr:getfreepage()
 //        XBufferChannel.clear();
 //    }
 public StructuralSummaryIndex(int indexPage, String indexName) throws IOException, DBException {
   // usage: openning of existing SSIndex object.
   this.indexName = indexName;
   this.indexPage = indexPage; // always the same
   this.workingPage = indexPage; // may change
   logicFileMgr = LogicalFileManager.Instance;
   bufMngr = BufferManager.getInstance();
   xBufferChannel = new XBufferChannel(); // new ham lazem nis //vasl bshe b buf mgr:getfreepage()
   // xBufferChannel.clear(); //LaZEM NIs
   xBufferChannel = bufMngr.getPage(indexPage); // may change
   xBufferChannel.position(Settings.freeOffsetPos); // 5
   freeOffset = xBufferChannel.getInt(); // init
   xBufferChannel.position(Settings.numOfKeysPos);
   numOfKeys = xBufferChannel.getInt(); // init
   unTakenNodes = numOfKeys;
 }
 public StructuralSummaryIndex(String indexName) throws IOException, DBException {
   // usage: creation of nonExisting index
   this.indexName = indexName;
   this.numOfKeys = 0;
   nodeList = new ArrayList();
   logicFileMgr = LogicalFileManager.Instance;
   bufMngr = BufferManager.getInstance();
   xBufferChannel = bufMngr.getFreePage();
   this.indexPage = createSSIndexPage();
   this.workingPage = this.indexPage;
   // XBufferChannel=bufMngr.getPage(indexPage);//vasl bshe b buf mgr:getfreepage()
   // XBufferChannel.clear();
   int keysAdded = 0;
   freeOffset = Settings.dataStartPos; // initialize
   xBufferChannel.position(Settings.dataStartPos);
 }
 private void loadSSPage(int PageNo) throws IOException, DBException {
   // LOADs a page into memory:
   xBufferChannel.clear();
   xBufferChannel = bufMngr.getPage(PageNo);
   xBufferChannel.position(Settings.freeOffsetPos); // 5
   freeOffset = xBufferChannel.getInt();
   xBufferChannel.position(Settings.numOfKeysPos);
   numOfKeys = xBufferChannel.getInt();
   unTakenNodes = numOfKeys;
 }
  public StructuralSummaryNode next() throws IOException, DBException // for Iterator
      {
    // read record from file
    // transform into ssNode
    int CID;
    int qNameLen;
    String qName;
    byte[] qNameSeq;
    int parentCID;
    StructuralSummaryNode currentNode = new StructuralSummaryNode();
    int nextPage; // needed in case that data is written in more than one page
    while (true) {
      // if(unTakenNodes!=0 &&XBufferChannel.position()+2*Settings.sizeOfInt<=Settings.pageSize)
      // agar dorost bkhunim har bar position sare yek record mi iste:
      if (unTakenNodes != 0 && xBufferChannel.position() < freeOffset) {
        // bayd daghigh tar chek bshe.
        // stroage format in ssIndex: cid(int),qlen(int),qSeq(byte array),parentCId(int)

        CID = xBufferChannel.getInt();
        logManager.LogManager.log(1, "CID: " + CID);
        qNameLen = xBufferChannel.getInt();
        int reqSpace = qNameLen + Settings.sizeOfInt;
        int capacity = Settings.pageSize - xBufferChannel.position();
        if (reqSpace <= capacity) {
          qNameSeq = new byte[qNameLen];
          xBufferChannel.get(qNameSeq, 0, qNameLen);
          qName = new String(qNameSeq);
          parentCID = xBufferChannel.getInt();
          currentNode.setCID(CID);
          currentNode.setParentCID(parentCID);
          currentNode.setQName(qName);
          unTakenNodes--;
          return currentNode; // return breaks loop!
        }
      } else {

        //               if(unTakenNodes==0)//in k ghalate baz :(
        //               {   return null;  }
        //               else //if(XBufferChannel.position()+2*Settings.sizeOfInt>Settings.pageSize)
        //               {    //goTOnextPAge;
        xBufferChannel.position(Settings.nextPagePos);
        nextPage = xBufferChannel.getInt();
        bufMngr.unpinBuffer(xBufferChannel);
        if (nextPage > 0) {
          loadSSPage(nextPage); // prepares XBufferChannel and loads page into it.
          //                      System.out.println("loaded:"+nextPage);

        } else return null;
        workingPage = nextPage;
        xBufferChannel.position(
            Settings.dataStartPos); // instead of calling open(somehow opens nextpage)
        //               }
      }
    } // end while
  }
  public BTreeInternalNode(InternalBuffer xBuf, String indexName) {

    logicFileMgr = LogicalFileManager.Instance;
    this.indexName = indexName;
    bufMngr = BufferManager.getInstance();
    InternalBuffer = xBuf;
    InternalBuffer.position(Settings.freeOffsetPos); // 5
    testPos = InternalBuffer.position();
    freeOffset = InternalBuffer.getInt();
    // byteBuf=xBuf.getByteBuffer();
    InternalBuffer.position(Settings.isLeafPos);
    testPos = InternalBuffer.position();

    isLeaf = InternalBuffer.get();
    // InternalBuffer.position(Settings.numOfKeysPos);
    numOfKeys = InternalBuffer.getInt();
  }
 public HashMap checkBufPool() {
   logManager.LogManager.log(0, "Checking bufPool status");
   return bufMgr.getPoolStatus();
 }
/** @author MohsenCs */
public class PostgreDB {
  private String docName; // haman db name
  private BTreeIndex bTreeIndex;
  private ElementIndex elemIndex;
  private StructuralSummaryIndex structSumIndex;
  private PostgreDBManager dbdocMgr;
  private BufferManager bufMgr = BufferManager.getInstance();
  // IndexManager indexMgr;
  private List<DeweyID> Deweylist;
  private static int num = 0;
  private Connection con = null;

  public PostgreDB() {}

  public PostgreDB(String docName) throws IOException {
    dbdocMgr = PostgreDBManager.Instance;

    try {
      String dbURL = "jdbc:postgresql://localhost:5432/XMLDB";
      String userName = "******";
      String password = "******";
      con = DriverManager.getConnection(dbURL, userName, password);
    } catch (SQLException e) {
      System.out.println("JDBC Driver is not Configured Correctly !");
      e.printStackTrace();

      return;
    }
  }

  public List getDeweyList() {
    return Deweylist;
  }

  public HashMap checkBufPool() {
    logManager.LogManager.log(0, "Checking bufPool status");
    return bufMgr.getPoolStatus();
  }

  public void addElement(DeweyID deweyID, String qName, int parentCID, boolean qNameFound)
      throws IOException, Exception, SQLException {
    try {
      if (con != null) {
        String sqlQuery = "INSERT INTO \"OrderedByCID\" (\"CID\", \"DeweyID\") VALUES (?, ?);";
        PreparedStatement pst = con.prepareStatement(sqlQuery);
        pst.setInt(1, deweyID.getCID());
        pst.setString(2, deweyID.toString());
        pst.executeUpdate();
        pst.close();
      }
    } catch (SQLException e) {
      System.out.println("JDBC Driver is not Configured Correctly !");
      e.printStackTrace();
      return;
    }
  }

  public void addAttribute(DeweyID deweyID, String qName, int parentCID, boolean qNameFound)
      throws IOException, Exception {
    // parentCID is curNode's CID
    //        logManager.LogManager.log(6,qName);
    //        logManager.LogManager.log(6,"Attrib: "+deweyID.toString());
    //        logManager.LogManager.log(6,String.valueOf(deweyID.getCID()));
    //        }
    //         elemIndex.insert(deweyID.getCID(),deweyID);
    //         bTreeIndex.insert(deweyID,deweyID.getCID(),qName);
    if (!qNameFound) {
      // structSumIndex.add(deweyID.getCID(),qName,parentCID);
      try {
        if (con != null) {
          String sqlQuery = "INSERT INTO \"OrderedByCID\" (\"CID\", \"DeweyID\") VALUES (?, ?);";
          PreparedStatement pst = con.prepareStatement(sqlQuery);
          pst.setInt(1, deweyID.getCID());
          pst.setString(2, deweyID.toString());
          //                    pst.setString(3, String.v);
          pst.executeUpdate();
          pst.close();
        }
      } catch (SQLException e) {
        System.out.println("JDBC Driver is not Configured Correctly !");
        e.printStackTrace();
        return;
      }
    }
    // checkBufPool();
  }

  public void addValue(DeweyID deweyID, String value, int parentCID) throws IOException, Exception {
    // we may have problem here
    // maybe we should update previously inserted attributes instead of insert new one

    // FOR ATTrib Values (Actually they are behaved the same as text contents)
    // text contents are not added to element index,thier CID shouldn be Zero
    // elemIndex.insert(0,deweyID);
    // bTreeIndex.insert(deweyID,-1,value);
    logManager.LogManager.log(6, value);
    logManager.LogManager.log(6, "Value: " + deweyID.toString());
    try {
      if (con != null) {
        String sqlQuery =
            "INSERT INTO \"OrderedByCID\" (\"CID\", \"DeweyID\" , \"Value\") VALUES (?, ? ,?);";
        PreparedStatement pst = con.prepareStatement(sqlQuery);
        pst.setInt(1, deweyID.getCID());
        pst.setString(2, deweyID.toString());
        pst.setString(3, value);
        pst.executeUpdate();
        pst.close();
      }
    } catch (SQLException e) {
      System.out.println("JDBC Driver is not Configured Correctly !");
      e.printStackTrace();
      return;
    }

    // structSumIndex.add(-1,value,parentCID);THIS IS NOT NEEDED!
    // checkBufPool();
  }

  public void addContent(DeweyID deweyID, StringBuilder contentBuilder, int parentCID)
      throws IOException, Exception {
    String content = new String(contentBuilder);
    try {
      if (con != null) {
        String sqlQuery =
            "INSERT INTO \"OrderedByCID\" (\"CID\", \"DeweyID\" , \"Value\") VALUES (?, ? ,?);";
        PreparedStatement pst = con.prepareStatement(sqlQuery);
        pst.setInt(1, deweyID.getCID());
        pst.setString(2, deweyID.toString());
        pst.setString(3, content);
        pst.executeUpdate();
        pst.close();
      }
    } catch (SQLException e) {
      System.out.println("JDBC Driver is not Configured Correctly !");
      e.printStackTrace();
      return;
    }
    // text contents are not added to element index,thier CID shouldn be Zero
    // elemIndex.insert(0,deweyID);
    //         bTreeIndex.insert(deweyID,-2,content);
    //         System.out.println("CID: -2");
    // structSumIndex.add(-2,content,parentCID);THIS IS NOT NEEDED
    // checkBufPool();
  }

  public void endOfDocSignal() throws IOException, DBException, SQLException {
    structSumIndex.endDoc();
    //         docMgr.closeDoc(this.docName);
    con.close();
  }
}
 public void endDoc() throws IOException {
   bufMngr.unpinBuffer(xBufferChannel, true);
 }
  public void add(int CID, String qName, int parentCID) throws IOException, DBException {
    boolean keyFound = false;
    byte[] qNameArray;
    int qLength;
    // we need this in case that keys being added exceed page size:
    // storage format in ssIndex: cid(int),qlen(int),qSeq(byte array),parentCId(int)

    qNameArray = qName.getBytes();
    qLength = qNameArray.length; // qname only
    xBufferChannel.position(freeOffset); // Every Iteration it must be set.
    int capacity = Settings.pageSize - freeOffset;
    int reqSpace = qLength + 3 * Settings.sizeOfInt;
    if (reqSpace <= capacity) {

      xBufferChannel.putInt(CID); // CID
      //            logManager.LogManager.log(0,"QNAME: "+ qName);
      //            logManager.LogManager.log(0,"Position: "+ xBufferChannel.position());
      xBufferChannel.putInt(qLength);
      xBufferChannel.put(qNameArray, 0, qLength);
      xBufferChannel.putInt(parentCID); // parentCId
      numOfKeys++;
      freeOffset += (qLength + 3 * Settings.sizeOfInt);
    } else {

      // must add page
      // twinNode=new BTreeNode(indexName,(byte)0,(byte)1, -1);//numOfKeys is not valid yet
      XBufferChannel hlpr = bufMngr.getFreePage();
      workingPage = hlpr.getPageNumber();
      logManager.LogManager.log(5, "Structural Sum P: " + workingPage);
      // numOfKeys=keysAdded;
      // unTakenRecs=numOfKeys;maybe we need this in the future
      xBufferChannel.position(Settings.nextPagePos);
      xBufferChannel.putInt(workingPage);
      //            if(workingPage==19995)
      //                logManager.LogManager.log(5, "nextPage:19915"+"workingPage:
      // "+xBufferChannel.getPageNumber());
      xBufferChannel.position(Settings.freeOffsetPos); // not needed
      xBufferChannel.putInt(freeOffset);
      // if(freeOffset==511)
      //            logManager.LogManager.log(5,"freeOffset: "+ freeOffset);
      xBufferChannel.position(Settings.numOfKeysPos);
      xBufferChannel.putInt(numOfKeys);
      // if(numOfKeys==26)
      //            logManager.LogManager.log(5,"numOfKeys: "+ numOfKeys);
      bufMngr.unpinBuffer(xBufferChannel, true);
      changeWorkingPageTo(hlpr);
      // write remaining record:
      xBufferChannel.putInt(CID); // CID
      xBufferChannel.putInt(qLength);
      xBufferChannel.put(qNameArray, 0, qLength);
      xBufferChannel.putInt(parentCID); // parentCId
      numOfKeys++;
      freeOffset += (qLength + 3 * Settings.sizeOfInt);
    }
    // unTakenRecs=numOfKeys;maybe we need this in the future
    // here nexP should be -1 cause there is no page after!
    xBufferChannel.position(Settings.freeOffsetPos);
    xBufferChannel.putInt(freeOffset);
    xBufferChannel.position(Settings.numOfKeysPos);
    xBufferChannel.putInt(numOfKeys);
    /// bufMngr.unpinBuffer(xBufferChannel, true);

  }
示例#10
0
 public BufferManager getBufferManager() {
   return BufferManager.getInstance();
 }