📄 bufferedmanager.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package bufferedManager;import database.*;import java.util.ArrayList;import fileManager.*;import java.io.*;import catalogManager.*;/** * * @author outlaw */public class BufferedManager { public static ArrayList<Block> blocks=new ArrayList<Block>(); public static final int BUFFER_SIZE=100; public BufferedManager(){ } public static void writeBlocksBack() throws Exception { Block block=null; int len=blocks.size(); for(int i=0;i<len;i++) { block=blocks.get(i); if(block.dirty) FileManager.writeBlockBack(block); } } public static void addBlock(Block block) throws Exception { block.setDirty(); rearrange(block); } public static void rearrange(int index) { blocks.add(blocks.remove(index)); } public static void rearrange(Block newBlock) throws Exception { if(blocks.size()>=BUFFER_SIZE) { Block oldBlock; oldBlock=blocks.remove(0); if(oldBlock.isDirty()) { FileManager.writeBlockBack(oldBlock); } } TableData tb=CatalogManager.getTableByName(newBlock.getTBName()); tb.setBlockNotInFile(newBlock.getBlockID()); blocks.add(newBlock); } public static Block getBlock(TableData tb) throws Exception { return getBlockFromMemory(tb); } public static Block getBlockFromMemory(TableData tb) throws Exception { String tbName=tb.getName(); int len=blocks.size(); Block block; for(int i=len-1;i>=0;i--) { block=blocks.get(i); if(block.getTBName().equals(tbName)&&!block.isFull()) { rearrange(i); return block; } //如果这个block已经full了,该怎么办。 } block=getBlockFromFile(tb); return block; } public static Block putBlockIntoMemory(TableData tb,int blockID) throws Exception { try { RandomAccessFile raf=new RandomAccessFile(tb.getFullName()+".tbl","rw"); raf.seek(Block.BLOCKSIZE*blockID); byte[] data=new byte[Block.BLOCKSIZE]; raf.read(data); Block block=new Block(data,tb.getName(),blockID); raf.close(); return block; }catch(Exception ex) { ex.printStackTrace(); throw new OperationInfo("Get block for table:["+tb.getName()+"] from file error!"); } } public static Block getBlockFromFile(TableData tb) throws Exception { int len=tb.getBlockNumber(); Block block=null; for(int i=0;i<len;i++) { if(tb.isBlockInFile(i)&&!tb.isBlockFull(i)) { block=putBlockIntoMemory(tb,i); tb.setBlockNotInFile(i); rearrange(block); break; } } return block; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -