Advertisement
Guest User

Untitled

a guest
May 26th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Security.Policy;
  7. using System.Text;
  8. using System.IO;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Security.Cryptography;
  11. using System.Collections;
  12. using System.Threading.Tasks;
  13.  
  14. namespace creditinfogroup_export.BLL
  15. {
  16. public class Cacher : IDisposable
  17. {
  18. readonly Hashtable Hashes = Hashtable.Synchronized(new Hashtable());
  19. private const int hashLen = 16;
  20. private const string filename = "cache.txt";
  21. private int count = 0;
  22. const int threads = 2;
  23. readonly byte[] globalBuffer;
  24. public Cacher()
  25. {
  26. try
  27. {
  28. using (var sr = new FileStream(filename, FileMode.OpenOrCreate, FileSystemRights.Read, FileShare.Read, 1024*100, FileOptions.SequentialScan))
  29. {
  30. Hashes = Hashtable.Synchronized(new Hashtable((int)sr.Length / hashLen + 10000));
  31. int length = (int)sr.Length;
  32. globalBuffer=new byte[length];
  33. int sum = 0;
  34. while ((count = sr.Read(globalBuffer, sum, length - sum)) > 0)
  35. sum += count;
  36. }
  37. var tf = new TaskFactory();
  38. var taskQueue = new Queue<Task>();
  39. var tasks = new Task[threads];
  40. Action<object > action = readHashFile;
  41. for (int i = 0; i < threads; i++ )
  42. {
  43. var t = Task.Factory.StartNew(action,i);
  44. taskQueue.Enqueue(t);
  45.  
  46. }
  47. Task.WaitAll(taskQueue.ToArray());
  48.  
  49.  
  50. }
  51. catch
  52. {
  53. Hashes = Hashtable.Synchronized(new Hashtable());
  54. }
  55. }
  56. private void readHashFile(object thread)
  57. {
  58. var buff = new byte[hashLen];
  59. var part = (int)thread;
  60. var hashesCount = (globalBuffer.Length / hashLen);
  61. var hashesForThread = (int)Math.Floor(hashesCount / (decimal)threads);
  62. var start = hashesForThread * hashLen*part;
  63. var end = start + (hashesForThread * hashLen);
  64. if (end + hashLen >= globalBuffer.Length)
  65. end = globalBuffer.Length;
  66. var sb = new StringBuilder();
  67. for (int i = start; i < end-hashLen; i+= hashLen)
  68. {
  69. Buffer.BlockCopy(globalBuffer,i,buff,0,hashLen);
  70. count++;
  71. Hashes.Add(Convert.ToBase64String(buff), true);
  72. }
  73. }
  74.  
  75. public void Add(object obj)
  76. {
  77. try
  78. {
  79. Hashes.Add(ToCacheHash(obj.ToString()), true);
  80. }
  81. catch { }
  82. }
  83. public bool Exists (object o)
  84. {
  85. if (Hashes.ContainsKey(ToCacheHash(o.ToString())))
  86. return true;
  87. else
  88. return false;
  89. }
  90.  
  91. #region IDisposable Members
  92.  
  93. public void Dispose()
  94. {
  95. try
  96. {
  97. if (this.count < Hashes.Count)
  98. {
  99. using (
  100. var sr = new FileStream(filename, FileMode.Create, FileSystemRights.Modify, FileShare.None,
  101. 1024*10, FileOptions.SequentialScan))
  102. {
  103. foreach (string hash in Hashes.Keys)
  104. {
  105. var bytes = Convert.FromBase64String(hash);
  106. sr.Write(bytes, 0, bytes.Length);
  107. }
  108. sr.Flush();
  109. sr.Close();
  110. }
  111. }
  112.  
  113. }
  114. catch
  115. {
  116. }
  117. }
  118.  
  119. #endregion
  120.  
  121. byte[] ToHash (string s, HashAlgorithm h, int bytesLength)
  122. {
  123. byte[] data = System.Text.Encoding.ASCII.GetBytes(s);
  124. var ret = new byte[bytesLength];
  125. data = h.ComputeHash(data);
  126. for (int i = 0; i < Math.Min(data.Length, bytesLength); i++)
  127. ret[i] = data[i];
  128. return ret;
  129. }
  130.  
  131. string ToCacheHash(string s)
  132. {
  133. if (s == null)
  134. s = string.Empty;
  135. var result = new byte[hashLen];
  136. var sha = ToHash(s, new System.Security.Cryptography.SHA1Managed(), hashLen-5);
  137. var crc = ToHash(s, new Crc32(), 4);
  138. sha.CopyTo(result, 0);
  139. crc.CopyTo(result, 11);
  140. result[hashLen-1] = (byte)(s.Length%255);
  141.  
  142. return Convert.ToBase64String(result);
  143.  
  144. }
  145.  
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement