public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
		{
			var result = new FacetResults();

			foreach (var response in responses.Select(response => (RavenJObject)response.Result))
			{
				var facet = response.JsonDeserialization<FacetResults>();
				foreach (var facetResult in facet.Results)
				{
					if (!result.Results.ContainsKey(facetResult.Key))
						result.Results[facetResult.Key] = new FacetResult();

					var newFacetResult = result.Results[facetResult.Key];
					foreach (var facetValue in facetResult.Value.Values)
					{
						var existingFacetValueRange = newFacetResult.Values.Find((x) => x.Range == facetValue.Range);
						if (existingFacetValueRange != null)
							existingFacetValueRange.Hits += facetValue.Hits;
						else
							newFacetResult.Values.Add(new FacetValue() { Hits = facetValue.Hits, Range = facetValue.Range });
					}

					foreach (var facetTerm in facetResult.Value.RemainingTerms)
					{
						if (!newFacetResult.RemainingTerms.Contains(facetTerm))
							newFacetResult.RemainingTerms.Add(facetTerm);
					}
				}
			}

			Result = result;
		}
Beispiel #2
0
			public MultiGetHttpContext(IRavenHttpConfiguration configuration, IHttpContext realContext, GetRequest req)
			{
				this.configuration = configuration;
				this.realContext = realContext;
				getResponse = new GetResponse();
				Request = new MultiGetHttpRequest(req, realContext.Request);
				Response = new MultiGetHttpResponse(getResponse, realContext.Response);
			}
Beispiel #3
0
		private void HandleRequest(GetRequest[] requests, GetResponse[] results, int i, IHttpContext context, IRavenHttpConfiguration ravenHttpConfiguration, MultiGetHttpContext[] contexts)
		{
			var request = requests[i];
			if (request == null)
				return;
			server.HandleActualRequest(contexts[i]);
			results[i] = contexts[i].Complete();
		}
Beispiel #4
0
		private void HandleRequest(GetRequest[] requests, GetResponse[] results, int i, IHttpContext context, IRavenHttpConfiguration ravenHttpConfiguration)
		{
			var request = requests[i];
			if (request == null)
				return;
			var ctx = new MultiGetHttpContext(ravenHttpConfiguration, context, request, TenantId);
			server.HandleActualRequest(ctx);
			results[i] = ctx.Complete();
		}
		public void HandleResponse(GetResponse response)
		{
            if (response.RequestHasErrors())
            {
                throw new InvalidOperationException("Got an unexpected response code for the request: " + response.Status + "\r\n" + response.Result);
            }

			var result = (RavenJObject)response.Result;
			Result = result.JsonDeserialization<FacetResults>();
		}
		public void HandleResponse(GetResponse response)
		{
			if (response.Status != 200)
			{
				throw new InvalidOperationException("Got an unexpected response code for the request: " + response.Status + "\r\n" +
				                                    response.Result);
			}

			var result = RavenJObject.Parse(response.Result);
			Result = result.JsonDeserialization<IDictionary<string, IEnumerable<FacetValue>>>();
		}
		public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
		{
			var result = new SuggestionQueryResult
			{
				Suggestions = responses
					.Select(item => RavenJObject.Parse(item.Result))
					.SelectMany(data => ((RavenJArray) data["Suggestions"]).Select(x => x.Value<string>()))
					.Distinct()
					.ToArray()
			};

			Result = result;
		}
		public void HandleResponse(GetResponse response)
		{
			if (response.Status != 200 && response.Status != 304)
			{
				throw new InvalidOperationException("Got an unexpected response code for the request: " + response.Status + "\r\n" +
													response.Result);
			}

			var result = (RavenJObject)response.Result;
			Result = new SuggestionQueryResult
			{
				Suggestions = ((RavenJArray)result["Suggestions"]).Select(x => x.Value<string>()).ToArray(),
			};
		}
		public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
		{
			var result = new SuggestionQueryResult
			{
				Suggestions = (from item in responses
							   let data = (RavenJObject)item.Result
							   from suggestion in (RavenJArray)data["Suggestions"]
							   select suggestion.Value<string>())
							  .Distinct()
							  .ToArray()
			};

			Result = result;
		}
Beispiel #10
0
		private void HandleRequest(GetRequest[] requests, GetResponse[] results, int i, IHttpContext context, InMemoryRavenConfiguration ravenHttpConfiguration, MultiGetHttpContext[] contexts)
		{
			var request = requests[i];
			if (request == null)
				return;
		    ravenHttpConfiguration.ConcurrentMultiGetRequests.Wait();
		    try
		    {
		        server.HandleActualRequest(contexts[i]);
		    }
		    finally 
            {
                ravenHttpConfiguration.ConcurrentMultiGetRequests.Release();
		    }
			results[i] = contexts[i].Complete();
		}
