Skip to content

Commit eaf2c34

Browse files
authored
feat(toolkit): show db root error to console (#5748)
1 parent 074e0d6 commit eaf2c34

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

plugins/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,14 @@ Execute move command.
134134
java -jar Toolkit.jar db mv -c main_net_config.conf -d /data/tron/output-directory
135135
```
136136

137+
## DB Root
138+
139+
DB root provides a helper which can compute merkle root for tiny db.
140+
141+
NOTE: large db may GC overhead limit exceeded.
142+
143+
### Available parameters:
144+
145+
- `<src>`: Source path for database. Default: output-directory/database
146+
- `--db`: db name.
147+
- `-h | --help`: provide the help info

plugins/src/main/java/org/tron/plugins/DbRoot.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,20 @@ public Integer call() throws Exception {
6767
.errorText("Specify at least one exit database: --db dbName."));
6868
return 404;
6969
}
70-
if (dbs.size() > 1) {
71-
ProgressBar.wrap(dbs.stream(), "root task").parallel().forEach(this::calcMerkleRoot);
72-
} else {
73-
calcMerkleRoot(dbs.get(0));
70+
List<Ret> task = ProgressBar.wrap(dbs.stream(), "root task").parallel()
71+
.map(this::calcMerkleRoot).collect(Collectors.toList());
72+
task.forEach(this::printInfo);
73+
int code = (int) task.stream().filter(r -> r.code == 1).count();
74+
if (code > 0) {
75+
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
76+
.errorText("There are some errors, please check toolkit.log for detail."));
7477
}
7578
spec.commandLine().getOut().println("root task done.");
76-
return 0;
79+
return code;
7780
}
7881

79-
private void calcMerkleRoot(String name) {
82+
private Ret calcMerkleRoot(String name) {
83+
Ret info = new Ret();
8084
try (DBInterface database = DbTool.getDB(this.db, name)) {
8185
DBIterator iterator = database.iterator();
8286
iterator.seekToFirst();
@@ -85,15 +89,33 @@ private void calcMerkleRoot(String name) {
8589
.collect(Collectors.toCollection(ArrayList::new));
8690
Sha256Hash root = MerkleRoot.root(ids);
8791
logger.info("db: {},root: {}", database.getName(), root);
88-
spec.commandLine().getOut().println(String.format("db: %s,root: %s",
89-
database.getName(), root));
92+
info.code = 0;
93+
info.msg = String.format("db: %s,root: %s", database.getName(), root);
9094
} catch (RocksDBException | IOException e) {
9195
logger.error("calc db {} fail", name, e);
96+
info.code = 1;
97+
info.msg = String.format("db: %s,fail: %s",
98+
name, e.getMessage());
9299
}
100+
return info;
93101
}
94102

95103
private Sha256Hash getHash(Map.Entry<byte[], byte[]> entry) {
96104
return Sha256Hash.of(true,
97105
Bytes.concat(entry.getKey(), entry.getValue()));
98106
}
107+
108+
private void printInfo(Ret ret) {
109+
if (ret.code == 0) {
110+
spec.commandLine().getOut().println(ret.msg);
111+
} else {
112+
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
113+
.errorText(ret.msg));
114+
}
115+
}
116+
117+
private static class Ret {
118+
private int code;
119+
private String msg;
120+
}
99121
}

plugins/src/test/java/org/tron/plugins/DbRootTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void testRoot() throws IOException, RocksDBException {
5959
errorDb.put(("" + i).getBytes(), (ERROR_DB + "-" + i).getBytes());
6060
}
6161
args = new String[] {"db", "root", database.toString(), "--db", ERROR_DB};
62-
Assert.assertEquals(0, cli.execute(args));
62+
Assert.assertEquals(1, cli.execute(args));
6363
}
6464

6565
}

0 commit comments

Comments
 (0)