📄 node.java
字号:
/*
* Node.java
*
* Created on 2006年11月18日, 上午10:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package btree;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* BTree的节点类
* @author Andy
*/
public class Node {
/**
* 每个节点包含键数的最小值
*/
public static final int MIN_KEY_AMOUNT=1;
/**
* 包含键的数量
*/
public int numKeys;
/**
* 键数组
*/
public long[] keys=new long[MIN_KEY_AMOUNT*2];
/**
* 值数组
*/
public long[] valuePointers=new long[MIN_KEY_AMOUNT*2];
/**
* 子节点数组
*/
public long[] childrenPointers=new long[MIN_KEY_AMOUNT*2+1];
/**
* 子节点本身指针
*/
public long pointer;
/**
* 序列化节点
* @return 返回字节流
*/
public byte[] serialize() throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
//写入各Fields
dos.writeInt(numKeys);
for (int i = 0; i < MIN_KEY_AMOUNT*2; i++)
dos.writeLong(keys[i]);
for (int i = 0; i < MIN_KEY_AMOUNT*2; i++)
dos.writeLong(valuePointers[i]);
for (int i = 0; i < MIN_KEY_AMOUNT*2+1; i++)
dos.writeLong(childrenPointers[i]);
dos.writeLong(pointer);
baos.close();
dos.close();
return baos.toByteArray();
}
/**
* 反序列化
* @param b 字节流
* @return 节点类
*/
public static Node deserialize(byte[] b) throws IOException{
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream dis = new DataInputStream(bais);
//读取各Fields
Node newNode=new Node();
newNode.numKeys=dis.readInt();
for (int i = 0; i < MIN_KEY_AMOUNT*2; i++)
newNode.keys[i]=dis.readLong();
for (int i = 0; i < MIN_KEY_AMOUNT*2; i++)
newNode.valuePointers[i]=dis.readLong();
for (int i = 0; i < MIN_KEY_AMOUNT*2+1; i++)
newNode.childrenPointers[i]=dis.readLong();
newNode.pointer=dis.readLong();
bais.close();
dis.close();
return newNode;
}
/**
* 获取当前的秩下的节点大小。
* @return 返回节点大小
*/
public static int size() {
int size= 8*MIN_KEY_AMOUNT*2*2+
8*(MIN_KEY_AMOUNT*2+1)+
4+8;
return size;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -