18

I know how to implement btree in memory, but not clear about how to store btree in disc. I think there are two major difference:

  1. Conversion between memory pointer and disc address, see this post.
  2. How to split page when insert new k/v item? It is very easy to implement in memory.

Thanks

Chang
  • 3,953
  • 2
  • 30
  • 43
  • Possible duplicate http://stackoverflow.com/questions/872070/saving-btrees-to-a-disk-file-and-read-it even though the answers in there are not really good. – Manuel Salvadores Jan 14 '11 at 09:24
  • 1
    Did you ever figure this out? There is a nice open source implementation here: https://github.com/jankotek/JDBM3 but it takes time to read through it. To start you can take a look at: https://github.com/jankotek/JDBM3/blob/master/src/test/java/org/apache/jdbm/BTreeTest.java. If you found a better resource please share it as well. – Sohaib Dec 06 '16 at 11:16

2 Answers2

4

it all depends on DBMS you use. If you wanna know how it is implemented in MS SQL Server, things to read about are:

  • Pages (I guess they are in almost all modern DBMS's) - in SQL Server they are 8Kb. Database file is composed from pages.
  • Extents - logical groups of 8 continous pages
  • (S)GAM - (Shared) Global Allocation Map. Bitmap containing information about free and occupied extents. This is one of the first pages on database file.
  • IAM - Index Allocation Map. You can find out which index/heap is stored in which extents. Having this information, you can get place in the file where index/heap is stored.

Using IAM and GAM (or SGAM) you can split page - just move part of the page (which is supposed to be overflowed) to the another page on file.

IAM and GAM are also answers to your first question.

Most of these names are taken from MS SQL Server but I'm pretty sure, that in other DBMS's it is solved quite similar.

Hope it helps.

Marcin Krupowicz
  • 536
  • 6
  • 16
1

My recommendation is to have a look at the book Database System Implementation"

Chapter 2 "data storage" and chapter 3 "representing data elements" wil give you some hints about this problem.

Chapter 4 index structures has a section on Btrees

It's the best source of information I have found so far on this topic.

Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
  • 2
    There are many books on this topic, for example, *database system concepts*(http://codex.cs.yale.edu/avi/db-book/) chapter 11. however none of them talked about reality implmentation. – Chang Jan 28 '11 at 02:49