virtual protected void OnInsertBlocks(List <Block> blocks) { this.logger.LogTrace("({0}.{1}:{2})", nameof(blocks), nameof(blocks.Count), blocks?.Count); var transactions = new List <(Transaction, Block)>(); var byteListComparer = new ByteListComparer(); var blockDict = new Dictionary <uint256, Block>(); // Gather blocks foreach (var block in blocks) { var blockId = block.GetHash(); blockDict[blockId] = block; } // Sort blocks. Be consistent in always converting our keys to byte arrays using the ToBytes method. var blockList = blockDict.ToList(); blockList.Sort((pair1, pair2) => byteListComparer.Compare(pair1.Key.ToBytes(), pair2.Key.ToBytes())); // Index blocks foreach (KeyValuePair <uint256, Block> kv in blockList) { var blockId = kv.Key; var block = kv.Value; // if the block is already in store don't write it again var item = this.session.Transaction.Select <byte[], Block>("Block", blockId.ToBytes()); if (!item.Exists) { this.PerformanceCounter.AddRepositoryMissCount(1); this.PerformanceCounter.AddRepositoryInsertCount(1); this.session.Transaction.Insert <byte[], Block>("Block", blockId.ToBytes(), block); if (this.TxIndex) { foreach (var transaction in block.Transactions) { transactions.Add((transaction, block)); } } } else { this.PerformanceCounter.AddRepositoryHitCount(1); } } if (this.TxIndex) { this.OnInsertTransactions(transactions); } this.logger.LogTrace("(-)"); }
protected virtual void OnInsertBlocks(List <Block> blocks) { var transactions = new List <(Transaction, Block)>(); var byteListComparer = new ByteListComparer(); var blockDict = new Dictionary <uint256, Block>(); // Gather blocks. foreach (Block block in blocks) { uint256 blockId = block.GetHash(); blockDict[blockId] = block; } // Sort blocks. Be consistent in always converting our keys to byte arrays using the ToBytes method. List <KeyValuePair <uint256, Block> > blockList = blockDict.ToList(); blockList.Sort((pair1, pair2) => byteListComparer.Compare(pair1.Key.ToBytes(), pair2.Key.ToBytes())); using (var batch = new WriteBatch()) { // Index blocks. foreach (KeyValuePair <uint256, Block> kv in blockList) { uint256 blockId = kv.Key; Block block = kv.Value; // If the block is already in store don't write it again. byte[] blockRow = this.leveldb.Get(BlockTableName, blockId.ToBytes()); if (blockRow == null) { batch.Put(BlockTableName, blockId.ToBytes(), this.dBreezeSerializer.Serialize(block)); if (this.TxIndex) { foreach (Transaction transaction in block.Transactions) { transactions.Add((transaction, block)); } } } } this.leveldb.Write(batch, new WriteOptions() { Sync = true }); } if (this.TxIndex) { this.OnInsertTransactions(transactions); } }
protected virtual void OnInsertBlocks(DBreeze.Transactions.Transaction dbreezeTransaction, List <Block> blocks) { this.logger.LogTrace("({0}.{1}:{2})", nameof(blocks), nameof(blocks.Count), blocks?.Count); var transactions = new List <(Transaction, Block)>(); var byteListComparer = new ByteListComparer(); var blockDict = new Dictionary <uint256, Block>(); // Gather blocks. foreach (Block block in blocks) { uint256 blockId = block.GetHash(); blockDict[blockId] = block; } // Sort blocks. Be consistent in always converting our keys to byte arrays using the ToBytes method. List <KeyValuePair <uint256, Block> > blockList = blockDict.ToList(); blockList.Sort((pair1, pair2) => byteListComparer.Compare(pair1.Key.ToBytes(), pair2.Key.ToBytes())); // Index blocks. foreach (KeyValuePair <uint256, Block> kv in blockList) { uint256 blockId = kv.Key; Block block = kv.Value; // If the block is already in store don't write it again. Row <byte[], Block> blockRow = dbreezeTransaction.Select <byte[], Block>(BlockTableName, blockId.ToBytes()); if (!blockRow.Exists) { dbreezeTransaction.Insert <byte[], Block>(BlockTableName, blockId.ToBytes(), block); if (this.TxIndex) { foreach (Transaction transaction in block.Transactions) { transactions.Add((transaction, block)); } } } } if (this.TxIndex) { this.OnInsertTransactions(dbreezeTransaction, transactions); } this.logger.LogTrace("(-)"); }
protected virtual void OnInsertBlocks(List <Block> blocks) { var transactions = new List <(Transaction, Block)>(); var byteListComparer = new ByteListComparer(); var blockDict = new Dictionary <uint256, Block>(); // Gather blocks. foreach (Block block in blocks) { uint256 blockId = block.GetHash(); blockDict[blockId] = block; } // Sort blocks. Be consistent in always converting our keys to byte arrays using the ToBytes method. List <KeyValuePair <uint256, Block> > blockList = blockDict.ToList(); blockList.Sort((pair1, pair2) => byteListComparer.Compare(pair1.Key.ToBytes(), pair2.Key.ToBytes())); using (var batch = new WriteBatch()) { // Index blocks. foreach (KeyValuePair <uint256, Block> kv in blockList) { uint256 blockId = kv.Key; Block block = kv.Value; batch.Put(DBH.Key(BlockTableName, blockId.ToBytes()), this.dataStoreSerializer.Serialize(block)); if (this.TxIndex) { foreach (Transaction transaction in block.Transactions) { transactions.Add((transaction, block)); } } } this.rocksdb.Write(batch); } if (this.TxIndex) { this.OnInsertTransactions(transactions); } }