protected override void WriteNextEntryToDatabase() { var killEntry = WriteQueue.Take(); //LOG.Debug(string.Format("{0}: writing next entry to database", killEntry.CharacterId)); try { IList <Kill> newKills; var start = DateTime.UtcNow; using (var context = new DatabaseContext()) { context.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;"); context.Configuration.AutoDetectChangesEnabled = false; context.Configuration.ValidateOnSaveEnabled = false; newKills = GetNewKills(context, killEntry.Kills); foreach (var curKill in newKills) { if (LAST_KILL_IDS.Count > MAX_LAST_KILL_COUNT) { var id = LAST_KILL_IDS.First(); LAST_KILL_IDS_HASH.Remove(id); LAST_KILL_IDS.RemoveFirst(); } LAST_KILL_IDS_HASH.Add(curKill.KillID); LAST_KILL_IDS.AddLast(curKill.KillID); curKill.Victim.KillID = curKill.KillID; } if (newKills.Count < killEntry.Kills.Count) { LOG.Debug(string.Format("{0}: {1} kills ignored: {2} [{3}]", killEntry.CharacterId, killEntry.Kills.Count - newKills.Count, killEntry.Kills.Count == 1 ? killEntry.Kills.First().KillID : -1, Thread.CurrentThread.ManagedThreadId)); } try { context.Kills.AddRange(newKills); context.SaveChanges(); } catch (Exception) { LOG.Error("ERROR IN WRITE: " + String.Join(", ", newKills.Select(x => x.KillID)) + " : " + String.Join(", ", newKills.Select(x => x.Victim.KillID))); throw; } } if (newKills.Any()) { var elapsedSeconds = (DateTime.UtcNow - start).TotalSeconds; LOG.Debug(string.Format("{0}: {2} kills successfully written in {1}s [{3}]", killEntry.CharacterId, elapsedSeconds, newKills.Count(), Thread.CurrentThread.ManagedThreadId)); } } catch (Exception e) { LOG.Error(string.Format("{0}: error during write to kills [{1}]", killEntry.Kills.FirstOrDefault() == null ? killEntry.CharacterId : killEntry.Kills.First().KillID, Thread.CurrentThread.ManagedThreadId ), e); } }
public void WriteNext() { IsWriting = true; string SQL_INSERT_COLLECT_RESULT = "insert or ignore into collect (run_id, result_type, row_key, identity, serialized) values "; if (settings.BatchSize > 199) { Log.Warning("Maximum batch size is 199. Setting Batch size to 199"); settings.BatchSize = 199; } var count = Math.Min(settings.BatchSize, WriteQueue.Count); var innerQueue = WriteQueue.Take(count).ToList(); if (count > 0) { var stringBuilder = new StringBuilder(); stringBuilder.Append(SQL_INSERT_COLLECT_RESULT); using var cmd = new SqliteCommand(stringBuilder.ToString(), Connection, Transaction); for (int i = 0; i < count; i++) { stringBuilder.Append($"(@run_id_{i}, @result_type_{i}, @row_key_{i}, @identity_{i}, @serialized_{i}),"); cmd.Parameters.AddWithValue($"@run_id_{i}", innerQueue[i].RunId); cmd.Parameters.AddWithValue($"@row_key_{i}", innerQueue[i].RowKey); cmd.Parameters.AddWithValue($"@identity_{i}", innerQueue[i].ColObj?.Identity); cmd.Parameters.AddWithValue($"@serialized_{i}", innerQueue[i].Serialized); cmd.Parameters.AddWithValue($"@result_type_{i}", innerQueue[i].ColObj?.ResultType); } // remove trailing comma stringBuilder.Remove(stringBuilder.Length - 1, 1); cmd.CommandText = stringBuilder.ToString(); try { cmd.ExecuteNonQuery(); } catch (SqliteException e) { Log.Warning(exception: e, $"Error writing to database."); } } WriteQueue.RemoveRange(0, count); IsWriting = false; }