Serializing BTree to a file
I'm try开发者_Go百科ing 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.
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?
You might want to take a look at this question.
精彩评论