forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbMove.java
More file actions
245 lines (218 loc) · 8.18 KB
/
DbMove.java
File metadata and controls
245 lines (218 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package org.tron.plugins;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import me.tongfei.progressbar.ProgressBar;
import org.tron.plugins.utils.FileUtils;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@Slf4j(topic = "move")
@Command(name = "mv", aliases = "move",
description = "Move db to pre-set new path . For example HDD,reduce storage expenses.")
public class DbMove implements Callable<Integer> {
private static final String PROPERTIES_CONFIG_KEY = "storage.properties";
private static final String DB_DIRECTORY_CONFIG_KEY = "storage.db.directory";
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 = "There is no database to be moved, exist.";
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;
@CommandLine.Option(names = {"-d", "--database-directory"},
defaultValue = "output-directory",
converter = PathConverter.class,
description = "database directory path. Default: ${DEFAULT-VALUE}")
static Path database;
@CommandLine.Option(names = {"-c", "--config"},
defaultValue = "config.conf",
converter = ConfigConverter.class,
description = " config file. Default: ${DEFAULT-VALUE}")
Config config;
@CommandLine.Option(names = {"-h", "--help"})
static boolean help;
@Override
public Integer call() throws Exception {
if (help) {
spec.commandLine().usage(System.out);
help = false;
return 0;
}
if (config.hasPath(PROPERTIES_CONFIG_KEY)) {
List<? extends Config> dbs = config.getConfigList(PROPERTIES_CONFIG_KEY);
if (dbs.isEmpty()) {
printNotExist();
return 0;
}
String dbPath = config.hasPath(DB_DIRECTORY_CONFIG_KEY)
? config.getString(DB_DIRECTORY_CONFIG_KEY) : DEFAULT_DB_DIRECTORY;
dbs = dbs.stream()
.filter(c -> c.hasPath(NAME_CONFIG_KEY) && c.hasPath(PATH_CONFIG_KEY))
.collect(Collectors.toList());
if (dbs.isEmpty()) {
printNotExist();
return 0;
}
List<Property> toBeMove = dbs.stream()
.map(c -> {
try {
return new Property(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);
}
return null;
}).filter(Objects::nonNull)
.filter(p -> !p.destination.equals(p.original)).collect(Collectors.toList());
if (toBeMove.isEmpty()) {
printNotExist();
return 0;
}
toBeMove = toBeMove.stream()
.filter(property -> {
if (property.destination.toFile().exists()) {
spec.commandLine().getOut().println(String.format("%s already exist,skip.",
property.destination));
return false;
} else {
return true;
}
}).collect(Collectors.toList());
if (toBeMove.isEmpty()) {
printNotExist();
return 0;
}
ProgressBar.wrap(toBeMove.stream(), "mv task").forEach(this::run);
spec.commandLine().getOut().println("move db done.");
} else {
printNotExist();
return 0;
}
return 0;
}
private void run(Property p) {
if (p.destination.toFile().mkdirs()) {
ProgressBar.wrap(Arrays.stream(Objects.requireNonNull(p.original.toFile().listFiles()))
.filter(File::isFile).map(File::getName).parallel(), p.name).forEach(file -> {
Path original = Paths.get(p.original.toString(), file);
Path destination = Paths.get(p.destination.toString(), file);
try {
Files.copy(original, destination,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
spec.commandLine().getErr().println(e);
}
});
try {
if (FileUtils.deleteDir(p.original.toFile())) {
Files.createSymbolicLink(p.original, p.destination);
}
} catch (IOException | UnsupportedOperationException x) {
spec.commandLine().getErr().println(x);
}
} else {
spec.commandLine().getErr().println(String.format("%s create failed.", p.destination));
}
}
private void printNotExist() {
spec.commandLine().getErr().println(NOT_FIND);
}
static class Property {
private final String name;
private final Path original;
final Path destination;
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!");
}
if (FileUtils.isSymbolicLink(original.toFile())) {
throw new IOException(original + " is symbolicLink!");
}
this.destination = destination.toFile().getCanonicalFile().toPath();
if (this.destination.toFile().exists()) {
throw new IOException(this.destination + " already exist!");
}
if (this.destination.equals(this.original)) {
throw new IOException("destination and original can not be same:[" + this.original + "]!");
}
}
}
static class ConfigConverter implements CommandLine.ITypeConverter<Config> {
private final Exception notFind =
new IllegalArgumentException("There is no database to be moved,please check.");
ConfigConverter() {
}
public Config convert(String value) throws Exception {
if (help) {
return null;
}
File file = Paths.get(value).toFile();
if (file.exists() && file.isFile()) {
Config config = ConfigFactory.parseFile(Paths.get(value).toFile());
if (config.hasPath(PROPERTIES_CONFIG_KEY)) {
List<? extends Config> dbs = config.getConfigList(PROPERTIES_CONFIG_KEY);
if (dbs.isEmpty()) {
throw notFind;
}
String dbPath = config.hasPath(DB_DIRECTORY_CONFIG_KEY)
? config.getString(DB_DIRECTORY_CONFIG_KEY) : DEFAULT_DB_DIRECTORY;
dbs = dbs.stream()
.filter(c -> c.hasPath(NAME_CONFIG_KEY) && c.hasPath(PATH_CONFIG_KEY))
.collect(Collectors.toList());
if (dbs.isEmpty()) {
throw notFind;
}
Set<String> toBeMove = new HashSet<>();
for (Config c : dbs) {
if (!toBeMove.add(new Property(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))).name)) {
throw new IllegalArgumentException(
"DB config has duplicate key:[" + c.getString(NAME_CONFIG_KEY)
+ "],please check! ");
}
}
} else {
throw notFind;
}
return config;
} else {
throw new IOException("DB config [" + value + "] not exist!");
}
}
}
static class PathConverter implements CommandLine.ITypeConverter<Path> {
PathConverter() {
}
public Path convert(String value) throws IOException {
if (help) {
return null;
}
File file = Paths.get(value).toFile();
if (file.exists() && file.isDirectory()) {
return file.toPath();
} else {
throw new IOException("DB path [" + value + "] not exist!");
}
}
}
}