Beispiel #1
0
        public Task Status()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WritePropertyName(nameof(IndexingStatus.Status));
                    writer.WriteString(Database.IndexStore.Status.ToString());
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(IndexingStatus.Indexes));
                    writer.WriteStartArray();
                    var isFirst = true;
                    foreach (var index in Database.IndexStore.GetIndexes())
                    {
                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }

                        isFirst = false;

                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(IndexingStatus.IndexStatus.Name));
                        writer.WriteString(index.Name);

                        writer.WriteComma();

                        writer.WritePropertyName(nameof(IndexingStatus.IndexStatus.Status));
                        writer.WriteString(index.Status.ToString());

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();

                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
        public Task GetAllConnections()
        {
            var start = GetStart();
            var take  = GetPageSize(Database.Configuration.Core.MaxPageSize);
            DocumentsOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
            {
                var connections = Database.RunningTcpConnections;
                HttpContext.Response.StatusCode = 200;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    var isFirst = true;

                    writer.WriteStartArray();

                    foreach (var connection in connections)
                    {
                        if (start > 0)
                        {
                            start--;
                            continue;
                        }
                        if (--take <= 0)
                        {
                            break;
                        }

                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }

                        isFirst = false;

                        context.Write(writer, connection.GetConnectionStats(context));
                    }
                    writer.WriteEndArray();
                }
            }
            return(Task.CompletedTask);
        }
Beispiel #3
0
        public async Task PingTest()
        {
            var dest     = GetStringQueryString("url", false) ?? GetStringQueryString("node", false);
            var topology = ServerStore.GetClusterTopology();
            var tasks    = new List <Task <PingResult> >();

            if (string.IsNullOrEmpty(dest))
            {
                foreach (var node in topology.AllNodes)
                {
                    tasks.Add(PingOnce(node.Value));
                }
            }
            else
            {
                var url = topology.GetUrlFromTag(dest);
                tasks.Add(PingOnce(url ?? dest));
            }

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var write = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    write.WriteStartObject();
                    write.WritePropertyName("Result");

                    write.WriteStartArray();
                    while (tasks.Count > 0)
                    {
                        var task = await Task.WhenAny(tasks);

                        tasks.Remove(task);
                        context.Write(write, task.Result.ToJson());
                        if (tasks.Count > 0)
                        {
                            write.WriteComma();
                        }
                        write.Flush();
                    }
                    write.WriteEndArray();
                    write.WriteEndObject();
                    write.Flush();
                }
        }
        protected override void PersistMapFields(JsonOperationContext context, BlittableJsonTextWriter writer)
        {
            writer.WritePropertyName(nameof(MapFields));
            writer.WriteStartArray();
            var first = true;

            foreach (var field in MapFields.Values.Select(x => x.As <AutoIndexField>()))
            {
                if (first == false)
                {
                    writer.WriteComma();
                }

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(field.Name));
                writer.WriteString(field.Name);
                writer.WriteComma();

                writer.WritePropertyName(nameof(field.Indexing));
                writer.WriteString(field.Indexing.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(field.Aggregation));
                writer.WriteInteger((int)field.Aggregation);
                writer.WriteComma();

                writer.WritePropertyName(nameof(field.Spatial));
                if (field.Spatial == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteObject(EntityToBlittable.ConvertCommandToBlittable(field.Spatial, context));
                }

                writer.WriteEndObject();

                first = false;
            }
            writer.WriteEndArray();
        }
Beispiel #5
0
        public Task Report()
        {
            DocumentsOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
            {
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    var first = true;
                    writer.WriteStartArray();
                    foreach (var env in Database.GetAllStoragesEnvironment())
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }

                        first = false;

                        writer.WriteStartObject();

                        writer.WritePropertyName("Name");
                        writer.WriteString(env.Name);
                        writer.WriteComma();

                        writer.WritePropertyName("Type");
                        writer.WriteString(env.Type.ToString());
                        writer.WriteComma();

                        var djv = (DynamicJsonValue)TypeConverter.ToBlittableSupportedType(GetReport(env));
                        writer.WritePropertyName("Report");
                        writer.WriteObject(context.ReadObject(djv, env.Name));

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                }
            }

            return(Task.CompletedTask);
        }
        public void Persist(JsonOperationContext context, BlittableJsonTextWriter writer)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(Name));
            writer.WriteString(Name);
            writer.WriteComma();

            writer.WritePropertyName(nameof(Version));
            writer.WriteInteger(Version);
            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 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);
                }
            }
        }
Beispiel #8
0
        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);
        }
Beispiel #9
0
        public Task GetAllGetApiKey()
        {
            var start    = GetStart();
            var pageSize = GetPageSize();

            TransactionOperationContext context;

            using (ServerStore.ContextPool.AllocateOperationContext(out context))
            {
                context.OpenReadTransaction();


                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();
                    bool first = true;
                    foreach (var item in ServerStore.StartingWith(context, Constants.ApiKeyPrefix, start, pageSize))
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        else
                        {
                            first = false;
                        }

                        string username = item.Key.Substring(Constants.ApiKeyPrefix.Length);

                        item.Data.Modifications = new DynamicJsonValue(item.Data)
                        {
                            ["UserName"] = username
                        };
                        context.Write(writer, item.Data);
                    }
                    writer.WriteEndArray();
                }
            }

            return(Task.CompletedTask);
        }
                public override void Prepare(ref RecordingState state)
                {
                    if (IsShutdown)
                    {
                        state = null;
                        return;
                    }

                    using (_txOpMerger._parent.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                        using (var writer = new BlittableJsonTextWriter(context, _txOpMerger._recording.Stream))
                        {
                            writer.WriteStartArray();

                            var commandDetails       = new StartRecordingDetails();
                            var commandDetailsReader = SerializeRecordingCommandDetails(context, commandDetails);

                            context.Write(writer, commandDetailsReader);
                        }

                    state = new EnabledRecordingState(_txOpMerger);
                }
Beispiel #11
0
            private void WriteHubPullReplications(List<PullReplicationDefinition> hubPullReplications)
            {
                if (hubPullReplications == null)
                {
                    _writer.WriteNull();
                    return;
                }
                _writer.WriteStartArray();
                var first = true;
                foreach (var pullReplication in hubPullReplications)
                {
                    if (first == false)
                        _writer.WriteComma();
                    first = false;

                    _context.Write(_writer, pullReplication.ToJson());
                }
                _writer.WriteEndArray();
            }
        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();
        }
Beispiel #14
0
        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);
            }
        }
Beispiel #15
0
        public Task GetConnectionsDebugInfo()
        {
            JsonOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();
                    var first = true;
                    foreach (var connection in Database.Notifications.Connections)
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;
                        context.Write(writer, connection.Value.GetDebugInfo());
                    }
                    writer.WriteEndArray();
                }
            return(Task.CompletedTask);
        }
        public static void WriteDocuments(this BlittableJsonTextWriter writer, JsonOperationContext context, IEnumerable <Document> documents, bool metadataOnly, out int numberOfResults)
        {
            numberOfResults = 0;

            writer.WriteStartArray();

            var first = true;

            foreach (var document in documents)
            {
                numberOfResults++;

                if (first == false)
                {
                    writer.WriteComma();
                }
                first = false;

                WriteDocument(writer, context, document, metadataOnly);
            }

            writer.WriteEndArray();
        }
Beispiel #17
0
        public override HttpRequestMessage CreateRequest(ServerNode node, out string url)
        {
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
            };

            request.Content = new BlittableJsonContent(stream =>
            {
                using (var writer = new BlittableJsonTextWriter(Context, stream))
                {
                    writer.WriteStartArray();
                    bool first = true;
                    foreach (var command in Commands)
                    {
                        if (!(first))
                        {
                            writer.WriteComma();
                        }
                        first = false;
                        Context.Write(writer, command);
                    }
                    writer.WriteEndArray();
                }
            });

            var sb = new StringBuilder($"{node.Url}/databases/{node.Database}/bulk_docs");

            AppendOptions(sb);


            IsReadRequest = false;

            url = sb.ToString();

            return(request);
        }
        private Task ReturnResources(string prefix)
        {
            TransactionOperationContext context;

            using (ServerStore.ContextPool.AllocateOperationContext(out context))
            {
                context.OpenReadTransaction();
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();
                    var first = true;
                    foreach (var db in ServerStore.StartingWith(context, prefix, GetStart(), GetPageSize()))
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;

                        //TODO: Actually handle this properly - do we need all those files in here? right now we are using /resources in studio
                        var doc = new DynamicJsonValue
                        {
                            ["Bundles"] = new DynamicJsonArray(),
                            ["Name"]    = db.Key.Substring(prefix.Length),
                            ["RejectClientsEnabled"] = false,
                            ["IndexingDisabled"]     = false,
                            ["Disabled"]             = false,
                            ["IsAdminCurrentTenant"] = true
                        };
                        context.Write(writer, doc);
                    }
                    writer.WriteEndArray();
                }
            }
            return(Task.CompletedTask);
        }
