Пример #1
0
        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.WriteComma();

                writer.WritePropertyName(nameof(field.HasSuggestions));
                writer.WriteBool(field.HasSuggestions);

                writer.WriteEndObject();

                first = false;
            }
            writer.WriteEndArray();
        }
Пример #2
0
            private void WriteTablesArray(string name, List <SqlMigrationTable> tables, BlittableJsonTextWriter writer)
            {
                writer.WritePropertyName(name);
                writer.WriteStartArray();

                var first = true;

                if (tables != null)
                {
                    foreach (var table in tables)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            writer.WriteComma();
                        }

                        writer.WriteStartObject();

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

                        writer.WritePropertyName(nameof(table.Query));
                        writer.WriteString(table.Query);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(table.Patch));
                        writer.WriteString(table.Patch);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(table.NewName));
                        writer.WriteString(table.NewName);
                        writer.WriteComma();

                        WriteTablesArray(nameof(table.EmbeddedTables), table.EmbeddedTables, writer);

                        writer.WriteEndObject();
                    }
                }

                writer.WriteEndArray();
            }
Пример #3
0
            public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
            {
                url = $"{node.Url}/databases/{node.Database}/attachments/bulk";

                var request = new HttpRequestMessage
                {
                    Method  = HttpMethods.Post,
                    Content = new BlittableJsonContent(stream =>
                    {
                        using (var writer = new BlittableJsonTextWriter(_context, stream))
                        {
                            writer.WriteStartObject();

                            writer.WritePropertyName(nameof(AttachmentType));
                            writer.WriteString(_type.ToString());
                            writer.WriteComma();

                            writer.WritePropertyName(nameof(Attachments));

                            writer.WriteStartArray();
                            var first = true;
                            foreach (var attachment in Attachments)
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }
                                first = false;

                                writer.WriteStartObject();
                                writer.WritePropertyName(nameof(AttachmentRequest.DocumentId));
                                writer.WriteString(attachment.DocumentId);
                                writer.WriteComma();
                                writer.WritePropertyName(nameof(AttachmentRequest.Name));
                                writer.WriteString(attachment.Name);
                                writer.WriteEndObject();
                            }
                            writer.WriteEndArray();

                            writer.WriteEndObject();
                        }
                    })
                };

                return(request);
            }
Пример #4
0
        public Task Trees()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var tx = context.OpenReadTransaction())
                {
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName("Results");
                        writer.WriteStartArray();
                        var first = true;

                        foreach (var treeType in new[] { RootObjectType.VariableSizeTree, RootObjectType.FixedSizeTree, RootObjectType.EmbeddedFixedSizeTree })
                        {
                            foreach (var name in GetTreeNames(tx.InnerTransaction, treeType))
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }

                                first = false;

                                writer.WriteStartObject();

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

                                writer.WritePropertyName("Type");
                                writer.WriteString(treeType.ToString());

                                writer.WriteEndObject();
                            }
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                }

            return(Task.CompletedTask);
        }
Пример #5
0
        public Task Status()
        {
            DocumentsOperationContext context;

            using (ContextPool.AllocateOperationContext(out 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);
        }
Пример #6
0
        public Task HugeDocuments()
        {
            DocumentsOperationContext context;

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

                        var isFirst = true;

                        foreach (var pair in context.DocumentDatabase.HugeDocuments.GetHugeDocuments())
                        {
                            if (isFirst == false)
                            {
                                writer.WriteComma();
                            }

                            isFirst = false;

                            writer.WriteStartObject();

                            writer.WritePropertyName(("Id"));
                            writer.WriteString((pair.Key.Item1));

                            writer.WriteComma();

                            writer.WritePropertyName(("Size"));
                            writer.WriteInteger(pair.Value);

                            writer.WriteComma();

                            writer.WritePropertyName(("Last Access"));
                            writer.WriteString(pair.Key.Item2.ToString("o", CultureInfo.InvariantCulture));

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }

            return(Task.CompletedTask);
        }
Пример #7
0
        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);
        }
Пример #8
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();
                }
        }
Пример #9
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);
        }
Пример #10
0
        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();
        }
Пример #11
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);
        }
Пример #12
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);
        }
Пример #13
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.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);
                }
            }
        }
Пример #14
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();
            }
Пример #15
0
        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();
        }
Пример #16
0
        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);
        }
Пример #17
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);
            }
        }
Пример #18
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();
        }
Пример #20
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);
        }
Пример #21
0
        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);
        }
Пример #22
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();
            }
        }
Пример #23
0
        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();
        }
        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();
        }
Пример #25
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();
                }
            }
        }
Пример #26
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);
        }
Пример #27
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);
        }
        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();
        }
Пример #29
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);
        }
Пример #30
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");
                }
        }