📄 apimanager.java
字号:
return val1==val2; else if(op.equals("<>")) return val1!=val2; else if(op.equals("<")) return val1<val2; else if(op.equals(">")) return val1>val2; else if(op.equals("<=")) return val1<=val2; else if(op.equals(">=")) return val1>=val2; else return false; } public static void deleteTuplesWithCondition(TableData tb,Condition[] conds) throws Exception { String attribName=null; int id=-1,k=-1; for(int i=0;i<conds.length;i++) { id=tb.getAttribIDByName(conds[i].attribute); if(tb.isAttriUnique(id)&&tb.hasThisIndex(conds[i].attribute)&&!conds[i].operand.equals("<>")) { attribName=conds[i].attribute; k=i; break; } } //如果输入的条件当中没有index的attribute if(attribName==null) { int count=deleteTuplesWithConditionNoIndex(tb,conds); //tb.decTupleNumber(count); if(count==0) throw new Success("There are no tuples with the specified conditions !"); else throw new Success("Delete successed!["+count+"]tuples have been successfully been deleted!"); } else { String attribType=tb.getAttribType(id); IndexTag tag=tb.getIndexTag(attribName); Index index=getIndex(tb,tag); int count=deleteTuplesWithConditionFromIndex(tb,index,conds,attribType,id,k); //tb.decTupleNumber(count); if(count==0) throw new Success("There are no tuples with the specified conditions !"); else throw new Success("Delete successed!["+count+"]tuples have been successfully been deleted!"); } } public static void deleteFromIndex(TableData tb,Tuple tuple) throws Exception { ArrayList<Index> indices=IndexManager.getIndices(); int len=indices.size(); String tbName=tb.getName(); String[] values=tuple.getValues(); for(int i=0;i<len;i++) { Index index=indices.get(i); if(index.getTBName().equals(tbName)) { int id=tb.getAttribIDByName(index.getAttribName()); String type=tb.getAttribType(id); Comparable key=getKeyForIndex(values[id],type); index.remove(key); Bucket bucket=index.find(key); //System.out.println(bucket); index.setDirty(); } } for(int i=0;i<tb.getIndices().size();i++) { IndexTag tag=tb.getIndices().get(i); if(tag.isInFile()) { Index index=IndexManager.readIndexIntoMemory(tag.getFullName()); int id=tb.getAttribIDByName(index.getAttribName()); String type=tb.getAttribType(id); Comparable key=getKeyForIndex(values[id],type); tag.setNotInFile(); index.remove(key); Bucket bucket=index.find(key); //System.out.println(bucket); index.setDirty(); } } } public static int deleteTuplesWithConditionNoIndex(TableData tb,Condition[] conds) throws Exception { String tbName=tb.getName(); ArrayList<Block> blocks=BufferedManager.blocks; int len=blocks.size(); int count=0; for(int i=0;i<len;i++) { Block block=blocks.get(i); if(block.getTBName().equals(tbName)) { Tuple[] tuples=block.getTuples(); for(int j=0;j<block.getTupeNumber();j++) { if(block.isHold(j)) { if(isSatisfy(tb,tuples[j],conds)) { deleteFromIndex(tb,tuples[j]); tuples[j]=null; block.setNotHold(j); block.setDirty(); count++; } } } } } for(int i=0;i<tb.getBlockNumber();i++) { if(tb.isBlockInFile(i)) { Block block=BufferedManager.putBlockIntoMemory(tb, i); tb.setBlockNotInFile(i); BufferedManager.rearrange(block);//rearrange已经包含了setBlockInFile if(block.getTBName().equals(tbName)) { Tuple[] tuples=block.getTuples(); for(int j=0;j<block.getTupeNumber();j++) { if(block.isHold(j)) { if(isSatisfy(tb,tuples[j],conds)) { deleteFromIndex(tb,tuples[j]); tuples[j]=null; block.setNotHold(j); block.setDirty(); count++; } } } } } } return count; } public static int deleteTuplesWithConditionFromIndex(TableData tb,Index index, Condition[] conds, String attribType,int id,int k) throws Exception { ArrayList<Tuple> tuples=getTuplesWithConditionFromIndex(tb,index,conds,attribType,id,k,true); int len=tuples.size(); for(int i=0;i<len;i++) { deleteFromIndex(tb,tuples.get(i)); } return len; } public static ArrayList<Tuple> getTuplesWithCondition(TableData tb,Condition[] conds) throws Exception { String attribName=null; int id=-1,k=-1; for(int i=0;i<conds.length;i++) { id=tb.getAttribIDByName(conds[i].attribute); if(tb.isAttriUnique(id)&&tb.hasThisIndex(conds[i].attribute)&&!conds[i].operand.equals("<>")) { attribName=conds[i].attribute; k=i; break; } } //如果输入的条件当中没有index的attribute if(attribName==null) { ArrayList<Tuple> tuples=getAllTuplesForTable(tb); ArrayList<Tuple> tpwc=filterTupleWithCondition(tb,tuples, conds); return tpwc; } else { String attribType=tb.getAttribType(id); IndexTag tag=tb.getIndexTag(attribName); Index index=getIndex(tb,tag); return getTuplesWithConditionFromIndex(tb,index,conds,attribType,id,k,false); } } //找到每一个tuple所对应的blockID与tupleID public static ArrayList<Tuple> getTuplesWithConditionFromIndex(TableData tb,Index index, Condition[] conds, String attribType,int id,int k,boolean delete) throws Exception { String op=conds[k].operand; String value=conds[k].compValue; Comparable key=null; ArrayList<Integer> tuplePointers=null; try { if(attribType.equals("int")) try { key=Integer.parseInt(value); }catch(Exception ex) { throw new OperationInfo("The compare value specified:["+value+"] is not a int!" + "You must specify a int value!"); } else if(attribType.equals("float")) try { key=Float.parseFloat("float"); }catch(Exception ex) { throw new OperationInfo("The compare value specified:["+value+"] is not a float!" + "You must specify a float value!"); } else if(attribType.startsWith("char")) { key=value.substring(value.indexOf("'")+1,value.lastIndexOf("'")).trim(); String temp1=value.substring(0,value.indexOf("'")).trim(); String temp2=value.substring(value.lastIndexOf("'")+1).trim(); if(temp1.length()!=0||temp2.length()!=0) throw new OperationInfo("Delete syntax error!"); } if(op.equals("=")) tuplePointers=index.findEqual(key); else if(op.equals("<")) tuplePointers=index.findSmaller(key); else if(op.equals("<=")) tuplePointers=index.findSmallerOrEqual(key); else if(op.equals(">=")) tuplePointers=index.findLargerOrEqual(key); else if(op.equals(">")) tuplePointers=index.findLarger(key); else throw new OperationInfo("The operand specfied in the condition["+op+"] is not valid!"); }catch(Exception ex) { throw ex; } if(tuplePointers==null||tuplePointers.size()==0) throw new Success("There are no tuples found!"); ArrayList<Tuple> tuples=getTuplesWithPointers(tb, tuplePointers); if(!delete) { return filterTupleWithCondition(tb, tuples, conds); } else { ArrayList<Tuple> tp=new ArrayList<Tuple>(); for(int i=0;i<tuples.size();i++) tp.add(tuples.get(i)); int len=tuplePointers.size(); for(int i=0;i<len;i+=2) { int blockID=tuplePointers.get(i); int tupleID=tuplePointers.get(i+1); deleteOneTuple(blockID,tupleID,tb); } return tp; } } //依据blokID与tupleID从block中读取数据 public static ArrayList<Tuple> getTuplesWithPointers(TableData tb,ArrayList<Integer> pointers) throws Exception { ArrayList<Tuple> tuples=new ArrayList<Tuple>(); int len=pointers.size(); for(int i=0;i<len;i+=2) { int blockID=pointers.get(i); int tupleID=pointers.get(i+1); Tuple tuple=findTuple(tb,blockID,tupleID); tuples.add(tuple); } return tuples; } public static Index getIndex(TableData tb,IndexTag tag) throws Exception { if(!tag.isInFile()) { ArrayList<Index> indices=IndexManager.indices; for(int i=0;i<indices.size();i++) { Index index=indices.get(i); if(index.getTBName().equals(tb.getName())&&index.getAttribName().equals(tag.getAttribName())) return index; } return null; } else { Index index=FileManager.readInIndex(tag.getFullName()); IndexManager.rearrange(index); tag.setNotInFile(); return index; } } public static void deleteOneTuple(int blockID, int tupleID, TableData tb) throws Exception { String tbName=tb.getName(); if(!tb.isBlockInFile(blockID)) { ArrayList<Block> blocks=BufferedManager.blocks; for(int i=0;i<blocks.size();i++) { Block block=blocks.get(i); if(block.getTBName().equals(tbName)&&block.getBlockID()==blockID) { block.getTuples()[tupleID]=null; block.setNotHold(tupleID); block.setDirty(); } } } else { Block block=BufferedManager.putBlockIntoMemory(tb, blockID); tb.setBlockNotInFile(blockID); BufferedManager.rearrange(block); block.getTuples()[tupleID]=null; block.setNotHold(tupleID); block.setDirty(); } } private static void removeBlockFromMemory(String tbName) { ArrayList<Block> blocks=BufferedManager.blocks; for(int i=0;i<blocks.size();i++) { if(blocks.get(i).getTBName().equals(tbName)) { blocks.remove(i); i--; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -