Extract key/val data source init to the DefaultConfig

This commit is contained in:
Roman Mandeleil 2015-01-27 02:24:34 +02:00
parent 86e7643f63
commit 8959f14c44
10 changed files with 117 additions and 57 deletions

View File

@ -31,6 +31,13 @@ public class LevelDbDataSource implements KeyValueDataSource {
String name;
private DB db;
public LevelDbDataSource() {
}
public LevelDbDataSource(String name) {
this.name = name;
}
@Override
public void init() {

View File

@ -23,8 +23,14 @@ public class RedisDataSource implements KeyValueDataSource{
int index;
Jedis jedis;
public RedisDataSource() {
}
public RedisDataSource(String name) {
this.name = name;
}
@Override
public void init() {

View File

@ -1,21 +1,20 @@
package org.ethereum.db;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.datasource.RedisDataSource;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
* Generic interface for Ethereum database
*
@ -28,32 +27,24 @@ public class DatabaseImpl implements Database {
private static final Logger logger = LoggerFactory.getLogger("db");
private String name;
private KeyValueDataSource dataSource;
private KeyValueDataSource keyValueDataSource;
public DatabaseImpl(KeyValueDataSource keyValueDataSource) {
this.keyValueDataSource = keyValueDataSource;
}
public DatabaseImpl(String name) {
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);
keyValueDataSource.setName(name);
keyValueDataSource.init();
}
@Override
public byte[] get(byte[] key) {
return dataSource.get(key);
return keyValueDataSource.get(key);
}
@Override
@ -63,7 +54,7 @@ public class DatabaseImpl implements Database {
logger.debug("put: key: [{}], value: [{}]",
Hex.toHexString(key),
Hex.toHexString(value));
dataSource.put(key, value);
keyValueDataSource.put(key, value);
}
@Override
@ -71,23 +62,23 @@ public class DatabaseImpl implements Database {
if (logger.isDebugEnabled())
logger.debug("delete: key: [{}]");
dataSource.delete(key);
keyValueDataSource.delete(key);
}
public KeyValueDataSource getDb() {
return this.dataSource;
return this.keyValueDataSource;
}
@Override
public void close() {
dataSource.close();
keyValueDataSource.close();
}
public List<ByteArrayWrapper> dumpKeys() {
ArrayList<ByteArrayWrapper> keys = new ArrayList<>();
for (byte[] key : dataSource.keys()) {
for (byte[] key : keyValueDataSource.keys()) {
keys.add(ByteUtil.wrap(key));
}
Collections.sort(keys);

View File

@ -2,6 +2,7 @@ package org.ethereum.db;
import org.ethereum.core.AccountState;
import org.ethereum.core.Block;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.facade.Repository;
import org.ethereum.json.EtherObjectMapper;
import org.ethereum.json.JSONHelper;
@ -41,8 +42,8 @@ import static org.ethereum.util.ByteUtil.wrap;
*/
public class RepositoryImpl implements Repository {
final static String DETAILS_DB = "details";
final static String STATE_DB = "state";
public final static String DETAILS_DB = "details";
public final static String STATE_DB = "state";
private static final Logger logger = LoggerFactory.getLogger("repository");
@ -51,10 +52,28 @@ public class RepositoryImpl implements Repository {
private DatabaseImpl detailsDB = null;
private DatabaseImpl stateDB = null;
KeyValueDataSource detailsDS = null;
KeyValueDataSource stateDS = null;
public RepositoryImpl() {
this(DETAILS_DB, STATE_DB);
}
public RepositoryImpl(KeyValueDataSource detailsDS, KeyValueDataSource stateDS) {
detailsDS.setName(DETAILS_DB);
detailsDS.init();
this.detailsDS = detailsDS;
stateDS.setName(STATE_DB);
stateDS.init();
this.stateDS = stateDS;
detailsDB = new DatabaseImpl(detailsDS);
stateDB = new DatabaseImpl(stateDS);
worldState = new TrieImpl(stateDB.getDb());
}
public RepositoryImpl(String detailsDbName, String stateDbName) {
detailsDB = new DatabaseImpl(detailsDbName);
stateDB = new DatabaseImpl(stateDbName);
@ -65,8 +84,12 @@ public class RepositoryImpl implements Repository {
@Override
public void reset() {
close();
detailsDB = new DatabaseImpl(DETAILS_DB);
stateDB = new DatabaseImpl(STATE_DB);
detailsDS.init();
detailsDB = new DatabaseImpl(detailsDS);
stateDS.init();
stateDB = new DatabaseImpl(stateDS);
worldState = new TrieImpl(stateDB.getDb());
}

View File

@ -1,6 +1,9 @@
package org.ethereum.facade;
import org.ethereum.config.SystemProperties;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.datasource.RedisDataSource;
import org.ethereum.db.RepositoryImpl;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
@ -9,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
@ -17,6 +21,8 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.sql.SQLException;
import java.util.Properties;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
*
* @author: Roman Mandeleil
@ -34,8 +40,20 @@ public class DefaultConfig {
@Bean
Repository repository(){
return new RepositoryImpl();
return new RepositoryImpl(keyValueDataSource(), keyValueDataSource());
}
@Bean
@Scope("prototype")
public KeyValueDataSource keyValueDataSource(){
if (CONFIG.getKeyValueDataSource().equals("redis")) {
return new RedisDataSource();
}
return new LevelDbDataSource();
}
@Bean
public SessionFactory sessionFactory() throws SQLException {

View File

@ -18,17 +18,18 @@ import org.springframework.stereotype.Component;
public class EthereumFactory {
private static final Logger logger = LoggerFactory.getLogger("general");
public static ApplicationContext context = null;
public static Ethereum createEthereum() {
return createEthereum(DefaultConfig.class);
}
public static Ethereum createEthereum(Class clazz) {
logger.info("capability eth version: [{}]", EthHandler.VERSION);
logger.info("capability shh version: [{}]", ShhHandler.VERSION);
ApplicationContext context = new AnnotationConfigApplicationContext(clazz);
context = new AnnotationConfigApplicationContext(clazz);
return context.getBean(Ethereum.class);
}

View File

@ -2,6 +2,7 @@ package org.ethereum.vm;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.BlockStore;
import org.ethereum.db.BlockStoreDummy;
import org.ethereum.db.RepositoryImpl;
@ -30,7 +31,9 @@ public class ProgramInvokeMockImpl implements ProgramInvoke {
}
public ProgramInvokeMockImpl() {
this.repository = new RepositoryImpl("detailsMoc", "stateMoc");
this.repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
this.repository.createAccount(ownerAddress);
this.repository.createAccount(contractAddress);

View File

@ -3,6 +3,7 @@ package test.ethereum.db;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.RepositoryImpl;
import org.ethereum.facade.Repository;
import org.ethereum.vm.DataWord;
@ -30,7 +31,7 @@ public class RepositoryTest {
public void test1() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -49,7 +50,7 @@ public class RepositoryTest {
public void test2() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -68,7 +69,7 @@ public class RepositoryTest {
public void test3() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -89,7 +90,7 @@ public class RepositoryTest {
public void test4() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -114,7 +115,7 @@ public class RepositoryTest {
public void test5() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -145,7 +146,7 @@ public class RepositoryTest {
public void test6() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -179,7 +180,7 @@ public class RepositoryTest {
public void test7() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -204,7 +205,7 @@ public class RepositoryTest {
public void test8() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -228,7 +229,7 @@ public class RepositoryTest {
public void test9() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -258,7 +259,7 @@ public class RepositoryTest {
public void test10() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -289,7 +290,7 @@ public class RepositoryTest {
public void test11() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -317,7 +318,7 @@ public class RepositoryTest {
public void test12() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
@ -344,7 +345,7 @@ public class RepositoryTest {
public void test13() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
Repository track = repository.startTracking();
for (String address : Genesis.getPremine()) {
@ -363,7 +364,7 @@ public class RepositoryTest {
public void test14() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -401,7 +402,7 @@ public class RepositoryTest {
public void test15() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -438,7 +439,7 @@ public class RepositoryTest {
public void test16() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
@ -501,7 +502,7 @@ public class RepositoryTest {
public void test17() {
SystemProperties.CONFIG.setDataBaseDir("test_db/" + RepositoryTest.class);
Repository repository = new RepositoryImpl();
Repository repository = new RepositoryImpl(new LevelDbDataSource(), new LevelDbDataSource());
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");

View File

@ -1,5 +1,7 @@
package test.ethereum.db;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.DatabaseImpl;
import org.ethereum.db.TrackDatabase;
@ -25,7 +27,10 @@ public class TrackDatabaseTest {
@Test
public void test1() {
DatabaseImpl db1 = new DatabaseImpl("temp");
KeyValueDataSource keyValueDataSource = new LevelDbDataSource("temp");
keyValueDataSource.init();
DatabaseImpl db1 = new DatabaseImpl(keyValueDataSource);
TrackDatabase trackDatabase1 = new TrackDatabase(db1);
trackDatabase1.put(Hex.decode("abcdef"), Hex.decode("abcdef"));

View File

@ -1,5 +1,7 @@
package test.ethereum.trie;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import test.ethereum.db.MockDB;
import org.ethereum.core.AccountState;
@ -863,7 +865,10 @@ public class TrieTest {
JSONParser parser = new JSONParser();
JSONArray dbDumpJSONArray = (JSONArray) parser.parse(testSrc);
DatabaseImpl db = new DatabaseImpl("testState");
KeyValueDataSource keyValueDataSource = new LevelDbDataSource("testState");
keyValueDataSource.init();
DatabaseImpl db = new DatabaseImpl(keyValueDataSource);
for (Object aDbDumpJSONArray : dbDumpJSONArray) {