private void FillHandsPlayersTable() { MigrationUtils.SetStatusMessage("Preparing data for migration"); using (var session = ModelEntities.OpenStatelessSession()) { LogProvider.Log.Info("Preparing data to insert."); var playersId = session.Query <Players>() .Select(x => x.PlayerId).ToArray(); var handsQuery = session.Query <Handhistory>() .Select(x => new { x.HandhistoryId, x.Gamenumber, x.PokersiteId }); var hands = new Dictionary <HandHistoryKey, int>(); foreach (var hand in handsQuery) { var handKey = new HandHistoryKey(hand.Gamenumber, hand.PokersiteId); if (!hands.ContainsKey(handKey)) { hands.Add(handKey, hand.HandhistoryId); } } var statisticFiles = new HashSet <string>(); foreach (var playerId in playersId) { var playerStatsFiles = playerStatisticRepository.GetPlayerFiles(playerId); statisticFiles.AddRange(playerStatsFiles); } var conn = session.Connection as SQLiteConnection; if (fastModeAllowedHands >= hands.Count) { InsertInFastMode(statisticFiles, hands, conn); return; } InsertInLowMemoryMode(statisticFiles, hands, conn); } }
private void InsertInFastMode(HashSet <string> statisticFiles, Dictionary <HandHistoryKey, int> hands, SQLiteConnection conn) { LogProvider.Log.Info("Data will be proccessed in fast mode."); var handsPlayers = new ConcurrentStack <HandPlayer>(); var counter = 0; LogProvider.Log.Info($"Begin the reading of statistic: {statisticFiles.Count} files to read."); Parallel.ForEach(statisticFiles, file => { playerStatisticRepository.GetPlayerStatisticFromFile(file).Where(stat => !stat.IsTourney).ForEach(stat => { var handKey = new HandHistoryKey(stat.GameNumber, (short)stat.PokersiteId); if (hands.ContainsKey(handKey)) { var handPlayer = new HandPlayer { PlayerId = stat.PlayerId, HandId = hands[handKey], NetWon = Utils.ConvertToCents(stat.NetWon) }; handsPlayers.Push(handPlayer); if (counter % 100 == 0) { MigrationUtils.SetStatusMessage($"Reading statistic {counter}/{statisticFiles.Count}"); } } }); Interlocked.Increment(ref counter); }); counter = 0; LogProvider.Log.Info($"Begin data inserting : {handsPlayers.Count} rows to insert."); using (var cmd = new SQLiteCommand("insert into HandsPlayers (HandId, PlayerId, NetWon) values (@HandId, @PlayerId, @NetWon)", conn)) { cmd.Parameters.Add("@HandId", DbType.Int32); cmd.Parameters.Add("@PlayerId", DbType.Int32); cmd.Parameters.Add("@NetWon", DbType.Int64); cmd.Prepare(); using (var transaction = conn.BeginTransaction()) { foreach (var hand in handsPlayers) { if (counter % 25000 == 0) { MigrationUtils.SetStatusMessage($"Inserting data {counter}/{handsPlayers.Count}"); } cmd.Parameters[0].Value = hand.HandId; cmd.Parameters[1].Value = hand.PlayerId; cmd.Parameters[2].Value = hand.NetWon; cmd.ExecuteNonQuery(); counter++; } transaction.Commit(); } } }
private void InsertInLowMemoryMode(HashSet <string> statisticFiles, Dictionary <HandHistoryKey, int> hands, SQLiteConnection conn) { LogProvider.Log.Info("Data will be proccessed in low memory mode."); var insertedRows = 0; LogProvider.Log.Info($"Begin data processing: {statisticFiles.Count} files for processing."); using (var transaction = conn.BeginTransaction()) { using (var cmd = new SQLiteCommand("insert into HandsPlayers (HandId, PlayerId, NetWon) values (@HandId, @PlayerId, @NetWon)", conn)) { var counter = 0; Parallel.ForEach(statisticFiles, file => { playerStatisticRepository.GetPlayerStatisticFromFile(file).Where(stat => !stat.IsTourney).ForEach(stat => { var handKey = new HandHistoryKey(stat.GameNumber, (short)stat.PokersiteId); if (hands.ContainsKey(handKey)) { lock (locker) { var handPlayer = new HandPlayer { PlayerId = stat.PlayerId, HandId = hands[handKey], NetWon = Utils.ConvertToCents(stat.NetWon) }; cmd.Parameters.Add("@HandId", DbType.Int32); cmd.Parameters.Add("@PlayerId", DbType.Int32); cmd.Parameters.Add("@NetWon", DbType.Int64); cmd.Prepare(); if (counter % 25 == 0) { MigrationUtils.SetStatusMessage($"Processing statistic {counter}/{statisticFiles.Count}"); } cmd.Parameters[0].Value = handPlayer.HandId; cmd.Parameters[1].Value = handPlayer.PlayerId; cmd.Parameters[2].Value = handPlayer.NetWon; cmd.ExecuteNonQuery(); insertedRows++; } } }); Interlocked.Increment(ref counter); }); } LogProvider.Log.Info($"Inserted data: {insertedRows} rows"); transaction.Commit(); } }