Beispiel #19
0
        private void Explain(DocumentsOperationContext context, string indexName)
        {
            var indexQuery = IndexQueryServerSide.Create(HttpContext, GetStart(), GetPageSize(Database.Configuration.Core.MaxPageSize), context);
            var runner     = new QueryRunner(Database, context);

            var explanations = runner.ExplainDynamicIndexSelection(indexName, indexQuery);

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                var isFirst = true;
                writer.WriteStartArray();
                foreach (var explanation in explanations)
                {
                    if (isFirst == false)
                    {
                        writer.WriteComma();
                    }

                    isFirst = false;
                    writer.WriteExplanation(context, explanation);
                }
                writer.WriteEndArray();
            }
        }
        protected void PersistMapFields(JsonOperationContext context, BlittableJsonTextWriter writer)
        {
            writer.WritePropertyName((nameof(MapFields)));
            writer.WriteStartArray();
            var first = true;

            foreach (var field in MapFields.Values)
            {
                if (first == false)
                {
                    writer.WriteComma();
                }

                writer.WriteStartObject();

                writer.WritePropertyName((nameof(field.Name)));
                writer.WriteString((field.Name));
                writer.WriteComma();

                writer.WritePropertyName((nameof(field.Highlighted)));
                writer.WriteBool(field.Highlighted);
                writer.WriteComma();

                writer.WritePropertyName((nameof(field.SortOption)));
                writer.WriteInteger((int)(field.SortOption ?? SortOptions.None));
                writer.WriteComma();

                writer.WritePropertyName((nameof(field.MapReduceOperation)));
                writer.WriteInteger((int)(field.MapReduceOperation));

                writer.WriteEndObject();

                first = false;
            }
            writer.WriteEndArray();
        }
Beispiel #21
0
        public Task GetAll()
        {
            var name = GetStringQueryString("name", required: false);

            var start     = GetStart();
            var pageSize  = GetPageSize();
            var namesOnly = GetBoolValueQueryString("namesOnly", required: false) ?? false;

            DocumentsOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    IndexDefinition[] indexDefinitions;
                    if (string.IsNullOrEmpty(name))
                    {
                        indexDefinitions = Database.IndexStore
                                           .GetIndexes()
                                           .OrderBy(x => x.Name)
                                           .Skip(start)
                                           .Take(pageSize)
                                           .Select(x => x.GetIndexDefinition())
                                           .ToArray();
                    }
                    else
                    {
                        var index = Database.IndexStore.GetIndex(name);
                        if (index == null)
                        {
                            HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                            return(Task.CompletedTask);
                        }

                        indexDefinitions = new[] { index.GetIndexDefinition() };
                    }

                    writer.WriteStartArray();

                    var isFirst = true;
                    foreach (var indexDefinition in indexDefinitions)
                    {
                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }

                        isFirst = false;

                        if (namesOnly)
                        {
                            writer.WriteString((indexDefinition.Name));
                            continue;
                        }

                        writer.WriteIndexDefinition(context, indexDefinition);
                    }

                    writer.WriteEndArray();
                }

            return(Task.CompletedTask);
        }
Beispiel #22
0
        public Task Debug()
        {
            var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");

            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(Task.CompletedTask);
            }

            var operation = GetStringQueryString("op");

            JsonOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    if (string.Equals(operation, "map-reduce-tree", StringComparison.OrdinalIgnoreCase))
                    {
                        if (index.Type.IsMapReduce() == false)
                        {
                            HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;

                            context.Write(writer, new DynamicJsonValue
                            {
                                ["Error"] = $"{index.Name} is not map-reduce index",
                            });

                            return(Task.CompletedTask);
                        }

                        var docId = GetStringQueryString("docId", required: false);

                        IEnumerable <ReduceTree> trees;
                        using (index.GetReduceTree(docId, out trees))
                        {
                            writer.WriteReduceTrees(trees);
                        }

                        return(Task.CompletedTask);
                    }

                    if (string.Equals(operation, "source-doc-ids", StringComparison.OrdinalIgnoreCase))
                    {
                        IEnumerable <string> ids;
                        using (index.GetIdentifiersOfMappedDocuments(GetStringQueryString("startsWith", required: false), GetStart(), GetPageSize(), out ids))
                        {
                            writer.WriteArrayOfResultsAndCount(ids);
                        }

                        return(Task.CompletedTask);
                    }

                    if (string.Equals(operation, "entries-fields", StringComparison.OrdinalIgnoreCase))
                    {
                        var fields = index.GetEntriesFields();

                        var first = true;
                        writer.WriteStartArray();

                        foreach (var field in fields)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }

                            first = false;

                            writer.WriteString(field);
                        }

                        writer.WriteEndArray();

                        return(Task.CompletedTask);
                    }

                    throw new NotSupportedException($"{operation} is not supported");
                }
        }
        public static void WriteIndexDefinition(this BlittableJsonTextWriter writer, JsonOperationContext context, IndexDefinition indexDefinition, bool removeAnalyzers = false)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(indexDefinition.Name));
            writer.WriteString(indexDefinition.Name);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Etag));
            writer.WriteInteger(indexDefinition.Etag);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Type));
            writer.WriteString(indexDefinition.Type.ToString());
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.LockMode));
            if (indexDefinition.LockMode.HasValue)
            {
                writer.WriteString(indexDefinition.LockMode.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Priority));
            if (indexDefinition.Priority.HasValue)
            {
                writer.WriteString(indexDefinition.Priority.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.OutputReduceToCollection));
            writer.WriteString(indexDefinition.OutputReduceToCollection);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Configuration));
            writer.WriteStartObject();
            var isFirstInternal = true;

            foreach (var kvp in indexDefinition.Configuration)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WritePropertyName(kvp.Key);
                writer.WriteString(kvp.Value);
            }
            writer.WriteEndObject();
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.AdditionalSources));
            writer.WriteStartObject();
            isFirstInternal = true;
            foreach (var kvp in indexDefinition.AdditionalSources)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WritePropertyName(kvp.Key);
                writer.WriteString(kvp.Value);
            }
            writer.WriteEndObject();
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.IsTestIndex));
            writer.WriteBool(indexDefinition.IsTestIndex);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Reduce));
            if (string.IsNullOrWhiteSpace(indexDefinition.Reduce) == false)
            {
                writer.WriteString(indexDefinition.Reduce);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Maps));
            writer.WriteStartArray();
            isFirstInternal = true;
            foreach (var map in indexDefinition.Maps)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;
                writer.WriteString(map);
            }
            writer.WriteEndArray();
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Fields));
            writer.WriteStartObject();
            isFirstInternal = true;
            foreach (var kvp in indexDefinition.Fields)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;
                writer.WritePropertyName(kvp.Key);
                if (kvp.Value != null)
                {
                    writer.WriteIndexFieldOptions(context, kvp.Value, removeAnalyzers);
                }
                else
                {
                    writer.WriteNull();
                }
            }
            writer.WriteEndObject();

            writer.WriteEndObject();
        }
        public static void WriteDatabaseStatistics(this BlittableJsonTextWriter writer, JsonOperationContext context, DatabaseStatistics statistics)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(statistics.CountOfIndexes));
            writer.WriteInteger(statistics.CountOfIndexes);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.CountOfDocuments));
            writer.WriteInteger(statistics.CountOfDocuments);
            writer.WriteComma();

            if (statistics.CountOfRevisionDocuments > 0)
            {
                writer.WritePropertyName(nameof(statistics.CountOfRevisionDocuments));
                writer.WriteInteger(statistics.CountOfRevisionDocuments);
                writer.WriteComma();
            }

            writer.WritePropertyName(nameof(statistics.CountOfAttachments));
            writer.WriteInteger(statistics.CountOfAttachments);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.CountOfUniqueAttachments));
            writer.WriteInteger(statistics.CountOfUniqueAttachments);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.DatabaseChangeVector));
            writer.WriteString(statistics.DatabaseChangeVector);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.DatabaseId));
            writer.WriteString(statistics.DatabaseId);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Is64Bit));
            writer.WriteBool(statistics.Is64Bit);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Pager));
            writer.WriteString(statistics.Pager);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.LastDocEtag));
            if (statistics.LastDocEtag.HasValue)
            {
                writer.WriteInteger(statistics.LastDocEtag.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName((nameof(statistics.DatabaseChangeVector)));
            writer.WriteString(statistics.DatabaseChangeVector);
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.LastIndexingTime));
            if (statistics.LastIndexingTime.HasValue)
            {
                writer.WriteDateTime(statistics.LastIndexingTime.Value, isUtc: true);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(statistics.Indexes));
            writer.WriteStartArray();
            var isFirstInternal = true;

            foreach (var index in statistics.Indexes)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(index.IsStale));
                writer.WriteBool(index.IsStale);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Name));
                writer.WriteString(index.Name);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Etag));
                writer.WriteInteger(index.Etag);
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.LockMode));
                writer.WriteString(index.LockMode.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Priority));
                writer.WriteString(index.Priority.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.State));
                writer.WriteString(index.State.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.Type));
                writer.WriteString(index.Type.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(index.LastIndexingTime));
                if (index.LastIndexingTime.HasValue)
                {
                    writer.WriteDateTime(index.LastIndexingTime.Value, isUtc: true);
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
            }
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
        public Task PreviewCollection()
        {
            var start        = GetStart();
            var pageSize     = GetPageSize();
            var collection   = GetStringQueryString("collection", required: false);
            var bindings     = GetStringValuesQueryString("binding", required: false);
            var fullBindings = GetStringValuesQueryString("fullBinding", required: false);

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                {
                    Document[] documents;
                    HashSet <LazyStringValue> availableColumns;
                    HashSet <string>          propertiesPreviewToSend;
                    HashSet <string>          fullPropertiesToSend = new HashSet <string>(fullBindings);

                    long   totalResults;
                    string changeVector;
                    string etag = null;

                    if (string.IsNullOrEmpty(collection))
                    {
                        changeVector = DocumentsStorage.GetDatabaseChangeVector(context);
                        totalResults = Database.DocumentsStorage.GetNumberOfDocuments(context);
                        etag         = $"{changeVector}/{totalResults}";
                    }
                    else
                    {
                        changeVector = Database.DocumentsStorage.GetLastDocumentChangeVector(context, collection);
                        totalResults = Database.DocumentsStorage.GetCollection(collection, context).Count;

                        if (changeVector != null)
                        {
                            etag = $"{changeVector}/{totalResults}";
                        }
                    }

                    if (etag != null && GetStringFromHeaders("If-None-Match") == etag)
                    {
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotModified;
                        return(Task.CompletedTask);
                    }
                    HttpContext.Response.Headers["ETag"] = "\"" + etag + "\"";

                    if (string.IsNullOrEmpty(collection))
                    {
                        documents               = Database.DocumentsStorage.GetDocumentsInReverseEtagOrder(context, start, pageSize).ToArray();
                        availableColumns        = ExtractColumnNames(documents, context);
                        propertiesPreviewToSend = bindings.Count > 0 ? new HashSet <string>(bindings) : new HashSet <string>();
                    }
                    else
                    {
                        documents               = Database.DocumentsStorage.GetDocumentsInReverseEtagOrder(context, collection, start, pageSize).ToArray();
                        availableColumns        = ExtractColumnNames(documents, context);
                        propertiesPreviewToSend = bindings.Count > 0 ? new HashSet <string>(bindings) : availableColumns.Take(ColumnsSamplingLimit).Select(x => x.ToString()).ToHashSet();
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Results");

                        {
                            writer.WriteStartArray();

                            var first = true;
                            foreach (var document in documents)
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }
                                first = false;

                                using (document.Data)
                                {
                                    WriteDocument(writer, context, document, propertiesPreviewToSend, fullPropertiesToSend);
                                }
                            }

                            writer.WriteEndArray();
                        }

                        writer.WriteComma();

                        writer.WritePropertyName("TotalResults");
                        writer.WriteInteger(totalResults);

                        writer.WriteComma();

                        writer.WriteArray("AvailableColumns", availableColumns);

                        writer.WriteEndObject();
                    }

                    return(Task.CompletedTask);
                }
        }
