コード例 #1
0
		public string AsJson()
		{
			var sb = new StringBuilder();
			var jw = new JsonTextWriter(new StringWriter(sb));
			new JsonSerializer().Serialize(jw, this);
			return sb.ToString();
		}
コード例 #2
0
		public override void Execute(object parameter)
		{
			var saveFile = new SaveFileDialog
						   {
							   DefaultFileName = string.Format("Dump of {0}, {1}", ApplicationModel.Database.Value.Name, DateTimeOffset.Now.ToString("MMM dd yyyy HH-mm", CultureInfo.InvariantCulture)),
							   DefaultExt = ".raven.dump",
							   Filter = "Raven Dumps|*.raven.dump",
						   };

			if (saveFile.ShowDialog() != true)
				return;

			stream = saveFile.OpenFile();
			gZipStream = new GZipStream(stream, CompressionMode.Compress);
			streamWriter = new StreamWriter(gZipStream);
			jsonWriter = new JsonTextWriter(streamWriter)
						 {
							 Formatting = Formatting.Indented
						 };
			taskModel.TaskStatus = TaskStatus.Started;
			output(String.Format("Exporting to {0}", saveFile.SafeFileName));

			output("Begin reading indexes");

			jsonWriter.WriteStartObject();
			jsonWriter.WritePropertyName("Indexes");
			jsonWriter.WriteStartArray();

			ReadIndexes(0)
				.Catch(exception => Infrastructure.Execute.OnTheUI(() => Finish(exception)));
		}
コード例 #3
0
		public static string ToJson(object obj, bool format=false)
		{
			if(format)
			{
				JsonSerializer serializer = new JsonSerializer();
				StringBuilder sb = new StringBuilder();
				using (var writer = new StringWriter(sb))
				{
					using(var jsonWriter = new JsonTextWriter(writer) { Formatting=Formatting.Indented })
					{
						serializer.Serialize(jsonWriter, obj);
						return sb.ToString();
					}
				}
			}
			else 
			{
				JsonSerializer serializer = new JsonSerializer();
				StringBuilder sb = new StringBuilder();
				using(var writer = new StringWriter(sb))
				{
					serializer.Serialize(writer, obj);
					return sb.ToString();
				}
			}
		}
コード例 #4
0
ファイル: DataDumper.cs プロジェクト: robashton/ravendb
		protected override async Task<Etag> ExportAttachments(JsonTextWriter jsonWriter, Etag lastEtag)
		{
			var totalCount = 0;
			while (true)
			{
				var array = GetAttachments(totalCount, lastEtag);
				if (array.Length == 0)
				{
					var databaseStatistics = await GetStats();
					if (lastEtag == null) lastEtag = Etag.Empty;
					if (lastEtag.CompareTo(databaseStatistics.LastAttachmentEtag) < 0)
					{
						lastEtag = EtagUtil.Increment(lastEtag, SmugglerOptions.BatchSize);
						ShowProgress("Got no results but didn't get to the last attachment etag, trying from: {0}", lastEtag);
						continue;
					}
					ShowProgress("Done with reading attachments, total: {0}", totalCount);
					return lastEtag;
				}
				totalCount += array.Length;
				ShowProgress("Reading batch of {0,3} attachments, read so far: {1,10:#,#;;0}", array.Length, totalCount);
				foreach (var item in array)
				{
					item.WriteTo(jsonWriter);
				}
				lastEtag = Etag.Parse(array.Last().Value<string>("Etag"));
			}
		}
コード例 #5
0
        private void StreamToClient(Stream stream, int pageSize, Etag etag)
        {
            using (var cts = new CancellationTokenSource())
			using (var timeout = cts.TimeoutAfter(FileSystemsLandlord.SystemConfiguration.DatabaseOperationTimeout))
			using (var writer = new JsonTextWriter(new StreamWriter(stream)))
			{
			    writer.WriteStartObject();
			    writer.WritePropertyName("Results");
			    writer.WriteStartArray();

                Storage.Batch(accessor =>
                {
                    var files = accessor.GetFilesAfter(etag, pageSize);
                    foreach (var file in files)
                    {
                        timeout.Delay();
                        var doc = RavenJObject.FromObject(file);
                        doc.WriteTo(writer);

                        writer.WriteRaw(Environment.NewLine);
                    }
                });

                writer.WriteEndArray();
                writer.WriteEndObject();
                writer.Flush();
			}
        }
コード例 #6
0
    public void ValueFormatting()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue('@');
        jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
        jsonWriter.WriteValue(true);
        jsonWriter.WriteValue(10);
        jsonWriter.WriteValue(10.99);
        jsonWriter.WriteValue(0.99);
        jsonWriter.WriteValue(0.000000000000000001d);
        jsonWriter.WriteValue(0.000000000000000001m);
        jsonWriter.WriteValue((string)null);
        jsonWriter.WriteValue((object)null);
        jsonWriter.WriteValue("This is a string.");
        jsonWriter.WriteNull();
        jsonWriter.WriteUndefined();
        jsonWriter.WriteEndArray();
      }

      string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,null,null,""This is a string."",null,undefined]";
      string result = sb.ToString();

      Console.WriteLine("ValueFormatting");
      Console.WriteLine(result);

      Assert.AreEqual(expected, result);
    }
コード例 #7
0
ファイル: StorageExporter.cs プロジェクト: GorelH/ravendb
        private void WriteDocuments(JsonTextWriter jsonWriter)
        {
            long totalDococCount = 0;
            long currentDocsCount = 0;
            Etag currLastEtag = Etag.Empty;
            storage.Batch(accsesor => totalDococCount = accsesor.Documents.GetDocumentsCount());
            try
            {
                CancellationToken ct = new CancellationToken();
                do
                {

                    storage.Batch(accsesor =>
                    {
                        var docs = accsesor.Documents.GetDocumentsAfter(currLastEtag, BatchSize, ct);
                        foreach (var doc in docs)
                        {
                            doc.ToJson(true).WriteTo(jsonWriter);
                        }
                        currLastEtag = docs.Last().Etag;
                        currentDocsCount += docs.Count();
                        ReportProgress("documents", currentDocsCount, totalDococCount);
                    });
                } while (totalDococCount > currentDocsCount);
            }
            catch (Exception e)
            {
                ConsoleUtils.PrintErrorAndFail("Failed to export documents, error:" + e.Message);
            }
        }
コード例 #8
0
		protected override Task<Etag> ExportAttachments(RavenConnectionStringOptions src, JsonTextWriter jsonWriter, Etag lastEtag, Etag maxEtag)
		{
			if (maxEtag != null)
				throw new ArgumentException("We don't support maxEtag in SmugglerDatabaseApi", maxEtag);

			return base.ExportAttachments(src, jsonWriter, lastEtag, null);
		}
コード例 #9
0
        private void StreamToClient(Stream stream, int pageSize, Etag etag, OrderedPartCollection<AbstractFileReadTrigger> readTriggers)
        {
            using (var cts = new CancellationTokenSource())
            using (var timeout = cts.TimeoutAfter(FileSystemsLandlord.SystemConfiguration.DatabaseOperationTimeout))
            using (var writer = new JsonTextWriter(new StreamWriter(stream)))
            {
                writer.WriteStartObject();
                writer.WritePropertyName("Results");
                writer.WriteStartArray();

                Storage.Batch(accessor =>
                {
                    var files = accessor.GetFilesAfter(etag, pageSize);
                    foreach (var file in files)
                    {
                        if (readTriggers.CanReadFile(file.FullPath, file.Metadata, ReadOperation.Load) == false)
                            continue;

                        timeout.Delay();
                        var doc = RavenJObject.FromObject(file);
                        doc.WriteTo(writer);

                        writer.WriteRaw(Environment.NewLine);
                    }
                });

                writer.WriteEndArray();
                writer.WriteEndObject();
                writer.Flush();
            }
        }
