Repository navigation
Expand file tree
/
Copy pathLevelDB.java
More file actions
56 lines (46 loc) · 1.38 KB
/
Copy pathLevelDB.java
File metadata and controls
56 lines (46 loc) · 1.38 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
package org.tron.core.db2.common;
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import org.iq80.leveldb.WriteOptions;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
import org.tron.core.db.common.WrappedByteArray;
import org.tron.core.db.common.iterator.DBIterator;
public class LevelDB implements DB<byte[], byte[]> {
@Getter
private LevelDbDataSourceImpl db;
private WriteOptions writeOptions = new WriteOptions().sync(true);
public LevelDB(String parentName, String name) {
db = new LevelDbDataSourceImpl(parentName, name);
db.initDB();
}
@Override
public byte[] get(byte[] key) {
return db.getData(key);
}
@Override
public void put(byte[] key, byte[] value) {
db.putData(key, value);
}
@Override
public void remove(byte[] key) {
db.deleteData(key);
}
@Override
public DBIterator iterator() {
return db.iterator();
}
public void flush(Map<WrappedByteArray, WrappedByteArray> batch) {
Map<byte[], byte[]> rows = batch.entrySet().stream()
.map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes()))
.collect(HashMap::new, (m, k) -> m.put(k.getKey(), k.getValue()), HashMap::putAll);
db.updateByBatch(rows, writeOptions);
}
public void close() {
db.closeDB();
}
public void reset() {
db.resetDb();
}
}