Reviewing Lightning Memory-Mapped Database Library: On Disk Data
Join the DZone community and get the full member experience.Join For Free
as mentioned, the data in lmdb is actually stored in page. and i am currently tracking through the process in which we add a new item to the database. the first thing that happen is that we allocate a leaf page. i think that i’ll have to go over how pages are allocated now, which should explain a lot about how lmdb reuses disk space.
a new page is allocated by first finding the oldest transaction that is still active. this is how lmdb implements mvcc. basically, any free page that is older than any existing transaction is eligible for reuse. the first db (although a better term would be to call it btree, or maybe a table) contains the free pages, and at first we search there for an available page. if we can’t find such a page, we use the end of the file for that. this happens in mdb_page_alloc.
an very interesting aspect is the fact that lmdb allows direct memory writes (with the additional risk of corrupting the db if you messed the data), interestingly, this is done in the following code:
if the options allows directly memory writes, you get a point to the mmap file. otherwise, lmdb will allocate a page (or re-use one that has already been allocated).
i am not really sure what is going on with the insert yet. this is a function pointer (delegate for c#). and it select which behavior will happen later on. i am just not sure what those two different function do yet.
i think i got it, the key is here:
you can see that we use the insert method to add the mid variable to the dirty list. so if we give you a direct pointer, we append it to the list. but if we give you allocate a page, we need to add it in order.
the mdb_mid2l_insert will add an item to the list in the appropriate location to ensure sorting. i think that this is done so when we actually write the dirty page to disk, if we do that using file i/o, we will do that in ascending file order, so we can get good seek times from the disk. a new page has 4,084 bytes available for it (12 bytes are taken by header data).
a database is actually created the first time data is written to it. the root page is allocated and recorded. and then the data is added to the page.
as you might recall, data inside a page is kept in a sorted list. but remember that the data is also stored on the page, and there is the whole notion of overflow pages, so i think that i am going to have to draw a picture to figure out what is going on.
this is more or less how it works. but i don’t think that i really get it. note that there are two collections here. first is the positions of nodes in the page, and the second is the node themselves. the reason we have this complexity is that we need to maintain both of them in an efficient manner. and we need to do that without too much copying. therefor, the list of nodes in the page is in the beginning, growing downward, and the actual nodes are in the end, growing upward. the page is full when you can’t fit anything between those two lists.
a node, by the way, is the lmdb internal name for a key (and maybe data). but i think that i might have an error here, because i don’t see the code here to actually add nodes to the page in a sorted fashion, so it might be doing linear search on the content of a node. i am not tracking through the code for adding a second value into a database. and i think that i have to be wrong about linear node search. the code i was looking at was invoked at the the new db creation section, so it might be taking shortcuts that aren’t generally available.
at any rate, the logic for adding an item goes like this… first we need to find the appropriate page. and we do this by starting from the root and asking for the actual page. which gets us to mdb_page_get, where we have the following:
the really interesting part here is that each transaction have a dirty list of pages, so if you modified a page, you’ll get your own version, even before the data was committed. otherwise, we will just hand you the current version. this is quite nice.
and then we get to the already familiar mdb_page_search_root function, and i can confirm that the nodes in a page are actually sorted, which only make sense. i am not sure where that happens yet, but i have got the feeling that i am going to see that in a bit.
okay, i am going to go on a bit of a rant here, mostly against c, to be honest.
take a look at this code. mdb_page_search does something, but what it does it mutate the state for the cursor (mc), and you are then going to access the mc->mc_pg[mc->mc_top] to get the actual result you wanted. to make things more interesting, the is also a mc->mc_ki, which is the index of the node inside the node. and it drove me crazy, because i just couldn’t see the association between those three values, especially because i am so not used to this type of code that i never even considered this as a possibility.
at any rate, now i know how it gets to insert things in the right order. when doing mdb_page_search, it calls to mdb_node_search, which does the appropriate setup to tell the next call where it need to actually do the insert to get things in a sorted fashion. i am currently in the mdb_cursor_put, which is trice cursed and really hard to follow. a 400+ lines method with gotos galore.
but i am in the section where we are actually going to be writing a new value. (new_sub: goto header, if you care). and the code is pretty straight forward from there.
next up, i want to see how it handles a page split, but that is a subject to another post.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Insider Threats and Software Development: What You Should Know
Real-Time Made Easy: An Introduction to SignalR
How To Design Reliable IIoT Architecture
Hyperion Essbase Technical Functionality