Skip to content

Commit

Permalink
now correclty converts to padded 10 bytes from BigInteger (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Jan 13, 2022
1 parent 79c05fd commit 8ea8086
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/MsSqlCdc/Cdc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static async Task<BigInteger> MapTimeToLsn(
/// </returns>
public static async Task<DateTime> MapLsnToTime(SqlConnection connection, BigInteger lsn)
{
var binaryLsn = DataConvert.ConvertLsnBigEridian(lsn);
var binaryLsn = DataConvert.ConvertLsnBigEndian(lsn);
var lsnToTime = await CdcDatabase.MapLsnToTime(connection, binaryLsn);
if (!lsnToTime.HasValue)
throw new Exception($"Could not convert LSN to time with LSN being '{lsn}'");
Expand Down Expand Up @@ -233,7 +233,7 @@ public static async Task<BigInteger> GetMaxLsn(SqlConnection connection)
/// <returns>Return the high endpoint of the change data capture timeline for any capture instance.</returns>
public static async Task<BigInteger> GetPreviousLsn(SqlConnection connection, BigInteger lsn)
{
var binaryLsn = DataConvert.ConvertLsnBigEridian(lsn);
var binaryLsn = DataConvert.ConvertLsnBigEndian(lsn);
var previousLsnBytes = await CdcDatabase.DecrementLsn(connection, binaryLsn);
if (previousLsnBytes is null)
throw new Exception($"Could not get previous lsn on {nameof(lsn)}: '{lsn}'.");
Expand All @@ -249,7 +249,7 @@ public static async Task<BigInteger> GetPreviousLsn(SqlConnection connection, Bi
/// <returns>Get the next log sequence number (LSN) in the sequence based upon the specified LSN.</returns>
public static async Task<BigInteger> GetNextLsn(SqlConnection connection, BigInteger lsn)
{
var lsnBinary = DataConvert.ConvertLsnBigEridian(lsn);
var lsnBinary = DataConvert.ConvertLsnBigEndian(lsn);
var nextLsnBytes = await CdcDatabase.IncrementLsn(connection, lsnBinary);
if (nextLsnBytes is null)
throw new Exception($"Could not get next lsn on {nameof(lsn)}: '{lsn}'.");
Expand All @@ -274,8 +274,8 @@ public static async Task<IReadOnlyCollection<ChangeRow<dynamic>>> GetNetChanges(
BigInteger toLsn,
NetChangesRowFilterOption netChangesRowFilterOption = NetChangesRowFilterOption.All)
{
var beginLsnBinary = DataConvert.ConvertLsnBigEridian(fromLsn);
var endLsnBinary = DataConvert.ConvertLsnBigEridian(toLsn);
var beginLsnBinary = DataConvert.ConvertLsnBigEndian(fromLsn);
var endLsnBinary = DataConvert.ConvertLsnBigEndian(toLsn);
var filterOption = DataConvert.ConvertNetChangesRowFilterOption(netChangesRowFilterOption);
var cdcColumns = await CdcDatabase.GetNetChanges(
connection, captureInstance, beginLsnBinary, endLsnBinary, filterOption);
Expand All @@ -301,8 +301,8 @@ public static async Task<IReadOnlyCollection<ChangeRow<dynamic>>> GetAllChanges(
BigInteger endLsn,
AllChangesRowFilterOption allChangesRowFilterOption = AllChangesRowFilterOption.All)
{
var beginLsnBinary = DataConvert.ConvertLsnBigEridian(beginLsn);
var endLsnBinary = DataConvert.ConvertLsnBigEridian(endLsn);
var beginLsnBinary = DataConvert.ConvertLsnBigEndian(beginLsn);
var endLsnBinary = DataConvert.ConvertLsnBigEndian(endLsn);
var filterOption = DataConvert.ConvertAllChangesRowFilterOption(allChangesRowFilterOption);
var cdcColumns = await CdcDatabase.GetAllChanges(
connection, captureInstance, beginLsnBinary, endLsnBinary, filterOption);
Expand Down
12 changes: 10 additions & 2 deletions src/MsSqlCdc/DataConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ public static string ConvertAllChangesRowFilterOption(
};

/// <summary>
/// Convert LSN BigInteger to ByteArray in BigEndian format.
/// Convert LSN BigInteger to ByteArray in BigEndian format,
/// Also makes sure that the size of the returned byte array is always 10 bytes.
/// </summary>
/// <param name="representation">BigInteger representation of LSN.</param>
/// <returns>Binary array of BigInteger LSN.</returns>
public static byte[] ConvertLsnBigEridian(BigInteger lsn) => lsn.ToByteArray().Reverse().ToArray();
public static byte[] ConvertLsnBigEndian(BigInteger lsn)
{
var newArray = new byte[10];
var lsnBytes = lsn.ToByteArray(isBigEndian: true);
var startAt = newArray.Length - lsnBytes.Length;
Array.Copy(lsnBytes, 0, newArray, startAt, lsnBytes.Length);
return newArray;
}

/// <summary>
/// Convert the binary representation of the line-sequence-number to BigInteger.
Expand Down

0 comments on commit 8ea8086

Please sign in to comment.