SQLiteUserDataRepository.cs 5.9 KB

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