예제 #1
0
        public static Task <IEnumerable <DesignDocument> > GetAllDesignDocumentsAsync(this IViewIndexManager viewManager, DesignDocumentNamespace @namespace, Action <GetAllDesignDocumentsOptions> configureOptions)
        {
            var options = new GetAllDesignDocumentsOptions();

            configureOptions(options);

            return(viewManager.GetAllDesignDocumentsAsync(@namespace, options));
        }
        public async Task <IEnumerable <DesignDocument> > GetAllDesignDocumentsAsync(DesignDocumentNamespace @namespace, GetAllDesignDocumentsOptions options)
        {
            var uri = new UriBuilder(_context.GetRandomNodeForService(ServiceType.KeyValue, _bucketName).ViewsUri)
            {
                Port = _context.ClusterOptions.MgmtPort,
                Path = $"pools/default/buckets/{_bucketName}/ddocs"
            }.Uri;

            Logger.LogInformation($"Attempting to get all design documents for bucket {_bucketName} - {uri}");

            try
            {
                var result = await _client.GetAsync(uri, options.CancellationToken).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();

                var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                var json = JObject.Parse(content);

                var designDocuments = new List <DesignDocument>();
                if (json.TryGetValue("rows", out var rows))
                {
                    foreach (var row in rows)
                    {
                        var designDoc = new DesignDocument
                        {
                            Name = row.SelectToken("doc.meta.id").Value <string>().Replace("_design/", string.Empty)
                        };

                        foreach (var view in row.SelectTokens("doc.json.views"))
                        {
                            var name   = view.First.Path.Substring(view.First.Path.LastIndexOf(".") + 1);
                            var map    = view.First.First.SelectToken("map");
                            var reduce = view.First.First.SelectToken("reduce");
                            designDoc.Views.Add(name, new View
                            {
                                Map    = map.Value <string>(),
                                Reduce = reduce?.Value <string>()
                            });
                        }

                        designDocuments.Add(designDoc);
                    }
                }

                return(designDocuments);
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Failed to get all design documents for bucket {_bucketName} - {uri}");
                throw;
            }
        }