tags:

views:

150

answers:

1

I'm trying to implement a BTree. I'm pretty much done with the tree and works great for a smaller input which means I've implemented the tree in memory. Now I would like to play with large input for which I've to write the tree to a file. I don't know where to get started. I'm using Java and I haven't done too much of 'disk write' coding. Any help would be very appreciated. If someone wants to help me with an example that would be even better.

+3  A: 

If your BTree and its Nodes implement Serialiable, you can write the tree and its contents to a file using ObjectOutputStream and FileOutputStream. It'll be easy to test: write it out to a .ser file, read it back in, and see that you've got the original BTree back.

Java Almanac has some nice examples by package that'll help.

UPDATE: I'm not seeing either your use case for writing dynamically as modifications are made or "not wanting to write the whole tree."

What you're suggesting seems impossibly slow. Mirroring each change in memory with a corresponding modification to the file will take a long time.

If you're trying to do your own database implementation this makes sense. What scenario do you have in mind?

duffymo
Well, it's not like I want to write the whole BTree to a file. I want to write the tree as it is created. As BTree is dynamic - the root and the node changes, I've to do a disk write and disk read as I'm creating the tree which means I also need to save a pointer (or something like that)
newbie