SqliteDbAccessor.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using AIPlatform.Protocol.Entities;
  2. using AIPlatform.Protocol.Model;
  3. using SQLite.Net;
  4. using SQLite.Net.Interop;
  5. using SQLite.Net.Platform.Win32;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. namespace aipagnt
  10. {
  11. class TrainGroupCache : TrainGroupModel
  12. {
  13. }
  14. class TestGroupCache : TrainGroupCache
  15. {
  16. }
  17. class TrainLabeledFileCache : EntityBase
  18. {
  19. public bool IsVideo { get; set; }
  20. public byte[] FileData { get; set; }
  21. public string LabelContent { get; set; }
  22. public int Index { get; set; }
  23. public int GroupIndex { get; set; }
  24. /// <summary>
  25. /// 模态名称
  26. /// </summary>
  27. public string ModalTitle { get; set; }
  28. }
  29. class TestLabeledFileCache : TrainLabeledFileCache
  30. {
  31. }
  32. class SqliteDbAccessor
  33. {
  34. private static SqliteDbAccessor _instance;
  35. private readonly object _dbLock = new object();
  36. /// <summary>
  37. /// The sqlite connection.
  38. /// </summary>
  39. private SQLiteConnection _connection;
  40. private string _dbPath;
  41. private ISQLitePlatform _platform;
  42. private Dictionary<int, TrainGroupCache> _trainGroupCaches = new Dictionary<int, TrainGroupCache>();
  43. private Dictionary<int, TestGroupCache> _testGroupCaches = new Dictionary<int, TestGroupCache>();
  44. private Dictionary<long, TrainLabeledFileCache> _trainFileCaches = new Dictionary<long, TrainLabeledFileCache>();
  45. private Dictionary<long, TestLabeledFileCache> _testFileCaches = new Dictionary<long, TestLabeledFileCache>();
  46. private const int BatchSize = 1000;
  47. public static SqliteDbAccessor Instance => _instance ?? (_instance = new SqliteDbAccessor());
  48. /// <summary>
  49. /// Initialize db
  50. /// </summary>
  51. public void Initialize()
  52. {
  53. _platform = new SQLitePlatformWin32();
  54. ConnectDatabase();
  55. }
  56. private void ConnectDatabase()
  57. {
  58. var cacheFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "ImagesCache");
  59. if (!Directory.Exists(cacheFolder))
  60. {
  61. Directory.CreateDirectory(cacheFolder);
  62. }
  63. var dbFileName = "aipagent.imagescache.db";
  64. _dbPath = Path.Combine(cacheFolder, dbFileName);
  65. if (File.Exists(_dbPath))
  66. {
  67. File.SetAttributes(_dbPath, FileAttributes.Normal);
  68. File.Delete(_dbPath);
  69. }
  70. _connection = new SQLiteConnection(_platform, _dbPath);
  71. CreateTableGroup<TrainGroupCache>();
  72. CreateTableGroup<TestGroupCache>();
  73. CreateTable<TrainLabeledFileCache>();
  74. CreateTable<TestLabeledFileCache>();
  75. }
  76. #region Group
  77. /// <summary>
  78. /// Create table
  79. /// </summary>
  80. /// <typeparam name="T">Table type.</typeparam>
  81. private void CreateTableGroup<T>() where T : TrainGroupCache
  82. {
  83. lock (_dbLock)
  84. {
  85. _connection.CreateTable<T>();
  86. _connection.CreateIndex<T>(x => x.Index);
  87. }
  88. }
  89. public T GetGroup<T>(int index) where T : TrainGroupCache
  90. {
  91. lock (_dbLock)
  92. {
  93. return _connection.Find<T>(x => x.Index == index);
  94. }
  95. }
  96. /// <summary>
  97. /// Create or update a exam record cache
  98. /// </summary>
  99. /// <param name="model"></param>
  100. public void InsertGroup<T>(T model) where T : TrainGroupCache
  101. {
  102. lock (_dbLock)
  103. {
  104. if (model is TestGroupCache testGroupCache)
  105. {
  106. if (_testGroupCaches.ContainsKey(model.Index))
  107. {
  108. _connection.InsertAll(_testGroupCaches.Values);
  109. _testGroupCaches.Clear();
  110. }
  111. else
  112. {
  113. _testGroupCaches.Add(testGroupCache.Index, testGroupCache);
  114. }
  115. }
  116. else
  117. {
  118. if (_trainGroupCaches.ContainsKey(model.Index))
  119. {
  120. _connection.InsertAll(_trainGroupCaches.Values);
  121. _trainGroupCaches.Clear();
  122. }
  123. else
  124. {
  125. _trainGroupCaches.Add(model.Index, model);
  126. }
  127. }
  128. if (_trainGroupCaches.Count >= BatchSize)
  129. {
  130. _connection.InsertAll(_trainGroupCaches.Values);
  131. _trainGroupCaches.Clear();
  132. }
  133. if (_testGroupCaches.Count >= BatchSize)
  134. {
  135. _connection.InsertAll(_testGroupCaches.Values);
  136. _testGroupCaches.Clear();
  137. }
  138. }
  139. }
  140. #endregion
  141. #region File
  142. /// <summary>
  143. /// Create table
  144. /// </summary>
  145. /// <typeparam name="T">Table type.</typeparam>
  146. private void CreateTable<T>() where T : TrainLabeledFileCache
  147. {
  148. lock (_dbLock)
  149. {
  150. _connection.CreateTable<T>();
  151. _connection.CreateIndex<T>(x => x.Id);
  152. }
  153. }
  154. public T Get<T>(long id) where T : TrainLabeledFileCache
  155. {
  156. lock (_dbLock)
  157. {
  158. return _connection.Find<T>(x => x.Id == id);
  159. }
  160. }
  161. /// <summary>
  162. /// Create or update a exam record cache
  163. /// </summary>
  164. /// <param name="model"></param>
  165. public void Insert<T>(T model) where T : TrainLabeledFileCache
  166. {
  167. lock (_dbLock)
  168. {
  169. if (model is TestLabeledFileCache testLabeledImageCache)
  170. {
  171. if (_testFileCaches.ContainsKey(model.Id))
  172. {
  173. _connection.InsertAll(_testFileCaches.Values);
  174. _testFileCaches.Clear();
  175. }
  176. else
  177. {
  178. _testFileCaches.Add(testLabeledImageCache.Id, testLabeledImageCache);
  179. }
  180. }
  181. else
  182. {
  183. if (_trainFileCaches.ContainsKey(model.Id))
  184. {
  185. _connection.InsertAll(_trainFileCaches.Values);
  186. _trainFileCaches.Clear();
  187. }
  188. else
  189. {
  190. _trainFileCaches.Add(model.Id, model);
  191. }
  192. }
  193. if (_trainFileCaches.Count >= BatchSize)
  194. {
  195. _connection.InsertAll(_trainFileCaches.Values);
  196. _trainFileCaches.Clear();
  197. }
  198. if (_testFileCaches.Count >= BatchSize)
  199. {
  200. _connection.InsertAll(_testFileCaches.Values);
  201. _testFileCaches.Clear();
  202. }
  203. }
  204. }
  205. #endregion
  206. /// <summary>
  207. /// Clear current database
  208. /// </summary>
  209. public void Reset()
  210. {
  211. lock (_dbLock)
  212. {
  213. _trainGroupCaches.Clear();
  214. _testGroupCaches.Clear();
  215. _testFileCaches.Clear();
  216. _trainFileCaches.Clear();
  217. if (_connection != null)
  218. {
  219. _connection.Close();
  220. _connection.Dispose();
  221. }
  222. ConnectDatabase();
  223. }
  224. }
  225. }
  226. }