Beispiel #26
0
        public Task GetErrors()
        {
            var names = HttpContext.Request.Query["name"];

            List <Index> indexes;

            if (names.Count == 0)
            {
                indexes = Database.IndexStore.GetIndexes().ToList();
            }
            else
            {
                indexes = new List <Index>();
                foreach (var name in names)
                {
                    var index = Database.IndexStore.GetIndex(name);
                    if (index == null)
                    {
                        throw new InvalidOperationException("There is not index with name: " + name);
                    }

                    indexes.Add(index);
                }
            }

            DocumentsOperationContext context;

            using (ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();

                    var first = true;
                    foreach (var index in indexes)
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }

                        first = false;

                        writer.WriteStartObject();

                        writer.WritePropertyName(("Name"));
                        writer.WriteString((index.Name));
                        writer.WriteComma();

                        writer.WritePropertyName(("Errors"));
                        writer.WriteStartArray();
                        var firstError = true;
                        foreach (var error in index.GetErrors())
                        {
                            if (firstError == false)
                            {
                                writer.WriteComma();
                            }

                            firstError = false;

                            writer.WriteStartObject();

                            writer.WritePropertyName((nameof(error.Timestamp)));
                            writer.WriteString((error.Timestamp.GetDefaultRavenFormat()));
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(error.Document)));
                            if (string.IsNullOrWhiteSpace(error.Document) == false)
                            {
                                writer.WriteString((error.Document));
                            }
                            else
                            {
                                writer.WriteNull();
                            }
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(error.Action)));
                            if (string.IsNullOrWhiteSpace(error.Action) == false)
                            {
                                writer.WriteString((error.Action));
                            }
                            else
                            {
                                writer.WriteNull();
                            }
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(error.Error)));
                            if (string.IsNullOrWhiteSpace(error.Error) == false)
                            {
                                writer.WriteString((error.Error));
                            }
                            else
                            {
                                writer.WriteNull();
                            }

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                }

            return(Task.CompletedTask);
        }
