public IDisposable Initialize(DatabaseSmugglerOptionsServerSide options, SmugglerResult result, long buildVersion) { _gzipStream = new GZipStream(_stream, CompressionMode.Compress, leaveOpen: true); _writer = new BlittableJsonTextWriter(_context, _gzipStream); _options = options; SetupMetadataFilterMethod(_context); _writer.WriteStartObject(); _writer.WritePropertyName("BuildVersion"); _writer.WriteInteger(buildVersion); return new DisposableAction(() => { _writer.WriteEndObject(); _writer.Dispose(); _gzipStream.Dispose(); }); }
public async Task PostIndexType() { using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { using (var json = await context.ReadForMemoryAsync(RequestBodyStream(), "map")) { var indexDefinition = JsonDeserializationServer.IndexDefinition(json); var indexType = indexDefinition.DetectStaticIndexType(); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("IndexType"); writer.WriteString(indexType.ToString()); writer.WriteEndObject();; } } } }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { url = $"{node.Url}/studio-tasks/format"; return(new HttpRequestMessage { Method = HttpMethod.Post, Content = new BlittableJsonContent(stream => { using (var writer = new BlittableJsonTextWriter(_context, stream)) { writer.WriteStartObject(); writer.WritePropertyName(nameof(FormatOperation.Result.Expression)); writer.WriteString(_expression); writer.WriteEndObject(); } }) }); }
public async Task SetupAgreement() { AssertOnlyInSetupMode(); var email = GetQueryStringValueAndAssertIfSingleAndNotEmpty("email"); using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context)) { var baseUri = new Uri("https://letsencrypt.org/"); var uri = new Uri(baseUri, await SetupManager.LetsEncryptAgreement(email, ServerStore)); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Uri"); writer.WriteString(uri.AbsoluteUri); writer.WriteEndObject(); } } }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { url = $"{node.Url}/databases/{node.Database}" + (_allJavaScriptIndexes? "/indexes":"/admin/indexes"); var request = new HttpRequestMessage { Method = HttpMethod.Put, Content = new BlittableJsonContent(stream => { using (var writer = new BlittableJsonTextWriter(ctx, stream)) { writer.WriteStartObject(); writer.WriteArray("Indexes", _indexToAdd); writer.WriteEndObject(); } }) }; return(request); }
public Task GetDocumentsAttachmentMetadataWithCounts() { var id = GetStringQueryString("id", false); using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) using (context.OpenReadTransaction()) { var array = Database.DocumentsStorage.AttachmentsStorage.GetAttachmentsMetadataForDocumentWithCounts(context, id.ToLowerInvariant()); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Id"); writer.WriteString(id); writer.WriteComma(); writer.WriteArray("Attachments", array, context); writer.WriteEndObject(); } } return(Task.CompletedTask); }
private void WriteSchemaResponse(DocumentsOperationContext context, SqlSchemaResultTable[] Tables = null, string Error = null) { using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); WriteTablesArray(nameof(Tables), Tables, writer); writer.WriteComma(); writer.WritePropertyName(nameof(Error)); writer.WriteString(Error); writer.WriteComma(); var Success = Error == null; writer.WritePropertyName(nameof(Success)); writer.WriteBool(Success); writer.WriteEndObject(); } }
public Task GetNextOperationId() { var nextId = Database.Operations.GetNextOperationId(); using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Id"); writer.WriteInteger(nextId); writer.WriteComma(); writer.WritePropertyName(nameof(GetNextOperationIdCommand.NodeTag)); writer.WriteString(Server.ServerStore.NodeTag); writer.WriteEndObject(); } } return(Task.CompletedTask); }
public static void TrySavingTopologyToLocalCache(string serverHash, Topology topology, JsonOperationContext context) { try { var path = GetTopologyPath(serverHash); if (topology == null) { ClearTopologyFromLocalCache(serverHash); return; } using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) using (var writer = new BlittableJsonTextWriter(context, stream)) { writer.WriteStartObject(); writer.WritePropertyName(context.GetLazyString(nameof(Topology.Nodes))); writer.WriteStartArray(); for (var index = 0; index < topology.Nodes.Count; index++) { var node = topology.Nodes[index]; if (index != 0) { writer.WriteComma(); } WriteNode(writer, node, context); } writer.WriteEndArray(); writer.WriteComma(); writer.WriteEndObject(); writer.WriteEndObject(); } } catch (Exception e) { if (_logger.IsInfoEnabled) { _logger.Info("Could not persist the replication information", e); } } }
public Task FullDataDirectory() { var path = GetStringQueryString("path", required: false); var name = GetStringQueryString("name", required: false); var baseDataDirectory = ServerStore.Configuration.Core.DataDirectory.FullPath; // 1. Used as default when both Name & Path are Not defined var result = baseDataDirectory; // 2. Path defined, Path overrides any given Name if (string.IsNullOrEmpty(path) == false) { result = PathUtil.ToFullPath(path, baseDataDirectory); } // 3. Name defined, No path else if (string.IsNullOrEmpty(name) == false) { // 'Databases' prefix is added... result = RavenConfiguration.GetDataDirectoryPath(ServerStore.Configuration.Core, name, ResourceType.Database); } if (ServerStore.Configuration.Core.EnforceDataDirectoryPath) { if (PathUtil.IsSubDirectory(result, ServerStore.Configuration.Core.DataDirectory.FullPath) == false) { result = $"The administrator has restricted databases to be created only under the {RavenConfiguration.GetKey(x => x.Core.DataDirectory)} directory: '{ServerStore.Configuration.Core.DataDirectory.FullPath}'."; } } using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context)) using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("FullPath"); writer.WriteString(result); writer.WriteEndObject(); } return(Task.CompletedTask); }
private Task UnsafeRequestHandler(HttpContext context) { if (RoutesAllowedInUnsafeMode.Contains(context.Request.Path.Value)) { return(RequestHandler(context)); } context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; if (IsHtmlAcceptable(context)) { context.Response.Headers["Content-Type"] = "text/html; charset=utf-8"; return(context.Response.WriteAsync(HtmlUtil.RenderUnsafePage())); } context.Response.Headers["Content-Type"] = "application/json; charset=utf-8"; using (_server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext ctx)) using (var writer = new BlittableJsonTextWriter(ctx, context.Response.Body)) { writer.WriteStartObject(); writer.WritePropertyName("Message"); writer.WriteString(string.Join(" ", UnsafeWarning)); writer.WriteComma(); writer.WritePropertyName("MessageAsArray"); writer.WriteStartArray(); var first = true; foreach (var val in UnsafeWarning) { if (first == false) { writer.WriteComma(); } first = false; writer.WriteString(val); } writer.WriteEndArray(); writer.WriteEndObject(); } return(Task.CompletedTask); }
public Task GetRevisionsFor() { var versioningStorage = Database.BundleLoader.VersioningStorage; if (versioningStorage == null) { throw new InvalidOperationException("Versioning is disabled"); } var key = GetQueryStringValueAndAssertIfSingleAndNotEmpty("key"); DocumentsOperationContext context; using (ContextPool.AllocateOperationContext(out context)) using (context.OpenReadTransaction()) { int start = GetIntValueQueryString("start", false) ?? 0; int take = GetIntValueQueryString("pageSize", false) ?? 25; var revisions = versioningStorage.GetRevisions(context, key, start, take).ToList(); long actualEtag = revisions.Count == 0 ? int.MinValue : revisions[revisions.Count - 1].Etag; if (GetLongFromHeaders("If-None-Match") == actualEtag) { HttpContext.Response.StatusCode = 304; return(Task.CompletedTask); } HttpContext.Response.Headers["Content-Type"] = "application/json; charset=utf-8"; HttpContext.Response.Headers["ETag"] = actualEtag.ToString(); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Results"); writer.WriteDocuments(context, revisions, false); writer.WriteEndObject(); } } return(Task.CompletedTask); }
public static void WritePerformanceStats(this BlittableJsonTextWriter writer, JsonOperationContext context, IEnumerable <IndexPerformanceStats> stats) { writer.WriteStartObject(); writer.WriteArray(context, "Results", stats, (w, c, stat) => { w.WriteStartObject(); w.WritePropertyName(nameof(stat.Name)); w.WriteString(stat.Name); w.WriteComma(); w.WritePropertyName(nameof(stat.Etag)); w.WriteInteger(stat.Etag); w.WriteComma(); w.WriteArray(c, nameof(stat.Performance), stat.Performance, (wp, cp, performance) => { wp.WriteIndexingPerformanceStats(context, performance); }); w.WriteEndObject(); }); writer.WriteEndObject(); }
private void WriteHeaders(BlittableJsonTextWriter writer, HttpContext httpContext, LazyStringValue headersProperty) { writer.WritePropertyName(headersProperty); writer.WriteStartObject(); bool headerStart = true; foreach (var header in httpContext.Response.Headers) { foreach (var value in header.Value) { if (headerStart == false) { writer.WriteComma(); } headerStart = false; writer.WritePropertyName(header.Key); writer.WriteString(value); } } writer.WriteEndObject(); }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { url = $"{node.Url}/admin/certificates/replace-cluster-cert?replaceImmediately={_replaceImmediately}"; var request = new HttpRequestMessage { Method = HttpMethod.Post, Content = new BlittableJsonContent(stream => { using (var writer = new BlittableJsonTextWriter(ctx, stream)) { writer.WriteStartObject(); writer.WritePropertyName(nameof(CertificateDefinition.Certificate)); writer.WriteString(Convert.ToBase64String(_certBytes)); // keep the private key -> this is a server cert writer.WriteEndObject(); } }) }; return(request); }
public async Task Put() { using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { var id = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id"); var doc = context.ReadForDiskAsync(RequestBodyStream(), id).ConfigureAwait(false); if (id[id.Length - 1] == '|') { var(_, clusterId) = await ServerStore.GenerateClusterIdentityAsync(id, Database.Name); id = clusterId; } var changeVector = context.GetLazyString(GetStringQueryString("If-Match", false)); var cmd = new MergedPutCommand(await doc, id, changeVector, Database); await Database.TxMerger.Enqueue(cmd); cmd.ExceptionDispatchInfo?.Throw(); HttpContext.Response.StatusCode = (int)HttpStatusCode.Created; using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName(nameof(PutResult.Id)); writer.WriteString(cmd.PutResult.Id); writer.WriteComma(); writer.WritePropertyName(nameof(PutResult.ChangeVector)); writer.WriteString(cmd.PutResult.ChangeVector); writer.WriteEndObject(); } } }
public Task Resources() { //TODO: fill all required information (see: RavenDB-5438) - return Raven.Client.Data.ResourcesInfo TransactionOperationContext context; using (ServerStore.ContextPool.AllocateOperationContext(out context)) { context.OpenReadTransaction(); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName(nameof(ResourcesInfo.Databases)); writer.WriteStartArray(); var first = true; foreach (var dbDoc in ServerStore.StartingWith(context, Constants.Database.Prefix, GetStart(), GetPageSize(int.MaxValue))) { if (first == false) { writer.WriteComma(); } first = false; var databaseName = dbDoc.Key.Substring(Constants.Database.Prefix.Length); WriteDatabaseInfo(databaseName, dbDoc.Data, context, writer); } writer.WriteEndArray(); //TODO: write fs, cs, ts writer.WriteEndObject(); } } return(Task.CompletedTask); }
private void WriteErrorsArray(string name, SqlMigrationImportResult.Error[] errors, BlittableJsonTextWriter writer) { writer.WritePropertyName(name); writer.WriteStartArray(); var first = true; foreach (var error in errors) { if (first) { first = false; } else { writer.WriteComma(); } writer.WriteStartObject(); writer.WritePropertyName(nameof(error.Type)); writer.WriteString(error.Type.ToString()); writer.WriteComma(); writer.WritePropertyName(nameof(error.Message)); writer.WriteString(error.Message); writer.WriteComma(); writer.WritePropertyName(nameof(error.TableName)); writer.WriteString(error.TableName); writer.WriteComma(); writer.WritePropertyName(nameof(error.ColumnName)); writer.WriteString(error.ColumnName); writer.WriteEndObject(); } writer.WriteEndArray(); }
public Task Get() { using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) { Dictionary <string, SorterDefinition> sorters; using (context.OpenReadTransaction()) { var rawRecord = Server.ServerStore.Cluster.ReadRawDatabaseRecord(context, Database.Name); sorters = rawRecord?.GetSorters(); } if (sorters == null) { sorters = new Dictionary <string, SorterDefinition>(); } using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WriteArray(context, "Sorters", sorters.Values, (w, c, sorter) => { w.WriteStartObject(); w.WritePropertyName(nameof(SorterDefinition.Name)); w.WriteString(sorter.Name); w.WriteComma(); w.WritePropertyName(nameof(SorterDefinition.Code)); w.WriteString(sorter.Code); w.WriteEndObject(); }); writer.WriteEndObject(); } } return(Task.CompletedTask); }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { var path = new StringBuilder(node.Url) .Append("/databases/") .Append(node.Database) .Append("/queries") .Append("?allowStale=") .Append(_options.AllowStale) .Append("&maxOpsPerSec=") .Append(_options.MaxOpsPerSecond) .Append("&details=") .Append(_options.RetrieveDetails); if (_options.StaleTimeout != null) { path .Append("&staleTimeout=") .Append(_options.StaleTimeout.Value); } var request = new HttpRequestMessage { Method = HttpMethods.Patch, Content = new BlittableJsonContent(stream => { using (var writer = new BlittableJsonTextWriter(ctx, stream)) { writer.WriteStartObject(); writer.WritePropertyName("Query"); writer.WriteIndexQuery(_conventions, ctx, _queryToUpdate); writer.WriteEndObject(); } } ) }; url = path.ToString(); return(request); }
private static void WriteNode(BlittableJsonTextWriter writer, ServerNode node, JsonOperationContext context) { writer.WriteStartObject(); writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Url))); writer.WriteString(context.GetLazyString(node.Url)); writer.WriteComma(); writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Database))); writer.WriteString(context.GetLazyString(node.Database)); // ClusterTag and ServerRole included for debugging purpose only writer.WriteComma(); writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.ClusterTag))); writer.WriteString(context.GetLazyString(node.ClusterTag)); writer.WriteComma(); writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.ServerRole))); writer.WriteString(context.GetLazyString(node.ServerRole.ToString())); writer.WriteEndObject(); }
public Task Exists() { var hash = GetStringQueryString("hash"); using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) using (context.OpenReadTransaction()) using (Slice.From(context.Allocator, hash, out var hashSlice)) { var count = AttachmentsStorage.GetCountOfAttachmentsForHash(context, hashSlice); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Hash"); writer.WriteString(hash); writer.WriteComma(); writer.WritePropertyName("Count"); writer.WriteInteger(count); writer.WriteEndObject(); } } return(Task.CompletedTask); }
private static void WriteAttachmentDetails(BlittableJsonTextWriter writer, Attachment attachment, string documentId) { writer.WriteStartObject(); writer.WritePropertyName(nameof(AttachmentDetails.Name)); writer.WriteString(attachment.Name); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Hash)); writer.WriteString(attachment.Base64Hash.ToString()); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.ContentType)); writer.WriteString(attachment.ContentType); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.Size)); writer.WriteInteger(attachment.Size); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.ChangeVector)); writer.WriteString(attachment.ChangeVector); writer.WriteComma(); writer.WritePropertyName(nameof(AttachmentDetails.DocumentId)); writer.WriteString(documentId); writer.WriteEndObject(); }
public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) { var request = new HttpRequestMessage { Method = HttpMethod.Post, Content = new BlittableJsonContent(stream => { using (var writer = new BlittableJsonTextWriter(ctx, stream)) { writer.WriteStartObject(); writer.WriteArray("Commands", _commands); writer.WriteEndObject(); } }) }; if (_attachmentStreams != null && _attachmentStreams.Count > 0) { var multipartContent = new MultipartContent { request.Content }; foreach (var stream in _attachmentStreams) { PutAttachmentCommandHelper.PrepareStream(stream); var streamContent = new AttachmentStreamContent(stream, CancellationToken); streamContent.Headers.TryAddWithoutValidation("Command-Type", "AttachmentStream"); multipartContent.Add(streamContent); } request.Content = multipartContent; } var sb = new StringBuilder($"{node.Url}/databases/{node.Database}/bulk_docs"); AppendOptions(sb); url = sb.ToString(); return(request); }
public static void TrySavingTopologyToLocalCache(string serverHash, Topology topology, JsonOperationContext context) { try { var path = GetTopologyPath(serverHash); if (topology == null) { ClearTopologyFromLocalCache(serverHash); return; } using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) using (var writer = new BlittableJsonTextWriter(context, stream)) { writer.WriteStartObject(); writer.WritePropertyName(context.GetLazyString(nameof(Topology.LeaderNode))); WriteNode(writer, topology.LeaderNode, context); writer.WritePropertyName(context.GetLazyString(nameof(Topology.Nodes))); writer.WriteStartArray(); foreach (var node in topology.Nodes) { WriteNode(writer, node, context); } writer.WriteEndArray(); writer.WritePropertyName(context.GetLazyString(nameof(Topology.ReadBehavior))); writer.WriteString(context.GetLazyString(topology.ReadBehavior.ToString())); writer.WritePropertyName(context.GetLazyString(nameof(Topology.WriteBehavior))); writer.WriteString(context.GetLazyString(topology.WriteBehavior.ToString())); writer.WriteEndObject(); } } catch (Exception e) { Log.ErrorException("Could not persist the replication information", e); } }
public Task ListHubAccess() { var hub = GetStringQueryString("name", true); var filter = GetStringQueryString("filter", false); int pageSize = GetPageSize(); var start = GetStart(); using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) using (context.OpenReadTransaction()) { var results = Server.ServerStore.Cluster.GetReplicationHubCertificateByHub(context, Database.Name, hub, filter, start, pageSize); using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WriteArray(nameof(ReplicationHubAccessResult.Results), results); writer.WriteEndObject(); } return(Task.CompletedTask); } }
public async Task NextIdentityFor() { var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name"); if (name[name.Length - 1] != '|') { name += '|'; } var(_, _, newIdentityValue) = await Database.ServerStore.GenerateClusterIdentityAsync(name, Database.Name, GetRaftRequestIdFromQuery()); using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("NewIdentityValue"); writer.WriteInteger(newIdentityValue); writer.WriteEndObject(); } }
public void Persist(JsonOperationContext context, BlittableJsonTextWriter writer) { writer.WriteStartObject(); writer.WritePropertyName(nameof(Name)); writer.WriteString(Name); writer.WriteComma(); writer.WritePropertyName(nameof(Collections)); writer.WriteStartArray(); var isFirst = true; foreach (var collection in Collections) { if (isFirst == false) { writer.WriteComma(); } isFirst = false; writer.WriteString((collection)); } writer.WriteEndArray(); writer.WriteComma(); writer.WritePropertyName(nameof(LockMode)); writer.WriteInteger((int)LockMode); writer.WriteComma(); writer.WritePropertyName(nameof(Priority)); writer.WriteInteger((int)Priority); writer.WriteComma(); PersistFields(context, writer); writer.WriteEndObject(); }
public Task GetAll() { var id = GetLongQueryString("id", required: false); using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) { IEnumerable <Documents.Operations.Operations.Operation> operations; if (id.HasValue == false) { operations = Database.Operations.GetAll(); } else { var operation = Database.Operations.GetOperation(id.Value); if (operation == null) { HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound; return(Task.CompletedTask); } operations = new List <Documents.Operations.Operations.Operation> { operation }; } using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WriteArray(context, "Results", operations, (w, c, operation) => { c.Write(w, operation.ToJson()); }); writer.WriteEndObject(); } } return(Task.CompletedTask); }
public Task StreamDocsGet() { var transformerName = GetStringQueryString("transformer", required: false); var start = GetStart(); var pageSize = GetPageSize(); using (Database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) using (context.OpenReadTransaction()) { IEnumerable <Document> documents; if (HttpContext.Request.Query.ContainsKey("startsWith")) { documents = Database.DocumentsStorage.GetDocumentsStartingWith(context, HttpContext.Request.Query["startsWith"], HttpContext.Request.Query["matches"], HttpContext.Request.Query["excludes"], HttpContext.Request.Query["startAfter"], start, pageSize); } else // recent docs { documents = Database.DocumentsStorage.GetDocumentsInReverseEtagOrder(context, start, pageSize); } using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { writer.WriteStartObject(); writer.WritePropertyName("Results"); writer.WriteDocuments(context, documents, metadataOnly: false, numberOfResults: out int _); writer.WriteEndObject(); } } return(Task.CompletedTask); }