예제 #1
0
        public static void Any_AllFalse(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            IntegerRangeSet     seen  = new IntegerRangeSet(0, count);

            Assert.False(query.Any(x => !seen.Add(x)));
            seen.AssertComplete();
        }
예제 #2
0
        public static void Any_OneTrue(Labeled <ParallelQuery <int> > labeled, int count, int position)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.True(query.Any(x => x == position));
        }
예제 #3
0
        public static void Any_AllTrue(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(count > 0, query.Any(x => x >= 0));
        }
예제 #4
0
        public static void Any_Contents(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(count > 0, query.Any());
        }
예제 #5
0
        private static void RunAllTests(
            TestTracker result, ParallelQuery <int> q, bool orderPreserved,
            string leftOpName, bool leftOrderDefined)
        {
            LogTestRun(leftOpName, "All1", orderPreserved);
            result.MustEqual(
                q.All(i => i > 100),
                q.ToArray().Any(i => i > 100));

            LogTestRun(leftOpName, "All2", orderPreserved);
            result.MustEqual(
                q.All(i => i == 75),
                q.ToArray().All(i => i == 75));

            LogTestRun(leftOpName, "Any1", orderPreserved);
            result.MustEqual(
                q.Any(i => i > 100),
                q.ToArray().Any(i => i > 100));

            LogTestRun(leftOpName, "Any2", orderPreserved);
            result.MustEqual(
                q.Any(i => i == 75),
                q.ToArray().Any(i => i == 75));

            LogTestRun(leftOpName, "Concat", orderPreserved);
            result.MustSequenceEqual(
                q.Concat(q).Concat(new int[] { 1, 2, 3 }.AsParallel()),
                q.Reverse().Reverse().ToArray().Concat(q.Reverse().Reverse()).Concat(new int[] { 1, 2, 3 }),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "DefaultIfEmpty", orderPreserved);
            result.MustSequenceEqual(
                q.DefaultIfEmpty(),
                q.ToArray().DefaultIfEmpty(), orderPreserved && leftOrderDefined);

            LogTestRun(leftOpName, "ElementAt", orderPreserved);
            IEnumerable <int> q2 = q.ToArray();
            int        count1 = q.Count(), count2 = q2.Count();
            List <int> list1 = new List <int>();
            List <int> list2 = new List <int>();

            for (int i = 0; i < count1; i++)
            {
                list1.Add(q.ElementAt(i));
            }
            for (int i = 0; i < count2; i++)
            {
                list2.Add(q2.ElementAt(i));
            }
            result.MustSequenceEqual(list1, list2, leftOrderDefined);

            LogTestRun(leftOpName, "Except", orderPreserved);
            result.MustSequenceEqual(
                q.Except(Enumerable.Range(90, 50).AsParallel()),
                q.ToArray().Except(Enumerable.Range(90, 50)),
                false);

            LogTestRun(leftOpName, "First", orderPreserved);
            CheckFirstOrLast(
                result,
                q.First(),
                q.ToArray().First(),
                leftOrderDefined);

            LogTestRun(leftOpName, "GroupBy", orderPreserved);
            result.MustGroupByEqual(
                q.GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e)),
                q.ToArray().GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e)));

            LogTestRun(leftOpName, "GroupJoin", orderPreserved);
            result.MustSequenceEqual(
                q.GroupJoin(q, i => i, i => i, (i, e) => e.FirstOrDefault()),
                q.ToArray().GroupJoin(q.ToArray(), i => i, i => i, (i, e) => e.FirstOrDefault()),
                false);

            LogTestRun(leftOpName, "Intersect", orderPreserved);
            result.MustSequenceEqual(
                q.Intersect(Enumerable.Range(90, 50).AsParallel()),
                q.ToArray().Intersect(Enumerable.Range(90, 50)),
                false);

            LogTestRun(leftOpName, "Join1", orderPreserved);
            result.MustSequenceEqual(
                q.Join((new int[] { 1, 1, 2, 3, 3 }).AsParallel(), i => i, i => i, (i, j) => i + j),
                q.ToArray().Join(new int[] { 1, 1, 2, 3, 3 }, i => i, i => i, (i, j) => i + j),
                false);

            LogTestRun(leftOpName, "Join2", orderPreserved);
            result.MustSequenceEqual(
                q.Join((new int[] { 1, 1, 100, 3, 3 }).AsParallel(), i => new String('a', i), i => new String('a', i), (i, j) => i + j),
                q.ToArray().Join(new int[] { 1, 1, 100, 3, 3 }, i => new String('a', i), i => new String('a', i), (i, j) => i + j),
                false);

            LogTestRun(leftOpName, "Last", orderPreserved);
            CheckFirstOrLast(
                result,
                q.Last(),
                q.ToArray().Last(),
                leftOrderDefined);

            LogTestRun(leftOpName, "Min", orderPreserved);
            CheckFirstOrLast(
                result,
                q.Min(),
                q.ToArray().Min(),
                leftOrderDefined);

            LogTestRun(leftOpName, "Max", orderPreserved);
            CheckFirstOrLast(
                result,
                q.Min(),
                q.ToArray().Min(),
                leftOrderDefined);

            LogTestRun(leftOpName, "OrderBy-ThenBy", orderPreserved);
            result.MustSequenceEqual(
                q.Concat(q).OrderBy(i => i % 5).ThenBy(i => - i),
                q.ToArray().Concat(q).OrderBy(i => i % 5).ThenBy(i => - i),
                true);

            LogTestRun(leftOpName, "OrderByDescending-ThenByDescending", orderPreserved);
            result.MustSequenceEqual(
                q.Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i),
                q.ToArray().Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i),
                true);

            LogTestRun(leftOpName, "Reverse", orderPreserved);
            result.MustSequenceEqual(
                q.Concat(q).Reverse(),
                q.ToArray().Concat(q).Reverse(),
                orderPreserved && leftOrderDefined);

            LogTestRun(leftOpName, "Select", orderPreserved);
            result.MustSequenceEqual(
                q.Select(i => 5 * i - 17),
                q.ToArray().Select(i => 5 * i - 17),
                orderPreserved && leftOrderDefined);

            LogTestRun(leftOpName, "SelectMany", orderPreserved);
            result.MustSequenceEqual(
                q.SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j),
                q.ToArray().SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j),
                false);

            LogTestRun(leftOpName, "SequenceEqual", orderPreserved);
            if (orderPreserved && leftOrderDefined)
            {
                result.MustEqual(q.SequenceEqual(q), true);
            }
            else
            {
                // We don't check the return value as it can be either true or false
                q.SequenceEqual(q);
            }

            LogTestRun(leftOpName, "Skip", orderPreserved);
            CheckTakeSkip(
                result,
                q.Skip(10),
                q.ToArray().Skip(10),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "SkipWhile", orderPreserved);
            CheckTakeSkip(
                result,
                q.SkipWhile(i => i < 30),
                q.ToArray().SkipWhile(i => i < 30),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "SkipWhileIndexed", orderPreserved);
            CheckTakeSkip(
                result,
                q.SkipWhile((i, j) => j < 30),
                q.ToArray().SkipWhile((i, j) => j < 30),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "Take", orderPreserved);
            CheckTakeSkip(
                result,
                q.Take(10),
                q.ToArray().Take(10),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "TakeWhile", orderPreserved);
            CheckTakeSkip(
                result,
                q.TakeWhile(i => i < 30),
                q.ToArray().TakeWhile(i => i < 30),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "TakeWhileIndexed", orderPreserved);
            CheckTakeSkip(
                result,
                q.TakeWhile((i, j) => j < 30),
                q.ToArray().TakeWhile((i, j) => j < 30),
                leftOrderDefined && orderPreserved);

            LogTestRun(leftOpName, "Union", orderPreserved);
            result.MustSequenceEqual(
                q.Union(Enumerable.Range(90, 50).AsParallel()),
                q.ToArray().Union(Enumerable.Range(90, 50)),
                false);

            LogTestRun(leftOpName, "Where", orderPreserved);
            result.MustSequenceEqual(
                q.Where(i => i < 20 || i > 80),
                q.ToArray().Where(i => i < 20 || i > 80),
                orderPreserved && leftOrderDefined);

            LogTestRun(leftOpName, "Zip", orderPreserved);
            IEnumerable <KeyValuePair <int, int> > zipQ = q.Zip(q, (i, j) => new KeyValuePair <int, int>(i, j));

            result.MustSequenceEqual(
                zipQ.Select(p => p.Key),
                q.Reverse().Reverse().ToArray(),
                orderPreserved && leftOrderDefined);
            result.MustSequenceEqual(
                zipQ.Select(p => p.Value),
                q.Reverse().Reverse().ToArray(),
                orderPreserved && leftOrderDefined);
        }
        public async Task <string> UserSearch([FromBody] SearchViewModel searchViewModel)
        {
            var             resultId = "";
            ApplicationUser user     = await accountService.FindUserById(User.Claims.First(c => c.Type == "UserID").Value);

            string     userId     = user.Id;
            SearchLine searchline = new SearchLine()
            {
                user        = user,
                gender      = searchViewModel.Gender,
                age_max     = searchViewModel.AgeMax,
                age_min     = searchViewModel.AgeMin,
                searchStart = DateTime.Now
            };

            while (true)
            {
                bool isAdded = _searchList.TryAdd(userId, searchline);
                if (isAdded)
                {
                    break;
                }
            }

            await Task.Run(() =>
            {
                var isEnd = false;
                var child = Task.Run(() =>
                {
                    const int numberOfSecondsToWait = 30;

                    while (!isEnd)
                    {
                        //Находим других юзеров, которые ищут нас
                        //Оптимизируем поиск используя распаралелливание по ядрам процессора
                        ParallelQuery <SearchLine> searchingRightNow = _searchList.Select(d => d.Value)
                                                                       .AsParallel()
                                                                       .WithDegreeOfParallelism(Environment.ProcessorCount)
                                                                       .Where(u => EF.Functions.DateDiffYear(searchline.user.BirthDay, DateTime.Today) >= u.age_min &&
                                                                              EF.Functions.DateDiffYear(searchline.user.BirthDay, DateTime.Today) <= u.age_max &&
                                                                              (u.gender == searchline.user.Gender) || (u.gender == "no matter") &&
                                                                              EF.Functions.DateDiffSecond(u.searchStart, DateTime.Now) <= numberOfSecondsToWait &&
                                                                              u.user.Id != searchline.user.Id);

                        List <SearchLine> full_match = new List <SearchLine>();
                        if (searchingRightNow.Any())
                        {
                            //Выбираем из юзеров, которые искали нас тех, кого мы ищем
                            full_match = searchingRightNow.Where(u => EF.Functions.DateDiffYear(u.user.BirthDay, DateTime.Today) >= searchline.age_min &&
                                                                 EF.Functions.DateDiffYear(u.user.BirthDay, DateTime.Today) <= searchline.age_max &&
                                                                 (searchline.gender == u.user.Gender) || (searchline.gender == "no matter"))
                                         .ToList();
                        }

                        if (full_match.Count > 1)
                        {
                            resultId = full_match.OrderBy(sl => sl.searchStart).First().user.Id;
                            break;
                        }
                        else if (full_match.Count == 1)
                        {
                            resultId = full_match.First().user.Id;
                            break;
                        }

                        Thread.Sleep(500); //для оптимизации работы ЦП
                    }
                });

                for (int i = 0; i < 6; i++)
                {
                    Thread.Sleep(5000);

                    if (child.IsCompleted)
                    {
                        break;
                    }
                }

                isEnd = true;

                //Удалить юзера из списка после окончания поиска
                while (true)
                {
                    bool isRemoved = _searchList.TryRemove(userId, out var removedSearchLine);
                    if (isRemoved)
                    {
                        break;
                    }
                }
            });

            return(JsonConvert.SerializeObject(resultId));
        }
예제 #7
0
        public static Hashtable Parse(string strId, LanguageType language)
        {
            Hashtable objResults = new Hashtable();

            try
            {
                Uri    strUrl   = new Uri(string.Format(RootUrl + "movie/{2}?api_key={1}&language={0}&append_to_response={3}", language.ToString().ToLower(), "ec7abdb7bea26002ff70d02cb5b11224", strId, "casts,rating"));
                string response = Util.GetRest(strUrl, false, true);

                //Fix since version 2.6.0.0
                if (string.IsNullOrWhiteSpace(response))
                {
                    return(null);
                }

                JObject restResponse = JObject.Parse(response);
                TMDB    objTmdb      = TMDB.MovieToObject(restResponse);
                if (objTmdb != null)
                {
                    if (objTmdb.Cast != null && MySettings.FastSearch == false)
                    {
                        //FIX 2.8.9.0
                        ParallelQuery <Artist> artists = objTmdb.Cast.AsParallel().Select(item => GetArtist(item));
                        if (artists != null && artists.Any())
                        {
                            objResults.Add("Actors", artists.ToList());
                        }
                    }
                    objResults.Add("Country", objTmdb.Country);

                    if (!string.IsNullOrWhiteSpace(objTmdb.MovieDescription))
                    {
                        objResults.Add("Description", objTmdb.MovieDescription);
                    }

                    if (string.IsNullOrEmpty(objTmdb.Director) == false && MySettings.FastSearch == false)
                    {
                        List <Artist> directors = new List <Artist>();
                        Artist        director  = GetArtist(objTmdb.Director);
                        directors.Add(director);

                        objResults.Add("Director", directors);
                    }

                    objResults.Add("Types", objTmdb.Genres);

                    if (!string.IsNullOrWhiteSpace(objTmdb.PosterOriginal))
                    {
                        objResults.Add("Image", objTmdb.PosterOriginal);
                    }

                    objResults.Add("Background", objTmdb.BackdropOriginal);
                    if (string.IsNullOrWhiteSpace(objTmdb.Url) == false)
                    {
                        objResults.Add("Links", objTmdb.Url);
                    }

                    if (string.IsNullOrWhiteSpace(objTmdb.Rated) == false)
                    {
                        objResults.Add("Rated", objTmdb.Rated);
                    }

                    if (!string.IsNullOrWhiteSpace(objTmdb.Tagline))
                    {
                        objResults.Add("Comments", objTmdb.Tagline);
                    }

                    if (objTmdb.Rating != null)
                    {
                        decimal dclTemp;
                        if (decimal.TryParse(objTmdb.Rating.Replace(".", ","), out dclTemp))
                        {
                            if (dclTemp > 0)
                            {
                                objResults.Add("Rating", dclTemp * 2);
                            }
                        }
                    }

                    if (objTmdb.Released.HasValue)
                    {
                        objResults.Add("Released", objTmdb.Released.Value);
                    }

                    if (objTmdb.Runtime.HasValue)
                    {
                        objResults.Add("Runtime", objTmdb.Runtime.Value);
                    }

                    if (!string.IsNullOrWhiteSpace(objTmdb.Studio))
                    {
                        objResults.Add("Studio", objTmdb.Studio);
                    }

                    objResults.Add("Title", objTmdb.Title);
                }

                return(objResults);
            }
            catch (Exception ex)
            {
                Util.LogException(ex, strId);
                return(null);
            }
        }