Repository navigation
Expand file tree
/
Copy pathStorage.java
More file actions
334 lines (285 loc) · 9.9 KB
/
Copy pathStorage.java
File metadata and controls
334 lines (285 loc) · 9.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.core.config.args;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;
import org.iq80.leveldb.CompressionType;
import org.iq80.leveldb.Options;
import java.io.File;
import java.util.Map;
import java.util.stream.Collectors;
import org.tron.common.utils.FileUtil;
import lombok.Getter;
import lombok.Setter;
/**
* Custom storage configurations
*
* @author haoyouqiang
* @version 1.0
* @since 2018/5/25
*/
public class Storage {
/**
* Keys (names) of database config
*/
private static final String DB_DIRECTORY_CONFIG_KEY = "storage.db.directory";
private static final String DB_VERSION_CONFIG_KEY = "storage.db.version";
private static final String INDEX_DIRECTORY_CONFIG_KEY = "storage.index.directory";
private static final String PROPERTIES_CONFIG_KEY = "storage.properties";
private static final String NAME_CONFIG_KEY = "name";
private static final String PATH_CONFIG_KEY = "path";
private static final String CREATE_IF_MISSING_CONFIG_KEY = "createIfMissing";
private static final String PARANOID_CHECKS_CONFIG_KEY = "paranoidChecks";
private static final String VERITY_CHECK_SUMS_CONFIG_KEY = "verifyChecksums";
private static final String COMPRESSION_TYPE_CONFIG_KEY = "compressionType";
private static final String BLOCK_SIZE_CONFIG_KEY = "blockSize";
private static final String WRITE_BUFFER_SIZE_CONFIG_KEY = "writeBufferSize";
private static final String CACHE_SIZE_CONFIG_KEY = "cacheSize";
private static final String MAX_OPEN_FILES_CONFIG_KEY = "maxOpenFiles";
/**
* Default values of directory
*/
private static final int DEFAULT_DB_VERSION = 1;
private static final String DEFAULT_DB_DIRECTORY = "database";
private static final String DEFAULT_INDEX_DIRECTORY = "index";
/**
* Default values of db options:
* <p>
* DEFAULT_COMPRESSION_TYPE: compressed with snappy
* DEFAULT_BLOCK_SIZE: 4 KB = 4 * 1024 B
* DEFAULT_WRITE_BUFFER_SIZE: 10 MB = 10 * 1024 * 1024 B
* DEFAULT_CACHE_SIZE: 10 MB = 10 * 1024 * 1024 B
* DEFAULT_MAX_OPEN_FILES: 100
*/
private static final CompressionType DEFAULT_COMPRESSION_TYPE = CompressionType.SNAPPY;
private static final int DEFAULT_BLOCK_SIZE = 4 * 1024;
private static final int DEFAULT_WRITE_BUFFER_SIZE = 10 * 1024 * 1024;
private static final long DEFAULT_CACHE_SIZE = 10 * 1024 * 1024L;
private static final int DEFAULT_MAX_OPEN_FILES = 100;
/**
* Database storage directory: /path/to/{dbDirectory}
*/
@Getter
@Setter
private String dbDirectory;
@Getter
@Setter
private int dbVersion;
/**
* Index storage directory: /path/to/{indexDirectory}
*/
@Getter
@Setter
private String indexDirectory;
/**
* Other custom database configurations
*/
@Getter
@Setter
private static class Property {
private String name;
private String path;
private Options dbOptions;
}
/**
* Key: dbName, Value: Property object of that database
*/
private Map<String, Property> propertyMap;
public static int getDbVersionFromConfig(final Config config) {
return config.hasPath(DB_VERSION_CONFIG_KEY) ?
config.getInt(DB_VERSION_CONFIG_KEY) : DEFAULT_DB_VERSION;
}
public static String getDbDirectoryFromConfig(final Config config) {
return config.hasPath(DB_DIRECTORY_CONFIG_KEY) ?
config.getString(DB_DIRECTORY_CONFIG_KEY) : DEFAULT_DB_DIRECTORY;
}
public static String getIndexDirectoryFromConfig(final Config config) {
return config.hasPath(INDEX_DIRECTORY_CONFIG_KEY) ?
config.getString(INDEX_DIRECTORY_CONFIG_KEY) : DEFAULT_INDEX_DIRECTORY;
}
/**
* Set propertyMap of Storage object from Config
*
* @param config Config object from "config.conf" file
*/
public void setPropertyMapFromConfig(final Config config) {
if (config.hasPath(PROPERTIES_CONFIG_KEY)) {
propertyMap = config.getObjectList(PROPERTIES_CONFIG_KEY).stream()
.map(Storage::createProperty)
.collect(Collectors.toMap(Property::getName, p -> p));
}
}
/**
* Get storage path by name of database
*
* @param dbName name of database
* @return path of that database
*/
public String getPathByDbName(String dbName) {
if (hasProperty(dbName)) {
return getProperty(dbName).getPath();
}
return null;
}
/**
* Get database options by name of database
*
* @param dbName name of database
* @return options of that database
*/
public Options getOptionsByDbName(String dbName) {
if (hasProperty(dbName)) {
return getProperty(dbName).getDbOptions();
}
return createDefaultDbOptions();
}
/**
* Only for unit test on db
*/
public void deleteAllStoragePaths() {
if (propertyMap == null) {
return;
}
for (Property property : propertyMap.values()) {
String path = property.getPath();
if (path != null) {
FileUtil.recursiveDelete(path);
}
}
}
private boolean hasProperty(String dbName) {
if (propertyMap != null) {
return propertyMap.containsKey(dbName);
}
return false;
}
private Property getProperty(String dbName) {
return propertyMap.get(dbName);
}
private static Property createProperty(final ConfigObject conf) {
Property property = new Property();
// Database name must be set
if (!conf.containsKey(NAME_CONFIG_KEY)) {
throw new IllegalArgumentException("[storage.properties] database name must be set.");
}
property.setName(conf.get(NAME_CONFIG_KEY).unwrapped().toString());
// Check writable permission of path
if (conf.containsKey(PATH_CONFIG_KEY)) {
String path = conf.get(PATH_CONFIG_KEY).unwrapped().toString();
File file = new File(path);
if (!file.exists() && !file.mkdirs()) {
throw new IllegalArgumentException("[storage.properties] can not create storage path: " + path);
}
if (!file.canWrite()) {
throw new IllegalArgumentException("[storage.properties] permission denied to write to: " + path);
}
property.setPath(path);
}
// Check, get and set fields of Options
Options dbOptions = createDefaultDbOptions();
if (conf.containsKey(CREATE_IF_MISSING_CONFIG_KEY)) {
dbOptions.createIfMissing(
Boolean.parseBoolean(
conf.get(CREATE_IF_MISSING_CONFIG_KEY).unwrapped().toString()
)
);
}
if (conf.containsKey(PARANOID_CHECKS_CONFIG_KEY)) {
dbOptions.paranoidChecks(
Boolean.parseBoolean(
conf.get(PARANOID_CHECKS_CONFIG_KEY).unwrapped().toString()
)
);
}
if (conf.containsKey(VERITY_CHECK_SUMS_CONFIG_KEY)) {
dbOptions.verifyChecksums(
Boolean.parseBoolean(
conf.get(VERITY_CHECK_SUMS_CONFIG_KEY).unwrapped().toString()
)
);
}
if (conf.containsKey(COMPRESSION_TYPE_CONFIG_KEY)) {
try {
dbOptions.compressionType(
CompressionType.getCompressionTypeByPersistentId(
Integer.parseInt(
conf.get(COMPRESSION_TYPE_CONFIG_KEY).unwrapped().toString()
)
)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[storage.properties] compressionType must be Integer type.");
}
}
if (conf.containsKey(BLOCK_SIZE_CONFIG_KEY)) {
try {
dbOptions.blockSize(
Integer.parseInt(
conf.get(BLOCK_SIZE_CONFIG_KEY).unwrapped().toString()
)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[storage.properties] blockSize must be Integer type.");
}
}
if (conf.containsKey(WRITE_BUFFER_SIZE_CONFIG_KEY)) {
try {
dbOptions.writeBufferSize(
Integer.parseInt(
conf.get(WRITE_BUFFER_SIZE_CONFIG_KEY).unwrapped().toString()
)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[storage.properties] writeBufferSize must be Integer type.");
}
}
if (conf.containsKey(CACHE_SIZE_CONFIG_KEY)) {
try {
dbOptions.cacheSize(
Long.parseLong(
conf.get(CACHE_SIZE_CONFIG_KEY).unwrapped().toString()
)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[storage.properties] cacheSize must be Long type.");
}
}
if (conf.containsKey(MAX_OPEN_FILES_CONFIG_KEY)) {
try {
dbOptions.maxOpenFiles(
Integer.parseInt(
conf.get(MAX_OPEN_FILES_CONFIG_KEY).unwrapped().toString()
)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[storage.properties] maxOpenFiles must be Integer type.");
}
}
property.setDbOptions(dbOptions);
return property;
}
private static Options createDefaultDbOptions() {
Options dbOptions = new Options();
dbOptions.createIfMissing(true);
dbOptions.paranoidChecks(true);
dbOptions.verifyChecksums(true);
dbOptions.compressionType(DEFAULT_COMPRESSION_TYPE);
dbOptions.blockSize(DEFAULT_BLOCK_SIZE);
dbOptions.writeBufferSize(DEFAULT_WRITE_BUFFER_SIZE);
dbOptions.cacheSize(DEFAULT_CACHE_SIZE);
dbOptions.maxOpenFiles(DEFAULT_MAX_OPEN_FILES);
return dbOptions;
}
}