ravendb /Raven.Database/Server/Controllers/StreamsController.cs

Language C# Lines 402
MD5 Hash b6c0a9cbe94506a6299a273beec1c63b
Repository https://github.com/nwendel/ravendb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using Raven.Abstractions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Util;
using Raven.Database.Actions;
using Raven.Database.Extensions;
using Raven.Database.Impl;
using Raven.Database.Storage;
using Raven.Imports.Newtonsoft.Json;
using Raven.Imports.Newtonsoft.Json.Linq;
using Raven.Json.Linq;
using System.Linq;

namespace Raven.Database.Server.Controllers
{
	public class StreamsController : RavenDbApiController
	{
		[HttpGet]
		[Route("streams/docs")]
		[Route("databases/{databaseName}/streams/docs")]
		public HttpResponseMessage StreamDocsGet()
		{
			var start = GetStart();
			var etag = GetEtagFromQueryString();
			var startsWith = GetQueryStringValue("startsWith");
			var pageSize = GetPageSize(int.MaxValue);
			var matches = GetQueryStringValue("matches");
			var nextPageStart = GetNextPageStart();
			if (string.IsNullOrEmpty(GetQueryStringValue("pageSize")))
				pageSize = int.MaxValue;

			var skipAfter = GetQueryStringValue("skipAfter");

			return new HttpResponseMessage(HttpStatusCode.OK)
			{
				Content = new PushStreamContent((stream, content, transportContext) =>
					StreamToClient(stream, startsWith, start, pageSize, etag, matches, nextPageStart, skipAfter))
				{
					Headers =
					{
						ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }
					}
				}
			};
		}

