Exemplo n.º 1
0
        private List <PackageIndexEntity> GetPackages(DateTime?lastIndexTime)
        {
            using (var context = _contextThunk())
            {
                var packageRepo = new EntityRepository <Package>(context);

                IQueryable <Package> set = packageRepo.GetAll();

                if (lastIndexTime.HasValue)
                {
                    // Retrieve the Latest and LatestStable version of packages if any package for that registration changed since we last updated the index.
                    // We need to do this because some attributes that we index such as DownloadCount are values in the PackageRegistration table that may
                    // update independent of the package.
                    set = set.Where(
                        p => (_indexContainsAllVersions || p.IsLatest || p.IsLatestStable) &&
                        p.PackageRegistration.Packages.Any(p2 => p2.LastUpdated > lastIndexTime)
                        );
                    Trace.WriteLine("Lucene Indexer: Getting changed results from the database since {0}".format_with(lastIndexTime));
                }
                else if (!_indexContainsAllVersions)
                {
                    set = set.Where(p => p.IsLatest || p.IsLatestStable); // which implies that p.IsListed by the way!
                    Trace.WriteLine("Lucene Indexer: Getting latest/latest stable results from the database");
                }
                else
                {
                    // get everything including unlisted
                    set = set.Where(p => p.StatusForDatabase != _rejectedStatus || p.StatusForDatabase == null);
                    Trace.WriteLine("Lucene Indexer: Getting all results from the database");
                }

                var list = MethodExtensionWrappers.WrapExecutionTracingTime(() =>
                {
                    return(set
                           .Include(p => p.PackageRegistration)
                           .Include(p => p.PackageRegistration.Owners)
                           .Include(p => p.SupportedFrameworks)
                           .ToList());
                }, "Lucene Indexer: Select from database completed");

                var packagesForIndexing = MethodExtensionWrappers.WrapExecutionTracingTime(
                    () =>
                {
                    return(list.Select(
                               p => new PackageIndexEntity
                    {
                        Package = p
                    }));
                },
                    "Lucene Indexer: Converted results from what was pulled");

                return(packagesForIndexing.ToList());
            }
        }
 public void DeleteOnCommit(T entity)
 {
     if (TraceLogEvents)
     {
         MethodExtensionWrappers.WrapExecutionTracingTime(
             () => entities.Set <T>().Remove(entity),
             "Remove '{0}' ('{1}') from database".format_with(typeof(T).Name, entity.Key.to_string()));
     }
     else
     {
         entities.Set <T>().Remove(entity);
     }
 }
 public void CommitChanges()
 {
     if (TraceLogEvents)
     {
         MethodExtensionWrappers.WrapExecutionTracingTime(
             () => entities.SaveChanges(),
             "Save all changes to database");
     }
     else
     {
         entities.SaveChanges();
     }
 }
        public int InsertOnCommit(T entity)
        {
            if (TraceLogEvents)
            {
                MethodExtensionWrappers.WrapExecutionTracingTime(
                    () => entities.Set <T>().Add(entity),
                    () => "Added new '{0}' ('{1}') to database".format_with(typeof(T).Name, entity.Key.to_string()));
            }
            else
            {
                entities.Set <T>().Add(entity);
            }

            return(entity.Key);
        }
        public T Get(int key)
        {
            Func <T> getMethod = () => entities.Set <T>().Find(key);

            if (TraceLogEvents)
            {
                getMethod = () =>
                {
                    return(MethodExtensionWrappers.WrapExecutionTracingTime(
                               () => entities.Set <T>().Find(key),
                               "Get '{0}' ('{1}') from database".format_with(typeof(T).Name, key.to_string())));
                };
            }

            return(Cache.Get(
                       string.Format("item-{0}-{1}", typeof(T).Name, key),
                       DateTime.UtcNow.AddMinutes(Cache.DEFAULT_CACHE_TIME_MINUTES),
                       getMethod
                       ));
        }