public Task <IEnumerable <string> > Execute(
            string id,
            bool?includePrerelease = false,
            string semVerLevel     = null)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            // Create SQL filter on SemVerLevel
            // By default, we filter out SemVer v2.0.0 package versions.
            var semVerLevelSqlFilter = "p.[SemVerLevelKey] IS NULL";

            if (!string.IsNullOrEmpty(semVerLevel))
            {
                var semVerLevelKey = SemVerLevelKey.ForSemVerLevel(semVerLevel);
                if (semVerLevelKey == SemVerLevelKey.SemVer2)
                {
                    semVerLevelSqlFilter = $"(p.[SemVerLevelKey] IS NULL OR p.[SemVerLevelKey] <= {SemVerLevelKey.SemVer2})";
                }
            }

            var prereleaseFilter = string.Empty;

            if (!includePrerelease.HasValue || !includePrerelease.Value)
            {
                prereleaseFilter = "AND p.IsPrerelease = 0";
            }

            return(RunSqlQuery(string.Format(CultureInfo.InvariantCulture, _sqlFormat, semVerLevelSqlFilter, prereleaseFilter), id));
        }
Exemplo n.º 2
0
        public Task <IEnumerable <string> > Execute(
            string partialId,
            bool?includePrerelease = false,
            string semVerLevel     = null)
        {
            // Create SQL filter on SemVerLevel
            // By default, we filter out SemVer v2.0.0 package versions.
            var semVerLevelSqlFilter = "p.[SemVerLevelKey] IS NULL";

            if (!string.IsNullOrEmpty(semVerLevel))
            {
                var semVerLevelKey = SemVerLevelKey.ForSemVerLevel(semVerLevel);
                if (semVerLevelKey == SemVerLevelKey.SemVer2)
                {
                    semVerLevelSqlFilter = "p.[SemVerLevelKey] = " + SemVerLevelKey.SemVer2;
                }
            }

            if (string.IsNullOrWhiteSpace(partialId))
            {
                return(RunSqlQuery(string.Format(CultureInfo.InvariantCulture, _noPartialIdSql, semVerLevelSqlFilter)));
            }

            var prereleaseFilter = string.Empty;

            if (!includePrerelease.HasValue || !includePrerelease.Value)
            {
                prereleaseFilter = "AND p.IsPrerelease = {1}";
            }

            var sql = string.Format(CultureInfo.InvariantCulture, _partialIdSqlFormat, semVerLevelSqlFilter, prereleaseFilter);

            return(RunSqlQuery(sql, partialId + "%", includePrerelease ?? false));
        }
            public void DefaultsToUnknownKeyWhenVersionStringIsNull()
            {
                // Act
                var semVerLevelKey = SemVerLevelKey.ForSemVerLevel(null);

                // Assert
                Assert.Equal(SemVerLevelKey.Unknown, semVerLevelKey);
            }
            public void ReturnsSemVer2KeyWhenVersionStringAtLeastVersion200(string semVerLevel)
            {
                // Act
                var semVerLevelKey = SemVerLevelKey.ForSemVerLevel(semVerLevel);

                // Assert
                Assert.Equal(SemVerLevelKey.SemVer2, semVerLevelKey);
            }
            public void DefaultsToUnknownKeyWhenVersionStringIsInvalidOrLowerThanVersion200(string semVerLevel)
            {
                // Act
                var semVerLevelKey = SemVerLevelKey.ForSemVerLevel(semVerLevel);

                // Assert
                Assert.Equal(SemVerLevelKey.Unknown, semVerLevelKey);
            }
Exemplo n.º 6
0
        private SearchResults SearchCore(SearchFilter searchFilter)
        {
            // Get index timestamp
            DateTime timestamp = File.GetLastWriteTimeUtc(LuceneCommon.GetIndexMetadataPath());

            int numRecords = searchFilter.Skip + searchFilter.Take;

            var searcher = new IndexSearcher(_directory, readOnly: true);
            var query    = ParseQuery(searchFilter.SearchTerm);

            // IF searching by relevance, boost scores by download count.
            if (searchFilter.SortOrder == SortOrder.Relevance)
            {
                var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT);
                query = new CustomScoreQuery(query, downloadCountBooster);
            }

            string filterTerm;

            if (SemVerLevelKey.ForSemVerLevel(searchFilter.SemVerLevel) == SemVerLevelKey.SemVer2)
            {
                filterTerm = searchFilter.IncludePrerelease ? "IsLatestSemVer2" : "IsLatestStableSemVer2";
            }
            else
            {
                filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
            }

            Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));

            if (searchFilter.CuratedFeed != null)
            {
                var          feedFilterQuery  = new TermQuery(new Term("CuratedFeedKey", searchFilter.CuratedFeed.Key.ToString(CultureInfo.InvariantCulture)));
                BooleanQuery conjunctionQuery = new BooleanQuery();
                conjunctionQuery.Add(filterQuery, Occur.MUST);
                conjunctionQuery.Add(feedFilterQuery, Occur.MUST);
                filterQuery = conjunctionQuery;
            }

            Filter filter  = new QueryWrapperFilter(filterQuery);
            var    results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter)));

            if (results.TotalHits == 0 || searchFilter.CountOnly)
            {
                return(new SearchResults(results.TotalHits, timestamp));
            }

            var packages = results.ScoreDocs
                           .Skip(searchFilter.Skip)
                           .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc)))
                           .ToList();

            return(new SearchResults(
                       results.TotalHits,
                       timestamp,
                       packages.AsQueryable()));
        }