|
13 | 13 | import java.nio.file.Path;
|
14 | 14 | import java.nio.file.Paths;
|
15 | 15 | import java.util.Arrays;
|
| 16 | +import java.util.Collections; |
16 | 17 | import java.util.List;
|
17 | 18 | import java.util.NoSuchElementException;
|
18 | 19 | import java.util.Objects;
|
@@ -353,28 +354,12 @@ private void generateInfoProperties(String propertyfile, long num)
|
353 | 354 | }
|
354 | 355 |
|
355 | 356 | private long getLatestBlockHeaderNum(String databaseDir) throws IOException, RocksDBException {
|
356 |
| - // query latest_block_header_number from checkpoint first |
357 |
| - final String latestBlockHeaderNumber = "latest_block_header_number"; |
358 |
| - DBInterface checkpointDb = getCheckpointDb(databaseDir); |
359 |
| - Long blockNumber = getLatestBlockHeaderNumFromCP(checkpointDb, |
360 |
| - latestBlockHeaderNumber.getBytes()); |
361 |
| - if (blockNumber != null) { |
362 |
| - return blockNumber; |
363 |
| - } |
364 |
| - // query from propertiesDb if checkpoint not contains latest_block_header_number |
365 |
| - DBInterface propertiesDb = DbTool.getDB(databaseDir, PROPERTIES_DB_NAME); |
366 |
| - return Optional.ofNullable(propertiesDb.get(ByteArray.fromString(latestBlockHeaderNumber))) |
367 |
| - .map(ByteArray::toLong) |
368 |
| - .orElseThrow( |
369 |
| - () -> new IllegalArgumentException("not found latest block header number")); |
370 |
| - } |
371 |
| - |
372 |
| - private Long getLatestBlockHeaderNumFromCP(DBInterface db, byte[] key) { |
373 |
| - byte[] value = db.get(Bytes.concat(simpleEncode(PROPERTIES_DB_NAME), key)); |
374 |
| - if (value != null && value.length > 1) { |
375 |
| - return ByteArray.toLong(Arrays.copyOfRange(value, 1, value.length)); |
376 |
| - } |
377 |
| - return null; |
| 357 | + final byte[] latestBlockHeaderNumber = "latest_block_header_number".getBytes(); |
| 358 | + byte[] value = getDataFromSourceDB(databaseDir, PROPERTIES_DB_NAME, latestBlockHeaderNumber); |
| 359 | + return Optional.ofNullable(value) |
| 360 | + .map(ByteArray::toLong) |
| 361 | + .orElseThrow( |
| 362 | + () -> new IllegalArgumentException("not found latest block header number")); |
378 | 363 | }
|
379 | 364 |
|
380 | 365 | /**
|
@@ -585,16 +570,30 @@ private void mergeBak2Database(String liteDir, BlockNumInfo blockNumInfo) throws
|
585 | 570 |
|
586 | 571 | private byte[] getDataFromSourceDB(String sourceDir, String dbName, byte[] key)
|
587 | 572 | throws IOException, RocksDBException {
|
| 573 | + byte[] keyInCp = Bytes.concat(simpleEncode(dbName), key); |
| 574 | + byte[] valueInCp = null; |
588 | 575 | DBInterface sourceDb = DbTool.getDB(sourceDir, dbName);
|
589 |
| - DBInterface checkpointDb = getCheckpointDb(sourceDir); |
590 |
| - // get data from tmp first. |
591 |
| - byte[] valueFromTmp = checkpointDb.get(Bytes.concat(simpleEncode(dbName), key)); |
| 576 | + // get data from checkpoint first. |
| 577 | + List<String> cpList = getCheckpointV2List(sourceDir); |
| 578 | + if (cpList.size() > 0) { |
| 579 | + // reverse iteration |
| 580 | + Collections.reverse(cpList); |
| 581 | + for (String cp: cpList) { |
| 582 | + valueInCp = DbTool.getDB( |
| 583 | + sourceDir + "/" + DBUtils.CHECKPOINT_DB_V2, cp).get(keyInCp); |
| 584 | + if (!isEmptyBytes(valueInCp)) { |
| 585 | + break; |
| 586 | + } |
| 587 | + } |
| 588 | + } else { |
| 589 | + valueInCp = DbTool.getDB(sourceDir, CHECKPOINT_DB).get(keyInCp); |
| 590 | + } |
592 | 591 | byte[] value;
|
593 |
| - if (isEmptyBytes(valueFromTmp)) { |
| 592 | + if (isEmptyBytes(valueInCp)) { |
594 | 593 | value = sourceDb.get(key);
|
595 | 594 | } else {
|
596 |
| - value = valueFromTmp.length == 1 |
597 |
| - ? null : Arrays.copyOfRange(valueFromTmp, 1, valueFromTmp.length); |
| 595 | + value = valueInCp.length == 1 |
| 596 | + ? null : Arrays.copyOfRange(valueInCp, 1, valueInCp.length); |
598 | 597 | }
|
599 | 598 | if (isEmptyBytes(value)) {
|
600 | 599 | throw new RuntimeException(String.format("data not found in store, dbName: %s, key: %s",
|
@@ -664,19 +663,6 @@ private long getSecondBlock(String databaseDir) throws RocksDBException, IOExcep
|
664 | 663 | return num;
|
665 | 664 | }
|
666 | 665 |
|
667 |
| - private DBInterface getCheckpointDb(String sourceDir) throws IOException, RocksDBException { |
668 |
| - List<String> cpList = getCheckpointV2List(sourceDir); |
669 |
| - DBInterface checkpointDb; |
670 |
| - if (cpList.size() > 0) { |
671 |
| - String latestCp = cpList.get(cpList.size() - 1); |
672 |
| - checkpointDb = DbTool.getDB( |
673 |
| - sourceDir + "/" + DBUtils.CHECKPOINT_DB_V2, latestCp); |
674 |
| - } else { |
675 |
| - checkpointDb = DbTool.getDB(sourceDir, CHECKPOINT_DB); |
676 |
| - } |
677 |
| - return checkpointDb; |
678 |
| - } |
679 |
| - |
680 | 666 | @VisibleForTesting
|
681 | 667 | public static void setRecentBlks(long recentBlks) {
|
682 | 668 | RECENT_BLKS = recentBlks;
|
|
0 commit comments