Beispiel #11
0
		public GetResponse[] HandleCachingResponse(GetResponse[] responses, HttpJsonRequestFactory jsonRequestFactory)
		{
			var hasCachedRequests = false;
			var requestStatuses = new RequestStatus[responses.Length];
			for (int i = 0; i < responses.Length; i++)
			{
				if (responses[i] == null || responses[i].Status == 304)
				{
					hasCachedRequests = true;

					requestStatuses[i] = responses[i] == null ? RequestStatus.AggressivelyCached : RequestStatus.Cached;
					responses[i] = responses[i] ?? new GetResponse { Status = 0 };

					foreach (string header in cachedData[i].Headers)
					{
						responses[i].Headers[header] = cachedData[i].Headers[header];
					}
					responses[i].Result = cachedData[i].Data.CloneToken();
					jsonRequestFactory.IncrementCachedRequests();
				}
				else
				{
					requestStatuses[i] = responses[i].RequestHasErrors() ? RequestStatus.ErrorOnServer : RequestStatus.SentToServer;

					var nameValueCollection = new NameValueCollection();
					foreach (var header in responses[i].Headers)
					{
						nameValueCollection[header.Key] = header.Value;
					}
					jsonRequestFactory.CacheResponse(url + requests[i].UrlAndQuery, responses[i].Result, nameValueCollection);
				}
			}

			if (hasCachedRequests == false || convention.DisableProfiling ||
                holdProfilingInformation.ProfilingInformation.Requests.Count == 0)
				return responses;

			var lastRequest = holdProfilingInformation.ProfilingInformation.Requests.Last();
			for (int i = 0; i < requestStatuses.Length; i++)
			{
				lastRequest.AdditionalInformation["NestedRequestStatus-" + i] = requestStatuses[i].ToString();
			}
			lastRequest.Result = JsonConvert.SerializeObject(responses);

			return responses;
		}
Beispiel #12
0
		public override void Respond(IHttpContext context)
		{
			if (recursive.Value)
				throw new InvalidOperationException("Nested requests to multi_get are not supported");
			recursive.Value = true;
			try
			{
				var requests = context.ReadJsonObject<GetRequest[]>();
				var results = new GetResponse[requests.Length];

				Executerequests(context, Settings, results, requests);
				
				context.WriteJson(results);
			}
			finally
			{
				recursive.Value = false;
			}
		}
Beispiel #13
0
		private void Executerequests(
			IHttpContext context, 
			IRavenHttpConfiguration ravenHttpConfiguration, 
			GetResponse[] results,
			GetRequest[] requests)
		{
			if ("yes".Equals(context.Request.QueryString["parallel"], StringComparison.InvariantCultureIgnoreCase))
			{
				Parallel.For(0, requests.Length, position =>
					HandleRequest(requests, results, position, context, ravenHttpConfiguration)
					);
			}
			else
			{
				for (var i = 0; i < requests.Length; i++)
				{
					HandleRequest(requests, results, i, context, ravenHttpConfiguration);
				}
			}
		}
		public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
		{
			var result = new Dictionary<string, IEnumerable<FacetValue>>();

			IEnumerable<IGrouping<string, KeyValuePair<string, IEnumerable<FacetValue>>>> list = responses.Select(response => RavenJObject.Parse(response.Result))
				.SelectMany(jsonResult => jsonResult.JsonDeserialization<IDictionary<string, IEnumerable<FacetValue>>>())
				.GroupBy(x => x.Key);

			foreach (var facet in list)
			{
				var individualFacet = facet.SelectMany(x=>x.Value).GroupBy(x=>x.Range)
					.Select(g=>new FacetValue
					{
						Count = g.Sum(y=>y.Count),
						Range = g.Key
					});
				result[facet.Key] = individualFacet.ToList();
			}

			Result = result;
		}
