1818import static org .fusesource .leveldbjni .JniDBFactory .factory ;
1919
2020import com .google .common .collect .Sets ;
21+ import com .google .common .primitives .Bytes ;
2122import java .io .File ;
2223import java .io .IOException ;
2324import java .nio .file .Files ;
3031import java .util .List ;
3132import java .util .Map ;
3233import java .util .Map .Entry ;
33- import java .util .Objects ;
3434import java .util .Set ;
3535import java .util .concurrent .locks .ReadWriteLock ;
3636import java .util .concurrent .locks .ReentrantReadWriteLock ;
3737import java .util .stream .Collectors ;
3838import java .util .stream .Stream ;
3939import java .util .stream .StreamSupport ;
40-
41- import com .google .common .primitives .Bytes ;
4240import lombok .NoArgsConstructor ;
4341import lombok .extern .slf4j .Slf4j ;
44- import org .iq80 .leveldb .CompressionType ;
4542import org .iq80 .leveldb .DB ;
4643import org .iq80 .leveldb .DBIterator ;
4744import org .iq80 .leveldb .Logger ;
5451import org .tron .common .storage .WriteOptionsWrapper ;
5552import org .tron .common .storage .metric .DbStat ;
5653import org .tron .common .utils .FileUtil ;
54+ import org .tron .common .utils .PropUtil ;
5755import org .tron .common .utils .StorageUtils ;
5856import org .tron .core .db .common .DbSourceInter ;
5957import org .tron .core .db .common .iterator .StoreIterator ;
@@ -75,6 +73,7 @@ public class LevelDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
7573 private ReadWriteLock resetDbLock = new ReentrantReadWriteLock ();
7674 private static final String LEVELDB = "LEVELDB" ;
7775 private static final org .slf4j .Logger innerLogger = LoggerFactory .getLogger (LEVELDB );
76+ private static final String KEY_ENGINE = "ENGINE" ;
7877 private Logger leveldbLogger = new Logger () {
7978 @ Override
8079 public void log (String message ) {
@@ -110,6 +109,10 @@ public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {
110109
111110 @ Override
112111 public void initDB () {
112+ if (!checkOrInitEngine ()) {
113+ throw new RuntimeException (
114+ String .format ("failed to check database: %s, engine do not match" , dataBaseName ));
115+ }
113116 resetDbLock .writeLock ().lock ();
114117 try {
115118 logger .debug ("Init DB: {}." , dataBaseName );
@@ -135,6 +138,28 @@ public void initDB() {
135138 }
136139 }
137140
141+ private boolean checkOrInitEngine () {
142+ String dir = getDbPath ().toString ();
143+ String enginePath = dir + File .separator + "engine.properties" ;
144+
145+ if (FileUtil .createDirIfNotExists (dir )) {
146+ if (!FileUtil .createFileIfNotExists (enginePath )) {
147+ return false ;
148+ }
149+ } else {
150+ return false ;
151+ }
152+
153+ // for the first init engine
154+ String engine = PropUtil .readProperty (enginePath , KEY_ENGINE );
155+ if (engine .isEmpty () && !PropUtil .writeProperty (enginePath , KEY_ENGINE , LEVELDB )) {
156+ return false ;
157+ }
158+ engine = PropUtil .readProperty (enginePath , KEY_ENGINE );
159+
160+ return LEVELDB .equals (engine );
161+ }
162+
138163 private void openDatabase (Options dbOptions ) throws IOException {
139164 final Path dbPath = getDbPath ();
140165 if (dbPath == null || dbPath .getParent () == null ) {
@@ -362,6 +387,7 @@ public Map<WrappedByteArray, byte[]> prefixQuery(byte[] key) {
362387 }
363388 }
364389
390+ @ Deprecated
365391 @ Override
366392 public long getTotal () throws RuntimeException {
367393 resetDbLock .readLock ().lock ();
@@ -378,13 +404,6 @@ public long getTotal() throws RuntimeException {
378404 }
379405 }
380406
381- private void updateByBatchInner (Map <byte [], byte []> rows ) throws Exception {
382- try (WriteBatch batch = database .createWriteBatch ()) {
383- innerBatchUpdate (rows ,batch );
384- database .write (batch , writeOptions );
385- }
386- }
387-
388407 private void updateByBatchInner (Map <byte [], byte []> rows , WriteOptions options ) throws Exception {
389408 try (WriteBatch batch = database .createWriteBatch ()) {
390409 innerBatchUpdate (rows ,batch );
@@ -404,30 +423,23 @@ private void innerBatchUpdate(Map<byte[], byte[]> rows, WriteBatch batch) {
404423
405424 @ Override
406425 public void updateByBatch (Map <byte [], byte []> rows , WriteOptionsWrapper options ) {
407- resetDbLock .readLock ().lock ();
408- try {
409- updateByBatchInner (rows , options .level );
410- } catch (Exception e ) {
411- try {
412- updateByBatchInner (rows , options .level );
413- } catch (Exception e1 ) {
414- throw new RuntimeException (e );
415- }
416- } finally {
417- resetDbLock .readLock ().unlock ();
418- }
426+ this .updateByBatch (rows , options .level );
419427 }
420428
421429 @ Override
422430 public void updateByBatch (Map <byte [], byte []> rows ) {
431+ this .updateByBatch (rows , writeOptions );
432+ }
433+
434+ private void updateByBatch (Map <byte [], byte []> rows , WriteOptions options ) {
423435 resetDbLock .readLock ().lock ();
424436 try {
425- updateByBatchInner (rows );
437+ updateByBatchInner (rows , options );
426438 } catch (Exception e ) {
427439 try {
428- updateByBatchInner (rows );
440+ updateByBatchInner (rows , options );
429441 } catch (Exception e1 ) {
430- throw new RuntimeException (e );
442+ throw new RuntimeException (e1 );
431443 }
432444 } finally {
433445 resetDbLock .readLock ().unlock ();
0 commit comments