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

📄 btreenode.java

📁 用JAVA实现的miniSQL
💻 JAVA
字号:

import java.util.*;

public class BtreeNode {
    int indexno; //the number of the index entry
    boolean isleaf; // is or not a leaf node
    int type; // the type of the entry
    int length; // the length of the entry
    long parent;// the parent of the node, if it is null, the node is the root
    int max;
    Block blk;
    LinkedList<Long> address; // the address in file for every entry
    LinkedList<Keyword> keyentry;
    public BtreeNode(Block block){  //i代表此节点有多少个索引项,s代表此节点是否为叶节点
        blk = block;
        blk.seek(0);
    	indexno = blk.readInt();
    	isleaf = blk.readBoolean();
    	type = blk.read();
    	length = blk.read()*2;
    	parent = blk.readLong();
    	max = 4073/(8+length);
    	if(type==1){
    		keyentry = new LinkedList<Keyword>();
    		address = new LinkedList<Long>();
    		for(int i=0; i<indexno; i++){
    			address.add(new Long(blk.readLong()));
    			keyentry.add(new Keyword(blk.readInt()));    			
    		}
    		address.add(new Long(blk.readLong()));
    	}
    	else if(type==2){
    		keyentry = new LinkedList<Keyword>();
    		address = new LinkedList<Long>();
    		for(int i=0; i<indexno; i++){
    			address.add(new Long(blk.readLong()));
    			keyentry.add(new Keyword(blk.readFloat()));    			
    		}
    		address.add(new Long(blk.readLong()));
    	}
    	else if(type==3){
    		keyentry = new LinkedList<Keyword>();
    		address = new LinkedList<Long>();
    		byte[] att = new byte[length];
    		for(int i=0; i<indexno; i++){
    			address.add(new Long(blk.readLong()));
    			blk.readFully(att);
    			try{
    			    keyentry.add(new Keyword(new String(att,"UTF16").trim()));
    			}catch(Exception e){
    			}
    		}
    		address.add(new Long(blk.readLong()));
    	}
    	blk.seek(0);
    }
    public BtreeNode(boolean i, int t, int l, long p, Block b){
    	this.blk = b;
    	this.isleaf = i;
    	this.indexno = 0;
    	this.type = t;
    	this.length = l;
    	this.parent = p;
    	this.max = 4073/(8+length);
    	keyentry = new LinkedList<Keyword>();
    	address = new LinkedList<Long>();
    }
    public int insert(Keyword e, long position){
    	int i;
    	int t;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if((t=e.compareTo(keyentry.get(i)))<0) break;
    		else if(t==0) return 0;
        address.add(i, new Long(position));
        keyentry.add(i, e);
        indexno++;
        updata();
        return 1;
    }
    public int insert(Keyword e, long position, boolean root){
    	int i;
    	int t;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if((t=e.compareTo(keyentry.get(i)))<0) break;
    		else if(t==0) return 0;
    	if(root)
            address.add(i+1, new Long(position));
    	else
    		address.add(i, new Long(position));
        keyentry.add(i, e);
        indexno++;
        updata();
        return 1;
    }
    public int delete(Keyword e)
    {
    	if(type!=e.type)
    	{
    		System.out.println("删除数据类型不匹配");
    		return -1;
    	}
    	if(keyentry.isEmpty())
    	{
    		System.out.println("此节点为空,无法删除");
    		return -1;
    	}
    	for(int i=0;i<indexno;i++)
    		if(e.compareTo(keyentry.get(i))==0)
    		{
    			keyentry.remove(i);
    			address.remove(i);
    			indexno--;
    			updata();
    			return i;
    		}
    	System.out.println("此节点中无此项");
    	return -1;
    }	
    public boolean Isfull()
    {
    	return indexno>=max;
    }
    public int searchchild(Keyword e)
    {
    	int i;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if(e.compareTo(keyentry.get(i))<0) break;
        return i;
    }
    public int search(Keyword e)
    {
    	int i;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if(e.compareTo(keyentry.get(i))==0) break;
    	if(i==indexno)
    	{
    		return -1;
    	}
        return i;
    }
    public int searchlarge(Keyword e){
    	int i;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if(e.compareTo(keyentry.get(i))<0) break;
        return i;
    }
    public int searchless(Keyword e){
    	int i;
    	if(type!=e.type)
    	{
    		System.out.println("存入数据类型不匹配");
    		return -1;
    	}
    	for(i=0;i<indexno;i++)
    		if(e.compareTo(keyentry.get(i))<=0) break;
        return i;
    }
    public int search(long p){
    	int i;
    	for(i=0;i<=indexno;i++)
    		if(p-address.get(i).longValue()==0) break;
    	if(i==indexno+1)
    	{
    		System.out.println("没找到此数据");
    		return -1;
    	}
        return i;
    }
    public void updata(){
    	blk.seek(0);
    	blk.writeInt(indexno);
    	blk.writeBoolean(isleaf);
    	blk.write(type);
    	blk.write(length/2);
    	blk.writeLong(parent);
    	int b=indexno;
    	if(indexno>max)
    		b=max;
    	if(type==1){
    		for(int i=0; i<b; i++){
    			blk.writeLong(address.get(i).longValue());
    			blk.writeInt(keyentry.get(i).ikey);    			
    		}
    		if(b>0)
    		   blk.writeLong(address.get(b).longValue());
    	}
    	else if(type==2){
    		for(int i=0; i<b; i++){
    			blk.writeLong(address.get(i).longValue());
    			blk.writeFloat(keyentry.get(i).fkey);      			
    		}
    		if(b>0)
               blk.writeLong(address.get(b).longValue());
    	}
    	else if(type==3){
    		for(int i=0; i<b; i++){
    			blk.writeLong(address.get(i).longValue());
    			blk.writeChars(keyentry.get(i).skey);
    			int sub = length - keyentry.get(i).skey.length()*2;
    			for(int j=0;j<sub;j++){
    				blk.write(0);
    			}
    		}
    		if(b>0)
    		    blk.writeLong(address.get(b).longValue());
    	}
    	blk.seek(0);
    }
    public long getAddress(){
    	return blk.head + blk.blockno*4096;
    }
}

⌨️ 快捷键说明

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