Skip to content

Commit

Permalink
Fixed issue that the the paging DataLoader key is not unique. (#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Sep 14, 2024
1 parent 57035c9 commit 0416e47
Showing 1 changed file with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@ private static string CreateBranchKey(
var requiredBufferSize = 1;

requiredBufferSize += EstimateIntLength(pagingArguments.First);
requiredBufferSize += pagingArguments.After?.Length ?? 0;
if(pagingArguments.After is not null)
{
requiredBufferSize += pagingArguments.After?.Length ?? 0;
requiredBufferSize += 2;
}
requiredBufferSize += EstimateIntLength(pagingArguments.Last);
requiredBufferSize += pagingArguments.Before?.Length ?? 0;

if(pagingArguments.Before is not null)
{
requiredBufferSize += pagingArguments.Before?.Length ?? 0;
requiredBufferSize += 2;
}

if (requiredBufferSize == 1)
{
Expand All @@ -131,31 +140,51 @@ private static string CreateBranchKey(

if (pagingArguments.First.HasValue)
{
var span = buffer.Slice(written);
span[0] = 'f';
span[1] = ':';
written += 2;

if (!pagingArguments.First.Value.TryFormat(buffer.Slice(written), out var charsWritten))
{
throw new InvalidOperationException("Buffer is too small.");
}
written += charsWritten;
}

if (pagingArguments.After != null)
if (pagingArguments.After is not null)
{
var span = buffer.Slice(written);
span[0] = 'a';
span[1] = ':';
written += 2;

var after = pagingArguments.After.AsSpan();
after.CopyTo(buffer.Slice(written));
written += after.Length;
}

if (pagingArguments.Last.HasValue)
{
var span = buffer.Slice(written);
span[0] = 'l';
span[1] = ':';
written += 2;

if (!pagingArguments.Last.Value.TryFormat(buffer.Slice(written), out var charsWritten))
{
throw new InvalidOperationException("Buffer is too small.");
}
written += charsWritten;
}

if (pagingArguments.Before != null)
if (pagingArguments.Before is not null)
{
var span = buffer.Slice(written);
span[0] = 'b';
span[1] = ':';
written += 2;

var before = pagingArguments.Before.AsSpan();
before.CopyTo(buffer.Slice(written));
written += before.Length;
Expand Down Expand Up @@ -183,7 +212,7 @@ private static int EstimateIntLength(int? value)
if (value == 0)
{
// to print 0 we need still 1 digit
return 1;
return 3;
}

// if the number is negative we need one more digit for the sign
Expand All @@ -192,6 +221,6 @@ private static int EstimateIntLength(int? value)
// we add the number of digits the number has to the length of the number.
length += (int)Math.Floor(Math.Log10(Math.Abs(value.Value)) + 1);

return length;
return length + 2;
}
}

0 comments on commit 0416e47

Please sign in to comment.