SqliteExtensions.cs 5.4 KB

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