public virtual IEnumerable <Playerstatistic> GetPlayerStatisticFromFiles(IEnumerable <string> files) { if (files == null) { throw new ArgumentNullException(nameof(files)); } foreach (var file in files) { var rwLock = GetLock(file); rwLock.EnterUpgradeableReadLock(); try { RestoreBackupFile(file, rwLock); using (var sr = new StreamReaderWrapper(file)) { string line = null; while (sr != null && ((line = sr.ReadLine()) != null)) { Playerstatistic stat = null; try { if (string.IsNullOrWhiteSpace(line)) { LogProvider.Log.Warn(this, $"Empty line in {file}"); } /* replace '-' and '_' characters in order to convert back from Modified Base64 (https://en.wikipedia.org/wiki/Base64#Implementations_and_history) */ byte[] byteAfter64 = Convert.FromBase64String(line.Replace('-', '+').Replace('_', '/').Trim()); using (var ms = new MemoryStream(byteAfter64)) { stat = Serializer.Deserialize <Playerstatistic>(ms); } } catch (Exception ex) { LogProvider.Log.Error($@"Could not process the file: {file}{Environment.NewLine}Error at line: {line}{Environment.NewLine}", ex); } if (stat != null) { yield return(stat); } } } } finally { rwLock.ExitUpgradeableReadLock(); } } }
private void DeletePlayerStatisticFromFile(string file, HashSet <long> handNumbers) { if (!File.Exists(file)) { return; } var rwLock = GetLock(file); rwLock.EnterWriteLock(); try { CreateBackupFile(file); var lines = new List <string>(); using (var sr = new StreamReaderWrapper(file)) { string line = null; while (sr != null && ((line = sr.ReadLine()) != null)) { if (!TryParsePlayerStatistic(line, file, out Playerstatistic stat)) { continue; } if (!handNumbers.Contains(stat.GameNumber)) { lines.Add(line); } } } File.WriteAllLines(file, lines); DeleteBackupFile(file); } catch { LogProvider.Log.Error(this, $"Could not delete player statistic from file '{file}'"); throw; } finally { rwLock.ExitWriteLock(); } }
protected virtual IEnumerable <Playerstatistic> GetAllPlayerStatisticFromFiles(IEnumerable <string> files, short?pokerSite) { if (files == null) { throw new ArgumentNullException(nameof(files)); } var maxThreads = Environment.ProcessorCount + 1; var runningTasks = new List <Task>(); var fileQueue = new Queue <string>(files); var stats = new BlockingCollection <Playerstatistic>(); while (fileQueue.Count > 0) { while (runningTasks.Count < maxThreads && fileQueue.Count > 0) { var file = fileQueue.Dequeue(); runningTasks.Add(Task.Run(() => { var rwLock = GetLock(file); rwLock.EnterUpgradeableReadLock(); try { RestoreBackupFile(file, rwLock); using (var sr = new StreamReaderWrapper(file)) { string line = null; while (sr != null && ((line = sr.ReadLine()) != null)) { Playerstatistic stat = null; try { if (string.IsNullOrWhiteSpace(line)) { LogProvider.Log.Warn(this, $"Empty line in {file}"); } /* replace '-' and '_' characters in order to convert back from Modified Base64 (https://en.wikipedia.org/wiki/Base64#Implementations_and_history) */ byte[] byteAfter64 = Convert.FromBase64String(line.Replace('-', '+').Replace('_', '/').Trim()); using (var ms = new MemoryStream(byteAfter64)) { stat = Serializer.Deserialize <Playerstatistic>(ms); } } catch (Exception ex) { LogProvider.Log.Error($@"Could not process the file: {file}{Environment.NewLine}Error at line: {line}{Environment.NewLine}", ex); } if (stat != null && (pokerSite.HasValue && stat.PokersiteId == pokerSite || !pokerSite.HasValue)) { stats.Add(stat); } } } } finally { rwLock.ExitUpgradeableReadLock(); } })); } var completedTask = Task.WhenAny(runningTasks).Result; runningTasks.Remove(completedTask); } Task.WhenAll(runningTasks).Wait(); return(stats.ToList()); }
public virtual IDictionary <string, T> GetPlayersIndicators <T>(string[] playerNames, short?pokersiteId) where T : Indicators, IThreadSafeIndicators { try { Players[] players; // get players ids using (var session = ModelEntities.OpenStatelessSession()) { players = session.Query <Players>() .Where(x => playerNames.Contains(x.Playername) && x.PokersiteId == pokersiteId) .ToArray(); } var playersStatFiles = (from player in players from file in GetPlayerFiles(player.PlayerId) select new PlayerStatFile { Player = player, File = file }).ToArray(); var maxThreads = Environment.ProcessorCount + 1; var runningTasks = new List <Task>(); var fileQueue = GetPlayerStatFileQueue(playersStatFiles); var playersIndicators = (from playersStatFile in playersStatFiles group playersStatFile by playersStatFile.Player.Playername into grouped where grouped.Any() let indicators = Activator.CreateInstance <T>() select new { grouped.Key, Indicators = indicators }).ToDictionary(x => x.Key, x => x.Indicators); while (fileQueue.Count > 0) { while (runningTasks.Count < maxThreads && fileQueue.Count > 0) { var playerStatFile = fileQueue.Dequeue(); runningTasks.Add(Task.Run(() => { var rwLock = GetLock(playerStatFile.File); rwLock.EnterUpgradeableReadLock(); try { RestoreBackupFile(playerStatFile.File, rwLock); using (var sr = new StreamReaderWrapper(playerStatFile.File)) { string line = null; while (sr != null && ((line = sr.ReadLine()) != null)) { if (!TryParsePlayerStatistic(line, playerStatFile.File, out Playerstatistic stat) || stat == null) { continue; } var indicators = playersIndicators[playerStatFile.Player.Playername]; indicators.AddStatistic(stat); } } } catch (Exception ex) { LogProvider.Log.Error(this, $"Failed to build indicators for {string.Join(", ", playerNames)} by reading '{playerStatFile.File}' [{pokersiteId}]", ex); } finally { rwLock.ExitUpgradeableReadLock(); } })); } var completedTask = Task.WhenAny(runningTasks).Result; runningTasks.Remove(completedTask); } Task.WhenAll(runningTasks).Wait(); return(playersIndicators); } catch (Exception e) { LogProvider.Log.Error(this, $"Could not build indicators for {string.Join(", ", playerNames)} [{pokersiteId}]", e); } return(null); }