Skip to content

Commit

Permalink
Convert Span to ReadOnlySpan for appends
Browse files Browse the repository at this point in the history
  • Loading branch information
badrishc committed Oct 4, 2019
1 parent 540d1a5 commit 80a2aeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions cs/playground/FasterLogSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Program
// Entry length can be between 1 and ((1 << FasterLogSettings.PageSizeBits) - 4)
const int entryLength = 1 << 10;
static readonly byte[] staticEntry = new byte[entryLength];
static readonly SpanBatch spanBatch = new SpanBatch(10);
static readonly ReadOnlySpanBatch spanBatch = new ReadOnlySpanBatch(10);
static FasterLog log;

static void ReportThread()
Expand Down Expand Up @@ -55,11 +55,11 @@ static void AppendThread()
while (true)
{
// TryAppend - can be used with throttling/back-off
// Accepts byte[] and Span<byte>
// Accepts byte[] and ReadOnlySpan<byte>
while (!log.TryAppend(staticEntry, out _)) ;

// Synchronous blocking append
// Accepts byte[] and Span<byte>
// Accepts byte[] and ReadOnlySpan<byte>
// log.Append(entry);

// Batched append - batch must fit on one page
Expand Down Expand Up @@ -224,11 +224,11 @@ static async Task AppendAsync(int id)
}
}

private struct SpanBatch : ISpanBatch
private struct ReadOnlySpanBatch : IReadOnlySpanBatch
{
private readonly int batchSize;
public SpanBatch(int batchSize) => this.batchSize = batchSize;
public Span<byte> Get(int index) => staticEntry;
public ReadOnlySpanBatch(int batchSize) => this.batchSize = batchSize;
public ReadOnlySpan<byte> Get(int index) => staticEntry;
public int TotalEntries() => batchSize;
}

Expand Down
26 changes: 13 additions & 13 deletions cs/src/core/Index/FasterLog/FasterLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void Dispose()
/// </summary>
/// <param name="entry"></param>
/// <returns>Logical address of added entry</returns>
public long Append(Span<byte> entry)
public long Append(ReadOnlySpan<byte> entry)
{
long logicalAddress;
while (!TryAppend(entry, out logicalAddress)) ;
Expand Down Expand Up @@ -136,7 +136,7 @@ public unsafe bool TryAppend(byte[] entry, out long logicalAddress)
/// <param name="entry">Entry to be appended to log</param>
/// <param name="logicalAddress">Logical address of added entry</param>
/// <returns>Whether the append succeeded</returns>
public unsafe bool TryAppend(Span<byte> entry, out long logicalAddress)
public unsafe bool TryAppend(ReadOnlySpan<byte> entry, out long logicalAddress)
{
logicalAddress = 0;

Expand All @@ -163,12 +163,12 @@ public unsafe bool TryAppend(Span<byte> entry, out long logicalAddress)
/// Try to append batch of entries as a single atomic unit. Entire batch
/// needs to fit on one page.
/// </summary>
/// <param name="spanBatch">Batch to be appended to log</param>
/// <param name="readOnlySpanBatch">Batch to be appended to log</param>
/// <param name="logicalAddress">Logical address of first added entry</param>
/// <returns>Whether the append succeeded</returns>
public bool TryAppend(ISpanBatch spanBatch, out long logicalAddress)
public bool TryAppend(IReadOnlySpanBatch readOnlySpanBatch, out long logicalAddress)
{
return TryAppend(spanBatch, out logicalAddress, out _);
return TryAppend(readOnlySpanBatch, out logicalAddress, out _);
}

/// <summary>
Expand Down Expand Up @@ -207,9 +207,9 @@ public async ValueTask<long> AppendAsync(byte[] entry)
/// <summary>
/// Append batch of entries to log (async) - completes after batch is flushed to storage
/// </summary>
/// <param name="spanBatch"></param>
/// <param name="readOnlySpanBatch"></param>
/// <returns></returns>
public async ValueTask<long> AppendAsync(ISpanBatch spanBatch)
public async ValueTask<long> AppendAsync(IReadOnlySpanBatch readOnlySpanBatch)
{
long logicalAddress;
int allocatedLength;
Expand All @@ -218,7 +218,7 @@ public async ValueTask<long> AppendAsync(ISpanBatch spanBatch)
while (true)
{
var task = CommitTask;
if (TryAppend(spanBatch, out logicalAddress, out allocatedLength))
if (TryAppend(readOnlySpanBatch, out logicalAddress, out allocatedLength))
break;
await task;
}
Expand Down Expand Up @@ -430,19 +430,19 @@ private void Restore()
/// Try to append batch of entries as a single atomic unit. Entire batch
/// needs to fit on one page.
/// </summary>
/// <param name="spanBatch">Batch to be appended to log</param>
/// <param name="readOnlySpanBatch">Batch to be appended to log</param>
/// <param name="logicalAddress">Logical address of first added entry</param>
/// <param name="allocatedLength">Actual allocated length</param>
/// <returns>Whether the append succeeded</returns>
private unsafe bool TryAppend(ISpanBatch spanBatch, out long logicalAddress, out int allocatedLength)
private unsafe bool TryAppend(IReadOnlySpanBatch readOnlySpanBatch, out long logicalAddress, out int allocatedLength)
{
logicalAddress = 0;

int totalEntries = spanBatch.TotalEntries();
int totalEntries = readOnlySpanBatch.TotalEntries();
allocatedLength = 0;
for (int i = 0; i < totalEntries; i++)
{
allocatedLength += Align(spanBatch.Get(i).Length) + 4;
allocatedLength += Align(readOnlySpanBatch.Get(i).Length) + 4;
}

epoch.Resume();
Expand All @@ -457,7 +457,7 @@ private unsafe bool TryAppend(ISpanBatch spanBatch, out long logicalAddress, out
var physicalAddress = allocator.GetPhysicalAddress(logicalAddress);
for (int i = 0; i < totalEntries; i++)
{
var span = spanBatch.Get(i);
var span = readOnlySpanBatch.Get(i);
var entryLength = span.Length;
*(int*)physicalAddress = entryLength;
fixed (byte* bp = &span.GetPinnableReference())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
namespace FASTER.core
{
/// <summary>
/// Interface to provide a batch of Span[byte] data to FASTER
/// Interface to provide a batch of ReadOnlySpan[byte] data to FASTER
/// </summary>
public interface ISpanBatch
public interface IReadOnlySpanBatch
{
/// <summary>
/// Number of entries in provided batch
Expand All @@ -21,6 +21,6 @@ public interface ISpanBatch
/// </summary>
/// <param name="index">Index</param>
/// <returns></returns>
Span<byte> Get(int index);
ReadOnlySpan<byte> Get(int index);
}
}

0 comments on commit 80a2aeb

Please sign in to comment.