Raven's Storage: Reading a Sorted String Table
Join the DZone community and get the full member experience.Join For Free
when reading a sst, we have to deal with values of potentially large sizes. i want to avoid loading anything into managed memory if i can possible avoid it. that, along with other considerations has led me to use memory mapped files as the underlying abstractions for reading from the table.
along the way, i think that i made the following assumptions:
- work in little endian only.
- can work in 32 bits, but 64 bits are preferred.
- doesn’t put pressure on the .net memory manager.
in particular, i am worried about users wanting to do store large values, or large number of keys.
as it turned out, .net’s memory mapped files are a pretty good answer for what i wanted to do. sure, it is a bit of a pain with regards to how to handle things like winrt / silverlight, etc. but i am mostly focused on server side for now. and i got some ideas on how to provide the mmap illusion on top regular streams for platforms that don’t support it.
the fact that sst is written by a single thread, and once it is written it is immutable has drastically simplified the codebase. although i have to admit that looking at hex dump to figure out that i wrote to the wrong position is a bit of a bother, but more on that later. a lot of the code is basically the leveldb code, tweaked for .net uses. one important difference that i made was with regards to the actual api.
- keys are assumed to be small. most of the time, less than 2kb in size, and there are optimizations in place to take advantage of that. (it will still work with keys bigger than that, but will consume more memory).
- in general, through the codebase i tried to put major emphasis on performance and memory use even at this early stage.
- values are not assumed to be small.
what does the last one mean?
well, to start with, we aren’t going to map the entire file into our memory space, to start with, it might be big enough to start fragmenting our virtual address space, but mostly because there is no need. we always map just a single blokc at at time, and usually we never bother to read the values into managed memory, instead just accessing the data directly from the memory mapped file.
here is an example of reading the key for an entry from the sst:
as you can see, we need to read just the key itself to memory, the value itself is not even touched. also, there is some buffer management going on to make sure that we don’t need to re-allocate buffers as we are scanning through the table.
when you want to get a value, you call createvaluestream, which gives you a stream that you can work with. here is how you use the api:
this is actually part of the internal codebase, we are actually storing data inside the sst that will later help us optimize things, but that is something i’ll touch on a later point in time.
except for the slight worry that i am going to have to change the underlying approach from memory mapped files to streams if i need to run it outside the server/client, this is very cool.
next on my list is to think on how to implement the memtable, again, without impacting too much on the managed memory.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.