Introduction
This article will describe the most commonly used database in blockchain technology; LevelDB which is an example of a NoSQL database and stores data as key-value. It keeps the data in the different levels that why it is named LevelDB.
What is LevelDB?
Heavyweight applications need stand-alone databases such as Oracle, MS SQL, MariaDB, Postgres, and so on. However, small apps which are portable and don’t need to host a server-side API to store their state is benefited by using a file-based database for instance SQLite. LevelDB is a light database and file-based database system as well as it is an example of a NoSQL database. NoSQL database doesn’t store data in a common relational database.
It stores data in files in a different format, for example, key-value, structured markup document, etc.
According to Wikipedia LevelDB is described as “an open-source on-disk key-value store”. LevelDB fast stores the data in key-value on disk. LevelDB uses keys and values for users based on which you can store and retrieve data. So, it is a type of NoSQL database. LevelDB was built by two googlers Jeff Dean and Sanjay Ghemawat who were inspired by the BigTable.
- Supports mapping from key to the corresponding value. Key and value are managed as adjacent string sequence in SSTable.
- The keys values are not just string it can be any byte array with arbitrary length. So, you can use encoded and non-encoded data.
- It supports fundamental operation, Get (), Put () and Delete () as like other.
- Stored data is sorted by key.
- We can run multiple operations at a time in a single call without any interruption.
Data Storage
It temporarily keeps the data into MemTable and periodically transfers data from MemTable to SSTable (Sorted String Table). SSTable is immutable so, the LevelDB data are immutable. Most of the blockchain uses a LevelDB database so blockchain transactions are immutable e.g Stratis, Etherum, Neo, and so on. Additionally, LevelDB compacts to minimize the invalid data in each level and then generates one new block at the next level.
In LevelDB, immutable data are stored in a disk, and this can be shared by different clustered nodes. Overall, there are a total of 7 levels to keep data as well as at most two in-memory tables. Firstly, the system buffer writes the data into an in-memory table called MemTable when this MemTable becomes full it flushes data to disk. Every 7 levels contain multiple tables called as SSTable. In LevelDb, the down-level maintains a larger capacity than the upper level. When the upper level becomes full, the system pushes the data to the down level, down level reads and writes data into multiple SSTable.
SSTable
Sorted String Table is a file of key-value string pairs which is sorted by keys. SSTable provides an immutable mapping of keys to values where operations are provided to lookup value associated with a specific key and the operation iterate over all key-value pairs in a specific key range. SSTable contains a sequence of blocks where each block is 64 KB; however, this is configurable.
Sample Query of LevelDB
Creating LevelDB Database
When you create a new leveldb database, the database name corresponds to a directory on the system and stores all files inside this particular directory.
var options = new Options { CreateIfMissing = true };
var db = new DB(options, @"C:\temp\tempsampleleveldb");
The above code creates the database if not found in the folder location: “C:\temp\tempsampleleveldb” and opens a connection to a new DB.
Database close
db.Close();// Close the connection
LevelDB supports Get, Put and Delete methods to read, update and delete data. Below are examples of those methods.
const string key=”London”;
// Put the value in the key to update the key value
keyValue.Put(key, "Kathmandu");
// Read and Print out the value
var keyValue = db.Get(key);
Console.WriteLine(keyValue);
// Delete the key
db.Delete(key);
Similarly, we can perform atomic updates, synchronous writes, forward and backward iteration. LevelDB is open source you can check for details in the git repository and documentation which is in C++. For .NET you can check leveldb.net as well.
Limitations
LevelDB doesn’t support
- SQL query
- Indexes
- Store procedure
- Joins
- Views
In short, the article described an immutable filesystem database; LevelDB which is a kind of NoSQL database. Moreover, in this article, we have learned how does LevelDB store the data and its limitations.
References
- https://dbdb.io/db/leveldb?fbclid=IwAR2nxQT1SyHFj1c5gswD0y_xsIpRZPYdKEpU3RKrPCRvGcrkcr6z6RNrn0Y
- https://dev.to/zenulabidin/what-is-leveldb-and-how-does-it-work-5ho3
- https://en.wikipedia.org/wiki/LevelDB