From 8ea8086b1a249a9272b10182091d01d443107c3d Mon Sep 17 00:00:00 2001 From: Rune Nielsen Date: Thu, 13 Jan 2022 09:53:17 +0100 Subject: [PATCH] now correclty converts to padded 10 bytes from BigInteger (#46) --- src/MsSqlCdc/Cdc.cs | 14 +++++++------- src/MsSqlCdc/DataConvert.cs | 12 ++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/MsSqlCdc/Cdc.cs b/src/MsSqlCdc/Cdc.cs index 4392bc6..956dec9 100644 --- a/src/MsSqlCdc/Cdc.cs +++ b/src/MsSqlCdc/Cdc.cs @@ -185,7 +185,7 @@ public static async Task MapTimeToLsn( /// public static async Task 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}'"); @@ -233,7 +233,7 @@ public static async Task GetMaxLsn(SqlConnection connection) /// Return the high endpoint of the change data capture timeline for any capture instance. public static async Task 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}'."); @@ -249,7 +249,7 @@ public static async Task GetPreviousLsn(SqlConnection connection, Bi /// Get the next log sequence number (LSN) in the sequence based upon the specified LSN. public static async Task 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}'."); @@ -274,8 +274,8 @@ public static async Task>> 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); @@ -301,8 +301,8 @@ public static async Task>> 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); diff --git a/src/MsSqlCdc/DataConvert.cs b/src/MsSqlCdc/DataConvert.cs index 9634947..8e2baa4 100644 --- a/src/MsSqlCdc/DataConvert.cs +++ b/src/MsSqlCdc/DataConvert.cs @@ -115,11 +115,19 @@ public static string ConvertAllChangesRowFilterOption( }; /// - /// 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. /// /// BigInteger representation of LSN. /// Binary array of BigInteger LSN. - 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; + } /// /// Convert the binary representation of the line-sequence-number to BigInteger.