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

📄 catalog.java

📁 用JAVA实现的miniSQL
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
public class Catalog {
	public RandomAccessFile catalog;
	public RandomAccessFile indexlog;
	public String reason;
    public Catalog(){
    }
    long existFile(String file){
    	long address=0;
    	long current=0;
    	byte length=0;
    	byte[] name;
    	try{
    	    catalog = new RandomAccessFile(new File("catalog.log"),"rws");
    	    while(current<catalog.length()){
    		    catalog.seek(current);
    		    address = current;
    		    current=current+catalog.readInt();;
    		    length = catalog.readByte();
    		    if(length==0)
    			    continue;
    		    name = new byte[length*2];    		
    		    catalog.read(name);
    		    if(file.equals(new String(name,"UTF16"))){
                    catalog.close();
                    return address;
    		    }
    	    }
    	    catalog.close();
    	}catch(Exception e){ 
    		reason = new String("无此表");
    	    return -1; 
    	}
    	reason = new String("无此表");
    	return -1;
    }
    boolean getinformation(Interpreter cmd , String table){
    	long pos = existFile(table);
    	if(pos==-1){
    		reason = new String("无此表");
    	    return false; 
    	}
    	try{
    		catalog = new RandomAccessFile(new File("catalog.log"),"rws");
    		long current = pos+515;
    		catalog.seek(current);
    		cmd.attnumber = catalog.read();
    		cmd.attribute = new String[cmd.attnumber];
            cmd.atttype = new int[cmd.attnumber];
            cmd.attlength = new int[cmd.attnumber];
            cmd.unique = new boolean[cmd.attnumber];
            cmd.attindex = new String[cmd.attnumber];
    		current = pos+ 528;
    		catalog.seek(current);
    		byte[] name;
    		int length= 0;
    		int type = 0;
    		for(int i=0;i<cmd.attnumber;i++){
    			length = catalog.read()*2;
    			name = new byte[length];
    			catalog.read(name);
    			cmd.attribute[i]= new String(name, "UTF16");
    			catalog.seek(current+511);
    			type = catalog.read();
    			cmd.atttype[i]=type/4;
    			if((type%4)/2==0)
    				cmd.unique[i]=false;
    			else
    				cmd.unique[i]=true;
    			if(type/2==1)
    				cmd.primarykey = cmd.attribute[i];
    			cmd.attlength[i]=catalog.read()*2;
    			length = catalog.read()*2;
    			if(length!=0){
    				name = new byte[length];
    			    catalog.read(name);
    			    cmd.attindex[i]= new String(name, "UTF16");
    			}
    			current = current+1024;
    			catalog.seek(current);
    		}
    	}catch(Exception e){
    		return false;
    	}
    	return true;
    }
    boolean createTable(Interpreter command){
    	if(command.mean!=1)
    		return false;
    	try{	
    	    catalog = new RandomAccessFile(new File("catalog.log"),"rws");
            int lenght=528+command.attnumber*1024;
            int rlenght=command.attnumber+1;
            for(int i=0;i<command.attnumber;i++)
        	    rlenght=rlenght+command.attlength[i];
            long filelenght=catalog.length();
            boolean isprimary = false;
            catalog.seek(filelenght);
            catalog.writeInt(lenght);
            catalog.write(command.table.length());
            long current=catalog.getFilePointer()+510;
            catalog.writeChars(command.table);
            catalog.seek(current);
            catalog.write(command.attnumber);
            catalog.writeLong(0);
            if(rlenght < 9)
            	rlenght = 9;
            catalog.writeInt(rlenght);
            for(int i=0;i<command.attnumber;i++){
        	    catalog.write(command.attribute[i].length());
        	    current=catalog.getFilePointer()+510;
        	    catalog.writeChars(command.attribute[i]);
        	    catalog.seek(current);
        	    if(command.attribute[i].equalsIgnoreCase(command.primarykey))
        		    isprimary = true;
        	    else
        		    isprimary = false;
        	    catalog.write(toType(command.atttype[i],command.unique[i],isprimary));
        	    catalog.write(command.attlength[i]/2);
        	    catalog.write(0);
        	    current=catalog.getFilePointer()+510;
        	    catalog.seek(current);
            }
            catalog.setLength(current);
    	    catalog.close();
    	}catch(Exception e){ return false; }
    	return createIndex(command , command.table, command.primarykey, command.primarykey+"index");
    }
    boolean deleteTable(Interpreter cmd, long pos){
    	if(pos<0)
    		return false;
    	getinformation(cmd, cmd.table);
    	try{
            catalog = new RandomAccessFile(new File("catalog.log"),"rws");
    	    catalog.seek(pos+4);
    	    catalog.write(0);
    	    catalog.close();
    	}catch(Exception e){ return false; }
    	return true;
    }
    boolean createIndex(Interpreter cmd, String table,String attribute,String index){
    	long pos= existFile(table);
    	if(pos<0)
    		return false;
    	long current = pos + 528;
    	int length=0;
    	byte[] name;
    	int total=0;
    	try{
    		catalog = new RandomAccessFile(new File("catalog.log"),"rws");
    		catalog.seek(pos);
    		total=catalog.readInt();
    		while(current<pos+total){
    			catalog.seek(current);
    		    length = catalog.read();
    		    name = new byte[length*2];
    		    catalog.read(name);
    		    if(attribute.equalsIgnoreCase(new String(name,"UTF16")))
    			    break;
    			current = current + 1024;
    		}
    		if(current>pos+total){
    			catalog.close();
    			reason = new String("不存在此属性");
    			return false;
    		}
    		catalog.seek(current+511);
    		length = catalog.read();
    		if((length%4)/2!=1){
    			catalog.close();
    			reason = new String("此属性不是unique型的");
    			return false;
    		}
    		catalog.seek(current+513);
    		length = catalog.read();
    		if(length!=0){
    			catalog.close();
    			reason = new String("该属性上的索引已存在");
    			return false;
    		}
    		catalog.seek(current+513);
    		catalog.write(index.length());
    		catalog.writeChars(index);
    		catalog.close();
    		indexlog = new RandomAccessFile(new File("indexlog.log"),"rws");
    		current = indexlog.length();
    		indexlog.seek(current);
    		indexlog.write(index.length());
    		indexlog.writeChars(index);
    		indexlog.seek(current+511);
    		indexlog.write(table.length());
    		indexlog.writeChars(table);
    		indexlog.seek(current+1022);
    		indexlog.write(attribute.length());
    		indexlog.writeChars(attribute);
    		indexlog.setLength(current+1533);
    		indexlog.close();
    	}catch(Exception e){ 
    		return false;
    	}
    	return getinformation(cmd, cmd.table);
    }
    void deleteIndexfromTable(String index){
    	long current = 0;
    	int length;
    	byte[] name;
    	try{
    	    indexlog = new RandomAccessFile(new File("indexlog.log"),"rws");
    	    while(current<indexlog.length()){
    	    	indexlog.seek(current);
    	    	length = indexlog.read();
    	    	if(length==0){
    	    		current = current+1533;
    	    		continue;
    	    	}
    	    	name = new byte[length*2];
    	    	indexlog.read(name);
    	    	if(index.equalsIgnoreCase(new String(name,"UTF16")))
    	    		break;
    	    	current = current+1533;
    	    }
    	    indexlog.seek(current);
    	    indexlog.write(0);
    	    indexlog.close();
    	}catch(Exception e){
    		System.out.println("Catalog error 1");
    		return;
    	}
    }
    boolean deleteIndex(String index){
    	long current = 0;
    	int length;
    	byte[] name;
    	String attribute;
    	String table;
    	try{
    	    indexlog = new RandomAccessFile(new File("indexlog.log"),"rws");
    	    while(current<indexlog.length()){
    	    	indexlog.seek(current);
    	    	length = indexlog.read();
    	    	if(length==0){
    	    		current = current+1533;
    	    		continue;
    	    	}
    	    	name = new byte[length*2];
    	    	indexlog.read(name);
    	    	if(index.equalsIgnoreCase(new String(name,"UTF16")))
    	    		break;
    	    	current = current+1533;
    	    }
    	    if(current>indexlog.length()){
    	    	reason = new String("无此索引");
    	    	indexlog.close();
    	    	return false;
    	    }
    	    indexlog.seek(current+511);
    	    length = indexlog.read();
    	    name = new byte[length*2];
    	    indexlog.read(name);
    	    table = new String(name, "UTF16");
    	    indexlog.seek(current+1022);
    	    length = indexlog.read();
    	    name = new byte[length*2];
    	    indexlog.read(name);
    	    attribute = new String(name, "UTF16");
    	    if(!deleteIndex(table,attribute,index)){
    	    	indexlog.close();
    	    	return false;
    	    }
    	    indexlog.seek(current);
    	    indexlog.write(0);
    	    indexlog.close();
    	}catch(Exception e){
    		return false;
    	}
    	return true;
    }
    boolean deleteIndex(String table,String attribute,String index){
    	long pos= existFile(table);
    	if(pos<0)
    		return false;
    	long current = pos + 528;
    	int length=0;
    	byte[] name;
    	int total=0;
    	try{
    		catalog = new RandomAccessFile(new File("catalog.log"),"rws");
    		catalog.seek(pos);
    		total=catalog.readInt();
    		while(current<pos+total){
    			catalog.seek(current);
    		    length = catalog.read();
    		    name = new byte[length*2];
    		    catalog.read(name);
    		    if(attribute.equalsIgnoreCase(new String(name,"UTF16")))
    			    break;
    			current = current + 1024;
    		}
    		if(current>pos+total){
    			catalog.close();

⌨️ 快捷键说明

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