Skip to content

Commit

Permalink
Converted unused map to simple list
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Polverini committed Oct 24, 2012
1 parent 46f9ee5 commit 57705e8
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import hu.netmind.bitcoin.BitcoinException;
import hu.netmind.bitcoin.Block;
import hu.netmind.bitcoin.ScriptFactory;
import hu.netmind.bitcoin.Transaction;
import hu.netmind.bitcoin.TransactionInput;
import hu.netmind.bitcoin.TransactionOutput;
import hu.netmind.bitcoin.block.BitcoinFactory;
import hu.netmind.bitcoin.block.BlockChainLink;
import hu.netmind.bitcoin.block.BlockChainLinkStorage;
import hu.netmind.bitcoin.block.BlockImpl;
import hu.netmind.bitcoin.block.Difficulty;
import hu.netmind.bitcoin.block.TransactionImpl;
import hu.netmind.bitcoin.block.TransactionInputImpl;
import hu.netmind.bitcoin.block.TransactionOutputImpl;
Expand All @@ -44,6 +42,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -71,8 +70,6 @@ public class JdbcChainLinkStorage implements BlockChainLinkStorage, NodeStorage
private boolean transactional = DEFAULT_TRANSACTIONAL;
private int idReserveSize = DEFAULT_RESERVE_SIZE;
private BitcoinFactory bitcoinFactory = null;
//private ScriptFactory scriptFactory = null;
//private boolean isTestnet = false;
private DataSource dataSource;
//
// Id generators for rows inserted in the DB tables
Expand Down Expand Up @@ -343,10 +340,10 @@ public BlockChainLink getGenesisLink()
{
try (Connection dbConnection = getDbConnection())
{
Map<byte[], SimplifiedStoredBlock> blocks = getBlocksAtHeight(dbConnection, BlockChainLink.ROOT_HEIGHT);
List<SimplifiedStoredBlock> blocks = getBlocksAtHeight(dbConnection, BlockChainLink.ROOT_HEIGHT);
if (blocks.isEmpty())
return null;
return getLink(blocks.keySet().iterator().next());
return getLink(blocks.get(0).hash);
} catch (SQLException ex)
{
throw new JdbcStorageException("getGenesisLinkEx: " + ex.getMessage(), ex);
Expand Down Expand Up @@ -598,14 +595,14 @@ public byte[] getHashOfMainChainAtHeight(long height)
{
try (Connection dbConnection = getDbConnection())
{
Map<byte[], SimplifiedStoredBlock> blocks = getBlocksAtHeight(dbConnection, height);
List<SimplifiedStoredBlock> blocks = getBlocksAtHeight(dbConnection, height);
if (blocks.isEmpty())
return null;
if (blocks.size() == 1)
return blocks.keySet().iterator().next();
return blocks.get(0).hash;
BlockChainLink top = getLastLink(); // TODO: Optimize that function, we can keep top cached
SimplifiedStoredBlock topBlock = new SimplifiedStoredBlock(top);
for (SimplifiedStoredBlock b : blocks.values())
for (SimplifiedStoredBlock b : blocks)
if (isReachable(dbConnection, b, topBlock))
return b.hash;

Expand Down Expand Up @@ -662,17 +659,17 @@ protected byte[] getClaimerHash(final Connection dbConnection, final BlockChainL
}
}

protected Map<byte[], SimplifiedStoredBlock> getBlocksAtHeight(final Connection dbConnection, long height) throws SQLException
protected List<SimplifiedStoredBlock> getBlocksAtHeight(final Connection dbConnection, long height) throws SQLException
{
Map<byte[], SimplifiedStoredBlock> blocks = new HashMap<>();
List<SimplifiedStoredBlock> blocks = new ArrayList<>();
try (PreparedStatement ps = dbConnection.prepareStatement(sqlGetSimplifiedBlockHeadersAtHeight))
{
ps.setLong(1, height);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
SimplifiedStoredBlock ssb = new SimplifiedStoredBlock(rs);
blocks.put(ssb.hash, ssb);
blocks.add(ssb);
}
}
return blocks;
Expand Down

0 comments on commit 57705e8

Please sign in to comment.