SqliteDisplayPreferencesRepository.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Extensions;
  8. using MediaBrowser.Controller.Persistence;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using SQLitePCL.pretty;
  14. namespace Emby.Server.Implementations.Data
  15. {
  16. /// <summary>
  17. /// Class SQLiteDisplayPreferencesRepository
  18. /// </summary>
  19. public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
  20. {
  21. private readonly IMemoryStreamFactory _memoryStreamProvider;
  22. public SqliteDisplayPreferencesRepository(ILogger logger, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IMemoryStreamFactory memoryStreamProvider)
  23. : base(logger)
  24. {
  25. _jsonSerializer = jsonSerializer;
  26. _memoryStreamProvider = memoryStreamProvider;
  27. DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
  28. }
  29. /// <summary>
  30. /// Gets the name of the repository
  31. /// </summary>
  32. /// <value>The name.</value>
  33. public string Name
  34. {
  35. get
  36. {
  37. return "SQLite";
  38. }
  39. }
  40. /// <summary>
  41. /// The _json serializer
  42. /// </summary>
  43. private readonly IJsonSerializer _jsonSerializer;
  44. /// <summary>
  45. /// Opens the connection to the database
  46. /// </summary>
  47. /// <returns>Task.</returns>
  48. public void Initialize()
  49. {
  50. using (var connection = CreateConnection())
  51. {
  52. connection.ExecuteAll(string.Join(";", new[]
  53. {
  54. "PRAGMA page_size=4096",
  55. "pragma default_temp_store = memory",
  56. "pragma temp_store = memory"
  57. }));
  58. string[] queries = {
  59. "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)",
  60. "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)"
  61. };
  62. connection.RunQueries(queries);
  63. }
  64. }
  65. /// <summary>
  66. /// Save the display preferences associated with an item in the repo
  67. /// </summary>
  68. /// <param name="displayPreferences">The display preferences.</param>
  69. /// <param name="userId">The user id.</param>
  70. /// <param name="client">The client.</param>
  71. /// <param name="cancellationToken">The cancellation token.</param>
  72. /// <returns>Task.</returns>
  73. /// <exception cref="System.ArgumentNullException">item</exception>
  74. public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
  75. {
  76. if (displayPreferences == null)
  77. {
  78. throw new ArgumentNullException("displayPreferences");
  79. }
  80. if (string.IsNullOrWhiteSpace(displayPreferences.Id))
  81. {
  82. throw new ArgumentNullException("displayPreferences.Id");
  83. }
  84. cancellationToken.ThrowIfCancellationRequested();
  85. using (var connection = CreateConnection())
  86. {
  87. using (WriteLock.Write())
  88. {
  89. connection.RunInTransaction(db =>
  90. {
  91. SaveDisplayPreferences(displayPreferences, userId, client, db);
  92. }, TransactionMode);
  93. }
  94. }
  95. }
  96. private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
  97. {
  98. var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
  99. using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)"))
  100. {
  101. statement.TryBind("@id", displayPreferences.Id.ToGuidParamValue());
  102. statement.TryBind("@userId", userId.ToGuidParamValue());
  103. statement.TryBind("@client", client);
  104. statement.TryBind("@data", serialized);
  105. statement.MoveNext();
  106. }
  107. }
  108. /// <summary>
  109. /// Save all display preferences associated with a user in the repo
  110. /// </summary>
  111. /// <param name="displayPreferences">The display preferences.</param>
  112. /// <param name="userId">The user id.</param>
  113. /// <param name="cancellationToken">The cancellation token.</param>
  114. /// <returns>Task.</returns>
  115. /// <exception cref="System.ArgumentNullException">item</exception>
  116. public async Task SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
  117. {
  118. if (displayPreferences == null)
  119. {
  120. throw new ArgumentNullException("displayPreferences");
  121. }
  122. cancellationToken.ThrowIfCancellationRequested();
  123. using (var connection = CreateConnection())
  124. {
  125. using (WriteLock.Write())
  126. {
  127. connection.RunInTransaction(db =>
  128. {
  129. foreach (var displayPreference in displayPreferences)
  130. {
  131. SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
  132. }
  133. }, TransactionMode);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the display preferences.
  139. /// </summary>
  140. /// <param name="displayPreferencesId">The display preferences id.</param>
  141. /// <param name="userId">The user id.</param>
  142. /// <param name="client">The client.</param>
  143. /// <returns>Task{DisplayPreferences}.</returns>
  144. /// <exception cref="System.ArgumentNullException">item</exception>
  145. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
  146. {
  147. if (string.IsNullOrWhiteSpace(displayPreferencesId))
  148. {
  149. throw new ArgumentNullException("displayPreferencesId");
  150. }
  151. var guidId = displayPreferencesId.GetMD5();
  152. using (var connection = CreateConnection(true))
  153. {
  154. using (WriteLock.Read())
  155. {
  156. using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
  157. {
  158. statement.TryBind("@id", guidId.ToGuidParamValue());
  159. statement.TryBind("@userId", userId.ToGuidParamValue());
  160. statement.TryBind("@client", client);
  161. foreach (var row in statement.ExecuteQuery())
  162. {
  163. return Get(row);
  164. }
  165. }
  166. return new DisplayPreferences
  167. {
  168. Id = guidId.ToString("N")
  169. };
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Gets all display preferences for the given user.
  175. /// </summary>
  176. /// <param name="userId">The user id.</param>
  177. /// <returns>Task{DisplayPreferences}.</returns>
  178. /// <exception cref="System.ArgumentNullException">item</exception>
  179. public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
  180. {
  181. var list = new List<DisplayPreferences>();
  182. using (var connection = CreateConnection(true))
  183. {
  184. using (WriteLock.Read())
  185. {
  186. using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
  187. {
  188. statement.TryBind("@userId", userId.ToGuidParamValue());
  189. foreach (var row in statement.ExecuteQuery())
  190. {
  191. list.Add(Get(row));
  192. }
  193. }
  194. }
  195. }
  196. return list;
  197. }
  198. private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row)
  199. {
  200. using (var stream = _memoryStreamProvider.CreateNew(row[0].ToBlob()))
  201. {
  202. stream.Position = 0;
  203. return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
  204. }
  205. }
  206. public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken)
  207. {
  208. return SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken);
  209. }
  210. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client)
  211. {
  212. return GetDisplayPreferences(displayPreferencesId, new Guid(userId), client);
  213. }
  214. }
  215. }