mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
calculate similarity at database level
This commit is contained in:
parent
0915d1f383
commit
e1f562e16f
@ -10,6 +10,7 @@ using System.Collections.Generic;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using MediaBrowser.Model.Querying;
|
||||||
|
|
||||||
namespace MediaBrowser.Api
|
namespace MediaBrowser.Api
|
||||||
{
|
{
|
||||||
@ -187,18 +188,40 @@ namespace MediaBrowser.Api
|
|||||||
/// <returns>System.Object.</returns>
|
/// <returns>System.Object.</returns>
|
||||||
public object Get(GetSimilarGames request)
|
public object Get(GetSimilarGames request)
|
||||||
{
|
{
|
||||||
var dtoOptions = GetDtoOptions(request);
|
var result = GetSimilarItemsResult(request);
|
||||||
|
|
||||||
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
|
|
||||||
_itemRepo,
|
|
||||||
_libraryManager,
|
|
||||||
_userDataRepository,
|
|
||||||
_dtoService,
|
|
||||||
Logger,
|
|
||||||
request, new[] { typeof(Game) },
|
|
||||||
SimilarItemsHelper.GetSimiliarityScore);
|
|
||||||
|
|
||||||
return ToOptimizedSerializedResultUsingCache(result);
|
return ToOptimizedSerializedResultUsingCache(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private QueryResult<BaseItemDto> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request)
|
||||||
|
{
|
||||||
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
||||||
|
|
||||||
|
var item = string.IsNullOrEmpty(request.Id) ?
|
||||||
|
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
||||||
|
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
||||||
|
|
||||||
|
var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||||
|
{
|
||||||
|
Limit = request.Limit,
|
||||||
|
IncludeItemTypes = new[]
|
||||||
|
{
|
||||||
|
typeof(Game).Name
|
||||||
|
},
|
||||||
|
SimilarTo = item
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
var dtoOptions = GetDtoOptions(request);
|
||||||
|
|
||||||
|
var result = new QueryResult<BaseItemDto>
|
||||||
|
{
|
||||||
|
Items = _dtoService.GetBaseItemDtos(itemsResult, dtoOptions, user).ToArray(),
|
||||||
|
|
||||||
|
TotalRecordCount = itemsResult.Count
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,18 +111,16 @@ namespace MediaBrowser.Api.Movies
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request">The request.</param>
|
/// <param name="request">The request.</param>
|
||||||
/// <returns>System.Object.</returns>
|
/// <returns>System.Object.</returns>
|
||||||
public async Task<object> Get(GetSimilarMovies request)
|
public object Get(GetSimilarMovies request)
|
||||||
{
|
{
|
||||||
var result = await GetSimilarItemsResult(
|
var result = GetSimilarItemsResult(request);
|
||||||
request, SimilarItemsHelper.GetSimiliarityScore).ConfigureAwait(false);
|
|
||||||
|
|
||||||
return ToOptimizedSerializedResultUsingCache(result);
|
return ToOptimizedSerializedResultUsingCache(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<object> Get(GetSimilarTrailers request)
|
public object Get(GetSimilarTrailers request)
|
||||||
{
|
{
|
||||||
var result = await GetSimilarItemsResult(
|
var result = GetSimilarItemsResult(request);
|
||||||
request, SimilarItemsHelper.GetSimiliarityScore).ConfigureAwait(false);
|
|
||||||
|
|
||||||
return ToOptimizedSerializedResultUsingCache(result);
|
return ToOptimizedSerializedResultUsingCache(result);
|
||||||
}
|
}
|
||||||
@ -131,42 +129,16 @@ namespace MediaBrowser.Api.Movies
|
|||||||
{
|
{
|
||||||
var user = _userManager.GetUserById(request.UserId);
|
var user = _userManager.GetUserById(request.UserId);
|
||||||
|
|
||||||
var query = new InternalItemsQuery(user)
|
|
||||||
{
|
|
||||||
IncludeItemTypes = new[]
|
|
||||||
{
|
|
||||||
typeof(Movie).Name,
|
|
||||||
typeof(Trailer).Name,
|
|
||||||
//typeof(LiveTvProgram).Name
|
|
||||||
},
|
|
||||||
// IsMovie = true
|
|
||||||
};
|
|
||||||
|
|
||||||
var parentIds = string.IsNullOrWhiteSpace(request.ParentId) ? new string[] { } : new[] { request.ParentId };
|
|
||||||
var movies = _libraryManager.GetItemList(query, parentIds)
|
|
||||||
.OrderBy(i => (int)i.SourceType);
|
|
||||||
|
|
||||||
var listEligibleForSuggestion = new List<BaseItem>();
|
|
||||||
|
|
||||||
var list = movies.ToList();
|
|
||||||
|
|
||||||
listEligibleForSuggestion.AddRange(list);
|
|
||||||
|
|
||||||
listEligibleForSuggestion = listEligibleForSuggestion
|
|
||||||
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
|
|
||||||
.DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString(), StringComparer.OrdinalIgnoreCase)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
var dtoOptions = GetDtoOptions(request);
|
var dtoOptions = GetDtoOptions(request);
|
||||||
|
|
||||||
dtoOptions.Fields = request.GetItemFields().ToList();
|
dtoOptions.Fields = request.GetItemFields().ToList();
|
||||||
|
|
||||||
var result = GetRecommendationCategories(user, request.ParentId, listEligibleForSuggestion, request.CategoryLimit, request.ItemLimit, dtoOptions);
|
var result = GetRecommendationCategories(user, request.ParentId, request.CategoryLimit, request.ItemLimit, dtoOptions);
|
||||||
|
|
||||||
return ToOptimizedResult(result);
|
return ToOptimizedResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ItemsResult> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
|
private QueryResult<BaseItemDto> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request)
|
||||||
{
|
{
|
||||||
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
||||||
|
|
||||||
@ -174,57 +146,32 @@ namespace MediaBrowser.Api.Movies
|
|||||||
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
||||||
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
||||||
|
|
||||||
var query = new InternalItemsQuery(user)
|
var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||||
{
|
{
|
||||||
|
Limit = request.Limit,
|
||||||
IncludeItemTypes = new[]
|
IncludeItemTypes = new[]
|
||||||
{
|
{
|
||||||
typeof(Movie).Name,
|
typeof(Movie).Name,
|
||||||
typeof(Trailer).Name,
|
typeof(Trailer).Name,
|
||||||
//typeof(LiveTvProgram).Name
|
typeof(LiveTvProgram).Name
|
||||||
},
|
},
|
||||||
//IsMovie = true
|
IsMovie = true,
|
||||||
};
|
SimilarTo = item
|
||||||
|
}).ToList();
|
||||||
var list = _libraryManager.GetItemList(query)
|
|
||||||
.OrderBy(i => (int)i.SourceType)
|
|
||||||
.DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
if (item is Video)
|
|
||||||
{
|
|
||||||
var imdbId = item.GetProviderId(MetadataProviders.Imdb);
|
|
||||||
|
|
||||||
// Use imdb id to try to filter duplicates of the same item
|
|
||||||
if (!string.IsNullOrWhiteSpace(imdbId))
|
|
||||||
{
|
|
||||||
list = list
|
|
||||||
.Where(i => !string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var items = SimilarItemsHelper.GetSimilaritems(item, _libraryManager, list, getSimilarityScore).ToList();
|
|
||||||
|
|
||||||
IEnumerable<BaseItem> returnItems = items;
|
|
||||||
|
|
||||||
if (request.Limit.HasValue)
|
|
||||||
{
|
|
||||||
returnItems = returnItems.Take(request.Limit.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
var dtoOptions = GetDtoOptions(request);
|
var dtoOptions = GetDtoOptions(request);
|
||||||
|
|
||||||
var result = new ItemsResult
|
var result = new QueryResult<BaseItemDto>
|
||||||
{
|
{
|
||||||
Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(),
|
Items = _dtoService.GetBaseItemDtos(itemsResult, dtoOptions, user).ToArray(),
|
||||||
|
|
||||||
TotalRecordCount = items.Count
|
TotalRecordCount = itemsResult.Count
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<RecommendationDto> GetRecommendationCategories(User user, string parentId, List<BaseItem> allMovies, int categoryLimit, int itemLimit, DtoOptions dtoOptions)
|
private IEnumerable<RecommendationDto> GetRecommendationCategories(User user, string parentId, int categoryLimit, int itemLimit, DtoOptions dtoOptions)
|
||||||
{
|
{
|
||||||
var categories = new List<RecommendationDto>();
|
var categories = new List<RecommendationDto>();
|
||||||
|
|
||||||
@ -260,7 +207,7 @@ namespace MediaBrowser.Api.Movies
|
|||||||
IsFavoriteOrLiked = true,
|
IsFavoriteOrLiked = true,
|
||||||
ExcludeItemIds = recentlyPlayedMovies.Select(i => i.Id.ToString("N")).ToArray()
|
ExcludeItemIds = recentlyPlayedMovies.Select(i => i.Id.ToString("N")).ToArray()
|
||||||
|
|
||||||
}, parentIds);
|
}, parentIds).ToList();
|
||||||
|
|
||||||
var mostRecentMovies = recentlyPlayedMovies.Take(6).ToList();
|
var mostRecentMovies = recentlyPlayedMovies.Take(6).ToList();
|
||||||
// Get recently played directors
|
// Get recently played directors
|
||||||
@ -273,8 +220,8 @@ namespace MediaBrowser.Api.Movies
|
|||||||
.OrderBy(i => Guid.NewGuid())
|
.OrderBy(i => Guid.NewGuid())
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var similarToRecentlyPlayed = GetSimilarTo(user, allMovies, recentlyPlayedMovies.Take(7).OrderBy(i => Guid.NewGuid()), itemLimit, dtoOptions, RecommendationType.SimilarToRecentlyPlayed).GetEnumerator();
|
var similarToRecentlyPlayed = GetSimilarTo(user, recentlyPlayedMovies, itemLimit, dtoOptions, RecommendationType.SimilarToRecentlyPlayed).GetEnumerator();
|
||||||
var similarToLiked = GetSimilarTo(user, allMovies, likedMovies, itemLimit, dtoOptions, RecommendationType.SimilarToLikedItem).GetEnumerator();
|
var similarToLiked = GetSimilarTo(user, likedMovies, itemLimit, dtoOptions, RecommendationType.SimilarToLikedItem).GetEnumerator();
|
||||||
|
|
||||||
var hasDirectorFromRecentlyPlayed = GetWithDirector(user, recentDirectors, itemLimit, dtoOptions, RecommendationType.HasDirectorFromRecentlyPlayed).GetEnumerator();
|
var hasDirectorFromRecentlyPlayed = GetWithDirector(user, recentDirectors, itemLimit, dtoOptions, RecommendationType.HasDirectorFromRecentlyPlayed).GetEnumerator();
|
||||||
var hasActorFromRecentlyPlayed = GetWithActor(user, recentActors, itemLimit, dtoOptions, RecommendationType.HasActorFromRecentlyPlayed).GetEnumerator();
|
var hasActorFromRecentlyPlayed = GetWithActor(user, recentActors, itemLimit, dtoOptions, RecommendationType.HasActorFromRecentlyPlayed).GetEnumerator();
|
||||||
@ -389,14 +336,23 @@ namespace MediaBrowser.Api.Movies
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<RecommendationDto> GetSimilarTo(User user, List<BaseItem> allMovies, IEnumerable<BaseItem> baselineItems, int itemLimit, DtoOptions dtoOptions, RecommendationType type)
|
private IEnumerable<RecommendationDto> GetSimilarTo(User user, List<BaseItem> baselineItems, int itemLimit, DtoOptions dtoOptions, RecommendationType type)
|
||||||
{
|
{
|
||||||
foreach (var item in baselineItems)
|
foreach (var item in baselineItems)
|
||||||
{
|
{
|
||||||
var similar = SimilarItemsHelper
|
var similar = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||||
.GetSimilaritems(item, _libraryManager, allMovies, SimilarItemsHelper.GetSimiliarityScore)
|
{
|
||||||
.Take(itemLimit)
|
Limit = itemLimit,
|
||||||
.ToList();
|
IncludeItemTypes = new[]
|
||||||
|
{
|
||||||
|
typeof(Movie).Name,
|
||||||
|
typeof(Trailer).Name,
|
||||||
|
typeof(LiveTvProgram).Name
|
||||||
|
},
|
||||||
|
IsMovie = true,
|
||||||
|
SimilarTo = item
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
if (similar.Count > 0)
|
if (similar.Count > 0)
|
||||||
{
|
{
|
||||||
|
@ -12,6 +12,7 @@ using ServiceStack;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using MediaBrowser.Model.Dto;
|
||||||
|
|
||||||
namespace MediaBrowser.Api
|
namespace MediaBrowser.Api
|
||||||
{
|
{
|
||||||
@ -273,20 +274,42 @@ namespace MediaBrowser.Api
|
|||||||
/// <returns>System.Object.</returns>
|
/// <returns>System.Object.</returns>
|
||||||
public object Get(GetSimilarShows request)
|
public object Get(GetSimilarShows request)
|
||||||
{
|
{
|
||||||
var dtoOptions = GetDtoOptions(request);
|
var result = GetSimilarItemsResult(request);
|
||||||
|
|
||||||
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
|
|
||||||
_itemRepo,
|
|
||||||
_libraryManager,
|
|
||||||
_userDataManager,
|
|
||||||
_dtoService,
|
|
||||||
Logger,
|
|
||||||
request, new[] { typeof(Series) },
|
|
||||||
SimilarItemsHelper.GetSimiliarityScore);
|
|
||||||
|
|
||||||
return ToOptimizedSerializedResultUsingCache(result);
|
return ToOptimizedSerializedResultUsingCache(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private QueryResult<BaseItemDto> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request)
|
||||||
|
{
|
||||||
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
||||||
|
|
||||||
|
var item = string.IsNullOrEmpty(request.Id) ?
|
||||||
|
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
||||||
|
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
||||||
|
|
||||||
|
var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
||||||
|
{
|
||||||
|
Limit = request.Limit,
|
||||||
|
IncludeItemTypes = new[]
|
||||||
|
{
|
||||||
|
typeof(Series).Name
|
||||||
|
},
|
||||||
|
SimilarTo = item
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
var dtoOptions = GetDtoOptions(request);
|
||||||
|
|
||||||
|
var result = new QueryResult<BaseItemDto>
|
||||||
|
{
|
||||||
|
Items = _dtoService.GetBaseItemDtos(itemsResult, dtoOptions, user).ToArray(),
|
||||||
|
|
||||||
|
TotalRecordCount = itemsResult.Count
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public object Get(GetUpcomingEpisodes request)
|
public object Get(GetUpcomingEpisodes request)
|
||||||
{
|
{
|
||||||
var user = _userManager.GetUserById(request.UserId);
|
var user = _userManager.GetUserById(request.UserId);
|
||||||
|
@ -19,6 +19,8 @@ namespace MediaBrowser.Controller.Entities
|
|||||||
|
|
||||||
public User User { get; set; }
|
public User User { get; set; }
|
||||||
|
|
||||||
|
public BaseItem SimilarTo { get; set; }
|
||||||
|
|
||||||
public bool? IsFolder { get; set; }
|
public bool? IsFolder { get; set; }
|
||||||
public bool? IsFavorite { get; set; }
|
public bool? IsFavorite { get; set; }
|
||||||
public bool? IsFavoriteOrLiked { get; set; }
|
public bool? IsFavoriteOrLiked { get; set; }
|
||||||
|
@ -6,5 +6,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
public interface IDbConnector
|
public interface IDbConnector
|
||||||
{
|
{
|
||||||
Task<IDbConnection> Connect(string dbPath);
|
Task<IDbConnection> Connect(string dbPath);
|
||||||
|
void BindSimilarityScoreFunction(IDbConnection connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,175 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SQLite;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var sqlConnection = (SQLiteConnection) connection;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SQLiteFunction(Name = "GetSimilarityScore", Arguments = 12, FuncType = FunctionType.Scalar)]
|
||||||
|
public class SimiliarToFunction : SQLiteFunction
|
||||||
|
{
|
||||||
|
internal static ILogger Logger;
|
||||||
|
|
||||||
|
public override object Invoke(object[] args)
|
||||||
|
{
|
||||||
|
var score = 0;
|
||||||
|
|
||||||
|
var inputOfficialRating = args[0] as string;
|
||||||
|
var rowOfficialRating = args[1] as string;
|
||||||
|
if (!string.IsNullOrWhiteSpace(inputOfficialRating) && string.Equals(inputOfficialRating, rowOfficialRating))
|
||||||
|
{
|
||||||
|
score += 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
long? inputYear = args[2] == null ? (long?)null : (long)args[2];
|
||||||
|
long? rowYear = args[3] == null ? (long?)null : (long)args[3];
|
||||||
|
|
||||||
|
if (inputYear.HasValue && rowYear.HasValue)
|
||||||
|
{
|
||||||
|
var diff = Math.Abs(inputYear.Value - rowYear.Value);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
score += GetListScore(args, 4, 5);
|
||||||
|
|
||||||
|
// tags
|
||||||
|
score += GetListScore(args, 6, 7);
|
||||||
|
|
||||||
|
// keywords
|
||||||
|
score += GetListScore(args, 8, 9);
|
||||||
|
|
||||||
|
// studios
|
||||||
|
score += GetListScore(args, 10, 11, 3);
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: People
|
||||||
|
// var item2PeopleNames = allPeople.Where(i => i.ItemId == item2.Id)
|
||||||
|
//.Select(i => i.Name)
|
||||||
|
//.Where(i => !string.IsNullOrWhiteSpace(i))
|
||||||
|
//.DistinctNames()
|
||||||
|
//.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
// points += item1People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>
|
||||||
|
// {
|
||||||
|
// if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
|
||||||
|
// {
|
||||||
|
// return 5;
|
||||||
|
// }
|
||||||
|
// if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
|
||||||
|
// {
|
||||||
|
// return 3;
|
||||||
|
// }
|
||||||
|
// if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase))
|
||||||
|
// {
|
||||||
|
// return 3;
|
||||||
|
// }
|
||||||
|
// if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
|
||||||
|
// {
|
||||||
|
// return 3;
|
||||||
|
// }
|
||||||
|
// if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
|
||||||
|
// {
|
||||||
|
// return 2;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return 1;
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return points;
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ using System.Globalization;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Common.Extensions;
|
using MediaBrowser.Common.Extensions;
|
||||||
@ -258,6 +259,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
new MediaStreamColumns(_connection, Logger).AddColumns();
|
new MediaStreamColumns(_connection, Logger).AddColumns();
|
||||||
|
|
||||||
DataExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb");
|
DataExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb");
|
||||||
|
|
||||||
|
dbConnector.BindSimilarityScoreFunction(_connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly string[] _retriveItemColumns =
|
private readonly string[] _retriveItemColumns =
|
||||||
@ -1575,7 +1578,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string[] GetFinalColumnsToSelect(InternalItemsQuery query, string[] startColumns)
|
private string[] GetFinalColumnsToSelect(InternalItemsQuery query, string[] startColumns, IDbCommand cmd)
|
||||||
{
|
{
|
||||||
var list = startColumns.ToList();
|
var list = startColumns.ToList();
|
||||||
|
|
||||||
@ -1590,6 +1593,45 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
list.Add("UserDataDb.UserData.rating");
|
list.Add("UserDataDb.UserData.rating");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (query.SimilarTo != null)
|
||||||
|
{
|
||||||
|
var item = query.SimilarTo;
|
||||||
|
|
||||||
|
var builder = new StringBuilder();
|
||||||
|
builder.Append("GetSimilarityScore(");
|
||||||
|
|
||||||
|
builder.Append("@ItemOfficialRating,");
|
||||||
|
builder.Append("OfficialRating,");
|
||||||
|
|
||||||
|
builder.Append("@ItemProductionYear,");
|
||||||
|
builder.Append("ProductionYear,");
|
||||||
|
|
||||||
|
builder.Append("@ItemGenres,");
|
||||||
|
builder.Append("Genres,");
|
||||||
|
|
||||||
|
builder.Append("@ItemTags,");
|
||||||
|
builder.Append("Tags,");
|
||||||
|
|
||||||
|
builder.Append("@ItemKeywords,");
|
||||||
|
builder.Append("(select group_concat((Select Value from ItemValues where ItemId=Guid and Type=5), '|')),");
|
||||||
|
|
||||||
|
builder.Append("@ItemStudios,");
|
||||||
|
builder.Append("Studios");
|
||||||
|
builder.Append(") as SimilarityScore");
|
||||||
|
|
||||||
|
list.Add(builder.ToString());
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemOfficialRating", DbType.String).Value = item.OfficialRating;
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemProductionYear", DbType.Int32).Value = item.ProductionYear ?? -1;
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemGenres", DbType.String).Value = string.Join("|", item.Genres.ToArray());
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemTags", DbType.String).Value = string.Join("|", item.Tags.ToArray());
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemKeywords", DbType.String).Value = string.Join("|", item.Keywords.ToArray());
|
||||||
|
cmd.Parameters.Add(cmd, "@ItemStudios", DbType.String).Value = string.Join("|", item.Studios.ToArray());
|
||||||
|
|
||||||
|
var excludeIds = query.ExcludeItemIds.ToList();
|
||||||
|
excludeIds.Add(item.Id.ToString("N"));
|
||||||
|
query.ExcludeItemIds = excludeIds.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
return list.ToArray();
|
return list.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1616,7 +1658,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
|
|
||||||
using (var cmd = _connection.CreateCommand())
|
using (var cmd = _connection.CreateCommand())
|
||||||
{
|
{
|
||||||
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns)) + " from TypedBaseItems";
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns, cmd)) + " from TypedBaseItems";
|
||||||
cmd.CommandText += GetJoinUserDataText(query);
|
cmd.CommandText += GetJoinUserDataText(query);
|
||||||
|
|
||||||
if (EnableJoinUserData(query))
|
if (EnableJoinUserData(query))
|
||||||
@ -1706,7 +1748,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
|
|
||||||
using (var cmd = _connection.CreateCommand())
|
using (var cmd = _connection.CreateCommand())
|
||||||
{
|
{
|
||||||
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns)) + " from TypedBaseItems";
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns, cmd)) + " from TypedBaseItems";
|
||||||
cmd.CommandText += GetJoinUserDataText(query);
|
cmd.CommandText += GetJoinUserDataText(query);
|
||||||
|
|
||||||
if (EnableJoinUserData(query))
|
if (EnableJoinUserData(query))
|
||||||
@ -1789,6 +1831,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
|
|
||||||
private string GetOrderByText(InternalItemsQuery query)
|
private string GetOrderByText(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
|
if (query.SimilarTo != null)
|
||||||
|
{
|
||||||
|
if (query.SortBy == null || query.SortBy.Length == 0)
|
||||||
|
{
|
||||||
|
query.SortBy = new[] { "SimilarityScore", "Random" };
|
||||||
|
query.SortOrder = SortOrder.Descending;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (query.SortBy == null || query.SortBy.Length == 0)
|
if (query.SortBy == null || query.SortBy.Length == 0)
|
||||||
{
|
{
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
@ -1879,7 +1930,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
|
|
||||||
using (var cmd = _connection.CreateCommand())
|
using (var cmd = _connection.CreateCommand())
|
||||||
{
|
{
|
||||||
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" })) + " from TypedBaseItems";
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" }, cmd)) + " from TypedBaseItems";
|
||||||
cmd.CommandText += GetJoinUserDataText(query);
|
cmd.CommandText += GetJoinUserDataText(query);
|
||||||
|
|
||||||
if (EnableJoinUserData(query))
|
if (EnableJoinUserData(query))
|
||||||
@ -2022,7 +2073,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
|
|
||||||
using (var cmd = _connection.CreateCommand())
|
using (var cmd = _connection.CreateCommand())
|
||||||
{
|
{
|
||||||
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" })) + " from TypedBaseItems";
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" }, cmd)) + " from TypedBaseItems";
|
||||||
|
|
||||||
var whereClauses = GetWhereClauses(query, cmd);
|
var whereClauses = GetWhereClauses(query, cmd);
|
||||||
cmd.CommandText += GetJoinUserDataText(query);
|
cmd.CommandText += GetJoinUserDataText(query);
|
||||||
@ -2148,24 +2199,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (query.IsMovie.Value)
|
whereClauses.Add("(IsMovie is null OR IsMovie=@IsMovie)");
|
||||||
{
|
|
||||||
var typeClauses = new List<string>();
|
|
||||||
var typeIndex = 0;
|
|
||||||
foreach (var type in alternateTypes)
|
|
||||||
{
|
|
||||||
var paramName = "@AlternateType" + typeIndex.ToString(CultureInfo.InvariantCulture);
|
|
||||||
typeClauses.Add("Type=" + paramName);
|
|
||||||
cmd.Parameters.Add(cmd, paramName, DbType.String).Value = type;
|
|
||||||
typeIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
whereClauses.Add("(IsMovie=@IsMovie OR " + string.Join(" OR ", typeClauses.ToArray()) + ")");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
whereClauses.Add("(IsMovie is null OR IsMovie=@IsMovie)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
cmd.Parameters.Add(cmd, "@IsMovie", DbType.Boolean).Value = query.IsMovie;
|
cmd.Parameters.Add(cmd, "@IsMovie", DbType.Boolean).Value = query.IsMovie;
|
||||||
}
|
}
|
||||||
|
@ -82,11 +82,14 @@
|
|||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="..\MediaBrowser.Server.Implementations\Persistence\SqliteExtensions.cs">
|
||||||
|
<Link>Native\SqliteExtensions.cs</Link>
|
||||||
|
</Compile>
|
||||||
<Compile Include="..\SharedVersion.cs">
|
<Compile Include="..\SharedVersion.cs">
|
||||||
<Link>Properties\SharedVersion.cs</Link>
|
<Link>Properties\SharedVersion.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Native\BaseMonoApp.cs" />
|
<Compile Include="Native\BaseMonoApp.cs" />
|
||||||
<Compile Include="Native\SqliteExtensions.cs" />
|
<Compile Include="Native\DbConnector.cs" />
|
||||||
<Compile Include="Networking\CertificateGenerator.cs" />
|
<Compile Include="Networking\CertificateGenerator.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
29
MediaBrowser.Server.Mono/Native/DbConnector.cs
Normal file
29
MediaBrowser.Server.Mono/Native/DbConnector.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SQLite;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Model.Logging;
|
||||||
|
using MediaBrowser.Server.Implementations.Persistence;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Server.Mono.Native
|
||||||
|
{
|
||||||
|
public class DbConnector : IDbConnector
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
public DbConnector(ILogger logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BindSimilarityScoreFunction(IDbConnection connection)
|
||||||
|
{
|
||||||
|
SqliteExtensions.BindGetSimilarityScore(connection, _logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<IDbConnection> Connect(string dbPath)
|
||||||
|
{
|
||||||
|
return SqliteExtensions.ConnectToDb(dbPath, _logger);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.SQLite;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Model.Logging;
|
|
||||||
using MediaBrowser.Server.Implementations.Persistence;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Mono.Native
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Class SQLiteExtensions
|
|
||||||
/// </summary>
|
|
||||||
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 class DbConnector : IDbConnector
|
|
||||||
{
|
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
public DbConnector(ILogger logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<IDbConnection> Connect(string dbPath)
|
|
||||||
{
|
|
||||||
return SqliteExtensions.ConnectToDb(dbPath, _logger);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -97,6 +97,9 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="..\MediaBrowser.Server.Implementations\Persistence\SqliteExtensions.cs">
|
||||||
|
<Link>Native\SqliteExtensions.cs</Link>
|
||||||
|
</Compile>
|
||||||
<Compile Include="..\SharedVersion.cs">
|
<Compile Include="..\SharedVersion.cs">
|
||||||
<Link>Properties\SharedVersion.cs</Link>
|
<Link>Properties\SharedVersion.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
@ -114,7 +117,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="MainStartup.cs" />
|
<Compile Include="MainStartup.cs" />
|
||||||
<Compile Include="Native\LnkShortcutHandler.cs" />
|
<Compile Include="Native\LnkShortcutHandler.cs" />
|
||||||
<Compile Include="Native\SqliteExtensions.cs" />
|
<Compile Include="Native\DbConnector.cs" />
|
||||||
<Compile Include="Native\Standby.cs" />
|
<Compile Include="Native\Standby.cs" />
|
||||||
<Compile Include="Native\ServerAuthorization.cs" />
|
<Compile Include="Native\ServerAuthorization.cs" />
|
||||||
<Compile Include="Native\WindowsApp.cs" />
|
<Compile Include="Native\WindowsApp.cs" />
|
||||||
|
38
MediaBrowser.ServerApplication/Native/DbConnector.cs
Normal file
38
MediaBrowser.ServerApplication/Native/DbConnector.cs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SQLite;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Model.Logging;
|
||||||
|
using MediaBrowser.Server.Implementations.Persistence;
|
||||||
|
|
||||||
|
namespace MediaBrowser.ServerApplication.Native
|
||||||
|
{
|
||||||
|
public class DbConnector : IDbConnector
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
public DbConnector(ILogger logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BindSimilarityScoreFunction(IDbConnection connection)
|
||||||
|
{
|
||||||
|
SqliteExtensions.BindGetSimilarityScore(connection, _logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IDbConnection> Connect(string dbPath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await SqliteExtensions.ConnectToDb(dbPath, _logger).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.ErrorException("Error opening database {0}", ex, dbPath);
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,71 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.SQLite;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Model.Logging;
|
|
||||||
using MediaBrowser.Server.Implementations.Persistence;
|
|
||||||
|
|
||||||
namespace MediaBrowser.ServerApplication.Native
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Class SQLiteExtensions
|
|
||||||
/// </summary>
|
|
||||||
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 class DbConnector : IDbConnector
|
|
||||||
{
|
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
public DbConnector(ILogger logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IDbConnection> Connect(string dbPath)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return await SqliteExtensions.ConnectToDb(dbPath, _logger).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error opening database {0}", ex, dbPath);
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user