Beispiel #15
0
		private void Executerequests(
			IHttpContext context, 
			IRavenHttpConfiguration ravenHttpConfiguration, 
			GetResponse[] results,
			GetRequest[] requests)
		{
			// Need to create this here to preserve any current TLS data that we have to copy
			var contexts = requests.Select(request => new MultiGetHttpContext(ravenHttpConfiguration, context, request, TenantId))
				.ToArray();
			if ("yes".Equals(context.Request.QueryString["parallel"], StringComparison.InvariantCultureIgnoreCase))
			{
				Parallel.For(0, requests.Length, position =>
					HandleRequest(requests, results, position, context, ravenHttpConfiguration, contexts)
					);
			}
			else
			{
				for (var i = 0; i < requests.Length; i++)
				{
					HandleRequest(requests, results, i, context, ravenHttpConfiguration, contexts);
				}
			}
		}
		public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func<string, RavenJObject, Etag, Task<ConflictException>> tryResolveConflictOrCreateConcurrencyException)
		{
			foreach (var response in responses)
			{
				if(response == null)
					continue;
				if (response.RequestHasErrors() && response.Status != 409)
					continue;

				var result = response.Result as RavenJObject;
				if (result == null)
					continue;

				if (result.ContainsKey("Results"))
				{
					var results = result["Results"] as RavenJArray;
					if (results == null)
						continue;

					foreach (RavenJToken value in results)
					{
						var docResult = value as RavenJObject;
						if (docResult == null)
							return;

						var metadata = docResult[Constants.Metadata];
						if (metadata == null)
							return;

						if (metadata.Value<int>("@Http-Status-Code") != 409)
							return;

						var id = metadata.Value<string>("@id");
						var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value<string>("@etag"));

						await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
							tryResolveConflictOrCreateConcurrencyException,
							id,
							etag,
							docResult,
							response);
					}

					continue;
				}
				if (result.ContainsKey("Conflicts"))
				{
					var id = response.Headers[Constants.DocumentIdFieldName];
					var etag = response.GetEtagHeader();

					await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
							tryResolveConflictOrCreateConcurrencyException,
							id,
							etag,
							result,
							response);
				}
			}
		}
Beispiel #17
0
		private static async Task TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
			Func<string, RavenJObject, Etag, Task<ConflictException>> tryResolveConflictOrCreateConcurrencyException,
			string id,
			Etag etag,
			RavenJObject docResult,
			GetResponse response)
		{
			var concurrencyException = await tryResolveConflictOrCreateConcurrencyException(id, docResult, etag);

			if (concurrencyException != null)
				throw concurrencyException;

			response.Status = 200;
			response.ForceRetry = true;
		}
Beispiel #18
0
		public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func<string, RavenJObject, Etag, Task<ConflictException>> tryResolveConflictOrCreateConcurrencyException)
		{
			// ReSharper disable once ForCanBeConvertedToForeach
			// see: http://ayende.com/blog/169377/the-bug-that-ruined-my-weekend
			for (int index = 0; index < responses.Length; index++)
			{
				var response = responses[index];
				if (response == null)
					continue;
				if (response.RequestHasErrors() && response.Status != 409)
					continue;

				var result = response.Result as RavenJObject;
				if (result == null)
					continue;

				if (result.ContainsKey("Results"))
				{
					var results = result["Results"] as RavenJArray;
					if (results == null)
						continue;

					foreach (RavenJToken value in results)
					{
						var docResult = value as RavenJObject;
						if (docResult == null)
							return;

						var metadata = docResult[Constants.Metadata];
						if (metadata == null)
							return;

						if (metadata.Value<int>("@Http-Status-Code") != 409)
							return;

						var id = metadata.Value<string>("@id");
						var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value<string>("@etag"));

						await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
							tryResolveConflictOrCreateConcurrencyException,
							id,
							etag,
							docResult,
							response);
					}

					continue;
				}
				if (result.ContainsKey("Conflicts"))
				{
					var id = response.Headers[Constants.DocumentIdFieldName];
					var etag = response.GetEtagHeader();

					await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
						tryResolveConflictOrCreateConcurrencyException,
						id,
						etag,
						result,
						response);
				}
			}
		}
Beispiel #19
0
			public MultiGetHttpResponse(GetResponse getResponse, IHttpResponse response)
			{
				this.getResponse = getResponse;
				RedirectionPrefix = response.RedirectionPrefix;
				OutputStream = new MemoryStream();

			}
Beispiel #20
0
			public MultiGetHttpContext(IRavenHttpConfiguration configuration, IHttpContext realContext, GetRequest req, string tenantId)
			{
				this.configuration = configuration;
				this.realContext = realContext;
				this.tenantId = tenantId;
				getResponse = new GetResponse();
				if (req == null)
					return;
				Request = new MultiGetHttpRequest(req, realContext.Request);
				Response = new MultiGetHttpResponse(getResponse, realContext.Response);
			}