/// <summary> /// Marks a insert xaction as reconciled in the file /// </summary> /// <param name="row">The row that was to be inserted</param> /// <returns>True if successful, otherwise false</returns> public bool MarkInsertXactAsReconciled(RowInsert row) { bool isSuccessful; _locker.EnterWriteLock(); // this sucks string[] lines = File.ReadAllLines(FileName()); var xacts = new List <XactLine>(lines.Length); foreach (var line in lines) { if (line.StartsWith("version")) { continue; } if (line.Length == 0) { continue; } var xact = new XactLine(line); if (xact.XactId == row.XactId) { xact.IsReconciled = true; } xacts.Add(xact); } string[] linesToWrite = new string[lines.Length]; int i = 0; foreach (var x in xacts) { linesToWrite[i] = x.ToString(); i++; } File.WriteAllLines(FileName(), linesToWrite); // remove the xaction from the pending open transactions Guid value; isSuccessful = _unreconciledXacts.TryRemove(row.XactId, out value); _locker.ExitWriteLock(); return(isSuccessful); }
/// <summary> /// Attempts to write to the xact log for this database the row to be inserted. Will insert the xact as un-reconciled. This method is blocking. /// </summary> /// <param name="row">The row to be inserted.</param> /// <returns>True if successful, otherwise false.</returns> public bool WriteTransactionForInsert(RowInsert row) { bool isSuccessful; if (IsPendingReconciliation(row.XactId)) { throw new InvalidOperationException($"xact {row.XactId.ToString()} is already in progress"); } if (DoesFileExist()) { _locker.EnterWriteLock(); _unreconciledXacts.TryAdd(row.XactId, row.XactId); // to do - need to come up with xact file format // xact xactId tableId isReconciled <action> { Insert | Update | Delete } <data> { RowValues | RowId, RowValues | RowId } var item = new XactLine(row); using (var file = File.AppendText(FileName())) { file.WriteLine(item.ToString()); file.Flush(); } _locker.ExitWriteLock(); isSuccessful = true; } else { isSuccessful = false; } // to do - is this right? return(isSuccessful); }