33import com .google .common .annotations .VisibleForTesting ;
44import com .google .common .collect .Lists ;
55import com .google .common .collect .Maps ;
6- import com .google .common .primitives .Bytes ;
7- import com .google .common .primitives .Ints ;
86import com .google .common .primitives .Longs ;
97import java .io .File ;
108import java .io .FileNotFoundException ;
@@ -155,10 +153,10 @@ public void generateSnapshot(String sourceDir, String snapshotDir) {
155153 long start = System .currentTimeMillis ();
156154 snapshotDir = Paths .get (snapshotDir , SNAPSHOT_DIR_NAME ).toString ();
157155 try {
156+ mergeCheckpoint (sourceDir );
158157 hasEnoughBlock (sourceDir );
159158 List <String > snapshotDbs = getSnapshotDbs (sourceDir );
160159 split (sourceDir , snapshotDir , snapshotDbs );
161- mergeCheckpoint2Snapshot (sourceDir , snapshotDir );
162160 // write genesisBlock , latest recent blocks and trans
163161 fillSnapshotBlockAndTransDb (sourceDir , snapshotDir );
164162 // save min block to info
@@ -192,9 +190,9 @@ public void generateHistory(String sourceDir, String historyDir) {
192190 throw new IllegalStateException (
193191 String .format ("Unavailable sourceDir: %s is not fullNode data." , sourceDir ));
194192 }
193+ mergeCheckpoint (sourceDir );
195194 hasEnoughBlock (sourceDir );
196195 split (sourceDir , historyDir , archiveDbs );
197- mergeCheckpoint2History (sourceDir , historyDir );
198196 // save max block to info
199197 generateInfoProperties (Paths .get (historyDir , INFO_FILE_NAME ).toString (),
200198 getLatestBlockHeaderNum (sourceDir ));
@@ -263,15 +261,6 @@ private List<String> getSnapshotDbs(String sourceDir) {
263261 return snapshotDbs ;
264262 }
265263
266- private void mergeCheckpoint2Snapshot (String sourceDir , String historyDir ) {
267- List <String > snapshotDbs = getSnapshotDbs (sourceDir );
268- mergeCheckpoint (sourceDir , historyDir , snapshotDbs );
269- }
270-
271- private void mergeCheckpoint2History (String sourceDir , String destDir ) {
272- mergeCheckpoint (sourceDir , destDir , archiveDbs );
273- }
274-
275264 private void split (String sourceDir , String destDir , List <String > dbs ) throws IOException {
276265 logger .info ("Begin to split the dbs." );
277266 spec .commandLine ().getOut ().println ("Begin to split the dbs." );
@@ -289,7 +278,7 @@ private void split(String sourceDir, String destDir, List<String> dbs) throws IO
289278 FileUtils .copyDatabases (Paths .get (sourceDir ), Paths .get (destDir ), dbs );
290279 }
291280
292- private void mergeCheckpoint (String sourceDir , String destDir , List < String > destDbs ) {
281+ private void mergeCheckpoint (String sourceDir ) {
293282 logger .info ("Begin to merge checkpoint to dataset." );
294283 spec .commandLine ().getOut ().println ("Begin to merge checkpoint to dataset." );
295284 try {
@@ -298,18 +287,18 @@ private void mergeCheckpoint(String sourceDir, String destDir, List<String> dest
298287 for (String cp : cpList ) {
299288 DBInterface checkpointDb = DbTool .getDB (
300289 sourceDir + "/" + DBUtils .CHECKPOINT_DB_V2 , cp );
301- recover (checkpointDb , destDir , destDbs );
290+ recover (checkpointDb , sourceDir );
302291 }
303292 } else if (Paths .get (sourceDir , CHECKPOINT_DB ).toFile ().exists ()) {
304293 DBInterface tmpDb = DbTool .getDB (sourceDir , CHECKPOINT_DB );
305- recover (tmpDb , destDir , destDbs );
294+ recover (tmpDb , sourceDir );
306295 }
307296 } catch (IOException | RocksDBException e ) {
308297 throw new RuntimeException (e );
309298 }
310299 }
311300
312- private void recover (DBInterface db , String destDir , List < String > destDbs )
301+ private void recover (DBInterface db , String destDir )
313302 throws IOException , RocksDBException {
314303 try (DBIterator iterator = db .iterator ()) {
315304 for (iterator .seekToFirst (); iterator .hasNext (); iterator .next ()) {
@@ -323,17 +312,15 @@ private void recover(DBInterface db, String destDir, List<String> destDbs)
323312 byte [] realKey = Arrays .copyOfRange (key , dbName .getBytes ().length + 4 , key .length );
324313 byte [] realValue =
325314 value .length == 1 ? null : Arrays .copyOfRange (value , 1 , value .length );
326- if (destDbs != null && destDbs .contains (dbName )) {
327- DBInterface destDb = DbTool .getDB (destDir , dbName );
328- if (realValue != null ) {
329- destDb .put (realKey , realValue );
315+ DBInterface destDb = DbTool .getDB (destDir , dbName );
316+ if (realValue != null ) {
317+ destDb .put (realKey , realValue );
318+ } else {
319+ byte op = value [0 ];
320+ if (DBUtils .Operator .DELETE .getValue () == op ) {
321+ destDb .delete (realKey );
330322 } else {
331- byte op = value [0 ];
332- if (DBUtils .Operator .DELETE .getValue () == op ) {
333- destDb .delete (realKey );
334- } else {
335- destDb .put (realKey , new byte [0 ]);
336- }
323+ destDb .put (realKey , new byte [0 ]);
337324 }
338325 }
339326 }
@@ -353,38 +340,14 @@ private void generateInfoProperties(String propertyfile, long num)
353340 }
354341
355342 private long getLatestBlockHeaderNum (String databaseDir ) throws IOException , RocksDBException {
356- // query latest_block_header_number from checkpoint first
357343 final String latestBlockHeaderNumber = "latest_block_header_number" ;
358- List <String > cpList = getCheckpointV2List (databaseDir );
359- DBInterface checkpointDb ;
360- if (cpList .size () > 0 ) {
361- String lastestCp = cpList .get (cpList .size () - 1 );
362- checkpointDb = DbTool .getDB (
363- databaseDir + "/" + DBUtils .CHECKPOINT_DB_V2 , lastestCp );
364- } else {
365- checkpointDb = DbTool .getDB (databaseDir , CHECKPOINT_DB );
366- }
367- Long blockNumber = getLatestBlockHeaderNumFromCP (checkpointDb ,
368- latestBlockHeaderNumber .getBytes ());
369- if (blockNumber != null ) {
370- return blockNumber ;
371- }
372- // query from propertiesDb if checkpoint not contains latest_block_header_number
373344 DBInterface propertiesDb = DbTool .getDB (databaseDir , PROPERTIES_DB_NAME );
374345 return Optional .ofNullable (propertiesDb .get (ByteArray .fromString (latestBlockHeaderNumber )))
375346 .map (ByteArray ::toLong )
376347 .orElseThrow (
377348 () -> new IllegalArgumentException ("not found latest block header number" ));
378349 }
379350
380- private Long getLatestBlockHeaderNumFromCP (DBInterface db , byte [] key ) {
381- byte [] value = db .get (Bytes .concat (simpleEncode (PROPERTIES_DB_NAME ), key ));
382- if (value != null && value .length > 1 ) {
383- return ByteArray .toLong (Arrays .copyOfRange (value , 1 , value .length ));
384- }
385- return null ;
386- }
387-
388351 /**
389352 * recent blocks, trans and genesis block.
390353 */
@@ -451,15 +414,6 @@ private byte[] getGenesisBlockHash(String parentDir) throws IOException, RocksDB
451414 return result ;
452415 }
453416
454- private static byte [] simpleEncode (String s ) {
455- byte [] bytes = s .getBytes ();
456- byte [] length = Ints .toByteArray (bytes .length );
457- byte [] r = new byte [4 + bytes .length ];
458- System .arraycopy (length , 0 , r , 0 , 4 );
459- System .arraycopy (bytes , 0 , r , 4 , bytes .length );
460- return r ;
461- }
462-
463417 private BlockNumInfo checkAndGetBlockNumInfo (String historyDir , String liteDir )
464418 throws IOException , RocksDBException {
465419 logger .info ("Check the compatibility of this history." );
@@ -531,7 +485,6 @@ private void trimExtraHistory(String liteDir, BlockNumInfo blockNumInfo)
531485 DBInterface transDb = DbTool .getDB (liteDir , TRANS_DB_NAME );
532486 DBInterface tranRetDb = DbTool .getDB (liteDir , TRANSACTION_RET_DB_NAME );
533487
534-
535488 ProgressBar .wrap (LongStream .rangeClosed (start , end )
536489 .boxed ()
537490 .sorted ((a , b ) -> Long .compare (b , a )), "trimHistory" ).forEach (n -> {
@@ -566,7 +519,6 @@ private void mergeBak2Database(String liteDir, BlockNumInfo blockNumInfo) throws
566519 return ;
567520 }
568521
569-
570522 Path bakDir = Paths .get (liteDir , BACKUP_DIR_PREFIX + START_TIME );
571523 logger .info ("Begin to merge {} to database, start {} end {}." , bakDir , start , end );
572524 spec .commandLine ().getOut ()
@@ -593,17 +545,7 @@ private void mergeBak2Database(String liteDir, BlockNumInfo blockNumInfo) throws
593545
594546 private byte [] getDataFromSourceDB (String sourceDir , String dbName , byte [] key )
595547 throws IOException , RocksDBException {
596- DBInterface sourceDb = DbTool .getDB (sourceDir , dbName );
597- DBInterface checkpointDb = DbTool .getDB (sourceDir , CHECKPOINT_DB );
598- // get data from tmp first.
599- byte [] valueFromTmp = checkpointDb .get (Bytes .concat (simpleEncode (dbName ), key ));
600- byte [] value ;
601- if (isEmptyBytes (valueFromTmp )) {
602- value = sourceDb .get (key );
603- } else {
604- value = valueFromTmp .length == 1
605- ? null : Arrays .copyOfRange (valueFromTmp , 1 , valueFromTmp .length );
606- }
548+ byte [] value = DbTool .getDB (sourceDir , dbName ).get (key );
607549 if (isEmptyBytes (value )) {
608550 throw new RuntimeException (String .format ("data not found in store, dbName: %s, key: %s" ,
609551 dbName , Arrays .toString (key )));
0 commit comments