diff --git a/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java index 4726905a..74a71b52 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java @@ -49,6 +49,8 @@ public class SystemProperties { private static Boolean DEFAULT_VM_TRACE = false; private static String DEFAULT_VM_TRACE_DIR = "dmp"; private static int DEFAULT_PEER_LISTEN_PORT = 30303; + private static String DEFAULT_KEY_VALUE_DATA_SOURCE = "leveldb"; + /* Testing */ private static Boolean DEFAULT_VMTEST_LOAD_LOCAL = false; @@ -270,6 +272,12 @@ public class SystemProperties { if (prop.isEmpty()) return DEFAULT_PEER_LISTEN_PORT; return Integer.parseInt(prop.getProperty("peer.listen.port")); } + + + public String getKeyValueDataSource(){ + if (prop.isEmpty()) return DEFAULT_KEY_VALUE_DATA_SOURCE; + return prop.getProperty("keyvalue.datasource"); + } public void setListenPort(Integer port) { prop.setProperty("peer.listen.port", port.toString()); diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/RedisDataSource.java b/ethereumj-core/src/main/java/org/ethereum/datasource/RedisDataSource.java index b10f1707..71fe3155 100644 --- a/ethereumj-core/src/main/java/org/ethereum/datasource/RedisDataSource.java +++ b/ethereumj-core/src/main/java/org/ethereum/datasource/RedisDataSource.java @@ -8,6 +8,8 @@ import redis.clients.jedis.Pipeline; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import static org.ethereum.config.SystemProperties.CONFIG; + /** * * @author: Roman Mandeleil @@ -27,7 +29,9 @@ public class RedisDataSource implements KeyValueDataSource{ if (name == null) throw new NullPointerException("no name set to the db"); this.jedis = new Jedis("localhost"); // todo: config.redisHost, config.redisPort - this.jedis.flushAll(); // todo: if config.reset so reset. + + if (CONFIG.databaseReset()) + this.jedis.flushAll(); } @Override diff --git a/ethereumj-core/src/main/java/org/ethereum/db/DatabaseImpl.java b/ethereumj-core/src/main/java/org/ethereum/db/DatabaseImpl.java index f48bcc4d..deec4ea0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/DatabaseImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/DatabaseImpl.java @@ -1,7 +1,9 @@ package org.ethereum.db; +import org.ethereum.config.SystemProperties; import org.ethereum.datasource.KeyValueDataSource; import org.ethereum.datasource.LevelDbDataSource; +import org.ethereum.datasource.RedisDataSource; import org.ethereum.util.ByteUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,6 +13,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import static org.ethereum.config.SystemProperties.CONFIG; + /** * Generic interface for Ethereum database * @@ -26,10 +30,23 @@ public class DatabaseImpl implements Database { private KeyValueDataSource dataSource; public DatabaseImpl(String name) { - // Initialize Database - dataSource = new LevelDbDataSource(); - dataSource.setName(name); - dataSource.init(); + + if (CONFIG.getKeyValueDataSource().equals("redis") ){ + dataSource = new RedisDataSource(); + dataSource.setName(name); + dataSource.init(); + return; + } + + if (CONFIG.getKeyValueDataSource().equals("leveldb") ){ + dataSource = new LevelDbDataSource(); + dataSource.setName(name); + dataSource.init(); + return; + } + + logger.info("Key/Value datasource was not configured."); + System.exit(-1); } diff --git a/ethereumj-core/src/main/resources/system.properties b/ethereumj-core/src/main/resources/system.properties index fbd13efb..997f98f4 100644 --- a/ethereumj-core/src/main/resources/system.properties +++ b/ethereumj-core/src/main/resources/system.properties @@ -151,3 +151,5 @@ root.hash.start = -1 # The protocols supported by peer peer.capabilities = eth, shh +# Key value data source values: [leveldb/redis] +keyvalue.datasource = "leveldb" \ No newline at end of file diff --git a/ethereumj-core/src/test/resources/system.properties b/ethereumj-core/src/test/resources/system.properties index 7275d231..221600e1 100644 --- a/ethereumj-core/src/test/resources/system.properties +++ b/ethereumj-core/src/test/resources/system.properties @@ -139,4 +139,7 @@ hello.phrase = Dev root.hash.start = -1 # if set true, json tests will be loaded from local repository -GitHubTests.VMTest.loadLocal = false \ No newline at end of file +GitHubTests.VMTest.loadLocal = false + +# Key value data source values: [leveldb/redis] +keyvalue.datasource = "leveldb" \ No newline at end of file