Beispiel #27
0
        public Task Performance()
        {
            var from  = 0;
            var froms = HttpContext.Request.Query["from"];

            if (froms.Count > 1)
            {
                throw new ArgumentException($"Query string value 'from' must appear exactly once");
            }
            if (froms.Count > 0 && int.TryParse(froms[0], out from) == false)
            {
                throw new ArgumentException($"Query string value 'from' must be a number");
            }

            var stats = GetIndexesToReportOn()
                        .Select(x => new IndexPerformanceStats
            {
                IndexName   = x.Name,
                IndexId     = x.IndexId,
                Performance = x.GetIndexingPerformance(from)
            })
                        .ToArray();

            JsonOperationContext context;

            using (Database.DocumentsStorage.ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();

                    var isFirst = true;
                    foreach (var stat in stats)
                    {
                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }

                        isFirst = false;

                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(stat.IndexName));
                        writer.WriteString((stat.IndexName));
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(stat.IndexId));
                        writer.WriteInteger(stat.IndexId);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(stat.Performance));
                        writer.WriteStartArray();
                        var isFirstInternal = true;
                        foreach (var performance in stat.Performance)
                        {
                            if (isFirstInternal == false)
                            {
                                writer.WriteComma();
                            }

                            isFirstInternal = false;

                            writer.WriteIndexingPerformanceStats(context, performance);
                        }
                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                }

            return(Task.CompletedTask);
        }
        public static void WriteFacetResult(this BlittableJsonTextWriter writer, JsonOperationContext context, FacetResult result)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(result.RemainingHits));
            writer.WriteInteger(result.RemainingHits);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.RemainingTermsCount));
            writer.WriteInteger(result.RemainingTermsCount);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.RemainingTerms));
            writer.WriteStartArray();
            var isFirstInternal = true;

            foreach (var term in result.RemainingTerms)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteString(term);
            }
            writer.WriteEndArray();
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.Values));
            writer.WriteStartArray();
            isFirstInternal = true;
            foreach (var value in result.Values)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(value.Average));
                if (value.Average.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Average.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Count));
                if (value.Count.HasValue)
                {
                    writer.WriteInteger(value.Count.Value);
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Hits));
                writer.WriteInteger(value.Hits);
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Max));
                if (value.Max.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Max.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Min));
                if (value.Min.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Min.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Range));
                writer.WriteString(value.Range);
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Sum));
                if (value.Sum.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Sum.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
            }
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
Beispiel #29
0
        public async Task Try()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var json = await context.ReadForMemoryAsync(RequestBodyStream(), null);

                var tryout = JsonDeserializationServer.SubscriptionTryout(json);

                var(collection, (script, functions), revisions) = SubscriptionConnection.ParseSubscriptionQuery(tryout.Query);
                SubscriptionPatchDocument patch = null;
                if (string.IsNullOrEmpty(script) == false)
                {
                    patch = new SubscriptionPatchDocument(script, functions);
                }

                if (collection == null)
                {
                    throw new ArgumentException("Collection must be specified");
                }

                var pageSize = GetIntValueQueryString("pageSize") ?? 1;

                var state = new SubscriptionState
                {
                    ChangeVectorForNextBatchStartingPoint = tryout.ChangeVector,
                    Query = tryout.Query
                };

                var fetcher = new SubscriptionDocumentsFetcher(Database, pageSize, -0x42,
                                                               new IPEndPoint(HttpContext.Connection.RemoteIpAddress, HttpContext.Connection.RemotePort), collection, revisions, state, patch);

                if (Enum.TryParse(
                        tryout.ChangeVector,
                        out Constants.Documents.SubscriptionChangeVectorSpecialStates changeVectorSpecialValue))
                {
                    switch (changeVectorSpecialValue)
                    {
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.BeginningOfTime:
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.DoNotChange:
                        state.ChangeVectorForNextBatchStartingPoint = null;
                        break;

                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.LastDocument:
                        state.ChangeVectorForNextBatchStartingPoint = Database.DocumentsStorage.GetLastDocumentChangeVector(context, collection);
                        break;
                    }
                }

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("Results");
                    writer.WriteStartArray();

                    using (context.OpenReadTransaction())
                    {
                        var first = true;

                        foreach (var itemDetails in fetcher.GetDataToSend(context, 0))
                        {
                            if (itemDetails.Doc.Data == null)
                            {
                                continue;
                            }

                            if (first == false)
                            {
                                writer.WriteComma();
                            }

                            if (itemDetails.Exception == null)
                            {
                                writer.WriteDocument(context, itemDetails.Doc, metadataOnly: false);
                            }
                            else
                            {
                                var docWithExcepton = new DocumentWithException
                                {
                                    Exception    = itemDetails.Exception.ToString(),
                                    ChangeVector = itemDetails.Doc.ChangeVector,
                                    Id           = itemDetails.Doc.Id,
                                    DocumentData = itemDetails.Doc.Data
                                };
                                writer.WriteObject(context.ReadObject(docWithExcepton.ToJson(), ""));
                            }

                            first = false;
                        }
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
            }
        }
        public static void WriteTreePagesRecursively(this BlittableJsonTextWriter writer, IEnumerable <ReduceTreePage> pages)
        {
            var first = true;

            foreach (var page in pages)
            {
                if (first == false)
                {
                    writer.WriteComma();
                }

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(TreePage.PageNumber));
                writer.WriteInteger(page.PageNumber);
                writer.WriteComma();

                writer.WritePropertyName(nameof(ReduceTreePage.AggregationResult));
                if (page.AggregationResult != null)
                {
                    writer.WriteObject(page.AggregationResult);
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(ReduceTreePage.Children));
                if (page.Children != null)
                {
                    writer.WriteStartArray();
                    WriteTreePagesRecursively(writer, page.Children);
                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(ReduceTreePage.Entries));
                if (page.Entries != null)
                {
                    writer.WriteStartArray();

                    var firstEntry = true;
                    foreach (var entry in page.Entries)
                    {
                        if (firstEntry == false)
                        {
                            writer.WriteComma();
                        }

                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(MapResultInLeaf.Data));
                        writer.WriteObject(entry.Data);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(MapResultInLeaf.Source));
                        writer.WriteString(entry.Source);

                        writer.WriteEndObject();

                        firstEntry = false;
                    }

                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
                first = false;
            }
        }