コード例 #10
0
 public virtual void Serialize(Stream stream, object instance)
 {
     using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
     using (var jsonWriter = new JsonTextWriter(streamWriter))
     {
         RavenJToken.FromObject(instance, this.serializer).WriteTo(jsonWriter);
     }
 }
コード例 #11
0
    public void WriteIConvertable()
    {
      var sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);
      writer.WriteValue(new ConvertibleInt(1));

      Assert.AreEqual("1", sw.ToString());
    }
コード例 #12
0
		public override void Execute(object parameter)
		{
            TaskCheckBox attachmentUI = taskModel.TaskInputs.FirstOrDefault(x => x.Name == "Include Attachments") as TaskCheckBox;
            includeAttachments = attachmentUI != null && attachmentUI.Value;

			var saveFile = new SaveFileDialog
			{
				DefaultExt = ".ravendump",
				Filter = "Raven Dumps|*.ravendump;*.raven.dump",
			};

			var name = ApplicationModel.Database.Value.Name;
			var normalizedName = new string(name.Select(ch => Path.GetInvalidPathChars().Contains(ch) ? '_' : ch).ToArray());
			var defaultFileName = string.Format("Dump of {0}, {1}", normalizedName, DateTimeOffset.Now.ToString("dd MMM yyyy HH-mm", CultureInfo.InvariantCulture));
			try
			{
				saveFile.DefaultFileName = defaultFileName;
			}
			catch { }

			if (saveFile.ShowDialog() != true)
				return;

			taskModel.CanExecute.Value = false;

			stream = saveFile.OpenFile();
			gZipStream = new GZipStream(stream, CompressionMode.Compress);
			streamWriter = new StreamWriter(gZipStream);
			jsonWriter = new JsonTextWriter(streamWriter)
			{
				Formatting = Formatting.Indented
			};
			taskModel.TaskStatus = TaskStatus.Started;

			output(String.Format("Exporting to {0}", saveFile.SafeFileName));
			jsonWriter.WriteStartObject();

		    Action finalized = () => 
            {
                jsonWriter.WriteEndObject();
                Infrastructure.Execute.OnTheUI(() => Finish(null));
		    };

		    Action readAttachments = () => ReadAttachments(Guid.Empty, 0, callback: finalized);
		    Action readDocuments = () => ReadDocuments(Guid.Empty, 0, callback: includeAttachments ? readAttachments : finalized);

            try
            {
                ReadIndexes(0, callback: readDocuments);
            }
            catch (Exception ex)
            {
                taskModel.ReportError(ex);
				Infrastructure.Execute.OnTheUI(() => Finish(ex));
            }
		}
コード例 #13
0
ファイル: DataDumper.cs プロジェクト: randacc/ravendb
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonWriter"></param>
        /// <param name="options"></param>
        /// <param name="result"></param>
        /// <param name="maxEtags">Max etags are inclusive</param>
        protected async override void ExportDeletions(JsonTextWriter jsonWriter, SmugglerOptions options, ExportDataResult result, LastEtagsInfo maxEtags)
        {
            jsonWriter.WritePropertyName("DocsDeletions");
            jsonWriter.WriteStartArray();
            result.LastDocDeleteEtag = await ExportDocumentsDeletion(options, jsonWriter, result.LastDocDeleteEtag, maxEtags.LastDocDeleteEtag.IncrementBy(1));
            jsonWriter.WriteEndArray();

            jsonWriter.WritePropertyName("AttachmentsDeletions");
            jsonWriter.WriteStartArray();
            result.LastAttachmentsDeleteEtag = await ExportAttachmentsDeletion(options, jsonWriter, result.LastAttachmentsDeleteEtag, maxEtags.LastAttachmentsDeleteEtag.IncrementBy(1));
            jsonWriter.WriteEndArray();
        }
コード例 #14
0
		public override async Task ExportDeletions(JsonTextWriter jsonWriter, ExportDataResult result, LastEtagsInfo maxEtagsToFetch)
		{
			jsonWriter.WritePropertyName("DocsDeletions");
			jsonWriter.WriteStartArray();
			result.LastDocDeleteEtag = await Operations.ExportDocumentsDeletion(jsonWriter, result.LastDocDeleteEtag, maxEtagsToFetch.LastDocDeleteEtag.IncrementBy(1));
			jsonWriter.WriteEndArray();

			jsonWriter.WritePropertyName("AttachmentsDeletions");
			jsonWriter.WriteStartArray();
			result.LastAttachmentsDeleteEtag = await Operations.ExportAttachmentsDeletion(jsonWriter, result.LastAttachmentsDeleteEtag, maxEtagsToFetch.LastAttachmentsDeleteEtag.IncrementBy(1));
			jsonWriter.WriteEndArray();
		}
コード例 #15
0
ファイル: SmugglerApi.cs プロジェクト: robashton/ravendb
		protected override async Task<Etag> ExportAttachments(JsonTextWriter jsonWriter, Etag lastEtag)
		{
			int totalCount = 0;
			while (true)
			{
				RavenJArray attachmentInfo = null;

				await commands.CreateRequest("/static/?pageSize=" + SmugglerOptions.BatchSize + "&etag=" + lastEtag, "GET")
				              .ReadResponseJsonAsync()
				              .ContinueWith(task => attachmentInfo = (RavenJArray) task.Result);

				if (attachmentInfo.Length == 0)
				{
					var databaseStatistics = await GetStats();
					var lastEtagComparable = new ComparableByteArray(lastEtag);
					if (lastEtagComparable.CompareTo(databaseStatistics.LastAttachmentEtag) < 0)
					{
						lastEtag = EtagUtil.Increment(lastEtag, SmugglerOptions.BatchSize);
						ShowProgress("Got no results but didn't get to the last attachment etag, trying from: {0}", lastEtag);
						continue;
					}

					ShowProgress("Done with reading attachments, total: {0}", totalCount);
					return lastEtag;
				}

				totalCount += attachmentInfo.Length;
				ShowProgress("Reading batch of {0,3} attachments, read so far: {1,10:#,#;;0}", attachmentInfo.Length, totalCount);
				foreach (var item in attachmentInfo)
				{
					ShowProgress("Downloading attachment: {0}", item.Value<string>("Key"));

					byte[] attachmentData = null;

					await commands.CreateRequest("/static/" + item.Value<string>("Key"), "GET")
					              .ReadResponseBytesAsync()
					              .ContinueWith(task => attachmentData = task.Result);

					new RavenJObject
					{
						{"Data", attachmentData},
						{"Metadata", item.Value<RavenJObject>("Metadata")},
						{"Key", item.Value<string>("Key")}
					}
						.WriteTo(jsonWriter);
				}

				lastEtag = Etag.Parse(attachmentInfo.Last().Value<string>("Etag"));
			}
		}
コード例 #16
0
ファイル: DocsStreams.cs プロジェクト: robashton/ravendb
		public override void Respond(IHttpContext context)
		{
			using (context.Response.Streaming())
			{
				context.Response.ContentType = "application/json; charset=utf-8";

				using (var writer = new JsonTextWriter(new StreamWriter(context.Response.OutputStream)))
				{
					writer.WriteStartObject();
					writer.WritePropertyName("Results");
					writer.WriteStartArray();

					Database.TransactionalStorage.Batch(accessor =>
					{
						var startsWith = context.Request.QueryString["startsWith"];
						int pageSize = context.GetPageSize(int.MaxValue);
						if (string.IsNullOrEmpty(context.Request.QueryString["pageSize"]))
							pageSize = int.MaxValue;

						// 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.GetDocuments(context.GetStart(), pageSize, context.GetEtagFromQueryString(),
								                      doc => doc.WriteTo(writer));
							}
							else
							{
								Database.GetDocumentsWithIdStartingWith(
									startsWith,
									context.Request.QueryString["matches"],
                                    context.Request.QueryString["exclude"],
									context.GetStart(),
									pageSize,
									doc => doc.WriteTo(writer));
							}
						}
					});

					writer.WriteEndArray();
					writer.WriteEndObject();
					writer.Flush();
				}
			}
		}
