update querying

This commit is contained in:
Luke Pulverenti 2015-11-14 13:57:26 -05:00
parent 743102b88f
commit f6e73a428e
7 changed files with 136 additions and 24 deletions

View File

@ -1939,6 +1939,36 @@ namespace MediaBrowser.Controller.Entities
return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id));
}
public BaseItem GetTopParent()
{
if (IsTopParent)
{
return this;
}
return GetParents().FirstOrDefault(i => i.IsTopParent);
}
[IgnoreDataMember]
public virtual bool IsTopParent
{
get
{
if (GetParent() is AggregateFolder || this is Channel || this is BasePluginFolder)
{
return true;
}
var view = this as UserView;
if (view != null && string.Equals(view.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
}
[IgnoreDataMember]
public virtual bool SupportsAncestors
{

View File

@ -181,9 +181,7 @@ namespace MediaBrowser.Controller.Entities
}
private List<LinkedChild> GetLinkedChildrenInternal()
{
return LibraryManager.RootFolder.Children
.OfType<Folder>()
.Where(i => i.Path != null && PhysicalLocations.Contains(i.Path, StringComparer.OrdinalIgnoreCase))
return GetPhysicalParents()
.SelectMany(c => c.LinkedChildren)
.ToList();
}
@ -199,11 +197,14 @@ namespace MediaBrowser.Controller.Entities
private IEnumerable<BaseItem> GetActualChildren()
{
return
LibraryManager.RootFolder.Children
return GetPhysicalParents().SelectMany(c => c.Children);
}
public IEnumerable<Folder> GetPhysicalParents()
{
return LibraryManager.RootFolder.Children
.OfType<Folder>()
.Where(i => i.Path != null && PhysicalLocations.Contains(i.Path, StringComparer.OrdinalIgnoreCase))
.SelectMany(c => c.Children);
.Where(i => i.Path != null && PhysicalLocations.Contains(i.Path, StringComparer.OrdinalIgnoreCase));
}
[IgnoreDataMember]

View File

@ -107,6 +107,7 @@ namespace MediaBrowser.Controller.Entities
public Guid? ParentId { get; set; }
public string[] AncestorIds { get; set; }
public string[] TopParentIds { get; set; }
public LocationType[] ExcludeLocationTypes { get; set; }
@ -131,6 +132,7 @@ namespace MediaBrowser.Controller.Entities
ChannelIds = new string[] { };
ItemIds = new string[] { };
AncestorIds = new string[] { };
TopParentIds = new string[] { };
ExcludeLocationTypes = new LocationType[] { };
}

View File

@ -170,7 +170,8 @@ namespace MediaBrowser.Controller.Entities
{
CollectionType.Games,
CollectionType.Books,
CollectionType.MusicVideos
CollectionType.MusicVideos ,
CollectionType.HomeVideos
};
return types.Contains(viewType ?? string.Empty, StringComparer.OrdinalIgnoreCase);

View File

@ -1336,16 +1336,16 @@ namespace MediaBrowser.Server.Implementations.Library
private void AddUserToQuery(InternalItemsQuery query, User user)
{
if (query.AncestorIds.Length == 0 && !query.ParentId.HasValue && query.ChannelIds.Length == 0)
if (query.AncestorIds.Length == 0 && !query.ParentId.HasValue && query.ChannelIds.Length == 0 && query.TopParentIds.Length == 0)
{
//var userViews = _userviewManager().GetUserViews(new UserViewQuery
//{
// UserId = user.Id.ToString("N"),
// IncludeHidden = true
var userViews = _userviewManager().GetUserViews(new UserViewQuery
{
UserId = user.Id.ToString("N"),
IncludeHidden = true
//}, CancellationToken.None).Result.ToList();
}, CancellationToken.None).Result.ToList();
//query.AncestorIds = userViews.SelectMany(i => i.GetIdsForAncestorQuery()).Distinct().Select(i => i.ToString("N")).ToArray();
query.TopParentIds = userViews.SelectMany(GetTopParentsForQuery).Select(i => i.Id.ToString("N")).ToArray();
}
// TODO: handle blocking by tags
@ -1358,6 +1358,60 @@ namespace MediaBrowser.Server.Implementations.Library
}
}
private IEnumerable<BaseItem> GetTopParentsForQuery(BaseItem item)
{
var view = item as UserView;
if (view != null)
{
if (string.Equals(view.ViewType, CollectionType.LiveTv))
{
return new[] { view };
}
if (string.Equals(view.ViewType, CollectionType.Channels))
{
// TODO: Return channels
return new[] { view };
}
// Translate view into folders
if (view.DisplayParentId != Guid.Empty)
{
var displayParent = GetItemById(view.DisplayParentId);
if (displayParent != null)
{
return GetTopParentsForQuery(displayParent);
}
return new BaseItem[] { };
}
if (view.ParentId != Guid.Empty)
{
var displayParent = GetItemById(view.ParentId);
if (displayParent != null)
{
return GetTopParentsForQuery(displayParent);
}
return new BaseItem[] { };
}
// Handle grouping
return new BaseItem[] { };
}
var collectionFolder = item as CollectionFolder;
if (collectionFolder != null)
{
return collectionFolder.GetPhysicalParents();
}
var topParent = item.GetTopParent();
if (topParent != null)
{
return new[] { topParent };
}
return new BaseItem[] { };
}
/// <summary>
/// Gets the intros.
/// </summary>
@ -1726,16 +1780,16 @@ namespace MediaBrowser.Server.Implementations.Library
private string GetTopFolderContentType(BaseItem item)
{
while (!(item.GetParent() is AggregateFolder) && item.GetParent() != null)
{
item = item.GetParent();
}
if (item == null)
{
return null;
}
while (!(item.GetParent() is AggregateFolder) && item.GetParent() != null)
{
item = item.GetParent();
}
return GetUserRootFolder().Children
.OfType<ICollectionFolder>()
.Where(i => string.Equals(i.Path, item.Path, StringComparison.OrdinalIgnoreCase) || i.PhysicalLocations.Contains(item.Path))

View File

@ -25,7 +25,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
public const int MigrationVersion = 4;
public const int MigrationVersion = 6;
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
{

View File

@ -82,7 +82,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _updateInheritedRatingCommand;
private const int LatestSchemaVersion = 32;
private const int LatestSchemaVersion = 37;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
@ -223,6 +223,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(_logger, "TypedBaseItems", "IsFolder", "BIT");
_connection.AddColumn(_logger, "TypedBaseItems", "InheritedParentalRatingValue", "INT");
_connection.AddColumn(_logger, "TypedBaseItems", "UnratedType", "Text");
_connection.AddColumn(_logger, "TypedBaseItems", "TopParentId", "Text");
PrepareStatements();
@ -451,7 +452,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
"ExternalServiceId",
"Tags",
"IsFolder",
"UnratedType"
"UnratedType",
"TopParentId"
};
_saveItemCommand = _connection.CreateCommand();
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (";
@ -725,7 +727,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.GetParameter(index++).Value = item.IsFolder;
_saveItemCommand.GetParameter(index++).Value = item.GetBlockUnratedType().ToString();
var topParent = item.GetTopParent();
if (topParent != null)
{
_saveItemCommand.GetParameter(index++).Value = topParent.Id.ToString("N");
}
else
{
_saveItemCommand.GetParameter(index++).Value = null;
}
_saveItemCommand.Transaction = transaction;
_saveItemCommand.ExecuteNonQuery();
@ -1940,6 +1952,18 @@ namespace MediaBrowser.Server.Implementations.Persistence
whereClauses.Add("LocationType not in (" + val + ")");
}
if (query.TopParentIds.Length == 1)
{
whereClauses.Add("(TopParentId is null or TopParentId=@TopParentId)");
cmd.Parameters.Add(cmd, "@TopParentId", DbType.String).Value = query.TopParentIds[0];
}
if (query.TopParentIds.Length > 1)
{
var val = string.Join(",", query.TopParentIds.Select(i => "'" + i + "'").ToArray());
whereClauses.Add("(TopParentId is null or TopParentId in (" + val + "))");
}
if (query.AncestorIds.Length == 1)
{
whereClauses.Add("Guid in (select itemId from AncestorIds where AncestorId=@AncestorId)");