• Post Reply Bookmark Topic Watch Topic
  • New Topic

B+ tree problem

 
Jacky Chow
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problem when I coding a B+ tree.
When use the following code, the B+ tree works correctly:
-------------------------------
// member variables
final int BUFFER = 4096;
RandomAccessFile lnodeAccessF = new RandomAccessFile("lnodeFile, "rw");
byte[] nodeContent= new nodeContent[BUFFER];
...
public int insert(Record rec){
// this method will call saveNode() and loadNode();
}
private void saveNode(int nodePtr, Node node){
...
lnodeAccessF.write(nodeContent);// write new node content to tree.
...

}
private Node loadNode(int nodePtr, int nodeType){
...

lnodeAccessF.close();
lnodeAccessF = new RandomAccessFile(lnodeFile,"rw");
lnodeAccessF.skipBytes(nodePtr * LNode.PAGE_SIZE);
lnodeAccessF.read(nodeContent);

...
}
-------------------------------
but when i use the following code to replace loadNode() for performance(no change in other code), the result of the B+ tree data is wrong,
-------------------------------
Node loadNode(int nodePtr, int nodeType){
...

lnodeAccessF.seek(nodePtr * LNode.PAGE_SIZE);

...
}
-------------------------------
The operation sequence of object lnodeAccessF in the orginal code(in the insert() method):
1.lnodeAccessF = new RandomAccessFile(inodeFile,"rw");
2.lnodeAccessF.write(nodeContent);
3.lnodeAccessF.close();
4.lnodeAccessF = new RandomAccessFile(inodeFile,"rw");
5.lnodeAccessF.skipBytes(nodePtr * LNode.PAGE_SIZE);
6.nodeAccessF.read(nodeContent);

7.if needed, go back step 1 to insert more record to the tree(the nodeContent will chage before write back to file).
The operation sequence of object lnodeAccessF after change the loadNode() method:
1.lnodeAccessF = new RandomAccessFile(inodeFile,"rw");
2.lnodeAccessF.write(nodeContent);[B]
3.lnodeAccessF.seek(nodePtr * LNode.PAGE_SIZE);
4.inodeAccessF.read(nodeContent);[\B]
5.if needed, go back step 1 to insert more record to the tree(the nodeContent will chage before write back to file).
I can't find that while the result of the two operation sequences are not same, could anyone point out any potential error in the new codes.
Thinks
Brian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!