コード例 #17
0
ファイル: StreamsController.cs プロジェクト: GorelH/ravendb
        private void StreamToClient(Stream stream, string startsWith, int start, int pageSize, Etag etag, string matches, int nextPageStart, string skipAfter)
        {
            var bufferStream = new BufferedStream(stream, 1024 * 64);
            using (var cts = new CancellationTokenSource())
            using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout))
            using (var writer = new JsonTextWriter(new StreamWriter(bufferStream)))
            {
                writer.WriteStartObject();
                writer.WritePropertyName("Results");
                writer.WriteStartArray();

                Action<JsonDocument> addDocument = doc =>
                {
                    timeout.Delay();
                    doc.ToJson().WriteTo(writer);
                    writer.WriteRaw(Environment.NewLine);
                };

                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, addDocument);
                        }
                        else
                        {
                            var nextPageStartInternal = nextPageStart;

                            Database.Documents.GetDocumentsWithIdStartingWith(startsWith, matches, null, start, pageSize, cts.Token, ref nextPageStartInternal, addDocument, skipAfter: skipAfter);

                            nextPageStart = nextPageStartInternal;
                        }
                    }
                });

                writer.WriteEndArray();
                writer.WritePropertyName("NextPageStart");
                writer.WriteValue(nextPageStart);
                writer.WriteEndObject();
                writer.Flush();
                bufferStream.Flush();
            }
        }
コード例 #18
0
    public void CloseOutput()
    {
      MemoryStream ms = new MemoryStream();
      JsonTextWriter writer = new JsonTextWriter(new StreamWriter(ms));

      Assert.IsTrue(ms.CanRead);
      writer.Close();
      Assert.IsFalse(ms.CanRead);

      ms = new MemoryStream();
      writer = new JsonTextWriter(new StreamWriter(ms)) { CloseOutput = false };

      Assert.IsTrue(ms.CanRead);
      writer.Close();
      Assert.IsTrue(ms.CanRead);
    }
コード例 #19
0
 public Task<Etag> ExportDocumentsDeletion(JsonTextWriter jsonWriter, Etag startDocsEtag, Etag maxEtag)
 {
     var lastEtag = startDocsEtag;
     database.TransactionalStorage.Batch(accessor =>
     {
         foreach (var listItem in accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, startDocsEtag, maxEtag, int.MaxValue))
         {
             var o = new RavenJObject
             {
                 {"Key", listItem.Key}
             };
             o.WriteTo(jsonWriter);
             lastEtag = listItem.Etag;
         }
     });
     return new CompletedTask<Etag>(lastEtag);
 }
コード例 #20
0
ファイル: QueryStreams.cs プロジェクト: samueldjack/ravendb
		public override void Respond(IHttpContext context)
		{
			context.Response.BufferOutput = false;
			
			var match = urlMatcher.Match(context.GetRequestUrl());
			var index = match.Groups[1].Value;

			var query = context.GetIndexQueryFromHttpContext(int.MaxValue);
			if (string.IsNullOrEmpty(context.Request.QueryString["pageSize"]))
				query.PageSize = int.MaxValue;
			var isHeadRequest = context.Request.HttpMethod == "HEAD";
			if (isHeadRequest)
				query.PageSize = 0;
			JsonWriter writer = null;
			Database.Query(index, query, information =>
			{
				context.Response.AddHeader("Raven-Result-Etag", information.ResultEtag.ToString());
				context.Response.AddHeader("Raven-Index-Etag", information.IndexEtag.ToString());
				context.Response.AddHeader("Raven-Is-Stale", information.IsStable ? "true" : "false");
				context.Response.AddHeader("Raven-Index", information.Index);
				context.Response.AddHeader("Raven-Total-Results", information.TotalResults.ToString(CultureInfo.InvariantCulture));
				context.Response.AddHeader("Raven-Index-Timestamp",
				                           information.IndexTimestamp.ToString(Default.DateTimeFormatsToWrite,
				                                                               CultureInfo.InvariantCulture));

				if (isHeadRequest)
					return;

				writer = new JsonTextWriter(new StreamWriter(context.Response.OutputStream));
				writer.WriteStartObject();
				writer.WritePropertyName("Results");
				writer.WriteStartArray();
			}, result => result.WriteTo(writer, Default.Converters));

			if (isHeadRequest)
				return;

			writer.WriteEndArray();
			writer.WriteEndObject();
			if (writer != null)
			{
				writer.Flush();
				writer.Close();
			}

		}
