jellyfin/MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs

164 lines
5.4 KiB
C#
Raw Normal View History

2016-05-31 22:50:00 -07:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2016-06-02 12:32:15 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
2016-05-31 22:50:00 -07:00
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Server.Implementations.Persistence
{
/// <summary>
/// Class SQLiteExtensions
/// </summary>
public static class SqliteExtensions
{
/// <summary>
/// Connects to db.
/// </summary>
/// <param name="dbPath">The db path.</param>
/// <param name="logger">The logger.</param>
/// <returns>Task{IDbConnection}.</returns>
/// <exception cref="System.ArgumentNullException">dbPath</exception>
public static async Task<IDbConnection> ConnectToDb(string dbPath, ILogger logger)
{
if (string.IsNullOrEmpty(dbPath))
{
throw new ArgumentNullException("dbPath");
}
logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, dbPath);
var connectionstr = new SQLiteConnectionStringBuilder
{
PageSize = 4096,
CacheSize = 2000,
SyncMode = SynchronizationModes.Normal,
DataSource = dbPath,
JournalMode = SQLiteJournalModeEnum.Wal
};
var connection = new SQLiteConnection(connectionstr.ConnectionString);
await connection.OpenAsync().ConfigureAwait(false);
return connection;
}
public static void BindGetSimilarityScore(IDbConnection connection, ILogger logger)
{
2016-06-02 12:32:15 -07:00
var sqlConnection = (SQLiteConnection)connection;
2016-05-31 22:50:00 -07:00
SimiliarToFunction.Logger = logger;
sqlConnection.BindFunction(new SimiliarToFunction());
}
public static void BindFunction(this SQLiteConnection connection, SQLiteFunction function)
{
var attributes = function.GetType().GetCustomAttributes(typeof(SQLiteFunctionAttribute), true).Cast<SQLiteFunctionAttribute>().ToArray();
if (attributes.Length == 0)
{
throw new InvalidOperationException("SQLiteFunction doesn't have SQLiteFunctionAttribute");
}
connection.BindFunction(attributes[0], function);
}
}
2016-06-03 09:24:04 -07:00
[SQLiteFunction(Name = "GetSimilarityScore", Arguments = 6, FuncType = FunctionType.Scalar)]
2016-05-31 22:50:00 -07:00
public class SimiliarToFunction : SQLiteFunction
{
internal static ILogger Logger;
2016-06-02 12:32:15 -07:00
private readonly Dictionary<string, int> _personTypeScores = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
{ PersonType.Actor, 3},
{ PersonType.Director, 5},
{ PersonType.Composer, 2},
{ PersonType.GuestStar, 3},
{ PersonType.Writer, 2},
{ PersonType.Conductor, 2},
{ PersonType.Producer, 2},
{ PersonType.Lyricist, 2}
};
2016-05-31 22:50:00 -07:00
public override object Invoke(object[] args)
{
var score = 0;
2016-06-03 09:24:04 -07:00
// Official rating equals
if ((long)args[0] == 1)
2016-05-31 22:50:00 -07:00
{
score += 10;
}
2016-06-03 09:24:04 -07:00
// Year difference
long? yearDifference = args[1] == null ? (long?)null : (long)args[1];
if (yearDifference.HasValue)
2016-05-31 22:50:00 -07:00
{
2016-06-03 09:24:04 -07:00
var diff = Math.Abs(yearDifference.Value);
2016-05-31 22:50:00 -07:00
// Add if they came out within the same decade
if (diff < 10)
{
score += 2;
}
// And more if within five years
if (diff < 5)
{
score += 2;
}
}
// genres
2016-06-03 09:24:04 -07:00
score += Convert.ToInt32((long)args[2]) * 10;
2016-05-31 22:50:00 -07:00
// tags
2016-06-03 09:24:04 -07:00
score += Convert.ToInt32((long)args[3]) * 10;
// # of common keywords
score += Convert.ToInt32((long)args[4]) *10;
2016-05-31 22:50:00 -07:00
2016-06-03 09:24:04 -07:00
// # of common studios
score += Convert.ToInt32((long)args[5]) * 3;
2016-05-31 22:50:00 -07:00
// studios
2016-06-03 09:24:04 -07:00
//score += GetListScore(args, 7, 8, 3);
2016-05-31 22:50:00 -07:00
2016-06-03 09:24:04 -07:00
//var rowPeopleNamesText = (args[12] as string) ?? string.Empty;
//var rowPeopleNames = rowPeopleNamesText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
2016-05-31 22:50:00 -07:00
2016-06-03 09:24:04 -07:00
//foreach (var name in rowPeopleNames)
//{
// // TODO: Send along person types
// score += 3;
//}
2016-05-31 22:50:00 -07:00
//Logger.Debug("Returning score {0}", score);
return score;
}
private int GetListScore(object[] args, int index1, int index2, int value = 10)
{
var score = 0;
var inputGenres = args[index1] as string;
var rowGenres = args[index2] as string;
var inputGenreList = string.IsNullOrWhiteSpace(inputGenres) ? new string[] { } : inputGenres.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var rowGenresList = string.IsNullOrWhiteSpace(rowGenres) ? new string[] { } : rowGenres.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var genre in inputGenreList)
{
if (rowGenresList.Contains(genre, StringComparer.OrdinalIgnoreCase))
{
score += value;
}
}
return score;
}
}
}