1---
2title: Migrating from LevelDB to RocksDB
3layout: post
4author: lgalanis
5category: blog
6redirect_from:
7  - /blog/1811/migrating-from-leveldb-to-rocksdb-2/
8---
9
10If you have an existing application that uses LevelDB and would like to migrate to using RocksDB, one problem you need to overcome is to map the options for LevelDB to proper options for RocksDB. As of release 3.9 this can be automatically done by using our option conversion utility found in rocksdb/utilities/leveldb_options.h. What is needed, is to first replace `leveldb::Options` with `rocksdb::LevelDBOptions`. Then, use `rocksdb::ConvertOptions( )` to convert the `LevelDBOptions` struct into appropriate RocksDB options. Here is an example:
11
12<!--truncate-->
13
14LevelDB code:
15
16```c++
17#include <string>
18#include "leveldb/db.h"
19
20using namespace leveldb;
21
22int main(int argc, char** argv) {
23  DB *db;
24
25  Options opt;
26  opt.create_if_missing = true;
27  opt.max_open_files = 1000;
28  opt.block_size = 4096;
29
30  Status s = DB::Open(opt, "/tmp/mydb", &db);
31
32  delete db;
33}
34```
35
36RocksDB code:
37
38```c++
39#include <string>
40#include "rocksdb/db.h"
41#include "rocksdb/utilities/leveldb_options.h"
42
43using namespace rocksdb;
44
45int main(int argc, char** argv) {
46  DB *db;
47
48  LevelDBOptions opt;
49  opt.create_if_missing = true;
50  opt.max_open_files = 1000;
51  opt.block_size = 4096;
52
53  Options rocksdb_options = ConvertOptions(opt);
54  // add rocksdb specific options here
55
56  Status s = DB::Open(rocksdb_options, "/tmp/mydb_rocks", &db);
57
58  delete db;
59}
60```
61
62The difference is:
63
64```diff
65-#include "leveldb/db.h"
66+#include "rocksdb/db.h"
67+#include "rocksdb/utilities/leveldb_options.h"
68
69-using namespace leveldb;
70+using namespace rocksdb;
71
72-  Options opt;
73+  LevelDBOptions opt;
74
75-  Status s = DB::Open(opt, "/tmp/mydb", &db);
76+  Options rocksdb_options = ConvertOptions(opt);
77+  // add rockdb specific options here
78+
79+  Status s = DB::Open(rocksdb_options, "/tmp/mydb_rocks", &db);
80```
81
82Once you get up and running with RocksDB you can then focus on tuning RocksDB further by modifying the converted options struct.
83
84The reason why ConvertOptions is handy is because a lot of individual options in RocksDB have moved to other structures in different components. For example, block_size is not available in struct rocksdb::Options. It resides in struct rocksdb::BlockBasedTableOptions, which is used to create a TableFactory object that RocksDB uses internally to create the proper TableBuilder objects. If you were to write your application from scratch it would look like this:
85
86RocksDB code from scratch:
87
88```c++
89#include <string>
90#include "rocksdb/db.h"
91#include "rocksdb/table.h"
92
93using namespace rocksdb;
94
95int main(int argc, char** argv) {
96  DB *db;
97
98  Options opt;
99  opt.create_if_missing = true;
100  opt.max_open_files = 1000;
101
102  BlockBasedTableOptions topt;
103  topt.block_size = 4096;
104  opt.table_factory.reset(NewBlockBasedTableFactory(topt));
105
106  Status s = DB::Open(opt, "/tmp/mydb_rocks", &db);
107
108  delete db;
109}
110```
111
112The LevelDBOptions utility can ease migration to RocksDB from LevelDB and allows us to break down the various options across classes as it is needed.
113