Horizon – Diff between revs 3 and 5

Subversion Repositories:
Rev:
Show entire fileRegard whitespace
Rev 3 Rev 5
Line 4... Line 4...
4 using System.Drawing; 4 using System.Drawing;
5 using System.Drawing.Imaging; 5 using System.Drawing.Imaging;
6 using System.Globalization; 6 using System.Globalization;
7 using System.IO; 7 using System.IO;
8 using System.IO.Compression; 8 using System.IO.Compression;
-   9 using System.Runtime.CompilerServices;
9 using System.Security.Cryptography; 10 using System.Security.Cryptography;
10 using System.Threading; 11 using System.Threading;
11 using System.Threading.Tasks; 12 using System.Threading.Tasks;
12 using Horizon.Snapshots; 13 using Horizon.Snapshots;
13 using Horizon.Utilities; 14 using Horizon.Utilities;
Line 94... Line 95...
94 #endregion 95 #endregion
Line 95... Line 96...
95   96  
Line 96... Line 97...
96 #region Private Delegates, Events, Enums, Properties, Indexers and Fields 97 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
97   -  
98 private readonly CancellationTokenSource _cancellationTokenSource; 98  
Line 99... Line 99...
99   99 private readonly CancellationTokenSource _cancellationTokenSource;
Line 100... Line 100...
100 private SemaphoreSlim _snapshotSemaphore; 100 private readonly SemaphoreSlim _databaseLock;
Line 101... Line 101...
101   101  
-   102 #endregion
-   103  
-   104 #region Constructors, Destructors and Finalizers
-   105  
-   106 private SnapshotDatabase()
-   107 {
-   108 Directory.CreateDirectory(Constants.DatabaseDirectory);
102 #endregion 109  
103   110 _databaseLock = new SemaphoreSlim(1, 1);
104 #region Constructors, Destructors and Finalizers 111 }
-   112  
-   113 public SnapshotDatabase(CancellationToken cancellationToken) : this()
-   114 {
Line 105... Line -...
105   -  
Line 106... Line -...
106 public SnapshotDatabase() -  
Line 107... Line 115...
107 { 115 _cancellationTokenSource = new CancellationTokenSource();
108 _cancellationTokenSource = new CancellationTokenSource(); 116 var localCancellationToken = _cancellationTokenSource.Token;
109 _cancellationToken = _cancellationTokenSource.Token; 117 var combinedCancellationTokenSource =
110   118 CancellationTokenSource.CreateLinkedTokenSource(localCancellationToken, cancellationToken);
Line 135... Line 143...
135 } 143 }
Line 136... Line 144...
136   144  
137 public void Dispose() 145 public void Dispose()
138 { 146 {
139 _cancellationTokenSource.Cancel(); -  
140   -  
141 _snapshotSemaphore?.Dispose(); -  
142 _snapshotSemaphore = null; 147 _cancellationTokenSource.Cancel();
Line 143... Line 148...
143 } 148 }
Line 144... Line 149...
144   149  
Line 151... Line 156...
151 var connectionString = new SQLiteConnectionStringBuilder 156 var connectionString = new SQLiteConnectionStringBuilder
152 { 157 {
153 ConnectionString = DatabaseConnectionString 158 ConnectionString = DatabaseConnectionString
154 }; 159 };
Line -... Line 160...
-   160  
-   161 await _databaseLock.WaitAsync(cancellationToken);
-   162 try
155   163 {
156 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 164 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
157 { 165 {
Line 158... Line 166...
158 await sqliteConnection.OpenAsync(cancellationToken); 166 await sqliteConnection.OpenAsync(cancellationToken);
159   167  
160 using (var dbTransaction = sqliteConnection.BeginTransaction()) 168 using (var dbTransaction = sqliteConnection.BeginTransaction())
161 { 169 {
162 // Insert the file change. 170 // Insert the file change.
163 using (var sqliteCommand = 171 using (var sqliteCommand =
164 new SQLiteCommand(RemoveScreenshotFromHashSql, sqliteConnection, dbTransaction)) -  
165 { -  
166 try 172 new SQLiteCommand(RemoveScreenshotFromHashSql, sqliteConnection, dbTransaction))
167 { 173 {
168 sqliteCommand.Parameters.AddRange(new[] 174 sqliteCommand.Parameters.AddRange(new[]
169 { 175 {
Line 170... Line 176...
170 new SQLiteParameter("@hash", hash) 176 new SQLiteParameter("@hash", hash)
Line -... Line 177...
-   177 });
-   178  
171 }); 179 sqliteCommand.Prepare();
Line 172... Line 180...
172   180  
173 sqliteCommand.Prepare(); 181 try
174   182 {
Line 184... Line 192...
184 } 192 }
185 } 193 }
186 } 194 }
187 } 195 }
188 } 196 }
-   197 finally
-   198 {
-   199 _databaseLock.Release();
-   200 }
-   201 }
Line 189... Line 202...
189   202  
190 public async Task NormalizeTime(string hash, CancellationToken cancellationToken) 203 public async Task NormalizeTime(string hash, CancellationToken cancellationToken)
191 { 204 {
192 var connectionString = new SQLiteConnectionStringBuilder 205 var connectionString = new SQLiteConnectionStringBuilder
193 { 206 {
194 ConnectionString = DatabaseConnectionString 207 ConnectionString = DatabaseConnectionString
Line -... Line 208...
-   208 };
-   209  
-   210 await _databaseLock.WaitAsync(cancellationToken);
195 }; 211 try
196   212 {
197 using (var sqliteConnection = 213 using (var sqliteConnection =
198 new SQLiteConnection(connectionString.ConnectionString)) 214 new SQLiteConnection(connectionString.ConnectionString))
Line 235... Line 251...
235 using (var writeSQLiteCommand = 251 using (var writeSQLiteCommand =
236 new SQLiteCommand(UpdateTimeFromHash, sqliteConnection, dbTransaction)) 252 new SQLiteCommand(UpdateTimeFromHash, sqliteConnection, dbTransaction))
237 { 253 {
238 writeSQLiteCommand.Parameters.AddRange(new[] 254 writeSQLiteCommand.Parameters.AddRange(new[]
239 { 255 {
-   256 new SQLiteParameter("@time",
240 new SQLiteParameter("@time", dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff")), 257 dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff")),
241 new SQLiteParameter("@hash", hash) 258 new SQLiteParameter("@hash", hash)
242 }); 259 });
Line 243... Line 260...
243   260  
Line 258... Line 275...
258 } 275 }
259 } 276 }
260 } 277 }
261 } 278 }
262 } 279 }
-   280 finally
-   281 {
-   282 _databaseLock.Release();
-   283 }
-   284 }
Line 263... Line 285...
263   285  
264 public async Task<long> CountSnapshots(CancellationToken cancellationToken) 286 public async Task<long> CountSnapshots(CancellationToken cancellationToken)
265 { 287 {
266 var connectionString = new SQLiteConnectionStringBuilder 288 var connectionString = new SQLiteConnectionStringBuilder
267 { 289 {
268 ConnectionString = DatabaseConnectionString 290 ConnectionString = DatabaseConnectionString
Line -... Line 291...
-   291 };
-   292  
-   293 await _databaseLock.WaitAsync(cancellationToken);
269 }; 294 try
270   295 {
271 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 296 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 272... Line 297...
272 { 297 {
Line 295... Line 320...
295 return count; 320 return count;
296 } 321 }
297 } 322 }
298 } 323 }
299 } 324 }
-   325 finally
-   326 {
-   327 _databaseLock.Release();
-   328 }
-   329 }
Line 300... Line 330...
300   330  
301 public async Task<IEnumerable<Snapshot>> LoadSnapshots(CancellationToken cancellationToken) 331 public async IAsyncEnumerable<Snapshot> LoadSnapshots([EnumeratorCancellation] CancellationToken cancellationToken)
302 { 332 {
303 var connectionString = new SQLiteConnectionStringBuilder 333 var connectionString = new SQLiteConnectionStringBuilder
304 { 334 {
305 ConnectionString = DatabaseConnectionString 335 ConnectionString = DatabaseConnectionString
Line -... Line 336...
-   336 };
-   337  
-   338 await _databaseLock.WaitAsync(cancellationToken);
306 }; 339 try
307   340 {
308 using (var sqliteConnection = 341 using (var sqliteConnection =
309 new SQLiteConnection(connectionString.ConnectionString)) 342 new SQLiteConnection(connectionString.ConnectionString))
Line 315... Line 348...
315 { 348 {
316 sqliteCommand.Prepare(); 349 sqliteCommand.Prepare();
Line 317... Line 350...
317   350  
318 using (var sqlDataReader = await sqliteCommand.ExecuteReaderAsync(cancellationToken)) 351 using (var sqlDataReader = await sqliteCommand.ExecuteReaderAsync(cancellationToken))
319 { 352 {
320 var snapshots = new List<Snapshot>(); 353 //var snapshots = new List<Snapshot>();
321 while (await sqlDataReader.ReadAsync(cancellationToken)) 354 while (await sqlDataReader.ReadAsync(cancellationToken))
322 { 355 {
323 var name = (string)sqlDataReader["Name"]; 356 var name = (string)sqlDataReader["Name"];
324 var path = (string)sqlDataReader["Path"]; 357 var path = (string)sqlDataReader["Path"];
Line 340... Line 373...
340 color = Color.FromArgb(dbColor); 373 color = Color.FromArgb(dbColor);
341 break; 374 break;
342 } 375 }
343 } 376 }
Line 344... Line 377...
344   377  
345 snapshots.Add(new Snapshot(name, path, time, hash, color)); 378 yield return new Snapshot(name, path, time, hash, color);
346 } -  
347   -  
348 return snapshots; 379 }
349 } 380 }
350 } 381 }
351 } 382 }
-   383 }
-   384 finally
-   385 {
-   386 _databaseLock.Release();
-   387 }
Line 352... Line 388...
352 } 388 }
353   389  
354 public async Task CreateSnapshot(string name, string path, Color color, CancellationToken cancellationToken) -  
355 { -  
356 await _snapshotSemaphore.WaitAsync(cancellationToken); 390 public async Task CreateSnapshot(string name, string path, Color color, CancellationToken cancellationToken)
357   391 {
358 var connectionString = new SQLiteConnectionStringBuilder 392 var connectionString = new SQLiteConnectionStringBuilder
359 { 393 {
Line -... Line 394...
-   394 ConnectionString = DatabaseConnectionString
-   395 };
-   396  
360 ConnectionString = DatabaseConnectionString 397 await _databaseLock.WaitAsync(cancellationToken);
361 }; 398 try
362   399 {
363 using (var sqliteConnection = 400 using (var sqliteConnection =
Line 441... Line 478...
441   478  
442 var rowId = 479 var rowId =
Line 443... Line 480...
443 (long)await sqliteCommand.ExecuteScalarAsync(cancellationToken); 480 (long)await sqliteCommand.ExecuteScalarAsync(cancellationToken);
444   481  
-   482 using (var sqliteBlob =
445 using (var sqliteBlob = 483 SQLiteBlob.Create(sqliteConnection, "main", "Snapshots",
446 SQLiteBlob.Create(sqliteConnection, "main", "Snapshots", "Data", 484 "Data",
447 rowId, 485 rowId,
448 false)) 486 false))
Line 449... Line 487...
449 { 487 {
-   488 var fileMemoryStreamData = fileMemoryStream.ToArray();
450 var fileMemoryStreamData = fileMemoryStream.ToArray(); 489  
451   490 sqliteBlob.Write(fileMemoryStreamData,
452 sqliteBlob.Write(fileMemoryStreamData, fileMemoryStreamData.Length, 491 fileMemoryStreamData.Length,
Line 453... Line 492...
453 0); 492 0);
Line 479... Line 518...
479 } 518 }
480 catch (Exception exception) 519 catch (Exception exception)
481 { 520 {
482 dbTransaction.Rollback(); 521 dbTransaction.Rollback();
Line -... Line 522...
-   522  
483   523 SnapshotCreate?.Invoke(this,
Line 484... Line 524...
484 SnapshotCreate?.Invoke(this, new SnapshotCreateFailureEventArgs(name, path, color, exception)); 524 new SnapshotCreateFailureEventArgs(name, path, color, exception));
485   525  
486 throw; -  
487 } -  
488 finally -  
489 { 526 throw;
490 _snapshotSemaphore.Release(); 527 }
491 } 528 }
-   529 }
-   530 }
-   531 finally
-   532 {
492 } 533 _databaseLock.Release();
Line 493... Line 534...
493 } 534 }
494 } 535 }
495   536  
496 public async Task CreateSnapshot(string name, string path, -  
497 Bitmap shot, Color color, CancellationToken cancellationToken) -  
498 { 537 public async Task CreateSnapshot(string name, string path,
499 await _snapshotSemaphore.WaitAsync(cancellationToken); 538 Bitmap shot, Color color, CancellationToken cancellationToken)
500   539 {
501 var connectionString = new SQLiteConnectionStringBuilder 540 var connectionString = new SQLiteConnectionStringBuilder
Line -... Line 541...
-   541 {
-   542 ConnectionString = DatabaseConnectionString
-   543 };
502 { 544  
503 ConnectionString = DatabaseConnectionString 545 await _databaseLock.WaitAsync(cancellationToken);
504 }; 546 try
505   547 {
Line 597... Line 639...
597 var rowId = 639 var rowId =
598 (long)await sqliteCommand.ExecuteScalarAsync( 640 (long)await sqliteCommand.ExecuteScalarAsync(
599 cancellationToken); 641 cancellationToken);
Line 600... Line 642...
600   642  
601 using (var sqliteBlob = 643 using (var sqliteBlob =
-   644 SQLiteBlob.Create(sqliteConnection, "main",
602 SQLiteBlob.Create(sqliteConnection, "main", "Snapshots", 645 "Snapshots",
603 "Data", 646 "Data",
604 rowId, 647 rowId,
605 false)) 648 false))
606 { 649 {
Line 610... Line 653...
610 fileMemoryStreamData.Length, 653 fileMemoryStreamData.Length,
611 0); 654 0);
612 } 655 }
Line 613... Line 656...
613   656  
614 using (var sqliteBlob = 657 using (var sqliteBlob =
-   658 SQLiteBlob.Create(sqliteConnection, "main",
615 SQLiteBlob.Create(sqliteConnection, "main", "Snapshots", 659 "Snapshots",
616 "Shot", 660 "Shot",
617 rowId, 661 rowId,
618 false)) 662 false))
-   663 {
619 { 664 var bitmapMemoryStreamData =
Line 620... Line 665...
620 var bitmapMemoryStreamData = bitmapMemoryStream.ToArray(); 665 bitmapMemoryStream.ToArray();
621   666  
622 sqliteBlob.Write(bitmapMemoryStreamData, 667 sqliteBlob.Write(bitmapMemoryStreamData,
623 bitmapMemoryStreamData.Length, 668 bitmapMemoryStreamData.Length,
Line 652... Line 697...
652 } 697 }
653 catch (Exception exception) 698 catch (Exception exception)
654 { 699 {
655 dbTransaction.Rollback(); 700 dbTransaction.Rollback();
Line -... Line 701...
-   701  
656   702 SnapshotCreate?.Invoke(this,
Line 657... Line 703...
657 SnapshotCreate?.Invoke(this, new SnapshotCreateFailureEventArgs(name, path, color, exception)); 703 new SnapshotCreateFailureEventArgs(name, path, color, exception));
658   704  
659 throw; -  
660 } -  
661 finally -  
662 { 705 throw;
663 _snapshotSemaphore.Release(); 706 }
664 } 707 }
-   708 }
-   709 }
-   710 finally
-   711 {
665 } 712 _databaseLock.Release();
Line 666... Line 713...
666 } 713 }
667 } 714 }
668   715  
669 public async Task SaveFile(string path, string hash, CancellationToken cancellationToken) 716 public async Task SaveFile(string path, string hash, CancellationToken cancellationToken)
670 { 717 {
671 var connectionString = new SQLiteConnectionStringBuilder 718 var connectionString = new SQLiteConnectionStringBuilder
Line -... Line 719...
-   719 {
-   720 ConnectionString = DatabaseConnectionString
-   721 };
672 { 722  
673 ConnectionString = DatabaseConnectionString 723 await _databaseLock.WaitAsync(cancellationToken);
674 }; 724 try
Line 675... Line 725...
675   725 {
Line 718... Line 768...
718 } 768 }
719 } 769 }
720 } 770 }
721 } 771 }
722 } 772 }
-   773 finally
-   774 {
-   775 _databaseLock.Release();
-   776 }
-   777 }
Line 723... Line 778...
723   778  
724 public async Task RevertFile(string name, string hash, CancellationToken cancellationToken, bool atomic = true) 779 public async Task RevertFile(string name, string hash, CancellationToken cancellationToken, bool atomic = true)
725 { -  
726 await _snapshotSemaphore.WaitAsync(cancellationToken); -  
727   780 {
728 var connectionString = new SQLiteConnectionStringBuilder 781 var connectionString = new SQLiteConnectionStringBuilder
729 { 782 {
730 ConnectionString = DatabaseConnectionString 783 ConnectionString = DatabaseConnectionString
Line -... Line 784...
-   784 };
-   785  
-   786 await _databaseLock.WaitAsync(cancellationToken);
731 }; 787 try
732   788 {
733 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 789 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 734... Line 790...
734 { 790 {
Line 834... Line 890...
834 { 890 {
835 SnapshotRevert?.Invoke(this, new SnapshotRevertFailureEventArgs(name)); 891 SnapshotRevert?.Invoke(this, new SnapshotRevertFailureEventArgs(name));
Line 836... Line 892...
836   892  
837 throw; 893 throw;
838 } -  
839 finally -  
840 { -  
841 _snapshotSemaphore.Release(); 894 }
842 } 895 }
843 } 896 }
-   897 }
-   898 finally
-   899 {
-   900 _databaseLock.Release();
844 } 901 }
Line 845... Line 902...
845 } 902 }
846   903  
847 public async Task RemoveFileFast(IEnumerable<string> hashes, CancellationToken cancellationToken) 904 public async Task RemoveFileFast(IEnumerable<string> hashes, CancellationToken cancellationToken)
848 { 905 {
849 var connectionString = new SQLiteConnectionStringBuilder 906 var connectionString = new SQLiteConnectionStringBuilder
850 { 907 {
Line -... Line 908...
-   908 ConnectionString = DatabaseConnectionString
-   909 };
-   910  
851 ConnectionString = DatabaseConnectionString 911 await _databaseLock.WaitAsync(cancellationToken);
852 }; 912 try
853   913 {
Line 854... Line 914...
854 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 914 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 891... Line 951...
891 throw; 951 throw;
892 } 952 }
893 } 953 }
894 } 954 }
895 } 955 }
-   956 finally
-   957 {
-   958 _databaseLock.Release();
-   959 }
-   960 }
Line 896... Line 961...
896   961  
897 public async Task RemoveFile(string hash, CancellationToken cancellationToken) 962 public async Task RemoveFile(string hash, CancellationToken cancellationToken)
898 { 963 {
899 var connectionString = new SQLiteConnectionStringBuilder 964 var connectionString = new SQLiteConnectionStringBuilder
900 { 965 {
901 ConnectionString = DatabaseConnectionString 966 ConnectionString = DatabaseConnectionString
-   967 };
-   968 await _databaseLock.WaitAsync(cancellationToken);
902 }; 969 try
903   970 {
904 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 971 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
905 { 972 {
Line 906... Line 973...
906 await sqliteConnection.OpenAsync(cancellationToken); 973 await sqliteConnection.OpenAsync(cancellationToken);
907   974  
908 using (var dbTransaction = sqliteConnection.BeginTransaction()) 975 using (var dbTransaction = sqliteConnection.BeginTransaction())
909 { 976 {
910 // Insert the file change. 977 // Insert the file change.
911 using (var sqliteCommand = 978 using (var sqliteCommand =
912 new SQLiteCommand(RemoveSnapshotFromHashSql, sqliteConnection, dbTransaction)) -  
913 { -  
914 try 979 new SQLiteCommand(RemoveSnapshotFromHashSql, sqliteConnection, dbTransaction))
915 { 980 {
916 sqliteCommand.Parameters.AddRange(new[] 981 sqliteCommand.Parameters.AddRange(new[]
917 { 982 {
Line 918... Line 983...
918 new SQLiteParameter("@hash", hash) 983 new SQLiteParameter("@hash", hash)
Line -... Line 984...
-   984 });
-   985  
919 }); 986 sqliteCommand.Prepare();
Line 920... Line 987...
920   987  
921 sqliteCommand.Prepare(); 988 try
922   989 {
Line 932... Line 999...
932 } 999 }
933 } 1000 }
934 } 1001 }
935 } 1002 }
936 } 1003 }
-   1004 finally
-   1005 {
-   1006 _databaseLock.Release();
-   1007 }
-   1008 }
Line 937... Line 1009...
937   1009  
938 public async Task UpdateColor(string hash, Color color, CancellationToken cancellationToken) 1010 public async Task UpdateColor(string hash, Color color, CancellationToken cancellationToken)
939 { 1011 {
940 var connectionString = new SQLiteConnectionStringBuilder 1012 var connectionString = new SQLiteConnectionStringBuilder
941 { 1013 {
942 ConnectionString = DatabaseConnectionString 1014 ConnectionString = DatabaseConnectionString
Line -... Line 1015...
-   1015 };
-   1016  
-   1017 await _databaseLock.WaitAsync(cancellationToken);
-   1018  
943 }; 1019 try
944   1020 {
945 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1021 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 946... Line 1022...
946 { 1022 {
947 await sqliteConnection.OpenAsync(cancellationToken); 1023 await sqliteConnection.OpenAsync(cancellationToken);
948   1024  
949 using (var dbTransaction = sqliteConnection.BeginTransaction()) 1025 using (var dbTransaction = sqliteConnection.BeginTransaction())
950 { 1026 {
951 // Insert the file change. 1027 // Insert the file change.
952 using (var sqliteCommand = -  
953 new SQLiteCommand(UpdateColorFromHashSql, sqliteConnection, dbTransaction)) -  
954 { 1028 using (var sqliteCommand =
955 try 1029 new SQLiteCommand(UpdateColorFromHashSql, sqliteConnection, dbTransaction))
956 { 1030 {
957 sqliteCommand.Parameters.AddRange(new[] 1031 sqliteCommand.Parameters.AddRange(new[]
958 { 1032 {
Line 959... Line 1033...
959 new SQLiteParameter("@hash", hash), 1033 new SQLiteParameter("@hash", hash),
Line -... Line 1034...
-   1034 new SQLiteParameter("@color", color.ToArgb())
-   1035 });
960 new SQLiteParameter("@color", color.ToArgb()) 1036  
Line 961... Line 1037...
961 }); 1037 sqliteCommand.Prepare();
962   1038  
963 sqliteCommand.Prepare(); 1039 try
Line 974... Line 1050...
974 } 1050 }
975 } 1051 }
976 } 1052 }
977 } 1053 }
978 } 1054 }
-   1055 finally
-   1056 {
-   1057 _databaseLock.Release();
-   1058 }
-   1059 }
Line 979... Line 1060...
979   1060  
980 public async Task RemoveColor(string hash, CancellationToken cancellationToken) 1061 public async Task RemoveColor(string hash, CancellationToken cancellationToken)
981 { 1062 {
982 var connectionString = new SQLiteConnectionStringBuilder 1063 var connectionString = new SQLiteConnectionStringBuilder
983 { 1064 {
984 ConnectionString = DatabaseConnectionString 1065 ConnectionString = DatabaseConnectionString
Line -... Line 1066...
-   1066 };
-   1067  
-   1068 await _databaseLock.WaitAsync(cancellationToken);
985 }; 1069 try
986   1070 {
987 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1071 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 988... Line 1072...
988 { 1072 {
989 await sqliteConnection.OpenAsync(cancellationToken); 1073 await sqliteConnection.OpenAsync(cancellationToken);
990   1074  
991 using (var dbTransaction = sqliteConnection.BeginTransaction()) 1075 using (var dbTransaction = sqliteConnection.BeginTransaction())
992 { 1076 {
993 // Insert the file change. 1077 // Insert the file change.
994 using (var sqliteCommand = -  
995 new SQLiteCommand(RemoveColorFromHashSql, sqliteConnection, dbTransaction)) -  
996 { 1078 using (var sqliteCommand =
997 try 1079 new SQLiteCommand(RemoveColorFromHashSql, sqliteConnection, dbTransaction))
998 { 1080 {
999 sqliteCommand.Parameters.AddRange(new[] 1081 sqliteCommand.Parameters.AddRange(new[]
Line 1000... Line 1082...
1000 { 1082 {
Line -... Line 1083...
-   1083 new SQLiteParameter("@hash", hash)
-   1084 });
1001 new SQLiteParameter("@hash", hash) 1085  
Line 1002... Line 1086...
1002 }); 1086 sqliteCommand.Prepare();
1003   1087  
1004 sqliteCommand.Prepare(); 1088 try
Line 1015... Line 1099...
1015 } 1099 }
1016 } 1100 }
1017 } 1101 }
1018 } 1102 }
1019 } 1103 }
-   1104 finally
-   1105 {
-   1106 _databaseLock.Release();
-   1107 }
-   1108 }
Line 1020... Line 1109...
1020   1109  
1021 public async Task<SnapshotPreview> RetrievePreview(string hash, CancellationToken cancellationToken) 1110 public async Task<SnapshotPreview> RetrievePreview(string hash, CancellationToken cancellationToken)
1022 { 1111 {
1023 var connectionString = new SQLiteConnectionStringBuilder 1112 var connectionString = new SQLiteConnectionStringBuilder
1024 { 1113 {
1025 ConnectionString = DatabaseConnectionString 1114 ConnectionString = DatabaseConnectionString
Line -... Line 1115...
-   1115 };
-   1116  
-   1117 await _databaseLock.WaitAsync(cancellationToken);
1026 }; 1118 try
1027   1119 {
1028 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1120 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1029... Line 1121...
1029 { 1121 {
Line 1073... Line 1165...
1073 return null; 1165 return null;
1074 } 1166 }
1075 } 1167 }
1076 } 1168 }
1077 } 1169 }
1078   -  
1079 /* -  
1080 public MemoryStream RetrieveFileStream(string hash, CancellationToken cancellationToken) -  
1081 { -  
1082 var connectionString = new SQLiteConnectionStringBuilder -  
1083 { -  
1084 ConnectionString = DatabaseConnectionString -  
1085 }; -  
1086   -  
1087 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) -  
1088 { -  
1089   -  
1090 sqliteConnection.Open(); -  
1091   -  
1092 // Insert the file change. -  
1093 using (var sqliteCommand = new SQLiteCommand(RetrieveDataFromHashSql, sqliteConnection)) -  
1094 { 1170 finally
1095 sqliteCommand.Parameters.AddRange(new[] -  
1096 { -  
1097 new SQLiteParameter("@hash", hash) -  
1098 }); -  
1099   -  
1100 sqliteCommand.Prepare(); -  
1101   -  
1102 using (var sqlDataReader = sqliteCommand.ExecuteReader()) -  
1103 { -  
1104   -  
1105 while (sqlDataReader.Read()) -  
1106 { -  
1107 using (var readStream = sqlDataReader.GetStream(1)) -  
1108 { -  
1109   -  
1110 using (var memoryStream = new MemoryStream()) -  
1111 { -  
1112   -  
1113 readStream.Position = 0L; -  
1114   -  
1115 readStream.CopyTo(memoryStream); -  
1116   -  
1117 memoryStream.Position = 0L; -  
1118   -  
1119 using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) -  
1120 { 1171 {
1121   -  
1122 var outputStream = new MemoryStream(); -  
1123   -  
1124 zipStream.CopyTo(outputStream); -  
1125   -  
1126 outputStream.Position = 0L; -  
1127   -  
1128 return outputStream; -  
1129 } -  
1130 } -  
1131 } -  
1132 } -  
1133   -  
1134 return null; 1172 _databaseLock.Release();
1135 } -  
1136 } -  
1137 } 1173 }
1138 } 1174 }
1139 */ -  
Line 1140... Line 1175...
1140   1175  
1141 public async Task<MemoryStream> RetrieveFileStream(string hash, CancellationToken cancellationToken) 1176 public async Task<MemoryStream> RetrieveFileStream(string hash, CancellationToken cancellationToken)
1142 { 1177 {
1143 var connectionString = new SQLiteConnectionStringBuilder 1178 var connectionString = new SQLiteConnectionStringBuilder
1144 { 1179 {
1145 ConnectionString = DatabaseConnectionString 1180 ConnectionString = DatabaseConnectionString
Line -... Line 1181...
-   1181 };
-   1182  
-   1183 await _databaseLock.WaitAsync(cancellationToken);
1146 }; 1184 try
1147   1185 {
1148 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1186 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1149... Line 1187...
1149 { 1187 {
Line 1191... Line 1229...
1191 return null; 1229 return null;
1192 } 1230 }
1193 } 1231 }
1194 } 1232 }
1195 } 1233 }
-   1234 finally
-   1235 {
-   1236 _databaseLock.Release();
-   1237 }
-   1238 }
Line 1196... Line 1239...
1196   1239  
1197 public async Task RelocateFile(string hash, string path, CancellationToken cancellationToken) 1240 public async Task RelocateFile(string hash, string path, CancellationToken cancellationToken)
1198 { 1241 {
1199 var connectionString = new SQLiteConnectionStringBuilder 1242 var connectionString = new SQLiteConnectionStringBuilder
1200 { 1243 {
1201 ConnectionString = DatabaseConnectionString 1244 ConnectionString = DatabaseConnectionString
Line -... Line 1245...
-   1245 };
-   1246  
-   1247 await _databaseLock.WaitAsync(cancellationToken);
1202 }; 1248 try
1203   1249 {
1204 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1250 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1205... Line 1251...
1205 { 1251 {
1206 await sqliteConnection.OpenAsync(cancellationToken); 1252 await sqliteConnection.OpenAsync(cancellationToken);
1207   1253  
1208 using (var dbTransaction = sqliteConnection.BeginTransaction()) 1254 using (var dbTransaction = sqliteConnection.BeginTransaction())
1209 { 1255 {
1210 // Insert the file change. 1256 // Insert the file change.
1211 using (var sqliteCommand = -  
1212 new SQLiteCommand(RelocateFileFromHashSql, sqliteConnection, dbTransaction)) -  
1213 { 1257 using (var sqliteCommand =
1214 try 1258 new SQLiteCommand(RelocateFileFromHashSql, sqliteConnection, dbTransaction))
1215 { 1259 {
1216 sqliteCommand.Parameters.AddRange(new[] 1260 sqliteCommand.Parameters.AddRange(new[]
1217 { 1261 {
Line 1218... Line 1262...
1218 new SQLiteParameter("@hash", hash), 1262 new SQLiteParameter("@hash", hash),
Line -... Line 1263...
-   1263 new SQLiteParameter("@path", path)
-   1264 });
1219 new SQLiteParameter("@path", path) 1265  
Line 1220... Line 1266...
1220 }); 1266 sqliteCommand.Prepare();
1221   1267  
1222 sqliteCommand.Prepare(); 1268 try
Line 1234... Line 1280...
1234 } 1280 }
1235 } 1281 }
1236 } 1282 }
1237 } 1283 }
Line -... Line 1284...
-   1284  
-   1285 finally
-   1286 {
-   1287 _databaseLock.Release();
-   1288 }
-   1289 }
1238   1290  
1239 public async Task UpdateNote(string hash, string note, CancellationToken cancellationToken) 1291 public async Task UpdateNote(string hash, string note, CancellationToken cancellationToken)
1240 { 1292 {
1241 var connectionString = new SQLiteConnectionStringBuilder 1293 var connectionString = new SQLiteConnectionStringBuilder
1242 { 1294 {
1243 ConnectionString = DatabaseConnectionString 1295 ConnectionString = DatabaseConnectionString
Line -... Line 1296...
-   1296 };
-   1297  
-   1298 await _databaseLock.WaitAsync(cancellationToken);
1244 }; 1299 try
1245   1300 {
1246 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1301 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1247... Line 1302...
1247 { 1302 {
1248 await sqliteConnection.OpenAsync(cancellationToken); 1303 await sqliteConnection.OpenAsync(cancellationToken);
1249   1304  
1250 using (var dbTransaction = sqliteConnection.BeginTransaction()) 1305 using (var dbTransaction = sqliteConnection.BeginTransaction())
1251 { 1306 {
1252 // Insert the file change. 1307 // Insert the file change.
1253 using (var sqliteCommand = -  
1254 new SQLiteCommand(UpdateNoteFromHashSql, sqliteConnection, dbTransaction)) -  
1255 { 1308 using (var sqliteCommand =
1256 try 1309 new SQLiteCommand(UpdateNoteFromHashSql, sqliteConnection, dbTransaction))
1257 { 1310 {
1258 sqliteCommand.Parameters.AddRange(new[] 1311 sqliteCommand.Parameters.AddRange(new[]
1259 { 1312 {
Line 1260... Line 1313...
1260 new SQLiteParameter("@hash", hash), 1313 new SQLiteParameter("@hash", hash),
Line -... Line 1314...
-   1314 new SQLiteParameter("@note", note)
-   1315 });
1261 new SQLiteParameter("@note", note) 1316  
Line 1262... Line 1317...
1262 }); 1317 sqliteCommand.Prepare();
Line 1263... Line 1318...
1263   1318  
Line 1279... Line 1334...
1279 } 1334 }
1280 } 1335 }
1281 } 1336 }
1282 } 1337 }
1283 } 1338 }
-   1339 finally
-   1340 {
-   1341 _databaseLock.Release();
-   1342 }
-   1343 }
Line 1284... Line 1344...
1284   1344  
1285 public async Task<string> UpdateFile(string hash, byte[] data, CancellationToken cancellationToken) 1345 public async Task<string> UpdateFile(string hash, byte[] data, CancellationToken cancellationToken)
1286 { -  
1287 using (var dataMemoryStream = new MemoryStream(data)) -  
1288 { 1346 {
1289 var connectionString = new SQLiteConnectionStringBuilder 1347 var connectionString = new SQLiteConnectionStringBuilder
1290 { 1348 {
1291 ConnectionString = DatabaseConnectionString 1349 ConnectionString = DatabaseConnectionString
Line -... Line 1350...
-   1350 };
-   1351  
-   1352 await _databaseLock.WaitAsync(cancellationToken);
-   1353 try
-   1354 {
1292 }; 1355 using (var dataMemoryStream = new MemoryStream(data))
1293   1356 {
1294 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1357 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1295... Line 1358...
1295 { 1358 {
Line 1322... Line 1385...
1322   1385  
Line 1323... Line 1386...
1323 fileMemoryStream.Position = 0L; 1386 fileMemoryStream.Position = 0L;
1324   1387  
1325 // Insert the file change. 1388 // Insert the file change.
-   1389 using (var sqliteCommand =
1326 using (var sqliteCommand = 1390 new SQLiteCommand(UpdateFileSql, sqliteConnection,
1327 new SQLiteCommand(UpdateFileSql, sqliteConnection, dbTransaction)) 1391 dbTransaction))
1328 { 1392 {
1329 sqliteCommand.Parameters.AddRange(new[] 1393 sqliteCommand.Parameters.AddRange(new[]
1330 { 1394 {
Line 1353... Line 1417...
1353 { 1417 {
1354 while (await sqlDataReader.ReadAsync(cancellationToken)) 1418 while (await sqlDataReader.ReadAsync(cancellationToken))
1355 { 1419 {
1356 if (sqlDataReader["id"] is long rowId) 1420 if (sqlDataReader["id"] is long rowId)
1357 { 1421 {
1358 using (var sqliteBlob = SQLiteBlob.Create(sqliteConnection, 1422 using (var sqliteBlob = SQLiteBlob.Create(
-   1423 sqliteConnection,
1359 "main", 1424 "main",
1360 "Snapshots", 1425 "Snapshots",
1361 "Data", 1426 "Data",
1362 rowId, false)) 1427 rowId, false))
1363 { 1428 {
-   1429 var fileMemoryStreamData =
1364 var fileMemoryStreamData = fileMemoryStream.ToArray(); 1430 fileMemoryStream.ToArray();
Line 1365... Line 1431...
1365   1431  
1366 sqliteBlob.Write(fileMemoryStreamData, 1432 sqliteBlob.Write(fileMemoryStreamData,
1367 fileMemoryStreamData.Length, 1433 fileMemoryStreamData.Length,
1368 0); 1434 0);
Line 1393... Line 1459...
1393 } 1459 }
1394 } 1460 }
1395 } 1461 }
1396 } 1462 }
1397 } 1463 }
-   1464 finally
-   1465 {
-   1466 _databaseLock.Release();
-   1467 }
-   1468 }
Line 1398... Line 1469...
1398   1469  
1399 public async Task UpdateHash(string from, string to, CancellationToken cancellationToken) 1470 public async Task UpdateHash(string from, string to, CancellationToken cancellationToken)
1400 { 1471 {
1401 var connectionString = new SQLiteConnectionStringBuilder 1472 var connectionString = new SQLiteConnectionStringBuilder
1402 { 1473 {
1403 ConnectionString = DatabaseConnectionString 1474 ConnectionString = DatabaseConnectionString
Line -... Line 1475...
-   1475 };
-   1476  
-   1477 await _databaseLock.WaitAsync(cancellationToken);
1404 }; 1478 try
1405   1479 {
1406 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1480 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1407... Line 1481...
1407 { 1481 {
1408 await sqliteConnection.OpenAsync(cancellationToken); 1482 await sqliteConnection.OpenAsync(cancellationToken);
1409   1483  
1410 using (var dbTransaction = sqliteConnection.BeginTransaction()) 1484 using (var dbTransaction = sqliteConnection.BeginTransaction())
1411 { 1485 {
1412 // Insert the file change. 1486 // Insert the file change.
1413 using (var sqliteCommand = -  
1414 new SQLiteCommand(UpdateHashFromHashSql, sqliteConnection, dbTransaction)) -  
1415 { 1487 using (var sqliteCommand =
1416 try 1488 new SQLiteCommand(UpdateHashFromHashSql, sqliteConnection, dbTransaction))
1417 { 1489 {
1418 sqliteCommand.Parameters.AddRange(new[] 1490 sqliteCommand.Parameters.AddRange(new[]
1419 { 1491 {
Line 1420... Line 1492...
1420 new SQLiteParameter("@from", from), 1492 new SQLiteParameter("@from", from),
Line -... Line 1493...
-   1493 new SQLiteParameter("@to", to)
-   1494 });
1421 new SQLiteParameter("@to", to) 1495  
Line 1422... Line 1496...
1422 }); 1496 sqliteCommand.Prepare();
1423   1497  
1424 sqliteCommand.Prepare(); 1498 try
Line 1435... Line 1509...
1435 } 1509 }
1436 } 1510 }
1437 } 1511 }
1438 } 1512 }
1439 } 1513 }
-   1514 finally
-   1515 {
-   1516 _databaseLock.Release();
-   1517 }
-   1518 }
Line 1440... Line 1519...
1440   1519  
Line 1441... Line 1520...
1441 #endregion 1520 #endregion
Line 1442... Line 1521...
1442   1521  
1443 #region Private Methods 1522 #region Private Methods
1444   1523  
1445 private static async Task SetAutoVacuum(CancellationToken cancellationToken) 1524 private async Task SetAutoVacuum(CancellationToken cancellationToken)
1446 { 1525 {
1447 var connectionString = new SQLiteConnectionStringBuilder 1526 var connectionString = new SQLiteConnectionStringBuilder
Line -... Line 1527...
-   1527 {
-   1528 ConnectionString = DatabaseConnectionString
-   1529 };
1448 { 1530  
1449 ConnectionString = DatabaseConnectionString 1531 await _databaseLock.WaitAsync(cancellationToken);
1450 }; 1532 try
1451   1533 {
Line 1459... Line 1541...
1459 { 1541 {
1460 await sqliteCommand.ExecuteNonQueryAsync(cancellationToken); 1542 await sqliteCommand.ExecuteNonQueryAsync(cancellationToken);
1461 } 1543 }
1462 } 1544 }
1463 } 1545 }
-   1546 finally
-   1547 {
-   1548 _databaseLock.Release();
-   1549 }
-   1550 }
Line 1464... Line 1551...
1464   1551  
1465 private static async Task CreateDatabase(CancellationToken cancellationToken) 1552 private async Task CreateDatabase(CancellationToken cancellationToken)
1466 { 1553 {
1467 var connectionString = new SQLiteConnectionStringBuilder 1554 var connectionString = new SQLiteConnectionStringBuilder
1468 { 1555 {
1469 ConnectionString = DatabaseConnectionString 1556 ConnectionString = DatabaseConnectionString
Line -... Line 1557...
-   1557 };
-   1558  
-   1559 await _databaseLock.WaitAsync(cancellationToken);
1470 }; 1560 try
1471   1561 {
1472 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString)) 1562 using (var sqliteConnection = new SQLiteConnection(connectionString.ConnectionString))
Line 1473... Line 1563...
1473 { 1563 {
Line 1492... Line 1582...
1492 } 1582 }
1493 } 1583 }
1494 } 1584 }
1495 } 1585 }
1496 } 1586 }
-   1587 finally
-   1588 {
-   1589 _databaseLock.Release();
-   1590 }
-   1591 }
Line 1497... Line 1592...
1497   1592  
1498 #endregion 1593 #endregion
1499 } 1594 }
1500 } 1595 }