Skip to content

Commit f358f42

Browse files
committed
sonar fix for db
1 parent bbe5ff9 commit f358f42

13 files changed

Lines changed: 59 additions & 94 deletions

‎src/main/java/org/tron/common/storage/WriteOptionsWrapper.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.tron.common.storage;
22

3-
public class WriteOptionsWrapper {
3+
import lombok.Getter;
44

5-
public org.rocksdb.WriteOptions rocks = null;
6-
public org.iq80.leveldb.WriteOptions level = null;
5+
public class WriteOptionsWrapper {
76

8-
private WriteOptionsWrapper() {
9-
10-
}
7+
@Getter
8+
private org.rocksdb.WriteOptions rocks = null;
9+
@Getter
10+
private org.iq80.leveldb.WriteOptions level = null;
1111

1212
public static WriteOptionsWrapper getInstance() {
1313
WriteOptionsWrapper wapper = new WriteOptionsWrapper();
@@ -16,7 +16,6 @@ public static WriteOptionsWrapper getInstance() {
1616
return wapper;
1717
}
1818

19-
2019
public WriteOptionsWrapper sync(boolean bool) {
2120
this.level.sync(bool);
2221
this.rocks.setSync(bool);

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.fusesource.leveldbjni.JniDBFactory.factory;
1919

20+
import com.google.common.base.Preconditions;
2021
import com.google.common.collect.Sets;
2122
import java.io.File;
2223
import java.io.IOException;
@@ -35,6 +36,7 @@
3536
import java.util.stream.StreamSupport;
3637
import lombok.NoArgsConstructor;
3738
import lombok.extern.slf4j.Slf4j;
39+
import org.apache.commons.lang3.StringUtils;
3840
import org.iq80.leveldb.CompressionType;
3941
import org.iq80.leveldb.DB;
4042
import org.iq80.leveldb.DBException;
@@ -54,9 +56,11 @@
5456
public class LevelDbDataSourceImpl implements DbSourceInter<byte[]>,
5557
Iterable<Map.Entry<byte[], byte[]>> {
5658

57-
String dataBaseName;
58-
DB database;
59-
boolean alive;
59+
private static final String ENGINE = "ENGINE";
60+
61+
private String dataBaseName;
62+
private DB database;
63+
private boolean alive;
6064
private String parentName;
6165
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
6266

@@ -85,18 +89,12 @@ public boolean checkOrInitEngine() {
8589
return false;
8690
}
8791

88-
String engine = PropUtil.readProperty(enginePath, "ENGINE");
89-
if (engine.equals("")) {
90-
if (!PropUtil.writeProperty(enginePath, "ENGINE", "LEVELDB")) {
91-
return false;
92-
}
93-
}
94-
engine = PropUtil.readProperty(enginePath, "ENGINE");
95-
if ("LEVELDB".equals(engine)) {
96-
return true;
97-
} else {
92+
String engine = PropUtil.readProperty(enginePath, ENGINE);
93+
if (StringUtils.isEmpty(engine) && !PropUtil.writeProperty(enginePath, ENGINE, "LEVELDB")) {
9894
return false;
9995
}
96+
engine = PropUtil.readProperty(enginePath, ENGINE);
97+
return "LEVELDB".equals(engine);
10098
}
10199

102100
@Override
@@ -113,9 +111,7 @@ public void initDB() {
113111
return;
114112
}
115113

116-
if (dataBaseName == null) {
117-
throw new NullPointerException("no name set to the dbStore");
118-
}
114+
Preconditions.checkNotNull(dataBaseName, "no name set to the dbStore");
119115

120116
Options dbOptions = Args.getInstance().getStorage().getOptionsByDbName(dataBaseName);
121117

@@ -244,7 +240,7 @@ public void putData(byte[] key, byte[] value) {
244240
public void putData(byte[] key, byte[] value, WriteOptionsWrapper options) {
245241
resetDbLock.readLock().lock();
246242
try {
247-
database.put(key, value, options.level);
243+
database.put(key, value, options.getLevel());
248244
} finally {
249245
resetDbLock.readLock().unlock();
250246
}
@@ -264,7 +260,7 @@ public void deleteData(byte[] key) {
264260
public void deleteData(byte[] key, WriteOptionsWrapper options) {
265261
resetDbLock.readLock().lock();
266262
try {
267-
database.delete(key, options.level);
263+
database.delete(key, options.getLevel());
268264
} finally {
269265
resetDbLock.readLock().unlock();
270266
}
@@ -453,10 +449,10 @@ public void updateByBatch(Map<byte[], byte[]> rows) {
453449
public void updateByBatch(Map<byte[], byte[]> rows, WriteOptionsWrapper options) {
454450
resetDbLock.readLock().lock();
455451
try {
456-
updateByBatchInner(rows, options.level);
452+
updateByBatchInner(rows, options.getLevel());
457453
} catch (Exception e) {
458454
try {
459-
updateByBatchInner(rows, options.level);
455+
updateByBatchInner(rows, options.getLevel());
460456
} catch (Exception e1) {
461457
throw new RuntimeException(e);
462458
}

‎src/main/java/org/tron/common/storage/leveldb/RocksDbDataSourceImpl.java‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.common.storage.leveldb;
22

3+
import com.google.common.base.Preconditions;
34
import com.google.common.collect.Sets;
45
import java.io.File;
56
import java.io.IOException;
@@ -15,6 +16,7 @@
1516
import java.util.concurrent.locks.ReentrantReadWriteLock;
1617
import lombok.NoArgsConstructor;
1718
import lombok.extern.slf4j.Slf4j;
19+
import org.apache.commons.lang3.StringUtils;
1820
import org.rocksdb.BlockBasedTableConfig;
1921
import org.rocksdb.BloomFilter;
2022
import org.rocksdb.Checkpoint;
@@ -37,12 +39,13 @@
3739
@NoArgsConstructor
3840
public class RocksDbDataSourceImpl implements DbSourceInter<byte[]>,
3941
Iterable<Map.Entry<byte[], byte[]>> {
42+
private static final String ENGINE = "ENGINE";
4043

4144
private String dataBaseName;
4245
private RocksDB database;
4346
private boolean alive;
4447
private String parentName;
45-
ReadOptions readOpts;
48+
private ReadOptions readOpts;
4649

4750
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
4851

@@ -153,18 +156,12 @@ public boolean checkOrInitEngine() {
153156
}
154157

155158
// for the first init engine
156-
String engine = PropUtil.readProperty(enginePath, "ENGINE");
157-
if (engine.equals("")) {
158-
if (!PropUtil.writeProperty(enginePath, "ENGINE", "ROCKSDB")) {
159-
return false;
160-
}
161-
}
162-
engine = PropUtil.readProperty(enginePath, "ENGINE");
163-
if ("ROCKSDB".equals(engine)) {
164-
return true;
165-
} else {
159+
String engine = PropUtil.readProperty(enginePath, ENGINE);
160+
if (StringUtils.isEmpty(engine) && !PropUtil.writeProperty(enginePath, ENGINE, "ROCKSDB")) {
166161
return false;
167162
}
163+
engine = PropUtil.readProperty(enginePath, ENGINE);
164+
return "ROCKSDB".equals(engine);
168165
}
169166

170167
public void initDB() {
@@ -181,9 +178,8 @@ public void initDB(RocksDbSettings settings) {
181178
if (isAlive()) {
182179
return;
183180
}
184-
if (dataBaseName == null) {
185-
throw new NullPointerException("no name set to the dbStore");
186-
}
181+
182+
Preconditions.checkNotNull(dataBaseName, "no name set to the dbStore");
187183

188184
try (Options options = new Options()) {
189185

@@ -259,7 +255,7 @@ public void putData(byte[] key, byte[] value) {
259255
try {
260256
database.put(key, value);
261257
} catch (RocksDBException e) {
262-
logger.error("RocksDBException:{}", e);
258+
logger.error(e.getMessage(), e);
263259
} finally {
264260
resetDbLock.readLock().unlock();
265261
}
@@ -272,9 +268,9 @@ public void putData(byte[] key, byte[] value, WriteOptionsWrapper optionsWrapper
272268
}
273269
resetDbLock.readLock().lock();
274270
try {
275-
database.put(optionsWrapper.rocks, key, value);
271+
database.put(optionsWrapper.getRocks(), key, value);
276272
} catch (RocksDBException e) {
277-
logger.error("RocksDBException:{}", e);
273+
logger.error(e.getMessage(), e);
278274
} finally {
279275
resetDbLock.readLock().unlock();
280276
}
@@ -289,7 +285,7 @@ public byte[] getData(byte[] key) {
289285
try {
290286
return database.get(key);
291287
} catch (RocksDBException e) {
292-
logger.error("RocksDBException: {}", e);
288+
logger.error(e.getMessage(), e);
293289
} finally {
294290
resetDbLock.readLock().unlock();
295291
}
@@ -305,7 +301,7 @@ public void deleteData(byte[] key) {
305301
try {
306302
database.delete(key);
307303
} catch (RocksDBException e) {
308-
logger.error("RocksDBException:{}", e);
304+
logger.error(e.getMessage(), e);
309305
} finally {
310306
resetDbLock.readLock().unlock();
311307
}
@@ -318,9 +314,9 @@ public void deleteData(byte[] key, WriteOptionsWrapper optionsWrapper) {
318314
}
319315
resetDbLock.readLock().lock();
320316
try {
321-
database.delete(optionsWrapper.rocks, key);
317+
database.delete(optionsWrapper.getRocks(), key);
322318
} catch (RocksDBException e) {
323-
logger.error("RocksDBException:{}", e);
319+
logger.error(e.getMessage(), e);
324320
} finally {
325321
resetDbLock.readLock().unlock();
326322
}
@@ -395,7 +391,7 @@ public void updateByBatch(Map<byte[], byte[]> rows, WriteOptionsWrapper optionsW
395391
}
396392
resetDbLock.readLock().lock();
397393
try {
398-
updateByBatchInner(rows, optionsWrapper.rocks);
394+
updateByBatchInner(rows, optionsWrapper.getRocks());
399395
} catch (Exception e) {
400396
try {
401397
updateByBatchInner(rows);

‎src/main/java/org/tron/core/db/AbstractRevokingStore.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ public void close() {
453453
@Getter // only for unit test
454454
static class RevokingState {
455455

456-
Map<RevokingTuple, byte[]> oldValues = new HashMap<>();
457-
Set<RevokingTuple> newIds = new HashSet<>();
458-
Map<RevokingTuple, byte[]> removed = new HashMap<>();
456+
private Map<RevokingTuple, byte[]> oldValues = new HashMap<>();
457+
private Set<RevokingTuple> newIds = new HashSet<>();
458+
private Map<RevokingTuple, byte[]> removed = new HashMap<>();
459459
}
460460

461461
@AllArgsConstructor

‎src/main/java/org/tron/core/db/CodeStore.java‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ public long getTotalCodes() {
2727
return Streams.stream(revokingDB.iterator()).count();
2828
}
2929

30-
private static CodeStore instance;
31-
32-
public static void destory() {
33-
instance = null;
34-
}
35-
36-
void destroy() {
37-
instance = null;
38-
}
39-
4030
public byte[] findCodeByHash(byte[] hash) {
4131
return revokingDB.getUnchecked(hash);
4232
}

‎src/main/java/org/tron/core/db/ContractStore.java‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ public long getTotalContracts() {
3030
return Streams.stream(revokingDB.iterator()).count();
3131
}
3232

33-
private static ContractStore instance;
34-
35-
public static void destory() {
36-
instance = null;
37-
}
38-
39-
void destroy() {
40-
instance = null;
41-
}
42-
4333
/**
4434
* find a transaction by it's id.
4535
*/

‎src/main/java/org/tron/core/db/EnergyProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public boolean useEnergy(AccountCapsule accountCapsule, long energy, long now) {
116116

117117
public long calculateGlobalEnergyLimit(AccountCapsule accountCapsule) {
118118
long frozeBalance = accountCapsule.getAllFrozenBalanceForEnergy();
119-
if (frozeBalance < 1000_000L) {
119+
if (frozeBalance < 1_000_000L) {
120120
return 0;
121121
}
122122

123-
long energyWeight = frozeBalance / 1000_000L;
123+
long energyWeight = frozeBalance / 1_000_000L;
124124
long totalEnergyLimit = dbManager.getDynamicPropertiesStore().getTotalEnergyCurrentLimit();
125125
long totalEnergyWeight = dbManager.getDynamicPropertiesStore().getTotalEnergyWeight();
126126

‎src/main/java/org/tron/core/db/KhaosDatabase.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public KhaosBlock(BlockCapsule blk) {
4141
}
4242

4343
@Getter
44-
BlockCapsule blk;
45-
Reference<KhaosBlock> parent = new WeakReference<>(null);
46-
BlockId id;
47-
Boolean invalid;
48-
long num;
44+
private BlockCapsule blk;
45+
private Reference<KhaosBlock> parent = new WeakReference<>(null);
46+
private BlockId id;
47+
private Boolean invalid;
48+
private long num;
4949

5050
public KhaosBlock getParent() {
5151
return parent == null ? null : parent.get();

‎src/main/java/org/tron/core/db/PendingManager.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
public class PendingManager implements AutoCloseable {
1212

1313
@Getter
14-
static List<TransactionCapsule> tmpTransactions = new ArrayList<>();
15-
Manager dbManager;
14+
private List<TransactionCapsule> tmpTransactions = new ArrayList<>();
15+
private Manager dbManager;
1616

1717
public PendingManager(Manager db) {
1818

@@ -25,7 +25,7 @@ public PendingManager(Manager db) {
2525
@Override
2626
public void close() {
2727

28-
for (TransactionCapsule tx : PendingManager.tmpTransactions) {
28+
for (TransactionCapsule tx : tmpTransactions) {
2929
try {
3030
if (tx.getTrxTrace() != null &&
3131
tx.getTrxTrace().getTimeResultType().equals(TimeResultType.NORMAL)) {

‎src/main/java/org/tron/core/db/StorageRowStore.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
@Component
1111
public class StorageRowStore extends TronStoreWithRevoking<StorageRowCapsule> {
1212

13-
private static StorageRowStore instance;
14-
1513
@Autowired
1614
private StorageRowStore(@Value("storage-row") String dbName) {
1715
super(dbName);
@@ -23,8 +21,4 @@ public StorageRowCapsule get(byte[] key) {
2321
row.setRowKey(key);
2422
return row;
2523
}
26-
27-
void destory() {
28-
instance = null;
29-
}
3024
}

0 commit comments

Comments
 (0)