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

📄 filehdr.java

📁 移动对象空间数据库中索引r树源码,能在eclipse下直接运行.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**     This method does a file IO. Can we make another method which is not static.     @return root index for any file.     @deprecated Use the non static one.  */  public static long getRootIndex(String fileName)     throws FileNotFoundException  {    RandomAccessFile fl = new RandomAccessFile(fileName,"r");    try{      if (fl.length() == 0)//new file        throw new FileNotFoundException("Node.getRootIndex : File not found");      fl.seek( Node.INTEGER_SIZE );      long rootIndx = fl.readLong();      fl.close();      return rootIndx;    }    catch(IOException e){      System.out.println("Node.getRootIndex: Couldn't get root index");       return  Node.NOT_DEFINED;    }  }  /**     Returns the <code>RandomAccessFile</code> object  */  public RandomAccessFile getFile()  {    return this.file;  }  /**     Will return the total nodes in the tree. This does not include the nodes that are deleted and are      in the stack.  */  public int getTotalNodes()  {    if(topIdx < 0)      return totalNodes;    else      return totalNodes - topIdx;  }  public long getRootIndex()  {    return rootIndex;  }  protected void finalize() throws Throwable   {    try {      flush();      file.close();    }catch (Exception e) {      e.printStackTrace();    }  }  /**     Will flush the file header if it is dirty. It will <b>not</b> flush the individual nodes at it it not     its responsiblity.  */  void flush()    throws IOException  {    if(dirty && !writeThr){      writeFileHeader();      dirty = false;    }  }  public boolean isWriteThr()  {    return writeThr;  }  void setDirty(boolean val)  {    this.dirty = val;  }  //-------------The following code is added by Ketan ...replacing my code!!!!------------------  /**retuns the index of the first WRITE thread in the queue*/  private int firstWriter()  {    Enumeration e=waiters.elements();            for(int index=0;e.hasMoreElements();index++)      {        ThreadInfo threadinfo = (ThreadInfo) e.nextElement();        if(threadinfo.lockType == Node.WRITE)          return index;      }    return Integer.MAX_VALUE;  }  private int getIndex(Thread t)  {    Enumeration e=waiters.elements();            /**  If thread is in the vector then      *          return it's index      *    else      *          return -1     */    for(int index=0;e.hasMoreElements();index++)      {        ThreadInfo threadinfo = (ThreadInfo) e.nextElement();        /**  If Thread is already in the vector then          *   return it's Index         */        if(threadinfo.t == t)          {            return index;          }      }    return -1;  }      public synchronized void lockRead()  {    ThreadInfo threadinfo;    Thread me = Thread.currentThread();    int index = getIndex(me);    /** if index = -1 then        the thread is not in the Vector, so create a new ThreadInfo        and add it to the vector        else        thread is in the queue and get the index of the thread    */    if(index == -1)      {        threadinfo = new ThreadInfo(me,Node.READ);        waiters.addElement(threadinfo);      }    else      {        threadinfo = (ThreadInfo) waiters.elementAt(index);      }            /**  If the currentThread has come after a Write Thread then     *   make it wait() until WRITE thread is serviced     */    while(getIndex(me) >= firstWriter())      {        try          {            wait();          }catch(Exception e){}      }    /**     *  increase the no. of locks the threadinfo has acquired     */    threadinfo.nAcquired++;    //System.out.println("FileHdr.lockRead : read locked for thread " + Thread.currentThread());    //+" when "+this.toString());  }      public synchronized void lockWrite() throws IllegalArgumentException  {    ThreadInfo threadinfo;    Thread me= Thread.currentThread();    int index = getIndex(me);    /** If the thread is not in the Vector then        create a new ThreadInfo with WRITE status and add it to the Vector        else        get the Index for the thread from the Vector    */    if(index==-1)      {        threadinfo = new ThreadInfo(me,Node.WRITE);        waiters.addElement(threadinfo);      }    else      {        //System.out.println("getIndex = " +getIndex(me));        threadinfo = (ThreadInfo) waiters.elementAt(index);        //if(threadinfo.lockType==Node.READ)        //threadinfo.lockType = Node.WRITE;      }    while(getIndex(me)!=0)      {        try          {            wait();          }catch(Exception e){}      }    threadinfo.nAcquired++;    //System.out.println("FileHdr.lockWrite : write locked for thread " + Thread.currentThread());  }  public synchronized void unlock() throws IllegalArgumentException  {    ThreadInfo threadinfo;    Thread me = Thread.currentThread();    int index = getIndex(me);            /** if the index is greater than first WRITE thread then     *          lock  is not held by the thread so throw Exception     else             */    if(index > firstWriter())      throw new IllegalArgumentException("FileHdr.unlock: Lock not Held for the thread");            threadinfo = (ThreadInfo) waiters.elementAt(index);    threadinfo.nAcquired--;            if(threadinfo.nAcquired==0)      {        waiters.removeElementAt(index);        if(waiters.size()>0){          //System.out.println("FileHdr.unlock : notifiying");          notifyAll();        }      }    //System.out.println("FileHdr.unlock : unlocking for thread " + Thread.currentThread());  }  /**     This method will return only internal varaibles.  */  public String toString()  {    try{      String str = new String();      str += "\nTotal Nodes " +  totalNodes;      str += "\nRoot Index " +  rootIndex;      str += "\nFile length " + file.length();      if(waiters != null){        str += "\nWaiters : total " + waiters.size();        for(int i=0; i<waiters.size();i++)          str += "\n" + i + " : " + waiters.get(i).toString();      }      return str;    }catch(Exception e){      e.printStackTrace();      return null;    }  }  Vector getWaiters()  {    return waiters;  }  synchronized void setWaiters(Vector wtrs)  {    waiters = wtrs;  }}/**  *   The class helps to store the details of a thread, like lockType */class ThreadInfo{  int lockType;  int nAcquired=0;  Thread t;  ThreadInfo(Thread t,int lockType)  {    this.t = t;    this.lockType = lockType;  }  public String toString()  {    String str = new String("\nThreadInfo");    str += "\n lockType : "+ lockType;    str += "\n nAcquired : "+ nAcquired;        str += "\n Thread : "+ t;        return str;  }}

⌨️ 快捷键说明

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