/// <summary> /// Copia masiva de un <see cref="IDataReader"/> sobre una tabla /// </summary> public long Process(IDbProvider provider, IDataReader reader, string table, Dictionary <string, string> mappings, int recordsPerBlock, TimeSpan?timeout = null) { long records = 0, blockRecords = 0; Dictionary <string, string> mappingsConverted = Convert(reader, mappings); string sql = GetInsertCommand(table, mappingsConverted); // Abre una transacción provider.BeginTransaction(); // Lee los registros e inserta while (reader.Read()) { // Ejecuta el comando de inserción provider.Execute(sql, GetParameters(reader, mappingsConverted), CommandType.Text, timeout); // Cierra la transacción blockRecords++; if (blockRecords % recordsPerBlock == 0) { // Confirma la transacción y abre una nueva provider.Commit(); provider.BeginTransaction(); // Reinicia el número de registros del bloque blockRecords = 0; } // Incrementa el número de registros records++; } // Cierra la transacción if (blockRecords != 0) { provider.Commit(); } // Devuelve el número de registros copiados return(records); }
/// <summary> /// Copia masiva de un <see cref="IDataReader"/> sobre una tabla de forma asíncrona /// </summary> public async Task <long> ProcessAsync(IDbProvider provider, IDataReader reader, string table, Dictionary <string, string> mappings, int recordsPerBlock, TimeSpan timeout, CancellationToken cancellationToken) { long records = 0, blockRecords = 0; Dictionary <string, string> mappingsConverted = Convert(reader, mappings); string sql = GetInsertCommand(table, mappingsConverted); // Abre una transacción provider.BeginTransaction(); // Lee los registros e inserta if (!cancellationToken.IsCancellationRequested) { while (await(reader as DbDataReader).ReadAsync(cancellationToken)) { // Ejecuta el comando de inserción await provider.ExecuteAsync(sql, GetParameters(reader, mappingsConverted), CommandType.Text, timeout, cancellationToken); // Cierra la transacción blockRecords++; if (blockRecords % recordsPerBlock == 0) { // Confirma la transacción y abre una nueva provider.Commit(); provider.BeginTransaction(); // Reinicia el número de registros del bloque blockRecords = 0; } // Incrementa el número de registros records++; } } // Cancela o confirma la transacción si es necesario if (cancellationToken.IsCancellationRequested) { provider.RollBack(); } else if (blockRecords != 0) { provider.Commit(); } // Devuelve el número de registros copiados return(records); }
private Keccak InitializeAccounts(IDictionary <string, TestAccount> alloc) { foreach (var account in alloc) { _stateProvider.CreateAccount(new Address(new Hex(account.Key)), account.Value.Balance.StartsWith("0x") ? new BigInteger(new Hex(account.Value.Balance)) : BigInteger.Parse(account.Value.Balance)); } _stateProvider.Commit(_specProvider.GenesisSpec); _dbProvider.Commit(_specProvider.GenesisSpec); return(_stateProvider.StateRoot); }
private Keccak InitializeAccounts(IDictionary <string, TestAccount> alloc) { foreach (var account in alloc) { UInt256.CreateFromBigEndian(out UInt256 allocation, Bytes.FromHexString(account.Value.Balance)); _stateProvider.CreateAccount(new Address(account.Key), account.Value.Balance.StartsWith("0x") ? allocation : UInt256.Parse(account.Value.Balance)); } _stateProvider.Commit(_specProvider.GenesisSpec); _dbProvider.Commit(_specProvider.GenesisSpec); return(_stateProvider.StateRoot); }
private Block ProcessOne(Block suggestedBlock, bool tryOnly) { IDb db = _dbProvider.GetOrCreateStateDb(); Block processedBlock = suggestedBlock; if (!suggestedBlock.IsGenesis) { processedBlock = ProcessNonGenesis(suggestedBlock, tryOnly); } db.StartBatch(); _stateProvider.CommitTree(); _storageProvider.CommitTrees(); db.CommitBatch(); _dbProvider.Commit(_specProvider.GetSpec(suggestedBlock.Number)); return(processedBlock); }
/// <summary> /// 提交 /// </summary> public void Commit() { provider.Commit(); }
public Block[] Process(Keccak branchStateRoot, Block[] suggestedBlocks, bool tryOnly) { if (suggestedBlocks.Length == 0) { return(Array.Empty <Block>()); } IDb db = _dbProvider.GetOrCreateStateDb(); int dbSnapshot = _dbProvider.TakeSnapshot(); Keccak snapshotStateRoot = _stateProvider.StateRoot; if (branchStateRoot != null && _stateProvider.StateRoot != branchStateRoot) { // discarding one of the branches _storageProvider.ClearCaches(); _stateProvider.Reset(); _stateProvider.StateRoot = branchStateRoot; } Block[] processedBlocks = new Block[suggestedBlocks.Length]; try { for (int i = 0; i < suggestedBlocks.Length; i++) { processedBlocks[i] = ProcessOne(suggestedBlocks[i], tryOnly); } if (tryOnly) { if (_logger.IsTrace) { _logger.Trace($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}"); } _dbProvider.Restore(dbSnapshot); _storageProvider.ClearCaches(); _stateProvider.Reset(); _stateProvider.StateRoot = snapshotStateRoot; if (_logger.IsTrace) { _logger.Trace($"REVERTED BLOCKS (JUST VALIDATED FOR MINING) - STATE ROOT {_stateProvider.StateRoot}"); } } else { db.StartBatch(); _dbProvider.Commit(_specProvider.GetSpec(suggestedBlocks[0].Number)); db.CommitBatch(); } return(processedBlocks); } catch (InvalidBlockException) // TODO: which exception to catch here? { if (_logger.IsTrace) { _logger.Trace($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}"); } _dbProvider.Restore(dbSnapshot); _storageProvider.ClearCaches(); _stateProvider.Reset(); _stateProvider.StateRoot = snapshotStateRoot; if (_logger.IsTrace) { _logger.Trace($"REVERTED BLOCKS - STATE ROOT {_stateProvider.StateRoot}"); } if (_logger.IsWarn) { _logger.Warn($"Invalid block"); } throw; } }