Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/ColumnizerLib.UnitTests/LogLineTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Runtime.CompilerServices;

using ColumnizerLib;

using NUnit.Framework;

namespace LogExpert.ColumnizerLib.Tests;

[TestFixture]
public class LogLineTests
{
[Test]
public void LogLine_StoresTheLineOnce ()
{
// One ReadOnlyMemory<char> plus the line number; every resident line pays for this.
Assert.That(Unsafe.SizeOf<LogLine>(), Is.LessThan(2 * Unsafe.SizeOf<ReadOnlyMemory<char>>()));
}

[Test]
public void LogLine_TextIsTheFullLine ()
{
var memory = "2026-11-20 INFO started".AsMemory(11);
var line = new LogLine(memory, 7);

Assert.Multiple(() =>
{
Assert.That(line.Text.Equals(line.FullLine), Is.True);
Assert.That(line.Text.ToString(), Is.EqualTo("INFO started"));
Assert.That(line.LineNumber, Is.EqualTo(7));
Assert.That(line, Is.EqualTo(new LogLine(memory, 7)));
});
}
}
7 changes: 3 additions & 4 deletions src/ColumnizerLib/LogLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ namespace ColumnizerLib;

public ReadOnlyMemory<char> FullLine { get; }

public ReadOnlyMemory<char> Text { get; }
// Not stored: a second copy of the memory would cost 16 bytes on every resident line.
public ReadOnlyMemory<char> Text => FullLine;

public LogLine (string fullLine, int lineNumber)
{
LineNumber = lineNumber;
FullLine = fullLine.AsMemory();
Text = fullLine.AsMemory();
}

public LogLine (ReadOnlyMemory<char> fullLine, int lineNumber)
{
LineNumber = lineNumber;
FullLine = fullLine;
Text = fullLine;
}
}
}
2 changes: 2 additions & 0 deletions src/LogExpert.Core/Classes/Log/Buffers/LogBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public long Size

public int PrevBuffersDroppedLinesSum { get; set; }

internal int CharBlockCapacity => _charBlocks?.Sum(block => block.Length) ?? 0;

#endregion

#region Public methods
Expand Down
40 changes: 24 additions & 16 deletions src/LogExpert.Core/Classes/Log/LogfileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ private bool ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start
try
{
using var fileStream = logFileInfo.OpenStream();
using var reader = GetLogStreamReader(fileStream, EncodingOptions) as ILogStreamReaderMemory;
using var reader = GetLogStreamReader(fileStream, EncodingOptions);

reader.Position = filePos;
_fileLength = logFileInfo.Length;
Expand Down Expand Up @@ -1270,7 +1270,7 @@ private bool ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start

// Detach char blocks from the reader and attach to the completed buffer.
// Must happen before Monitor.Exit so the buffer is still exclusively owned.
logBuffer.AttachCharBlocks(reader.DetachCharBlocks());
AttachReaderBlocks(logBuffer, reader);

Monitor.Exit(logBuffer);
try
Expand Down Expand Up @@ -1302,7 +1302,7 @@ private bool ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start
logBuffer.Size = filePos - logBuffer.StartPos;

// Attach remaining blocks to the final buffer
logBuffer.AttachCharBlocks(reader.DetachCharBlocks());
AttachReaderBlocks(logBuffer, reader);
}
finally
{
Expand Down Expand Up @@ -1395,10 +1395,7 @@ private void ReReadBuffer (LogBuffer logBuffer)

using Stream fileStream = openendFileStream;
{
//TODO LogStream Reader has to be changed to ILogStreamReaderMemory
var reader = GetLogStreamReader(fileStream, EncodingOptions) as ILogStreamReaderMemory;

using var readerDisposabel = reader as IDisposable;
using var reader = GetLogStreamReader(fileStream, EncodingOptions);
{
var filePos = logBuffer.StartPos;
reader.Position = logBuffer.StartPos;
Expand Down Expand Up @@ -1433,7 +1430,7 @@ private void ReReadBuffer (LogBuffer logBuffer)
}

// Attach char blocks from the reader to the re-read buffer
logBuffer.AttachCharBlocks(reader.DetachCharBlocks());
AttachReaderBlocks(logBuffer, reader);

if (maxLinesCount != logBuffer.LineCount)
{
Expand Down Expand Up @@ -1777,20 +1774,22 @@ private bool ReadLine (ILogStreamReader reader, int lineNum, int realLineNum, ou
/// the line as a string. If the reader supports memory-based access, the memory buffer is populated; otherwise, it
/// is null.
/// </returns>
private (bool Success, ReadOnlyMemory<char> LineMemory, bool wasDropped) ReadLineMemory (ILogStreamReaderMemory reader, int lineNum, int realLineNum)
private (bool Success, ReadOnlyMemory<char> LineMemory, bool wasDropped) ReadLineMemory (ILogStreamReader reader, int lineNum, int realLineNum)
{
if (reader is null)
if (reader is not ILogStreamReaderMemory memoryReader)
{
// Fallback to string-based reading if memory reader not available
if (ReadLine(reader, lineNum, realLineNum, out var outLine))
// String readers (XML mode); a null line after preprocessing means the line was dropped.
if (!ReadLine(reader, lineNum, realLineNum, out var outLine))
{
return (true, outLine.AsMemory(), false);
return (false, ReadOnlyMemory<char>.Empty, false);
}

return (false, ReadOnlyMemory<char>.Empty, false);
return outLine is null
? (true, ReadOnlyMemory<char>.Empty, true)
: (true, outLine.AsMemory(), false);
}

if (!reader.TryReadLine(out var lineMemory))
if (!memoryReader.TryReadLine(out var lineMemory))
{
return (false, ReadOnlyMemory<char>.Empty, false);
}
Expand All @@ -1811,6 +1810,15 @@ private bool ReadLine (ILogStreamReader reader, int lineNum, int realLineNum, ou
return (true, lineMemory, false);
}

// Only memory readers hand out pooled char blocks; string readers own no blocks to transfer.
private static void AttachReaderBlocks (LogBuffer logBuffer, ILogStreamReader reader)
{
if (reader is ILogStreamReaderMemory memoryReader)
{
logBuffer.AttachCharBlocks(memoryReader.DetachCharBlocks());
}
}

#if DEBUG
/// <summary>
/// Outputs detailed information about the specified log buffer to the trace logger for debugging purposes.
Expand Down Expand Up @@ -1992,4 +2000,4 @@ PinHandle IBufferPinning.PinRange (int startLine, int endLine)
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using System.Text;

using LogExpert.Core.Entities;

Expand Down Expand Up @@ -90,11 +90,7 @@ public sealed override long Position

public sealed override bool IsBufferComplete => true;

protected static int MaximumLineLength
{
get => field;
private set => field = value;
}
protected int MaximumLineLength { get; }

#endregion

Expand Down
Loading
Loading