• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

B+ tree problem

 
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
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic