InMemoryBlockStore implements BlockStore { @Override public void flush(){ long t_ = System.nanoTime(); Session s = sessionFactory.openSession(); s.beginTransaction(); s.createQuery("delete from BlockVO").executeUpdate(); s.getTransaction().commit(); s.beginTransaction(); int lastIndex = blocks.size() - 1; for (int i = 0; i < (blocks.size() > 1000 ? 1000 : blocks.size()); ++i){ Block block = blocks.get(lastIndex - i); BlockVO blockVO = new BlockVO(block.getNumber(), block.getHash(), block.getEncoded(), block.getCumulativeDifficulty()); s.save(blockVO); } s.getTransaction().commit(); Block block = getBestBlock(); blocks.clear(); hashIndex.clear(); numberIndex.clear(); saveBlock(block, null); long t__ = System.nanoTime(); logger.info("Flush block store in: {} ms", ((float)(t__ - t_) / 1_000_000)); totalDifficulty = (BigInteger) s.createQuery("select sum(cumulativeDifficulty) from BlockVO").uniqueResult(); s.close(); } InMemoryBlockStore(); @Override byte[] getBlockHashByNumber(long blockNumber); @Override Block getBlockByNumber(long blockNumber); @Override Block getBlockByHash(byte[] hash); @Override List<byte[]> getListHashesEndWith(byte[] hash, long qty); @Override void saveBlock(Block block, List<TransactionReceipt> receipts); @Override BigInteger getTotalDifficulty(); @Override Block getBestBlock(); byte[] dbGetBlockHashByNumber(long blockNumber); Block dbGetBlockByNumber(long blockNumber); Block dbGetBlockByHash(byte[] hash); @Override void flush(); void load(); @Override void setSessionFactory(SessionFactory sessionFactory);  }
@Test public void testFlush(){ BlockStore blockStore = new InMemoryBlockStore(); blockStore.setSessionFactory(sessionFactory()); for( Block block : blocks ){ blockStore.saveBlock(block, null); } blockStore.flush(); }