Exemplo n.º 1
0
        /// <summary>
        /// Modifies the query such that repeat calls use the same cutoff time as the first call.
        /// Useful when paginating over results obtained with WaitForNonStaleResultsAsOfNow so that all pages use the same now.
        /// </summary>
        public static IRavenQueryable <T> SyncCutoff <T>(this IRavenQueryable <T> queryable)
        {
            return(queryable.Customize(x => x.TransformResults((query, results) =>
            {
                if (query.Cutoff.HasValue)
                {
                    queryable.Customize(z => z.WaitForNonStaleResultsAsOf(query.Cutoff.Value));
                }

                return results;
            })));
        }
Exemplo n.º 2
0
        public IActionResult GetDatabases(string tenantId, bool ensureUpToDate = false)
        {
            Tenant tenant = DocumentSession.Load <Tenant>(tenantId);

            if (tenant == null)
            {
                return(NotFound(new
                {
                    Id = tenantId,
                    EntityType = "Tenant",
                    Message = $"Tenant not found with Id '{tenantId}."
                }));
            }

            IRavenQueryable <DatabaseInstance> query = DocumentSession.Query <DatabaseInstance, DatabaseInstanceDetails>();

            if (ensureUpToDate)
            {
                query = query.Customize(
                    queryConfig => queryConfig.WaitForNonStaleResults(
                        waitTimeout: TimeSpan.FromSeconds(5)
                        )
                    );
            }

            return(Json(
                       query.Where(
                           database => database.TenantId == tenantId
                           )
                       .ProjectFromIndexFieldsInto <DatabaseInstanceDetail>()
                       ));
        }
Exemplo n.º 3
0
        private IRavenQueryable <T> ApplyNonStaleResultsStrategy <T>(IRavenQueryable <T> query, StaleResultsMode mode)
        {
            if (mode != StaleResultsMode.WaitForNonStaleResults)
            {
                return(query);
            }

            var whait = _sessionSettings.StaleResultsWhait;

            switch (_sessionSettings.StaleResultWhaitMode)
            {
            case StaleResultWhaitMode.AtNow:
                query = whait.HasValue
                                ? query.Customize(x => x.WaitForNonStaleResultsAsOfNow(whait.Value))
                                : query.Customize(x => x.WaitForNonStaleResultsAsOfNow());
                break;

            case StaleResultWhaitMode.AllNonStale:
                query = whait.HasValue
                                ? query.Customize(x => x.WaitForNonStaleResults(whait.Value))
                                : query.Customize(x => x.WaitForNonStaleResults());
                break;

            case StaleResultWhaitMode.AtLastWrite:
                query = whait.HasValue
                                ? query.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite(whait.Value))
                                : query.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite());
                break;
            }
            return(query);
        }
        private static void AssertRetrievedOrdered(IRavenQueryable <User> query)
        {
            var all = query
                      .Customize(x => x.WaitForNonStaleResults())
                      .ToList();

            foreach (var mc in all)
            {
                Console.WriteLine(mc);
            }

            CollectionAssert.IsOrdered(all, new UserComparer());
        }
Exemplo n.º 5
0
        private static IRavenQueryable <T> ApplyLoadingStrategyToQuery <T>(IRavenQueryable <T> query, ILoadingStrategy <T> strategy)
        {
            var loadingStrategy = (LoadingStrategy <T>)strategy;

            if (loadingStrategy == null || loadingStrategy.IsEmpty)
            {
                return(query);
            }

            return(query.Customize(x =>
            {
                var property = loadingStrategy.Dequeue();
                x.Include(property);
                while (!loadingStrategy.IsEmpty)
                {
                    property = loadingStrategy.Dequeue();
                    x.Include(property);
                }
            }));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Includes the specified path in the query, loading the document specified in that path
 /// </summary>
 /// <typeparam name="TResult">The type of the object that holds the id that you want to include.</typeparam>
 /// <typeparam name="TInclude">The type of the object that you want to include.</typeparam>
 /// <param name="source">The source for querying</param>
 /// <param name="path">The path, which is name of the property that holds the id of the object to include.</param>
 /// <returns></returns>
 public static IRavenQueryable <TResult> Include <TResult, TInclude>(this IRavenQueryable <TResult> source, Expression <Func <TResult, object> > path)
 {
     source.Customize(x => x.Include <TResult, TInclude>(path));
     return(source);
 }
Exemplo n.º 7
0
        private static void AssertRetrievedOrdered(IRavenQueryable<User> query)
        {
            var all = query
                .Customize(x => x.WaitForNonStaleResults())
                .ToList();
            foreach (var mc in all)
            {
                Console.WriteLine(mc);
            }

            CollectionAssert.IsOrdered(all, new UserComparer());
        }
Exemplo n.º 8
0
 /// <summary>
 /// Order the results by the specified fields
 /// The fields are the names of the fields to sort, defaulting to sorting by descending.
 /// You can prefix a field name with '-' to indicate sorting by descending or '+' to sort by ascending
 /// </summary>
 public static IRavenQueryable <T> OrderByDescending <T>(this IRavenQueryable <T> source, params string[] fields)
 {
     return(source.Customize(x => ((IDocumentQuery <T>)x).OrderByDescending(fields)));
 }
 public HierarchicalUserAccount GetByID(Guid id)
 {
     return(items.Customize(x => x.WaitForNonStaleResultsAsOfNow())
            .Where(x => x.ID == id)
            .SingleOrDefault());
 }
Exemplo n.º 10
0
        public IEnumerable <TEntity> Enumerate(Func <IQueryable <TEntity>, IQueryable <TEntity> > filter = null, int pageSize = 120)
        {
            RavenQueryStatistics stats;
            var session        = OpenClonedSession(Session);
            var sessQueryCount = 1;

            IRavenQueryable <TEntity> ravenQuery = Session.Query <TEntity>().Statistics(out stats);

            IQueryable <TEntity> results = !_configuration.WaitForStaleIndexes
                ? ravenQuery
                : ravenQuery.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()); //ДЛЯ ТЕСТОВ

            if (filter != null)
            {
                results = filter(results);
            }

            results = results
                      .Skip(0 * pageSize) // retrieve results for the first page
                      .Take(pageSize);    // page size is 10;

            var pages = (stats.TotalResults - stats.SkippedResults) / pageSize;

            foreach (var entity in results)
            {
                yield return(entity);
            }

            try
            {
                for (int i = 1; i <= pages; i++)
                {
                    if (sessQueryCount++ >= session.Advanced.MaxNumberOfRequestsPerSession)
                    {
                        session.Dispose();
                        session = OpenClonedSession(Session);
                    }

                    ravenQuery = session.Query <TEntity>()
                                 .Statistics(out stats);

                    results = !_configuration.WaitForStaleIndexes
                        ? ravenQuery
                        : ravenQuery.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()); //ДЛЯ ТЕСТОВ

                    if (filter != null)
                    {
                        results = filter(results);
                    }

                    results = results
                              .Skip(i * pageSize + stats.SkippedResults)
                              .Take(pageSize);

                    foreach (var entity in results)
                    {
                        yield return(entity);
                    }
                }
            }
            finally
            {
                session.Dispose();
            }
        }
Exemplo n.º 11
0
 public override IRavenQueryable <T> RelatesToShape(IRavenIndexable geometry, SpatialRelation relation)
 {
     return(_source.Customize(x => x.RelatesToShape(SpatialField.NameFor(_property), _writer.Write(geometry.GetSpatial4nShape()), relation)));
 }