nodespacemanager.java

来自「支持并发访问的B+树」· Java 代码 · 共 138 行

JAVA
138
字号
/* * Created on Nov 19, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package com.nay0648.ds.bplustree;import java.io.*;import java.nio.*;import java.nio.channels.*;import java.util.*;/** * Description:<br> * this is used to manage file space.all space's allocate or recycle * must use this class * @abstract * @keywords * @author nay0648 * @version last modified:Jun 6, 2004 */class NodeSpaceManager{private String path;//tree file pathprivate ByteBuffer buff;//buffer used to increase the file lengthprivate List space;//the empty node list		/**	 * @param path 	 * the tree file path	 * @param nodelength 	 * a node length mesured in byte	 */	public NodeSpaceManager(String path,int nodelength)	{		this.path=path;		//init buffer equal to the node length filled with 0x00		buff=ByteBuffer.allocate(nodelength);		for(;buff.position()<buff.capacity();) buff.put((byte)0x00);		buff.flip();		//the empty node list		space=new LinkedList();	}		/**	 * allocate a new space for a node at the end of	 * the file	 * @param ch	 * file channel	 * @return	 * @throws IOException	 */	public synchronized long allocate(FileChannel ch) throws IOException	{		long pointer=BPlusTree.NULL;				//if there are empty node in space list,allocate a first one		if(space.size()>0) return ((Long)space.remove(0)).longValue();		//no empty space in list,allocate from the end of the tree file		else		{			pointer=ch.size();//the new space's pointer at the end of the file			//cover this space			ch.position(pointer);			if(ch.write(buff)!=buff.capacity()) throw new IOBufferSizeError();			buff.rewind();		}		return pointer;	}		/**	 * allocate a space for a node at the specified position	 * if there is not have empty node space left	 * @param ch	 * file channel	 * @param position	 * specified position	 * @return	 * @throws IOException	 */	public synchronized long allocate(FileChannel ch,long position) throws IOException	{		long pointer;				//if there are empty node in space list,allocate a first one		if(space.size()>0) return ((Long)space.remove(0)).longValue();		//no empty space in list,allocate from the end of the tree file		else		{			pointer=position;//the new space's pointer at the end of the file			//cover this space			ch.position(pointer);			if(ch.write(buff)!=buff.capacity()) throw new IOBufferSizeError();			buff.rewind();		}		return pointer;	}	/**	 * recycle a node's space	 * @param pointer the address of the first byte of the node space	 */	public synchronized void recycle(long pointer)	{		space.add(new Long(pointer));	}	/**	 * get the space list size	 * @return	 */	public int size()	{		return space.size();	}		/**	 * get a specified pointer	 * @param index	 * pointer index	 * @return	 */	public long getPointer(int index)	{		return ((Long)space.get(index)).longValue();	}		public String toString()	{		return space.toString();	}	/*	public static void main(String[] args) throws Exception	{}	*/}

⌨️ 快捷键说明

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