Skip to content

Commit

Permalink
feat(db/rocksdb): update rocksdbjni
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Jun 3, 2024
1 parent d4b1cc4 commit 73b6edf
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.util.stream.Collectors;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.AbstractComparator;
import org.rocksdb.BlockBasedTableConfig;
import org.rocksdb.BloomFilter;
import org.rocksdb.Checkpoint;
import org.rocksdb.DirectComparator;
import org.rocksdb.InfoLogLevel;
import org.rocksdb.Logger;
import org.rocksdb.Options;
Expand Down Expand Up @@ -60,11 +60,11 @@ public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
private static final String KEY_ENGINE = "ENGINE";
private static final String ROCKSDB = "ROCKSDB";
private DirectComparator comparator;
private AbstractComparator comparator;
private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB);

public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings,
DirectComparator comparator) {
AbstractComparator comparator) {
this.dataBaseName = name;
this.parentPath = parentPath;
this.comparator = comparator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.tron.common.utils;

import org.rocksdb.AbstractComparator;
import org.rocksdb.ComparatorOptions;
import org.rocksdb.DirectSlice;
import org.rocksdb.util.DirectBytewiseComparator;
import org.tron.core.capsule.utils.MarketUtils;

public class MarketOrderPriceComparatorForRockDB extends DirectBytewiseComparator {
import java.nio.ByteBuffer;

public class MarketOrderPriceComparatorForRockDB extends AbstractComparator {

public MarketOrderPriceComparatorForRockDB(final ComparatorOptions copt) {
super(copt);
Expand All @@ -17,21 +19,16 @@ public String name() {
}

@Override
public int compare(final DirectSlice a, final DirectSlice b) {
public int compare(final ByteBuffer a, final ByteBuffer b) {
return MarketUtils.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b));
}

/**
* DirectSlice.data().array will throw UnsupportedOperationException.
* */
public byte[] convertDataToBytes(DirectSlice directSlice) {
int capacity = directSlice.data().capacity();
byte[] bytes = new byte[capacity];

for (int i = 0; i < capacity; i++) {
bytes[i] = directSlice.get(i);
}

public byte[] convertDataToBytes(ByteBuffer buf) {
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
return bytes;
}

Expand Down
4 changes: 2 additions & 2 deletions chainbase/src/main/java/org/tron/core/db/TronDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.iq80.leveldb.WriteOptions;
import org.rocksdb.DirectComparator;
import org.rocksdb.AbstractComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.storage.WriteOptionsWrapper;
Expand Down Expand Up @@ -70,7 +70,7 @@ protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) {
return StorageUtils.getOptionsByDbName(dbName);
}

protected DirectComparator getDirectComparator() {
protected AbstractComparator getDirectComparator() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.iq80.leveldb.WriteOptions;
import org.rocksdb.DirectComparator;
import org.rocksdb.AbstractComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
Expand Down Expand Up @@ -81,7 +81,7 @@ protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) {
return StorageUtils.getOptionsByDbName(dbName);
}

protected DirectComparator getDirectComparator() {
protected AbstractComparator getDirectComparator() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.Collections;
import java.util.List;
import org.iq80.leveldb.Options;
import org.rocksdb.AbstractComparator;
import org.rocksdb.ComparatorOptions;
import org.rocksdb.DirectComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -35,7 +35,7 @@ protected Options getOptionsByDbNameForLevelDB(String dbName) {

//todo: to test later
@Override
protected DirectComparator getDirectComparator() {
protected AbstractComparator getDirectComparator() {
ComparatorOptions comparatorOptions = new ComparatorOptions();
return new MarketOrderPriceComparatorForRockDB(comparatorOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
compile group: 'com.beust', name: 'jcommander', version: '1.72'
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile group: leveldbGroup, name: leveldbName, version: leveldbVersion
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10'
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '7.7.3'
// https://mvnrepository.com/artifact/org.quartz-scheduler/quartz
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.2'
compile group: 'io.prometheus', name: 'simpleclient', version: '0.15.0'
Expand Down
4 changes: 1 addition & 3 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ DB convert provides a helper which can convert LevelDB data to RocksDB data, par

- `<src>`: Input path for leveldb, default: output-directory/database.
- `<dest>`: Output path for rocksdb, default: output-directory-dst/database.
- `--safe`: In safe mode, read data from leveldb then put into rocksdb, it's a very time-consuming procedure. If not, just change engine.properties from leveldb to rocksdb, rocksdb
is compatible with leveldb for the current version. This may not be the case in the future, default: false.
- `-h | --help`: Provide the help info.

### Examples:

```shell script
# full command
java -jar Toolkit.jar db convert [-h] [--safe] <src> <dest>
java -jar Toolkit.jar db convert [-h] <src> <dest>
# examples
java -jar Toolkit.jar db convert output-directory/database /tmp/database
```
Expand Down
2 changes: 1 addition & 1 deletion plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile group: 'me.tongfei', name: 'progressbar', version: '0.9.3'
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69'
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10'
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '7.7.3'
compile 'io.github.tronprotocol:leveldbjni-all:1.18.3'
compile 'io.github.tronprotocol:leveldb:1.18.3'
compile project(":protocol")
Expand Down
8 changes: 1 addition & 7 deletions plugins/src/main/java/org/tron/plugins/DbConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ public class DbConvert implements Callable<Integer> {
description = "Output path for rocksdb. Default: ${DEFAULT-VALUE}")
private File dest;

@CommandLine.Option(names = {"--safe"},
description = "In safe mode, read data from leveldb then put rocksdb."
+ "If not, just change engine.properties from leveldb to rocksdb,"
+ "rocksdb is compatible with leveldb for current version."
+ "This may not be the case in the future."
+ "Default: ${DEFAULT-VALUE}")
private boolean safe;
private final boolean safe = true;

@CommandLine.Option(names = {"-h", "--help"})
private boolean help;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.tron.plugins.comparator;

import java.nio.ByteBuffer;
import org.rocksdb.AbstractComparator;
import org.rocksdb.ComparatorOptions;
import org.rocksdb.DirectSlice;
import org.rocksdb.util.DirectBytewiseComparator;
import org.tron.plugins.utils.MarketUtils;

public class MarketOrderPriceComparatorForRockDB extends DirectBytewiseComparator {
public class MarketOrderPriceComparatorForRockDB extends AbstractComparator {

public MarketOrderPriceComparatorForRockDB(final ComparatorOptions copt) {
super(copt);
Expand All @@ -17,21 +17,16 @@ public String name() {
}

@Override
public int compare(final DirectSlice a, final DirectSlice b) {
public int compare(final ByteBuffer a, final ByteBuffer b) {
return MarketUtils.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b));
}

/**
* DirectSlice.data().array will throw UnsupportedOperationException.
* */
public byte[] convertDataToBytes(DirectSlice directSlice) {
int capacity = directSlice.data().capacity();
byte[] bytes = new byte[capacity];

for (int i = 0; i < capacity; i++) {
bytes[i] = directSlice.get(i);
}

public byte[] convertDataToBytes(ByteBuffer buf) {
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
return bytes;
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/src/test/java/org/tron/plugins/DbConvertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void testRun() throws IOException {
@Test
public void testRunWithSafe() throws IOException {
String[] args = new String[] { "db", "convert", INPUT_DIRECTORY,
temporaryFolder.newFolder().toString(),"--safe" };
temporaryFolder.newFolder().toString()};
Assert.assertEquals(0, cli.execute(args));
}

Expand Down

0 comments on commit 73b6edf

Please sign in to comment.