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

📄 block.java

📁 一个简单的数据库
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package bufferedManager;import database.*;import java.io.*;import catalogManager.*;/** * * @author outlaw */public class Block{    public static final int BLOCKSIZE=4096;    Tuple[] tuples;  //有数据,没数据总共的tuple集合    int blockID;    int tupleSize; //最多可存的tuple数    int tbID;    String tbName;    Holder holder;    boolean dirty;       public Block(String tbName,int id,int tupleSize)    {        this.tbName=tbName;        this.blockID=id;        this.tupleSize=tupleSize;        tuples=new Tuple[tupleSize];        holder=new Holder(tupleSize);        dirty=true;    }    public Block(byte[] data,String tbName,int blockID) throws Exception    {        ByteArrayInputStream bais=new ByteArrayInputStream(data);        DataInputStream dis=new DataInputStream(bais);        tupleSize=dis.readInt();        this.blockID=blockID;        holder=new Holder(tupleSize);        for(int i=0;i<tupleSize;i++)        {            boolean tag=dis.readBoolean();            holder.getHolders()[i]=tag;            if(tag)                holder.setHold(i);        }        TableData tb=CatalogManager.getTableByName(tbName);        this.tbName=tb.getName();        int tupleLengthInBytes=tb.getTupleLengtInBytes();        tuples=new Tuple[tupleSize];        boolean[] holders=holder.getHolders();        for(int i=0;i<tupleSize;i++)        {            if(holders[i])            {                byte[] tpByteData=new byte[tupleLengthInBytes];                dis.read(tpByteData);                tuples[i]=new Tuple(tpByteData,tb.getAttribTypes());                            }            else            {                dis.skip(tupleLengthInBytes);                tuples[i]=null;            }        }        dirty=false;    }    public void setHold(int index)    {        TableData tb=CatalogManager.getTableByName(tbName);        holder.setHold(index);        tb.incTupleNumber();        if(holder.isFull())        {            tb.blockIsFull.set(blockID,true);        }    }    public void setNotHold(int index)    {        TableData tb=CatalogManager.getTableByName(tbName);        holder.setNotHold(index);        tb.decTupleNumber();        if(!holder.isFull())        {            tb.blockIsFull.set(blockID,false);         }    }    public boolean isHold(int index)    {        return holder.isHold(index);    }    public boolean isFull()    {        return holder.isFull();    }    public int[] getAvailablePos()    {        return holder.getAvailablePos();    }    public int getFirstAvailablePos()    {        return holder.getFirstAvailablePos();    }    public String getTBName()    {        return tbName;    }    public int getBlockID()    {        return blockID;    }    public byte[] toBytes() throws Exception    {        ByteArrayOutputStream baos=new ByteArrayOutputStream();        DataOutputStream dos=new DataOutputStream(baos);        dos.writeInt(tupleSize);        boolean[] holders=holder.getHolders();        for(int i=0;i<holders.length;i++)        {            dos.writeBoolean(holders[i]);        }        for(int i=0;i<tupleSize;i++)        {            if(tuples[i]!=null)                dos.write(tuples[i].toBytes(CatalogManager.getTableByName(tbName).getAttribTypes()));            else            {                int len=CatalogManager.getTableByName(tbName).getTupleLengtInBytes();                for(int j=0;j<len;j++)                    dos.writeByte(0);            }        }        return baos.toByteArray();    }    public int addTuple(Tuple tp) throws Exception    {        if(isFull())            throw new OperationInfo("The block"+blockID+"is already full!");        int index=getFirstAvailablePos();        tuples[index]=tp;        holder.setHold(index);        return index;    }    public int[] getNotAvailablePos()    {        return holder.getNotAvailablePos();    }    public Tuple[] getTuples()    {        return tuples;    }    public boolean isDirty()    {        return dirty;    }    public void setDirty()    {        dirty=true;    }    public int getTupeNumber()    {        return this.tupleSize;    }}

⌨️ 快捷键说明

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