		private void StreamToClient(Stream stream, string startsWith, int start, int pageSize, Etag etag, string matches, int nextPageStart, string skipAfter)
		{
			using (var cts = new CancellationTokenSource())
			using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout))
			using (var writer = new JsonTextWriter(new StreamWriter(stream)))
			{
				writer.WriteStartObject();
				writer.WritePropertyName("Results");
				writer.WriteStartArray();

				Database.TransactionalStorage.Batch(accessor =>
				{
					// we may be sending a LOT of documents to the user, and most 
					// of them aren't going to be relevant for other ops, so we are going to skip
					// the cache for that, to avoid filling it up very quickly
					using (DocumentCacher.SkipSettingDocumentsInDocumentCache())
					{
						if (string.IsNullOrEmpty(startsWith))
							Database.Documents.GetDocuments(start, pageSize, etag, cts.Token, doc =>
							{
								timeout.Delay();
								doc.WriteTo(writer);
                                writer.WriteRaw(Environment.NewLine);
							});
						else
						{
							var nextPageStartInternal = nextPageStart;

							Database.Documents.GetDocumentsWithIdStartingWith(startsWith, matches, null, start, pageSize, cts.Token, ref nextPageStartInternal, doc =>
							{
								timeout.Delay();
								doc.WriteTo(writer);
                                writer.WriteRaw(Environment.NewLine);
							},skipAfter: skipAfter);

							nextPageStart = nextPageStartInternal;
						}
					}
				});

				writer.WriteEndArray();
				writer.WritePropertyName("NextPageStart");
				writer.WriteValue(nextPageStart);
				writer.WriteEndObject();
				writer.Flush();
			}
		}

		[HttpGet]
		[Route("streams/query/{*id}")]
		[Route("databases/{databaseName}/streams/query/{*id}")]
		public HttpResponseMessage SteamQueryGet(string id)
		{
			var cts = new CancellationTokenSource();
			var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout);
			var msg = GetEmptyMessage();

			var index = id;
			var query = GetIndexQuery(int.MaxValue);
			if (string.IsNullOrEmpty(GetQueryStringValue("pageSize"))) query.PageSize = int.MaxValue;
			var isHeadRequest = InnerRequest.Method == HttpMethod.Head;
			if (isHeadRequest) query.PageSize = 0;

			var accessor = Database.TransactionalStorage.CreateAccessor(); //accessor will be disposed in the StreamQueryContent.SerializeToStreamAsync!

			try
			{
				var queryOp = new QueryActions.DatabaseQueryOperation(Database, index, query, accessor, cts);
				queryOp.Init();
				msg.Content = new StreamQueryContent(InnerRequest, queryOp, accessor, timeout,
					mediaType => msg.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType) {CharSet = "utf-8"});

				msg.Headers.Add("Raven-Result-Etag", queryOp.Header.ResultEtag.ToString());
				msg.Headers.Add("Raven-Index-Etag", queryOp.Header.IndexEtag.ToString());
				msg.Headers.Add("Raven-Is-Stale", queryOp.Header.IsStale ? "true" : "false");
				msg.Headers.Add("Raven-Index", queryOp.Header.Index);
				msg.Headers.Add("Raven-Total-Results", queryOp.Header.TotalResults.ToString(CultureInfo.InvariantCulture));
				msg.Headers.Add(
					"Raven-Index-Timestamp", queryOp.Header.IndexTimestamp.ToString(Default.DateTimeFormatsToWrite, CultureInfo.InvariantCulture));


				if (IsCsvDownloadRequest(InnerRequest))
				{
					msg.Content.Headers.Add("Content-Disposition", "attachment; filename=export.csv");
				}
			}
			catch (Exception)
			{
				accessor.Dispose();
				throw;
			}

			return msg;
		}

		[HttpPost]
		[Route("streams/query/{*id}")]
		[Route("databases/{databaseName}/streams/query/{*id}")]
		public async Task<HttpResponseMessage> SteamQueryPost(string id)
		{
			if ("true".Equals(GetQueryStringValue("postQuery"), StringComparison.InvariantCultureIgnoreCase))
			{
				var postedQuery = await ReadStringAsync();

				SetPostRequestQuery(postedQuery);

				return SteamQueryGet(id);
			}

			return GetMessageWithString("Not idea how to handle a POST on " + id + " without a posted query", HttpStatusCode.BadRequest);
		}

		public class StreamQueryContent : HttpContent
		{
			private readonly HttpRequestMessage req;
			private readonly QueryActions.DatabaseQueryOperation queryOp;
			private readonly IStorageActionsAccessor accessor;
		    private readonly CancellationTimeout _timeout;
		    private readonly Action<string> outputContentTypeSetter;

			public StreamQueryContent(HttpRequestMessage req, QueryActions.DatabaseQueryOperation queryOp, IStorageActionsAccessor accessor, CancellationTimeout timeout, Action<string> contentTypeSetter)
			{
				this.req = req;
				this.queryOp = queryOp;
				this.accessor = accessor;
			    _timeout = timeout;
			    outputContentTypeSetter = contentTypeSetter;
			}

			protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
			{
				using (queryOp)
				using (accessor)
				using(_timeout)
				using (var writer = GetOutputWriter(req, stream))
				{
                    outputContentTypeSetter(writer.ContentType);

                    writer.WriteHeader();
				    try
				    {
				        queryOp.Execute(o =>
				        {
				            _timeout.Delay();
				            writer.Write(o);
				        });
				    }
				    catch (Exception e)
				    {
				        writer.WriteError(e);
				    }
				}

				return Task.FromResult(true);
			}

			protected override bool TryComputeLength(out long length)
			{
				length = -1;
				return false;
			}
		}

		private static IOutputWriter GetOutputWriter(HttpRequestMessage req, Stream stream)
		{
			var useExcelFormat = "excel".Equals(GetQueryStringValue(req, "format"), StringComparison.InvariantCultureIgnoreCase);
			return useExcelFormat ? (IOutputWriter)new ExcelOutputWriter(stream) : new JsonOutputWriter(stream);
		}

        private static Boolean IsCsvDownloadRequest(HttpRequestMessage req)
        {
            return "true".Equals(GetQueryStringValue(req, "download"), StringComparison.InvariantCultureIgnoreCase) 
                && "excel".Equals(GetQueryStringValue(req, "format"), StringComparison.InvariantCultureIgnoreCase);
        }


		public interface IOutputWriter : IDisposable
		{
			string ContentType { get; }

			void WriteHeader();
			void Write(RavenJObject result);
		    void WriteError(Exception exception);
		}

		private class ExcelOutputWriter : IOutputWriter
		{
			private const string CsvContentType = "text/csv";

			private readonly Stream stream;
			private StreamWriter writer;

			public ExcelOutputWriter(Stream stream)
			{
				this.stream = stream;
			}

			public string ContentType
			{
				get { return CsvContentType; }
			}

			public void Dispose()
			{
				if (writer == null)
					return;

				writer.Flush();
				writer.Close();
			}

			public void WriteHeader()
			{
				writer = new StreamWriter(stream, Encoding.UTF8);
			}

			public void Write(RavenJObject result)
			{
				if (properties == null)
				{
					GetPropertiesAndWriteCsvHeader(result);
					Debug.Assert(properties != null);
				}

				foreach (var property in properties)
				{
					var token = result.SelectToken(property);
					if (token != null)
					{
						switch (token.Type)
						{
							case JTokenType.Null:
								break;

							case JTokenType.Array:
							case JTokenType.Object:
								OutputCsvValue(token.ToString(Formatting.None));
								break;

							default:
								OutputCsvValue(token.Value<string>());
								break;
						}
					}

					writer.Write(',');
				}

				writer.WriteLine();
			}

		    public void WriteError(Exception exception)
		    {
		        writer.WriteLine();
                writer.WriteLine();
		        writer.WriteLine(exception.ToString());
		    }

		    private void GetPropertiesAndWriteCsvHeader(RavenJObject result)
			{
				properties = DocumentHelpers.GetPropertiesFromJObject(result,
					parentPropertyPath: "",
					includeNestedProperties: true,
					includeMetadata: false,
					excludeParentPropertyNames: true).ToList();

				foreach (var property in properties)
				{
					OutputCsvValue(property);
					writer.Write(',');
				}
				writer.WriteLine();
			}

			private static readonly char[] RequireQuotesChars = { ',', '\r', '\n', '"' };
			private IEnumerable<string> properties;

			private void OutputCsvValue(string val)
			{
				var needsQuoutes = val.IndexOfAny(RequireQuotesChars) != -1;
				if (needsQuoutes)
					writer.Write('"');

				writer.Write(needsQuoutes ? val.Replace("\"", "\"\"") : val);
				if (needsQuoutes)
					writer.Write('"');
			}

		}

		public class JsonOutputWriter : IOutputWriter
		{
			private const string JsonContentType = "application/json";
			private readonly Stream stream;
			private JsonWriter writer;
		    private bool closedArray = false;

			public JsonOutputWriter(Stream stream)
			{
				this.stream = stream;
			}

			public string ContentType
			{
				get { return JsonContentType; }
			}

			public void WriteHeader()
			{
				writer = new JsonTextWriter(new StreamWriter(stream));
				writer.WriteStartObject();
				writer.WritePropertyName("Results");
				writer.WriteStartArray();
			}

			public void Dispose()
			{
				if (writer == null)
					return;
			    if (closedArray == false)
			        writer.WriteEndArray();
				writer.WriteEndObject();

				writer.Flush();
				writer.Close();
			}

			public void Write(RavenJObject result)
			{
				result.WriteTo(writer, Default.Converters);
                writer.WriteRaw(Environment.NewLine);
			}

		    public void WriteError(Exception exception)
		    {
		        closedArray = true;
		        writer.WriteEndArray();
                writer.WritePropertyName("Error");
                writer.WriteValue(exception.ToString());
		    }
		}
	}
}
Back to Top