1+ package org .tron .program ;
2+
3+ import static org .fusesource .leveldbjni .JniDBFactory .factory ;
4+
5+ import java .io .File ;
6+ import java .io .IOException ;
7+ import lombok .extern .slf4j .Slf4j ;
8+ import org .iq80 .leveldb .CompressionType ;
9+ import org .iq80 .leveldb .DB ;
10+ import org .iq80 .leveldb .DBIterator ;
11+ import org .rocksdb .BlockBasedTableConfig ;
12+ import org .rocksdb .BloomFilter ;
13+ import org .rocksdb .Options ;
14+ import org .rocksdb .RocksDB ;
15+ import org .rocksdb .RocksDBException ;
16+ import org .tron .common .utils .FileUtil ;
17+
18+ @ Slf4j
19+ public class DBConvert {
20+
21+ static {
22+ RocksDB .loadLibrary ();
23+ }
24+
25+ private static org .iq80 .leveldb .Options newDefaultLevelDbOptions () {
26+ org .iq80 .leveldb .Options dbOptions = new org .iq80 .leveldb .Options ();
27+ dbOptions .createIfMissing (true );
28+ dbOptions .paranoidChecks (true );
29+ dbOptions .verifyChecksums (true );
30+ dbOptions .compressionType (CompressionType .SNAPPY );
31+ dbOptions .blockSize (4 * 1024 );
32+ dbOptions .writeBufferSize (10 * 1024 * 1024 );
33+ dbOptions .cacheSize (10 * 1024 * 1024L );
34+ dbOptions .maxOpenFiles (100 );
35+ return dbOptions ;
36+ }
37+
38+ public DB newLevelDB (String dir , String dbname ) throws IOException {
39+ DB database = null ;
40+ File file = new File (dir + dbname );
41+ org .iq80 .leveldb .Options dbOptions = newDefaultLevelDbOptions ();
42+ try {
43+ database = factory .open (file , dbOptions );
44+ } catch (IOException e ) {
45+ if (e .getMessage ().contains ("Corruption:" )) {
46+ factory .repair (file , dbOptions );
47+ database = factory .open (file , dbOptions );
48+ } else {
49+ throw e ;
50+ }
51+ }
52+ return database ;
53+ }
54+
55+ private Options newDefaultRocksDbOptions () {
56+ Options options = new Options ();
57+ options .setCreateIfMissing (true );
58+ options .setIncreaseParallelism (1 );
59+ options .setNumLevels (7 );
60+ options .setMaxOpenFiles (-1 );
61+ options .setTargetFileSizeBase (256 * 1024 * 1024 );
62+ options .setBaseBackgroundCompactions (Math .max (1 , Runtime .getRuntime ().availableProcessors ()));
63+ options .setLevel0FileNumCompactionTrigger (4 );
64+ final BlockBasedTableConfig tableCfg ;
65+ options .setTableFormatConfig (tableCfg = new BlockBasedTableConfig ());
66+ tableCfg .setBlockSize (64 * 1024 );
67+ tableCfg .setBlockCacheSize (32 * 1024 * 1024 );
68+ tableCfg .setCacheIndexAndFilterBlocks (true );
69+ tableCfg .setPinL0FilterAndIndexBlocksInCache (true );
70+ tableCfg .setFilter (new BloomFilter (10 , false ));
71+ return options ;
72+ }
73+
74+ public RocksDB newRocksDB (String dir , String dbname ) {
75+ RocksDB database = null ;
76+ try (Options options = newDefaultRocksDbOptions ()) {
77+ database = RocksDB .open (options , dir + dbname );
78+ } catch (Exception ignore ) {
79+ logger .error (ignore .getMessage ());
80+ }
81+ return database ;
82+ }
83+
84+ public boolean convertLeveltoRocks (DB level , RocksDB rocks ) {
85+ int count = 0 ;
86+
87+ DBIterator iterator = level .iterator ();
88+ try {
89+ for (iterator .seekToFirst (); iterator .hasNext (); iterator .next ()) {
90+ byte [] key = iterator .peekNext ().getKey ();
91+ byte [] value = iterator .peekNext ().getValue ();
92+ count ++;
93+ rocks .put (key , value );
94+ }
95+ } catch (RocksDBException e ) {
96+ logger .error (e .getMessage ());
97+ return false ;
98+ } finally {
99+ try {
100+ iterator .close ();
101+ } catch (IOException e1 ) {
102+ logger .error (e1 .getMessage ());
103+ }
104+ }
105+ logger .info ("covert {} items from LevelDb to RocksDb" , count );
106+ return true ;
107+ }
108+
109+ public boolean doConvert (String [] args ) {
110+ String levelDbDir = "" ;
111+ String rocksDbDir = "" ;
112+
113+ if (args .length < 2 ) {
114+ levelDbDir = "output-directory" ;
115+ rocksDbDir = "output-directory-rocks" ;
116+ } else {
117+ levelDbDir = args [0 ];
118+ rocksDbDir = args [1 ];
119+ }
120+
121+ String srcDir = levelDbDir + File .separator + "database" + File .separator ;
122+ String dstDir = rocksDbDir + File .separator + "database" + File .separator ;
123+
124+ File levels = new File (srcDir );
125+ if (!levels .exists ()) {
126+ System .out .println (srcDir + " not exists." );
127+ return false ;
128+ }
129+
130+ File [] aa = levels .listFiles ();
131+ if (aa == null || aa .length == 0 ) {
132+ return false ;
133+ }
134+ for (File file : aa ) {
135+ DB level = null ;
136+ try {
137+ level = newLevelDB (srcDir , file .getName ());
138+ } catch (IOException e ) {
139+ e .printStackTrace ();
140+ }
141+
142+ FileUtil .createDirIfNotExists (dstDir + file .getName ());
143+
144+ RocksDB rocks = null ;
145+ rocks = newRocksDB (dstDir , file .getName ());
146+
147+ if (convertLeveltoRocks (level , rocks )) {
148+ System .out .println ("success" );
149+ } else {
150+ System .out .println ("failure" );
151+ }
152+ }
153+
154+ return true ;
155+ }
156+
157+ public static void main (String [] args ) {
158+ DBConvert convert = new DBConvert ();
159+ convert .doConvert (args );
160+ }
161+ }
0 commit comments