コード例 #21
0
		public override void Execute(object parameter)
		{
			var saveFile = new SaveFileDialog
			{
				DefaultExt = ".ravendump",
				Filter = "Raven Dumps|*.ravendump;*.raven.dump",
			};

			var name = ApplicationModel.Database.Value.Name;
			var normalizedName = new string(name.Select(ch => Path.GetInvalidPathChars().Contains(ch) ? '_' : ch).ToArray());
			var defaultFileName = string.Format("Dump of {0}, {1}", normalizedName, DateTimeOffset.Now.ToString("dd MMM yyyy HH-mm", CultureInfo.InvariantCulture));
			try
			{
				saveFile.DefaultFileName = defaultFileName;
			}
			catch { }

			if (saveFile.ShowDialog() != true)
				return;

			taskModel.CanExecute.Value = false;

			stream = saveFile.OpenFile();
			gZipStream = new GZipStream(stream, CompressionMode.Compress);
			streamWriter = new StreamWriter(gZipStream);
			jsonWriter = new JsonTextWriter(streamWriter)
			{
				Formatting = Formatting.Indented
			};
			taskModel.TaskStatus = TaskStatus.Started;
			output(String.Format("Exporting to {0}", saveFile.SafeFileName));

			output("Begin reading indexes");

			jsonWriter.WriteStartObject();
			jsonWriter.WritePropertyName("Indexes");
			jsonWriter.WriteStartArray();

			ReadIndexes(0)
				.Catch(exception =>
				{
					taskModel.ReportError(exception);
					Infrastructure.Execute.OnTheUI(() => Finish(exception));
				});
		}
コード例 #22
0
ファイル: JsonNetResult.cs プロジェクト: gan123/Right-Recruit
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            var response = context.HttpContext.Response;
            response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;

            if (Data == null) return;
            var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };

            var serializer = JsonSerializer.Create(SerializerSettings);
            serializer.Serialize(writer, Data);
            writer.Flush();
        }
コード例 #23
0
        public void Export(string file)
        {
            using (var streamWriter =
                new StreamWriter(new GZipStream(File.Create(file), CompressionMode.Compress)))
            {
                var jsonWriter = new JsonTextWriter(streamWriter)
                    {
                        Formatting = Formatting.Indented
                    };

                jsonWriter.WriteStartObject();
                WriteItemsFromDb(jsonWriter, "Indexes", start => _documentDatabase.GetIndexes(start, 128));
                WriteItemsFromDb(jsonWriter, "Docs", start => _documentDatabase.GetDocuments(start, 128, null));
                WriteItemsFromDb(jsonWriter, "Attachments", GetAttachments);

                jsonWriter.WriteEndObject();
            }
        }
