Skip to content

Commit

Permalink
tool(db):db path move function.
Browse files Browse the repository at this point in the history
  1. print help if no params.
  2. remove toolkit name.
  3. if config or path not exist,print error
  • Loading branch information
halibobo1205 committed Aug 1, 2022
1 parent aee957f commit 0e74efc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
26 changes: 24 additions & 2 deletions plugins/src/main/java/org/tron/plugins/Db.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import picocli.CommandLine;

Expand All @@ -18,8 +21,27 @@ static class ConfigConverter implements CommandLine.ITypeConverter<Config> {
ConfigConverter() {
}

public Config convert(String value) {
return ConfigFactory.parseFile(Paths.get(value).toFile());
public Config convert(String value) throws IOException {
File file = Paths.get(value).toFile();
if (file.exists() && file.isFile()) {
return ConfigFactory.parseFile(Paths.get(value).toFile());
} else {
throw new IOException("DB config [" + value + "] not exist!");
}
}
}

static class PathConverter implements CommandLine.ITypeConverter<Path> {
PathConverter() {
}

public Path convert(String value) throws IOException {
File file = Paths.get(value).toFile();
if (file.exists() && file.isDirectory()) {
return file.toPath();
} else {
throw new IOException("DB path [" + value + "] not exist!");
}
}
}
}
29 changes: 21 additions & 8 deletions plugins/src/main/java/org/tron/plugins/DbMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ public class DbMove implements Callable<Integer> {
private static final String DEFAULT_DB_DIRECTORY = "database";
private static final String NAME_CONFIG_KEY = "name";
private static final String PATH_CONFIG_KEY = "path";
private static final String NOT_FIND = "The database to be moved cannot be found.";
private static final String NOT_FIND = "There is no database to be moved, exist.";

@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

@CommandLine.Option(names = {"-d", "--database-directory"},
defaultValue = "output-directory",
converter = Db.PathConverter.class,
description = "database directory path. Default: ${DEFAULT-VALUE}")
String database;
Path database;

@CommandLine.Option(names = {"-c", "--config"},
defaultValue = "config.conf",
Expand All @@ -50,10 +51,11 @@ public Integer call() throws Exception {
spec.commandLine().usage(System.out);
return 0;
}

if (config.hasPath(PROPERTIES_CONFIG_KEY)) {
List<? extends Config> dbs = config.getConfigList(PROPERTIES_CONFIG_KEY);
if (dbs.isEmpty()) {
spec.commandLine().getOut().println(NOT_FIND);
printNotExist();
return 0;
}
String dbPath = config.hasPath(DB_DIRECTORY_CONFIG_KEY)
Expand All @@ -64,14 +66,14 @@ public Integer call() throws Exception {
.collect(Collectors.toList());

if (dbs.isEmpty()) {
spec.commandLine().getOut().println(NOT_FIND);
printNotExist();
return 0;
}
List<Property> toBeMove = dbs.stream()
.map(c -> {
try {
return new Property(c.getString(NAME_CONFIG_KEY),
Paths.get(database, dbPath, c.getString(NAME_CONFIG_KEY)),
Paths.get(database.toString(), dbPath, c.getString(NAME_CONFIG_KEY)),
Paths.get(c.getString(PATH_CONFIG_KEY), dbPath, c.getString(NAME_CONFIG_KEY)));
} catch (IOException e) {
spec.commandLine().getErr().println(e);
Expand All @@ -81,7 +83,7 @@ public Integer call() throws Exception {
.filter(p -> !p.destination.equals(p.original)).collect(Collectors.toList());

if (toBeMove.isEmpty()) {
spec.commandLine().getOut().println(NOT_FIND);
printNotExist();
return 0;
}
toBeMove = toBeMove.stream()
Expand All @@ -96,14 +98,15 @@ public Integer call() throws Exception {
}).collect(Collectors.toList());

if (toBeMove.isEmpty()) {
spec.commandLine().getOut().println(NOT_FIND);
printNotExist();
return 0;
}
ProgressBar.wrap(toBeMove.stream(), "mv task").forEach(this::run);
spec.commandLine().getOut().println("move db done.");

} else {
spec.commandLine().getOut().println(NOT_FIND);
printNotExist();
return 0;
}
return 0;
}
Expand Down Expand Up @@ -133,6 +136,10 @@ private void run(Property p) {
}
}

private void printNotExist() {
spec.commandLine().getErr().println(NOT_FIND);
}

/**
* delete directory.
*/
Expand All @@ -157,6 +164,12 @@ static class Property {
public Property(String name, Path original, Path destination) throws IOException {
this.name = name;
this.original = original.toFile().getCanonicalFile().toPath();
if (!this.original.toFile().exists()) {
throw new IOException(this.original + " not exist!");
}
if (this.original.toFile().isFile()) {
throw new IOException(this.original + " is a file!");
}
this.destination = destination.toFile().getCanonicalFile().toPath();
}
}
Expand Down
11 changes: 8 additions & 3 deletions plugins/src/main/java/org/tron/plugins/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import java.util.concurrent.Callable;
import picocli.CommandLine;

@CommandLine.Command(name = "tron", subcommands = { CommandLine.HelpCommand.class, Db.class})
@CommandLine.Command(subcommands = { CommandLine.HelpCommand.class, Db.class})
public class Toolkit implements Callable<Integer> {


public static void main(String[] args) {
int exitCode = new CommandLine(new Toolkit()).execute(args);
System.exit(exitCode);
CommandLine cli = new CommandLine(new Toolkit());
if (args == null || args.length == 0) {
cli.usage(System.out);
} else {
int exitCode = cli.execute(args);
System.exit(exitCode);
}
}

@Override
Expand Down

0 comments on commit 0e74efc

Please sign in to comment.