📄 apimanager.java
字号:
for(int i=0;i<len;i++) tables.get(i).closeIndex(); } public static Index getIndexByFullName(String fullName) throws Exception { FileInputStream fis=new FileInputStream(fullName); ObjectInputStream ois=new ObjectInputStream(fis); Index index=(Index) ois.readObject(); return index; } public static Index getIndex(TableData tb,String attribName) throws Exception { String tbName=tb.getName(); ArrayList<Index> indices=IndexManager.indices; int len=indices.size(); for(int i=0;i<len;i++) { Index index=indices.get(i); if(index.getTBName().equals(tbName)&&index.getAttribName().equals(attribName)) return index; } //这里要优化 ArrayList<IndexTag> indexTags=tb.getIndices(); for(int i=0;i<indexTags.size();i++) { Index index=FileManager.readInIndex(indexTags.get(i).getFullName()); if(index.getTBName().equals(tbName)&&index.getAttribName().equals(attribName)) { IndexManager.rearrange(index); return index; } } return null; } public static Tuple selectTuple(String tbName,String attribName,String value) throws Exception { TableData tb=CatalogManager.getTableByName(tbName); int attribID=tb.getAttribIDByName(attribName); String attribType=tb.getAttribType(attribID); Index index=getIndex(tb,attribName); //Index index=getIndexByFullName("D:/workspace/outlaw/tables/ageIndex.ind"); //System.out.println(index.getFullName()); if(index!=null) { Comparable key=getSelectValue(value,attribType); //System.out.println(key); Bucket bucket=index.find(key); //System.out.println(bucket); if(bucket!=null) { ArrayList<Integer> pointers=bucket.getPointers(); for(int i=0;i<pointers.size();i+=2) { int blockID=pointers.get(i); int tupleID=pointers.get(i+1); Tuple tuple=findTuple(tb,blockID,tupleID); //这一步如果前面更新数据库与更新index都是对的话不可能出错 return tuple; } }else //如果根本就没有这个值的信息加代码啊 { return null; } }else //如果在这个attribName上并没有构造一个index加代码 { return null; } return null; } public static Tuple findTuple(TableData tb,int blockID,int tupleID) throws Exception { if(!tb.isBlockInFile(blockID)) { String tbName=tb.getName(); ArrayList<Block> blocks=BufferedManager.blocks; int len=blocks.size(); for(int i=len-1;i>=0;i--) { Block block=blocks.get(i); if(block.getTBName().equals(tbName)&&block.getBlockID()==blockID) { BufferedManager.rearrange(i); return block.getTuples()[tupleID]; } } }else if(tb.isBlockInFile(blockID)) { Block block=BufferedManager.putBlockIntoMemory(tb, blockID); tb.setBlockNotInFile(blockID); BufferedManager.rearrange(block); return block.getTuples()[tupleID]; } return null; } public static Comparable getSelectValue(String value,String type) { if(type.equals("int")) { return Integer.parseInt(value); } if(type.equals("float")) { return Float.parseFloat(value); } if(type.startsWith("char")) { return value; } return null; } public static void insertIntoIndex(TableData tb,int blockID, int tupleID,String[] values) throws Exception { int len=IndexManager.indices.size(); String tbName=tb.getName(); //Bucket bucket=new Bucket(); ArrayList<IndexTag> indices=tb.getIndices(); for(int i=0;i<len;i++) { Index index=IndexManager.indices.get(i); //这里可以优化 if(index.getTBName().equals(tbName)) { //如果在内存里面找到了者个table所属的index,看看它是不是对应了某一个attribute String attribName=index.getAttribName(); int indexLen=indices.size(); //IndexTag for(int j=0;j<indexLen;j++) { IndexTag tag=indices.get(j); if(tag.getAttribName().equals(attribName)) { int attribID=tb.getAttribIDByName(attribName); String attribType=tb.getAttribType(attribID); Comparable key=getKeyForIndex(values[attribID],attribType); Bucket bucket=new Bucket(key,blockID,tupleID); tag.setNotInFile(); index.insert(bucket); index.setDirty(); } } } } for(int i=0;i<indices.size();i++) { IndexTag tag=indices.get(i); if(tag.isInFile()) { String indexNameFullName=tag.getFullName(); Index index=IndexManager.readIndexIntoMemory(indexNameFullName); String attribName=tag.getAttribName(); int attribID=tb.getAttribIDByName(attribName); String type=tb.getAttribType(attribID); Comparable key=getKeyForIndex(values[attribID],type); Bucket bucket=new Bucket(key,blockID,tupleID); tag.setNotInFile(); index.insert(bucket); index.setDirty(); } } } public static int deleteAllTupleFromTable(String tbName) throws Exception { TableData tb=CatalogManager.getTableByName(tbName); int deleteNumber=tb.getTupleNumber(); ArrayList<Block> blocks=BufferedManager.blocks; for(int i=0;i<blocks.size();i++) { if(blocks.get(i).getTBName().equals(tbName)) { blocks.remove(i); i--; } } //FileManager.deleteFile(tb.getFullName()); FileManager.createFile(tb.getFullNameWithSuffix()); tb.reset(); return deleteNumber; } public static Comparable getKeyForIndex(String value,String type) { if(type.equals("int")) return Integer.parseInt(value); if(type.equals("float")) return Float.parseFloat(value); if(type.startsWith("char")) return value; return null; } public static void resetTableIndex(String tbName,ArrayList<IndexTag> tags) throws Exception { ArrayList<Index> indices=IndexManager.indices; TableData tb=CatalogManager.getTableByName(tbName); for(int i=0;i<indices.size();i++) { if(indices.get(i).getTBName().equals(tbName)) { indices.remove(i); i--; } } for(int i=0;i<tags.size();i++) { String indName=tags.get(i).getIndexName(); String attribName=tags.get(i).getAttribName(); Index index=new Index(tbName,tb.getPath(),indName,attribName); FileManager.writeOutIndex(index); tags.get(i).setInFile(); } } public static boolean isSatisfy(TableData tb,Tuple tuple,Condition[] conds) throws OperationInfo { try { String[] values=tuple.getValues(); int len=conds.length; String attribName=null; String op=null; String value=null; int attribID; String attribType; int i; for(i=0;i<len;i++) { attribName=conds[i].attribute; op=conds[i].operand; value=conds[i].compValue; attribID=tb.getAttribIDByName(attribName); attribType=tb.getAttribType(attribID); if(attribType.equals("int")) { if(compare("int",values[attribID],op,value)) continue; else break; } else if(attribType.equals("float")) { if(compare("float",values[attribID],op,value)) continue; else break; } else if(attribType.startsWith("char")) { if(compare("char",values[attribID],op,value.substring(value.indexOf("'")+1,value.lastIndexOf("'")))) continue; else break; } } if(i==len) return true; else return false; }catch(Exception ex) { ex.printStackTrace(); throw new OperationInfo("Select syntax error!"); } } public static ArrayList<Tuple> filterTupleWithCondition(TableData tb,ArrayList<Tuple> tuples,Condition[] conds) throws OperationInfo { ArrayList<Tuple> tp=new ArrayList<Tuple>(); int len=tuples.size(); for(int i=0;i<len;i++) { if(isSatisfy(tb, tuples.get(i), conds)) tp.add(tuples.get(i)); } return tp; } public static ArrayList<Tuple> getAllTuplesForTable(TableData tb) throws Exception { ArrayList<Tuple> tuples=new ArrayList<Tuple>(); ArrayList<Block> blocks=BufferedManager.blocks; int len=blocks.size(); String tbName=tb.getName(); for(int i=0;i<len;i++) { Block block=blocks.get(i); if(block.getTBName().equals(tbName)) { Tuple[] ts=block.getTuples(); for(int j=0;j<ts.length;j++) { if(block.isHold(j)) tuples.add(ts[j]); } } } for(int i=0;i<tb.getBlockNumber();i++) { if(tb.isBlockInFile(i)) { Block block=BufferedManager.putBlockIntoMemory(tb, i); tb.setBlockNotInFile(i); BufferedManager.rearrange(block); Tuple[] ts=block.getTuples(); for(int j=0;j<ts.length;j++) { if(block.isHold(j)) tuples.add(ts[j]); } } } return tuples; } public static boolean compare(String type,String value1,String op,String value2) { int val1=0,val2=0; //System.out.println(type.equals("float")); //System.out.println(type); //System.out.println(type.length()); if(type.equals("int")) { val1=Integer.parseInt(value1); val2=Integer.parseInt(value2); } else if(type.equals("float")) { val1=1; float f1=Float.parseFloat(value1); float f2=Float.parseFloat(value2); if(Math.abs(f1-f2)<=1e-5) val2=val1; else if(f1<f2) val2=val1+1; else if(f1>f2) val2=val1-1; } else if(type.startsWith("char")) { val1=1; if(value1.equals(value2)) val2=val1; int temp; temp=value1.compareTo(value2); if(temp==0) val2=val1; else if(temp<0) val2=val1+1; else val2=val1-1; } if(op.equals("="))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -