99using Geta . NotFoundHandler . Optimizely . Core . AutomaticRedirects ;
1010using Newtonsoft . Json ;
1111using Newtonsoft . Json . Converters ;
12+ using System . Security . Cryptography ;
13+ using System . Text ;
1214
1315namespace Geta . NotFoundHandler . Optimizely . Data
1416{
1517 public class SqlContentUrlHistoryRepository : IRepository < ContentUrlHistory > , IContentUrlHistoryLoader
1618 {
1719 private const string ContentUrlHistoryTable = "[dbo].[NotFoundHandler.ContentUrlHistory]" ;
18- private const string AllFields = "Id, ContentKey, Urls, CreatedUtc" ;
20+ private const string AllFields = "Id, ContentKey, Urls, CreatedUtc, md5_ContentKey " ;
1921 private readonly IDataExecutor _dataExecutor ;
2022
2123 public SqlContentUrlHistoryRepository ( IDataExecutor dataExecutor )
@@ -35,33 +37,46 @@ private static JsonSerializerSettings JsonSettings
3537 return settings ;
3638 }
3739 }
40+
41+ private static byte [ ] CalculateMd5Hash ( string input )
42+ {
43+ using var md5 = MD5 . Create ( ) ;
44+ var inputBytes = Encoding . Unicode . GetBytes ( input ) ;
45+ var hashBytes = md5 . ComputeHash ( inputBytes ) ;
46+
47+ return hashBytes ;
48+ }
3849
3950 public bool IsRegistered ( ContentUrlHistory entity )
4051 {
4152 var sqlCommand = $@ "SELECT TOP 1 { AllFields }
4253 FROM { ContentUrlHistoryTable }
43- WHERE ContentKey = @contentKey
54+ WHERE ContentKey = @contentKey AND md5_ContentKey = @contentKeyHash
4455 ORDER BY CreatedUtc DESC" ;
4556
4657 var dataTable = _dataExecutor . ExecuteQuery (
4758 sqlCommand ,
48- _dataExecutor . CreateStringParameter ( "contentKey" , entity . ContentKey ) ) ;
59+ _dataExecutor . CreateStringParameter ( "contentKey" , entity . ContentKey ) ,
60+ _dataExecutor . CreateBinaryParameter ( "contentKeyHash" , CalculateMd5Hash ( entity . ContentKey ) )
61+ ) ;
4962
5063 var last = ToContentUrlHistory ( dataTable ) . FirstOrDefault ( ) ;
5164
52- return last != null && last . Urls . Count == entity . Urls . Count && last . Urls . All ( entity . Urls . Contains ) ;
65+ var result = last != null && last . Urls . Count == entity . Urls . Count && last . Urls . All ( entity . Urls . Contains ) ;
66+
67+ return result ;
5368 }
5469
5570 public IEnumerable < ( string contentKey , IReadOnlyCollection < ContentUrlHistory > histories ) > GetAllMoved ( )
5671 {
57- var sqlCommand = $@ "SELECT h.Id, h.ContentKey, h.Urls, h.CreatedUtc
72+ var sqlCommand = $@ "SELECT h.Id, h.ContentKey, h.Urls, h.CreatedUtc, h.md5_ContentKey
5873 FROM { ContentUrlHistoryTable } h
5974 INNER JOIN
60- (SELECT ContentKey
75+ (SELECT ContentKey, md5_ContentKey
6176 FROM { ContentUrlHistoryTable }
62- GROUP BY ContentKey
77+ GROUP BY ContentKey, md5_ContentKey
6378 HAVING COUNT(*) > 1) k
64- ON h.ContentKey = k.ContentKey
79+ ON h.ContentKey = k.ContentKey AND h.md5_ContentKey = k.md5_ContentKey
6580 ORDER BY h.ContentKey, h.CreatedUtc DESC" ;
6681
6782 var dataTable = _dataExecutor . ExecuteQuery ( sqlCommand ) ;
@@ -73,18 +88,24 @@ HAVING COUNT(*) > 1) k
7388
7489 public IReadOnlyCollection < ContentUrlHistory > GetMoved ( string contentKey )
7590 {
76- var sqlCommand = $@ "SELECT h.Id, h.ContentKey, h.Urls, h.CreatedUtc
91+ var contentKeyHash = CalculateMd5Hash ( contentKey ) ;
92+
93+ var sqlCommand = $@ "SELECT h.Id, h.ContentKey, h.Urls, h.CreatedUtc, h.md5_ContentKey
7794 FROM { ContentUrlHistoryTable } h
7895 INNER JOIN
7996 (SELECT ContentKey
8097 FROM { ContentUrlHistoryTable }
98+ WHERE md5_ContentKey = @contentKeyHash
8199 GROUP BY ContentKey
82100 HAVING COUNT(*) > 1) k
83101 ON h.ContentKey = k.ContentKey
84- WHERE h.ContentKey = @contentKey
102+ WHERE h.ContentKey = @contentKey AND h.md5_ContentKey = @contentKeyHash
85103 ORDER BY h.ContentKey, h.CreatedUtc DESC" ;
86104
87- var dataTable = _dataExecutor . ExecuteQuery ( sqlCommand , _dataExecutor . CreateStringParameter ( "contentKey" , contentKey ) ) ;
105+ var dataTable = _dataExecutor . ExecuteQuery ( sqlCommand ,
106+ _dataExecutor . CreateStringParameter ( "contentKey" , contentKey ) ,
107+ _dataExecutor . CreateBinaryParameter ( "contentKeyHash" , contentKeyHash )
108+ ) ;
88109
89110 var histories = ToContentUrlHistory ( dataTable ) ;
90111
@@ -124,7 +145,8 @@ private void Create(ContentUrlHistory entity)
124145 _dataExecutor . CreateGuidParameter ( "id" , entity . Id ) ,
125146 _dataExecutor . CreateStringParameter ( "contentKey" , entity . ContentKey ) ,
126147 _dataExecutor . CreateStringParameter ( "urls" , ToJson ( entity . Urls ) , - 1 ) ,
127- _dataExecutor . CreateDateTimeParameter ( "createdUtc" , entity . CreatedUtc ) ) ;
148+ _dataExecutor . CreateDateTimeParameter ( "createdUtc" , entity . CreatedUtc )
149+ ) ;
128150 }
129151
130152 private void Update ( ContentUrlHistory entity )
@@ -145,7 +167,8 @@ private void Update(ContentUrlHistory entity)
145167 _dataExecutor . CreateGuidParameter ( "id" , entity . Id ) ,
146168 _dataExecutor . CreateStringParameter ( "contentKey" , entity . ContentKey ) ,
147169 _dataExecutor . CreateStringParameter ( "urls" , ToJson ( entity . Urls ) , - 1 ) ,
148- _dataExecutor . CreateDateTimeParameter ( "createdUtc" , entity . CreatedUtc ) ) ;
170+ _dataExecutor . CreateDateTimeParameter ( "createdUtc" , entity . CreatedUtc )
171+ ) ;
149172 }
150173
151174 private static string ToJson ( ICollection < TypedUrl > urls )
0 commit comments