SQLiteUserDataRepository.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Persistence;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.Sqlite
  13. {
  14. /// <summary>
  15. /// Class SQLiteUserDataRepository
  16. /// </summary>
  17. public class SQLiteUserDataRepository : SqliteRepository, IUserDataRepository
  18. {
  19. /// <summary>
  20. /// The repository name
  21. /// </summary>
  22. public const string RepositoryName = "SQLite";
  23. /// <summary>
  24. /// Gets the name of the repository
  25. /// </summary>
  26. /// <value>The name.</value>
  27. public string Name
  28. {
  29. get
  30. {
  31. return RepositoryName;
  32. }
  33. }
  34. /// <summary>
  35. /// The _protobuf serializer
  36. /// </summary>
  37. private readonly IProtobufSerializer _protobufSerializer;
  38. /// <summary>
  39. /// The _app paths
  40. /// </summary>
  41. private readonly IApplicationPaths _appPaths;
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="SQLiteUserDataRepository" /> class.
  44. /// </summary>
  45. /// <param name="appPaths">The app paths.</param>
  46. /// <param name="protobufSerializer">The protobuf serializer.</param>
  47. /// <param name="logManager">The log manager.</param>
  48. /// <exception cref="System.ArgumentNullException">protobufSerializer</exception>
  49. public SQLiteUserDataRepository(IApplicationPaths appPaths, IProtobufSerializer protobufSerializer, ILogManager logManager)
  50. : base(logManager)
  51. {
  52. if (protobufSerializer == null)
  53. {
  54. throw new ArgumentNullException("protobufSerializer");
  55. }
  56. if (appPaths == null)
  57. {
  58. throw new ArgumentNullException("appPaths");
  59. }
  60. _protobufSerializer = protobufSerializer;
  61. _appPaths = appPaths;
  62. }
  63. /// <summary>
  64. /// Opens the connection to the database
  65. /// </summary>
  66. /// <returns>Task.</returns>
  67. public async Task Initialize()
  68. {
  69. var dbFile = Path.Combine(_appPaths.DataPath, "userdata.db");
  70. await ConnectToDB(dbFile).ConfigureAwait(false);
  71. string[] queries = {
  72. "create table if not exists userdata (id GUID, userId GUID, data BLOB)",
  73. "create unique index if not exists userdataindex on userdata (id, userId)",
  74. "create table if not exists schema_version (table_name primary key, version)",
  75. //pragmas
  76. "pragma temp_store = memory"
  77. };
  78. RunQueries(queries);
  79. }
  80. /// <summary>
  81. /// Saves the user data.
  82. /// </summary>
  83. /// <param name="userId">The user id.</param>
  84. /// <param name="userDataId">The user data id.</param>
  85. /// <param name="userData">The user data.</param>
  86. /// <param name="cancellationToken">The cancellation token.</param>
  87. /// <returns>Task.</returns>
  88. /// <exception cref="System.ArgumentNullException">
  89. /// userData
  90. /// or
  91. /// cancellationToken
  92. /// or
  93. /// userId
  94. /// or
  95. /// userDataId
  96. /// </exception>
  97. public Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData, CancellationToken cancellationToken)
  98. {
  99. if (userData == null)
  100. {
  101. throw new ArgumentNullException("userData");
  102. }
  103. if (cancellationToken == null)
  104. {
  105. throw new ArgumentNullException("cancellationToken");
  106. }
  107. if (userId == Guid.Empty)
  108. {
  109. throw new ArgumentNullException("userId");
  110. }
  111. if (userDataId == Guid.Empty)
  112. {
  113. throw new ArgumentNullException("userDataId");
  114. }
  115. cancellationToken.ThrowIfCancellationRequested();
  116. return Task.Run(() =>
  117. {
  118. var serialized = _protobufSerializer.SerializeToBytes(userData);
  119. cancellationToken.ThrowIfCancellationRequested();
  120. var cmd = connection.CreateCommand();
  121. cmd.CommandText = "replace into userdata (id, userId, data) values (@1, @2, @3)";
  122. cmd.AddParam("@1", userDataId);
  123. cmd.AddParam("@2", userId);
  124. cmd.AddParam("@3", serialized);
  125. QueueCommand(cmd);
  126. });
  127. }
  128. /// <summary>
  129. /// Gets the user data.
  130. /// </summary>
  131. /// <param name="userId">The user id.</param>
  132. /// <param name="userDataId">The user data id.</param>
  133. /// <returns>Task{UserItemData}.</returns>
  134. /// <exception cref="System.ArgumentNullException">
  135. /// userId
  136. /// or
  137. /// userDataId
  138. /// </exception>
  139. public async Task<UserItemData> GetUserData(Guid userId, Guid userDataId)
  140. {
  141. if (userId == Guid.Empty)
  142. {
  143. throw new ArgumentNullException("userId");
  144. }
  145. if (userDataId == Guid.Empty)
  146. {
  147. throw new ArgumentNullException("userDataId");
  148. }
  149. var cmd = connection.CreateCommand();
  150. cmd.CommandText = "select data from userdata where id = @id and userId=@userId";
  151. var idParam = cmd.Parameters.Add("@id", DbType.Guid);
  152. idParam.Value = userDataId;
  153. var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid);
  154. userIdParam.Value = userId;
  155. using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false))
  156. {
  157. if (reader.Read())
  158. {
  159. using (var stream = GetStream(reader, 0))
  160. {
  161. return _protobufSerializer.DeserializeFromStream<UserItemData>(stream);
  162. }
  163. }
  164. }
  165. return null;
  166. }
  167. }
  168. }