package org.ethereum.config;

import org.ethereum.datasource.HashMapDB;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.BlockStore;
import org.ethereum.db.IndexedBlockStore;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.ethereum.db.IndexedBlockStore.BLOCK_INFO_SERIALIZER;

/**
 *
 * @author Roman Mandeleil
 * Created on: 27/01/2015 01:05
 */
@Configuration
@Import(CommonConfig.class)
public class DefaultConfig {

    @Autowired
    CommonConfig commonConfig;

    @Autowired
    SystemProperties config;

    @Bean
    public BlockStore blockStore(){

        String database = config.databaseDir();

        String blocksIndexFile = database + "/blocks/index";
        File dbFile = new File(blocksIndexFile);
        if (!dbFile.getParentFile().exists()) dbFile.getParentFile().mkdirs();

        DB indexDB = DBMaker.fileDB(dbFile)
                .closeOnJvmShutdown()
                .make();

        Map<Long, List<IndexedBlockStore.BlockInfo>> indexMap = indexDB.hashMapCreate("index")
                .keySerializer(Serializer.LONG)
                .valueSerializer(BLOCK_INFO_SERIALIZER)
                .counterEnable()
                .makeOrGet();

        KeyValueDataSource blocksDB = levelDbDataSource("blocks");
        blocksDB.init();


        IndexedBlockStore cache = new IndexedBlockStore();
        cache.init(new HashMap<Long, List<IndexedBlockStore.BlockInfo>>(), new HashMapDB(), null, null);

        IndexedBlockStore indexedBlockStore = new IndexedBlockStore();
        indexedBlockStore.init(indexMap, blocksDB, cache, indexDB);


        return indexedBlockStore;
    }

    @Bean
    LevelDbDataSource levelDbDataSource() {
        return new LevelDbDataSource();
    }
    @Bean
    LevelDbDataSource levelDbDataSource(String name) {
        return new LevelDbDataSource(name);
    }
}

@@ -382,16 +382,71 @@
 owired;%0A
+import org.springframework.context.ApplicationContext;%0A
 import o
@@ -913,16 +913,63 @@
 nfig %7B%0A%0A
+    @Autowired%0A    ApplicationContext appCtx;%0A%0A
     @Aut
@@ -1747,17 +1747,32 @@
 cksDB = 
-l
+appCtx.getBean(L
 evelDbDa
@@ -1779,17 +1779,24 @@
 taSource
-(
+.class, 
 %22blocks%22

Fix Spring bean creation