コード例 #1
0
        /// <summary>
        /// Adds a <see cref="RelationshipAspect"/> for the relation.
        /// </summary>
        /// <param name="roleExtractor">The <see cref="IRelationshipRoleExtractor"/> that extracted the relation.</param>
        /// <param name="linkedId">The media item id of the linked item.</param>
        /// <param name="aspects">The aspects of the media item.</param>
        /// <param name="extractedAspects">The aspects of the extracted item.</param>
        /// <param name="addToLinkedItem">Whether to to add the <see cref="RelationshipAspect"/> to the the linked item rather than the media item.</param>
        private static void AddRelationship(IRelationshipRoleExtractor roleExtractor, Guid linkedId, IDictionary <Guid, IList <MediaItemAspect> > aspects,
                                            IDictionary <Guid, IList <MediaItemAspect> > extractedAspects, bool addToLinkedItem)
        {
            int index;

            if (!roleExtractor.TryGetRelationshipIndex(aspects, extractedAspects, out index))
            {
                index = 0;
            }

            Guid role;
            Guid linkedRole;
            IDictionary <Guid, IList <MediaItemAspect> > roleAspects;

            //Reverse the role, linked role and aspects if adding to the linked item
            if (addToLinkedItem)
            {
                role        = roleExtractor.LinkedRole;
                linkedRole  = roleExtractor.Role;
                roleAspects = extractedAspects;
            }
            else
            {
                role        = roleExtractor.Role;
                linkedRole  = roleExtractor.LinkedRole;
                roleAspects = aspects;
            }

            //Get whether this relationship will update the parent's play percentage
            IRelationshipTypeRegistration rtr = ServiceRegistration.Get <IRelationshipTypeRegistration>();
            RelationshipType rt       = rtr.LocallyKnownRelationshipTypes.FirstOrDefault(r => r.ChildRole == role && r.ParentRole == linkedRole);
            bool             playable = rt != null ? rt.UpdatePlayPercentage : false;

            MediaItemAspect.AddOrUpdateRelationship(roleAspects, role, linkedRole, linkedId, playable, index);
        }
コード例 #2
0
        public bool TryAddItem(MediaItem item, IRelationshipRoleExtractor itemMatcher)
        {
            ICollection <string> identifiers = itemMatcher.GetExternalIdentifiers(item.Aspects);

            //We can only cache using external identifiers
            if (identifiers == null || identifiers.Count == 0)
            {
                return(false);
            }

            //Lazily created below
            CacheItem cacheItem = null;

            foreach (string identifier in identifiers)
            {
                IList <CacheItem> cacheList;
                if (!_cache.TryGetValue(identifier, out cacheList))
                {
                    //Identifier has never been cached, add it
                    cacheList = new List <CacheItem>();
                    _cache.Add(identifier, cacheList);
                }
                else if (cacheList.Any(c => c.Id == item.MediaItemId))
                {
                    //Already cached under this identifier
                    continue;
                }

                //Create the cache item if not already created.
                //Only the minimum number of aspects required by the itemMatcher
                //will be cached to avoid caching large thumbnails, etc
                if (cacheItem == null)
                {
                    cacheItem = CreateCacheItem(item, itemMatcher);
                }

                cacheList.Add(cacheItem);
            }

            if (cacheItem == null)
            {
                return(false);
            }

            //Item was added, add it to the cache history
            _cacheHistory.Add(item.MediaItemId);
            return(true);
        }
コード例 #3
0
        protected CacheItem CreateCacheItem(MediaItem item, IRelationshipRoleExtractor itemMatcher)
        {
            IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >();
            IList <MediaItemAspect> aspect;

            //Only cache the aspects needed by the item matcher, to avoid cacheing unnecessary
            //heavy aspects like thumbnails
            foreach (Guid aspectId in itemMatcher.MatchAspects)
            {
                if (item.Aspects.TryGetValue(aspectId, out aspect))
                {
                    aspects.Add(aspectId, aspect);
                }
            }
            return(new CacheItem(item.MediaItemId, aspects));
        }
コード例 #4
0
 public ExtractedRelation(IRelationshipRoleExtractor extractor, IDictionary <Guid, IList <MediaItemAspect> > aspects)
 {
     Extractor = extractor;
     Aspects   = aspects;
 }
コード例 #5
0
        public bool TryGetItemId(IDictionary <Guid, IList <MediaItemAspect> > aspects, IRelationshipRoleExtractor itemMatcher, out Guid mediaItemId)
        {
            mediaItemId = Guid.Empty;
            ICollection <string> identifiers = itemMatcher.GetExternalIdentifiers(aspects);

            //We can only match using external identifiers
            if (identifiers == null || identifiers.Count == 0)
            {
                return(false);
            }

            //All items that have been checked under the identifiers but didn't match
            ICollection <Guid> checkedIds = new HashSet <Guid>();

            foreach (string identifier in identifiers)
            {
                IList <CacheItem> cacheList;
                //Is the identifier in the cache?
                if (!_cache.TryGetValue(identifier, out cacheList))
                {
                    continue;
                }

                //Multiple media items can be cached under the same external identifier, we need
                //to delegate the actual matching to the itemMatcher
                foreach (CacheItem item in cacheList)
                {
                    //The same media items can appear under each identifier, don't bother checking
                    //any items that we've already checked.
                    if (checkedIds.Contains(item.Id))
                    {
                        continue;
                    }

                    if (itemMatcher.TryMatch(aspects, item.Aspects))
                    {
                        mediaItemId = item.Id;
                        return(true);
                    }
                    //Store this unmatching id so it's not checked again
                    checkedIds.Add(item.Id);
                }
            }
            return(false);
        }