Skip to content

Commit b4de313

Browse files
authored
Merge pull request #839 from tronprotocol/db_change_config
Add detailed configurations (storage path, LevelDB options and so on) for each database
2 parents 8279a22 + 9348bca commit b4de313

23 files changed

+727
-42
lines changed

src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ public class LevelDbDataSourceImpl implements DbSourceInter<byte[]>,
6060
* constructor.
6161
*/
6262
public LevelDbDataSourceImpl(String parentName, String name) {
63-
parentName += Args.getInstance().getStorage().getDirectory();
64-
this.parentName = parentName;
6563
this.dataBaseName = name;
64+
this.parentName = Paths.get(
65+
parentName,
66+
Args.getInstance().getStorage().getDbDirectory()
67+
).toString();
6668
}
6769

6870
@Override
@@ -79,7 +81,7 @@ public void initDB() {
7981
throw new NullPointerException("no name set to the dbStore");
8082
}
8183

82-
Options dbOptions = createDbOptions();
84+
Options dbOptions = Args.getInstance().getStorage().getOptionsByDbName(dataBaseName);
8385

8486
try {
8587
openDatabase(dbOptions);
@@ -109,6 +111,7 @@ private void openDatabase(Options dbOptions) throws IOException {
109111
}
110112
}
111113

114+
@Deprecated
112115
private Options createDbOptions() {
113116
Options dbOptions = new Options();
114117
dbOptions.createIfMissing(true);

src/main/java/org/tron/core/config/args/Args.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import lombok.extern.slf4j.Slf4j;
2828
import org.apache.commons.collections4.CollectionUtils;
2929
import org.apache.commons.lang3.StringUtils;
30+
import org.iq80.leveldb.Options;
3031
import org.spongycastle.util.encoders.Hex;
3132
import org.springframework.stereotype.Component;
3233
import org.tron.common.crypto.ECKey;
@@ -65,8 +66,11 @@ public class Args {
6566
@Parameter(names = {"-p", "--private-key"}, description = "private-key")
6667
private String privateKey = "";
6768

68-
@Parameter(names = {"--storage-directory"}, description = "Storage directory")
69-
private String storageDirectory = "";
69+
@Parameter(names = {"--storage-db-directory"}, description = "Storage db directory")
70+
private String storageDbDirectory = "";
71+
72+
@Parameter(names = {"--storage-index-directory"}, description = "Storage index directory")
73+
private String storageIndexDirectory = "";
7074

7175
@Getter
7276
private Storage storage;
@@ -218,8 +222,16 @@ public static void clearParam() {
218222
INSTANCE.witness = false;
219223
INSTANCE.seedNodes = new ArrayList<>();
220224
INSTANCE.privateKey = "";
221-
INSTANCE.storageDirectory = "";
222-
INSTANCE.storage = null;
225+
INSTANCE.storageDbDirectory = "";
226+
INSTANCE.storageIndexDirectory = "";
227+
228+
// FIXME: INSTANCE.storage maybe null ?
229+
if (INSTANCE.storage != null) {
230+
// WARNING: WILL DELETE DB STORAGE PATHS
231+
INSTANCE.storage.deleteAllStoragePaths();
232+
INSTANCE.storage = null;
233+
}
234+
223235
INSTANCE.overlay = null;
224236
INSTANCE.seedNode = null;
225237
INSTANCE.genesisBlock = null;
@@ -253,6 +265,7 @@ public static void clearParam() {
253265
INSTANCE.getTransactionsToThisCountFeature = false;
254266
INSTANCE.getTransactionsByTimestampFeature = false;
255267
INSTANCE.getTransactionsByTimestampCountFeature = false;
268+
256269
}
257270

258271
/**
@@ -281,9 +294,16 @@ public static void setParam(final String[] args, final String confFileName) {
281294
}
282295

283296
INSTANCE.storage = new Storage();
284-
INSTANCE.storage.setDirectory(Optional.ofNullable(INSTANCE.storageDirectory)
297+
INSTANCE.storage.setDbDirectory(Optional.ofNullable(INSTANCE.storageDbDirectory)
285298
.filter(StringUtils::isNotEmpty)
286-
.orElse(config.getString("storage.directory")));
299+
.orElse(config.getString("storage.db.directory")));
300+
301+
INSTANCE.storage.setIndexDirectory(Optional.ofNullable(INSTANCE.storageIndexDirectory)
302+
.filter(StringUtils::isNotEmpty)
303+
.orElse(config.getString("storage.index.directory")));
304+
305+
INSTANCE.storage.setPropertyMapFromConfig(config);
306+
287307
INSTANCE.seedNode = new SeedNode();
288308
INSTANCE.seedNode.setIpList(Optional.ofNullable(INSTANCE.seedNodes)
289309
.filter(seedNode -> 0 != seedNode.size())
@@ -436,6 +456,20 @@ public static Args getInstance() {
436456
return INSTANCE;
437457
}
438458

459+
/**
460+
* Get storage path by name of database
461+
*
462+
* @param dbName name of database
463+
* @return path of that database
464+
*/
465+
public String getOutputDirectoryByDbName(String dbName) {
466+
String path = storage.getPathByDbName(dbName);
467+
if (!StringUtils.isBlank(path)) {
468+
return path;
469+
}
470+
return getOutputDirectory();
471+
}
472+
439473
/**
440474
* get output directory.
441475
*/
@@ -475,7 +509,7 @@ private static String getGeneratedNodePrivateKey() {
475509
String nodeId;
476510
try {
477511
File file = new File(
478-
INSTANCE.outputDirectory + File.separator + INSTANCE.storage.getDirectory(),
512+
INSTANCE.outputDirectory + File.separator + INSTANCE.storage.getDbDirectory(),
479513
"nodeId.properties");
480514
Properties props = new Properties();
481515
if (file.canRead()) {

0 commit comments

Comments
 (0)