On 18 Aug 2009 12:09:58 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote, quoted or indirectly quoted someone who said :
This should be a common need. Yet I am not aware of anything
like it in Java SE. What is the most common (pure Java)
solution to it?
I would like to have an implementation of java.util.Map,
which is constructed with an int ?m? and a java.io.File ?f?.
It will use no more than ?m? bytes of memory, but ?swap? out
(the least often used) entries to the file ?f?, when they do
not fit into the given memory size anymore.
I rolled my own something similar. It is not that much code to handle
the random quotations you see on my website.
What you could do in write a class that uses a HashMap internally just
to hold the keys and objects that are offsets in a sequential file.
When you build the Map, you write the objects out with writeUTF or
writeObject, and record the size/offset of the stream before the
write.
Then to lookup in the Map, you look up the key, get the offset, seek
and do a read. You don't even need to know the length.
This is pretty fast, especially when the drive/OS does read caching.
If you wanted to make it even faster, you could put the objects in a
NIO memory mapped file, but that limits your file size. You also
might put the file on fast flash drive.
I would write such a beast to your specs for $50 US.
I think Stefan is capable of writing his own code.