コード例 #24
0
ファイル: StorageExporter.cs プロジェクト: IdanHaim/ravendb
 public void ExportDatabase()
 {
    
     using (var stream = File.Create(outputDirectory))
     using (var gZipStream = new GZipStream(stream, CompressionMode.Compress,leaveOpen: true))
     using (var streamWriter = new StreamWriter(gZipStream))
     {
         var jsonWriter = new JsonTextWriter(streamWriter)
         {
             Formatting = Formatting.Indented
         };
         jsonWriter.WriteStartObject();
         //Indexes
         jsonWriter.WritePropertyName("Indexes");
         jsonWriter.WriteStartArray();
         WriteIndexes(jsonWriter);
         jsonWriter.WriteEndArray();
         //Documents
         jsonWriter.WritePropertyName("Docs");
         jsonWriter.WriteStartArray();
         WriteDocuments(jsonWriter);
         jsonWriter.WriteEndArray();
         //Attachments
         jsonWriter.WritePropertyName("Attachments");
         jsonWriter.WriteStartArray();
         WriteAttachments(jsonWriter);
         jsonWriter.WriteEndArray();
         //Transformers
         jsonWriter.WritePropertyName("Transformers");
         jsonWriter.WriteStartArray();
         WriteTransformers(jsonWriter);
         jsonWriter.WriteEndArray();
         //Identities
         jsonWriter.WritePropertyName("Identities");
         jsonWriter.WriteStartArray();
         WriteIdentities(jsonWriter);
         jsonWriter.WriteEndArray();
         //end of export
         jsonWriter.WriteEndObject();
         streamWriter.Flush();
     }
 }
コード例 #25
0
ファイル: ToDoRepository.cs プロジェクト: 925coder/ravendb
		public Guid Save(ToDo todo)
		{
			database.BeginTransaction();

			var id = Guid.NewGuid();

			var ms = new MemoryStream();
			var jsonTextWriter = new JsonTextWriter(new StreamWriter(ms));
			new JsonSerializer().Serialize(
				jsonTextWriter,
				todo
				);
			jsonTextWriter.Flush();

			todos.Put(id.ToByteArray(), ms.ToArray());

			database.Commit();

			return id;
		}
コード例 #26
0
ファイル: DataDumper.cs プロジェクト: arelee/ravendb
		protected override Guid ExportAttachments(JsonTextWriter jsonWriter, Guid lastEtag)
		{
			var totalCount = 0;
			while (true)
			{
				var array = GetAttachments(totalCount, lastEtag);
				if (array.Length == 0)
				{
					ShowProgress("Done with reading attachments, total: {0}", totalCount);
					return lastEtag;
				}
				totalCount += array.Length;
				ShowProgress("Reading batch of {0,3} attachments, read so far: {1,10:#,#;;0}", array.Length, totalCount);
				foreach (var item in array)
				{
					item.WriteTo(jsonWriter);
				}
				lastEtag = new Guid(array.Last().Value<string>("Etag"));
			}
		}
コード例 #27
0
        public override void ExecuteResult(ControllerContext context)
        {
            if(context == null) {
                throw new ArgumentNullException("context");
            }

            var response = context.HttpContext.Response;
            response.ContentType = "application/json";

            if(data == null) {
                return;
            }

            var writer = new JsonTextWriter(response.Output);

            var serializer = new JsonSerializer();
            serializer.Configure();
            serializer.Serialize(writer, data);
            writer.Flush();
        }
