?? btreenode.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代表此節(jié)點(diǎn)有多少個(gè)索引項(xiàng),s代表此節(jié)點(diǎn)是否為葉節(jié)點(diǎn)
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("存入數(shù)據(jù)類型不匹配");
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("存入數(shù)據(jù)類型不匹配");
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("刪除數(shù)據(jù)類型不匹配");
return -1;
}
if(keyentry.isEmpty())
{
System.out.println("此節(jié)點(diǎn)為空,無法刪除");
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("此節(jié)點(diǎn)中無此項(xiàng)");
return -1;
}
public boolean Isfull()
{
return indexno>=max;
}
public int searchchild(Keyword e)
{
int i;
if(type!=e.type)
{
System.out.println("存入數(shù)據(jù)類型不匹配");
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("存入數(shù)據(jù)類型不匹配");
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("存入數(shù)據(jù)類型不匹配");
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("存入數(shù)據(jù)類型不匹配");
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("沒找到此數(shù)據(jù)");
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -