SqliteExtensions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Logging;
  9. namespace MediaBrowser.Server.Implementations.Persistence
  10. {
  11. /// <summary>
  12. /// Class SQLiteExtensions
  13. /// </summary>
  14. public static class SqliteExtensions
  15. {
  16. /// <summary>
  17. /// Connects to db.
  18. /// </summary>
  19. /// <param name="dbPath">The db path.</param>
  20. /// <param name="logger">The logger.</param>
  21. /// <returns>Task{IDbConnection}.</returns>
  22. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  23. public static async Task<IDbConnection> ConnectToDb(string dbPath, ILogger logger)
  24. {
  25. if (string.IsNullOrEmpty(dbPath))
  26. {
  27. throw new ArgumentNullException("dbPath");
  28. }
  29. logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, dbPath);
  30. var connectionstr = new SQLiteConnectionStringBuilder
  31. {
  32. PageSize = 4096,
  33. CacheSize = 2000,
  34. SyncMode = SynchronizationModes.Normal,
  35. DataSource = dbPath,
  36. JournalMode = SQLiteJournalModeEnum.Wal
  37. };
  38. var connection = new SQLiteConnection(connectionstr.ConnectionString);
  39. await connection.OpenAsync().ConfigureAwait(false);
  40. return connection;
  41. }
  42. public static void BindGetSimilarityScore(IDbConnection connection, ILogger logger)
  43. {
  44. var sqlConnection = (SQLiteConnection) connection;
  45. SimiliarToFunction.Logger = logger;
  46. sqlConnection.BindFunction(new SimiliarToFunction());
  47. }
  48. public static void BindFunction(this SQLiteConnection connection, SQLiteFunction function)
  49. {
  50. var attributes = function.GetType().GetCustomAttributes(typeof(SQLiteFunctionAttribute), true).Cast<SQLiteFunctionAttribute>().ToArray();
  51. if (attributes.Length == 0)
  52. {
  53. throw new InvalidOperationException("SQLiteFunction doesn't have SQLiteFunctionAttribute");
  54. }
  55. connection.BindFunction(attributes[0], function);
  56. }
  57. }
  58. [SQLiteFunction(Name = "GetSimilarityScore", Arguments = 12, FuncType = FunctionType.Scalar)]
  59. public class SimiliarToFunction : SQLiteFunction
  60. {
  61. internal static ILogger Logger;
  62. public override object Invoke(object[] args)
  63. {
  64. var score = 0;
  65. var inputOfficialRating = args[0] as string;
  66. var rowOfficialRating = args[1] as string;
  67. if (!string.IsNullOrWhiteSpace(inputOfficialRating) && string.Equals(inputOfficialRating, rowOfficialRating))
  68. {
  69. score += 10;
  70. }
  71. long? inputYear = args[2] == null ? (long?)null : (long)args[2];
  72. long? rowYear = args[3] == null ? (long?)null : (long)args[3];
  73. if (inputYear.HasValue && rowYear.HasValue)
  74. {
  75. var diff = Math.Abs(inputYear.Value - rowYear.Value);
  76. // Add if they came out within the same decade
  77. if (diff < 10)
  78. {
  79. score += 2;
  80. }
  81. // And more if within five years
  82. if (diff < 5)
  83. {
  84. score += 2;
  85. }
  86. }
  87. // genres
  88. score += GetListScore(args, 4, 5);
  89. // tags
  90. score += GetListScore(args, 6, 7);
  91. // keywords
  92. score += GetListScore(args, 8, 9);
  93. // studios
  94. score += GetListScore(args, 10, 11, 3);
  95. // TODO: People
  96. // var item2PeopleNames = allPeople.Where(i => i.ItemId == item2.Id)
  97. //.Select(i => i.Name)
  98. //.Where(i => !string.IsNullOrWhiteSpace(i))
  99. //.DistinctNames()
  100. //.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  101. // points += item1People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>
  102. // {
  103. // if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
  104. // {
  105. // return 5;
  106. // }
  107. // if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
  108. // {
  109. // return 3;
  110. // }
  111. // if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase))
  112. // {
  113. // return 3;
  114. // }
  115. // if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
  116. // {
  117. // return 3;
  118. // }
  119. // if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
  120. // {
  121. // return 2;
  122. // }
  123. // return 1;
  124. // });
  125. // return points;
  126. //Logger.Debug("Returning score {0}", score);
  127. return score;
  128. }
  129. private int GetListScore(object[] args, int index1, int index2, int value = 10)
  130. {
  131. var score = 0;
  132. var inputGenres = args[index1] as string;
  133. var rowGenres = args[index2] as string;
  134. var inputGenreList = string.IsNullOrWhiteSpace(inputGenres) ? new string[] { } : inputGenres.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  135. var rowGenresList = string.IsNullOrWhiteSpace(rowGenres) ? new string[] { } : rowGenres.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  136. foreach (var genre in inputGenreList)
  137. {
  138. if (rowGenresList.Contains(genre, StringComparer.OrdinalIgnoreCase))
  139. {
  140. score += value;
  141. }
  142. }
  143. return score;
  144. }
  145. }
  146. }