Ejemplo n.º 1
0
        public static IndexQuery GetIndexQueryFromHttpContext(this IHttpContext context, int maxPageSize)
        {
            var query = new IndexQuery
            {
                Query                = context.Request.QueryString["query"] ?? "",
                Start                = context.GetStart(),
                Cutoff               = context.GetCutOff(),
                CutoffEtag           = context.GetCutOffEtag(),
                PageSize             = context.GetPageSize(maxPageSize),
                SkipTransformResults = context.GetSkipTransformResults(),
                FieldsToFetch        = context.Request.QueryString.GetValues("fetch"),
                GroupBy              = context.Request.QueryString.GetValues("groupBy"),
                DefaultField         = context.Request.QueryString["defaultField"],

                DefaultOperator =
                    string.Equals(context.Request.QueryString["operator"], "AND", StringComparison.OrdinalIgnoreCase) ?
                    QueryOperator.And :
                    QueryOperator.Or,

                AggregationOperation = context.GetAggregationOperation(),
                SortedFields         = context.Request.QueryString.GetValues("sort")
                                       .EmptyIfNull()
                                       .Select(x => new SortedField(x))
                                       .ToArray(),
                HighlightedFields   = context.GetHighlightedFields().ToArray(),
                HighlighterPreTags  = context.Request.QueryString.GetValues("preTags"),
                HighlighterPostTags = context.Request.QueryString.GetValues("postTags"),
                ResultsTransformer  = context.Request.QueryString["resultsTransformer"],
                QueryInputs         = context.ExtractQueryInputs()
            };


            var          spatialFieldName = context.Request.QueryString["spatialField"] ?? Constants.DefaultSpatialFieldName;
            var          queryShape       = context.Request.QueryString["queryShape"];
            SpatialUnits units;
            bool         unitsSpecified = Enum.TryParse(context.Request.QueryString["spatialUnits"], out units);
            double       distanceErrorPct;

            if (!double.TryParse(context.Request.QueryString["distErrPrc"], NumberStyles.Any, CultureInfo.InvariantCulture, out distanceErrorPct))
            {
                distanceErrorPct = Constants.DefaultSpatialDistanceErrorPct;
            }
            SpatialRelation spatialRelation;

            if (Enum.TryParse(context.Request.QueryString["spatialRelation"], false, out spatialRelation) &&
                !string.IsNullOrWhiteSpace(queryShape))
            {
                return(new SpatialIndexQuery(query)
                {
                    SpatialFieldName = spatialFieldName,
                    QueryShape = queryShape,
                    RadiusUnitOverride = unitsSpecified ? units : (SpatialUnits?)null,
                    SpatialRelation = spatialRelation,
                    DistanceErrorPercentage = distanceErrorPct,
                });
            }
            return(query);
        }
Ejemplo n.º 2
0
        public override void Respond(IHttpContext context)
        {
            RavenJArray itemsToLoad;

            if (context.Request.HttpMethod == "POST")
            {
                itemsToLoad = context.ReadJsonArray();
            }
            else
            {
                itemsToLoad = new RavenJArray(context.Request.QueryString.GetValues("id"));
            }
            var result      = new MultiLoadResult();
            var loadedIds   = new HashSet <string>();
            var includes    = context.Request.QueryString.GetValues("include") ?? new string[0];
            var transformer = context.Request.QueryString["transformer"] ?? context.Request.QueryString["resultTransformer"];

            var queryInputs = context.ExtractQueryInputs();

            var transactionInformation = GetRequestTransaction(context);
            var includedEtags          = new List <byte>();

            Database.TransactionalStorage.Batch(actions =>
            {
                foreach (RavenJToken item in itemsToLoad)
                {
                    var value = item.Value <string>();
                    if (loadedIds.Add(value) == false)
                    {
                        continue;
                    }
                    JsonDocument documentByKey = string.IsNullOrEmpty(transformer)
                                                        ? Database.Get(value, transactionInformation)
                                        : Database.GetWithTransformer(value, transformer, transactionInformation, queryInputs);
                    if (documentByKey == null)
                    {
                        continue;
                    }
                    result.Results.Add(documentByKey.ToJson());

                    if (documentByKey.Etag != null)
                    {
                        includedEtags.AddRange(documentByKey.Etag.ToByteArray());
                    }
                    includedEtags.Add((documentByKey.NonAuthoritativeInformation ?? false) ? (byte)0 : (byte)1);
                }

                var addIncludesCommand = new AddIncludesCommand(Database, transactionInformation, (etag, includedDoc) =>
                {
                    includedEtags.AddRange(etag.ToByteArray());
                    result.Includes.Add(includedDoc);
                }, includes, loadedIds);

                foreach (var item in result.Results.Where(item => item != null))
                {
                    addIncludesCommand.Execute(item);
                }
            });

            Etag computedEtag;

            using (var md5 = MD5.Create())
            {
                var computeHash = md5.ComputeHash(includedEtags.ToArray());
                computedEtag = Etag.Parse(computeHash);
            }

            if (context.MatchEtag(computedEtag))
            {
                context.SetStatusToNotModified();
                return;
            }

            context.WriteETag(computedEtag);
            context.WriteJson(result);
        }
Ejemplo n.º 3
0
		public override void Respond(IHttpContext context)
		{
			RavenJArray itemsToLoad;
			if(context.Request.HttpMethod == "POST")
				itemsToLoad = context.ReadJsonArray();
			else
				itemsToLoad = new RavenJArray(context.Request.QueryString.GetValues("id"));
			var result = new MultiLoadResult();
			var loadedIds = new HashSet<string>();
			var includes = context.Request.QueryString.GetValues("include") ?? new string[0];
			var transformer = context.Request.QueryString["transformer"] ?? context.Request.QueryString["resultTransformer"];

		    var queryInputs = context.ExtractQueryInputs();
            
            var transactionInformation = GetRequestTransaction(context);
		    var includedEtags = new List<byte>();
			Database.TransactionalStorage.Batch(actions =>
			{
				foreach (RavenJToken item in itemsToLoad)
				{
					var value = item.Value<string>();
					if(loadedIds.Add(value)==false)
						continue;
					JsonDocument documentByKey = string.IsNullOrEmpty(transformer)
				                        ? Database.Get(value, transactionInformation)
                                        : Database.GetWithTransformer(value, transformer, transactionInformation, queryInputs);
				    if (documentByKey == null)
						continue;
					result.Results.Add(documentByKey.ToJson());

					if (documentByKey.Etag != null)
					{
						includedEtags.AddRange(documentByKey.Etag.ToByteArray());
					}
					includedEtags.Add((documentByKey.NonAuthoritativeInformation ?? false) ? (byte)0 : (byte)1);
				}

				var addIncludesCommand = new AddIncludesCommand(Database, transactionInformation, (etag, includedDoc) =>
				{
					includedEtags.AddRange(etag.ToByteArray());
					result.Includes.Add(includedDoc);
				}, includes, loadedIds);

				foreach (var item in result.Results.Where(item => item != null))
				{
					addIncludesCommand.Execute(item);
				}
			});

			Etag computedEtag;
            
			using (var md5 = MD5.Create())
			{
				var computeHash = md5.ComputeHash(includedEtags.ToArray());
				computedEtag = Etag.Parse(computeHash);
			}

			if (context.MatchEtag(computedEtag))
			{
				context.SetStatusToNotModified();
				return;
			}

			context.WriteETag(computedEtag);
			context.WriteJson(result);
		}