コード例 #28
0
ファイル: RavenDB_4189.cs プロジェクト: IdanHaim/ravendb
        public async Task Non_existing_index_storage_folder_should_throw_proper_exception()
        {
            using (var store = NewStore(requestedStorage: "voron"))
            {
                using (var session = store.OpenAsyncSession())
                {
                    session.RegisterUpload("test.file", new MemoryStream(new byte[] { 1, 2, 3 }));
                    await session.SaveChangesAsync();
                }

                await store.AsyncFilesCommands.Admin.StartBackup(backupLocation,
                    null, false, store.DefaultFileSystem);
                WaitForBackup(store.AsyncFilesCommands, true);

                FileSystemDocument document;
                using (var file = File.OpenText(Path.Combine(backupLocation, Constants.FilesystemDocumentFilename)))
                using (var reader = new JsonTextReader(file))
                    document = (new JsonSerializer()).Deserialize<FileSystemDocument>(reader);

                var drives = DriveInfo.GetDrives().Select(x => x.Name.ToLower()[0]).ToArray();
                var lastDriveLetter = 'a';
                while (lastDriveLetter != 'z')
                {
                    if (drives.Contains(lastDriveLetter) == false)
                        break;
                    lastDriveLetter++;
                }

                document.Settings[Constants.FileSystem.IndexStorageDirectory] = string.Format("{0}:\\", lastDriveLetter); //on purpose, non existing path

                using (var file = File.CreateText(Path.Combine(backupLocation, Constants.FilesystemDocumentFilename)))
                using (var writer = new JsonTextWriter(file))
                    (new JsonSerializer()).Serialize(writer, document);
                
                Assert.Throws<BadRequestException>(() => 
                    AsyncHelpers.RunSync(() => store.AsyncFilesCommands.Admin.StartRestore(new FilesystemRestoreRequest
                    {
                        BackupLocation = backupLocation
                    })));
            }
        }
コード例 #29
0
ファイル: StorageExporter.cs プロジェクト: j2jensen/ravendb
 private void WriteDocuments(JsonTextWriter jsonWriter)
 {
     long totalDocsCount = 0;
     long currentDocsCount = 0;
     long previesDocsCount = 0;
     Etag currLastEtag = DocumentsStartEtag;
     if (DocumentsStartEtag != Etag.Empty)
     {
         ConsoleUtils.ConsoleWriteLineWithColor(ConsoleColor.Yellow, "Starting to export documents as of etag={0}\n" +
         "TotalDocCount doesn't substract skipped items\n", DocumentsStartEtag);
         currLastEtag.DecrementBy(1);
     }
     storage.Batch(accsesor => totalDocsCount = accsesor.Documents.GetDocumentsCount());
     try
     {
         CancellationToken ct = new CancellationToken();
         do
         {
             previesDocsCount = currentDocsCount;
             storage.Batch(accsesor =>
             {
                 var docs = accsesor.Documents.GetDocumentsAfter(currLastEtag, BatchSize, ct);
                 foreach (var doc in docs)
                 {
                     doc.ToJson(true).WriteTo(jsonWriter);
                 }
                 var last = docs.LastOrDefault();
                 if (last != null)
                 {
                     currLastEtag = last.Etag;
                     currentDocsCount += docs.Count();
                     ReportProgress("documents", currentDocsCount, totalDocsCount);
                 }
             });
         } while (currentDocsCount > previesDocsCount);
     }
     catch (Exception e)
     {
         ConsoleUtils.PrintErrorAndFail("Failed to export documents, error:" + e.Message);
     }
 }
コード例 #30
0
        private void StreamToClient(Stream stream, ExportOptions options, Lazy<NameValueCollection> headers, IPrincipal user)
        {
            var old = CurrentOperationContext.Headers.Value;
            var oldUser = CurrentOperationContext.User.Value;
            try
            {
                CurrentOperationContext.Headers.Value = headers;
                CurrentOperationContext.User.Value = user;

                Database.TransactionalStorage.Batch(accessor =>
                {
                    var bufferStream = new BufferedStream(stream, 1024 * 64);

                    using (var cts = new CancellationTokenSource())
                    using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout))
                    using (var streamWriter = new StreamWriter(bufferStream))
                    using (var writer = new JsonTextWriter(streamWriter))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Results");
                        writer.WriteStartArray();

                        var exporter = new SmugglerExporter(Database, options);

                        exporter.Export(item => WriteToStream(writer, item, timeout), cts.Token);

                        writer.WriteEndArray();
                        writer.WriteEndObject();
                        writer.Flush();
                        bufferStream.Flush();
                    }
                });
            }
            finally
            {
                CurrentOperationContext.Headers.Value = old;
                CurrentOperationContext.User.Value = oldUser;
            }
        }