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 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);

  }