After a long hiatus I returned to blogging.
One of the key things that have perplexed is data base internals especially indexing. Never got a hang of it directly as I always got confused about how pointers are implemented in disk blocks from user space. These questions have always confused me.
Hence I decided to implement my own a no-sql store on a single node and choice of language is none other than Erlang on Mochiweb server. Webserver is just an interface for applications.
It just exposes two APIs in form of GET/POST.
1. GET for fetching the value associated with a key.
2. POST for storing the value associated with a key.
Transaction-managment etc I plan to implement later.
Some points about my choice of language Erlang:
a. Concurrency is baked in language and I don't need to use Thread pools like other boiler plate code.
b. Functional language so easier to think and less bugs.
c. Can easily to multi node in case I need it via OTP.
Now how do I store the key-value pairs. I used B-Tree like idea however instead of comparisons I just used hashes. These hashes are computed based on keys and these hashes form the directory names inside root path.
Let me explain using an example:
Consider a B-Tree of level 10 and each level we store till 1001 nodes.
Key is "K1" and its 10 different hashes (mod 1001) are :
10,20,30,40,50,60,70,80,90,100
So the data is stored at following file:
/10/20/30/40/50/60/70/80/90/100/K1
This is based on principle that file lookup inside directory is extremely fast as each directory has direct information about its subdirectories inside the directory inode.
So we have 10 disk based lookups and hence data structure is O(1).
I can store upto 1000000000000000000000000000000 bytes which serves my purpose very well.
Code is available at following location.
https://github.com/srivasrrahul/Simple-local-db.
One of the key things that have perplexed is data base internals especially indexing. Never got a hang of it directly as I always got confused about how pointers are implemented in disk blocks from user space. These questions have always confused me.
Hence I decided to implement my own a no-sql store on a single node and choice of language is none other than Erlang on Mochiweb server. Webserver is just an interface for applications.
It just exposes two APIs in form of GET/POST.
1. GET for fetching the value associated with a key.
2. POST for storing the value associated with a key.
Transaction-managment etc I plan to implement later.
Some points about my choice of language Erlang:
a. Concurrency is baked in language and I don't need to use Thread pools like other boiler plate code.
b. Functional language so easier to think and less bugs.
c. Can easily to multi node in case I need it via OTP.
Now how do I store the key-value pairs. I used B-Tree like idea however instead of comparisons I just used hashes. These hashes are computed based on keys and these hashes form the directory names inside root path.
Let me explain using an example:
Consider a B-Tree of level 10 and each level we store till 1001 nodes.
Key is "K1" and its 10 different hashes (mod 1001) are :
10,20,30,40,50,60,70,80,90,100
So the data is stored at following file:
/10/20/30/40/50/60/70/80/90/100/K1
This is based on principle that file lookup inside directory is extremely fast as each directory has direct information about its subdirectories inside the directory inode.
So we have 10 disk based lookups and hence data structure is O(1).
I can store upto 1000000000000000000000000000000 bytes which serves my purpose very well.
Code is available at following location.
https://github.com/srivasrrahul/Simple-local-db.