Пример #1
0
        public async Task CreateAsync(DesignDocument designDocument, CreateViewIndexOptions options)
        {
            var json = JsonConvert.SerializeObject(designDocument);
            var uri  = GetUri(designDocument.Name, options.IsProduction);

            Logger.LogInformation($"Attempting to create design document {_bucketName}/{designDocument.Name} - {uri}");
            Logger.LogDebug(json);

            try
            {
                try
                {
                    // Check design document doesn't already exist - will throw if not
                    await GetAsync(designDocument.Name, new GetViewIndexOptions { IsProduction = options.IsProduction }).ConfigureAwait(false);

                    throw new DesignDocumentAlreadyExistsException(_bucketName, designDocument.Name);
                }
                catch (DesignDocumentNotFoundException)
                {
                    // we expect this exception
                }

                var content = new StringContent(json, Encoding.UTF8, MediaType.Json);
                var result  = await _client.PutAsync(uri, content, options.CancellationToken).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Failed to create design document {_bucketName}/{designDocument.Name} - {uri} - {json}");
                throw;
            }
        }
Пример #2
0
        public static Task UpsertAsync(this IViewManager viewManager, DesignDocument designDocument, Action <UpsertViewIndexOptions> configureOptions)
        {
            var options = new UpsertViewIndexOptions();

            configureOptions(options);

            return(viewManager.UpsertAsync(designDocument, options));
        }
Пример #3
0
        public static Task CreateAsync(this IViewManager viewManager, DesignDocument designDocument, Action <CreateViewIndexOptions> configureOptions)
        {
            var options = new CreateViewIndexOptions();

            configureOptions(options);

            return(viewManager.CreateAsync(designDocument, options));
        }
Пример #4
0
        public async Task <IEnumerable <DesignDocument> > GetAllAsync(GetAllViewIndexOptions options)
        {
            var uri = new UriBuilder
            {
                Scheme = _scheme,
                Host   = _clusterOptions.Servers.GetRandom().Host,
                Port   = 8091,
                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;
            }
        }
Пример #5
0
        public async Task UpsertAsync(DesignDocument designDocument, UpsertViewIndexOptions options)
        {
            var json = JsonConvert.SerializeObject(designDocument);
            var uri  = GetUri(designDocument.Name, options.IsProduction);

            Logger.LogInformation($"Attempting to upsert design document {_bucketName}/{designDocument.Name} - {uri}");
            Logger.LogDebug(json);

            try
            {
                var content = new StringContent(json, Encoding.UTF8, MediaType.Json);
                var result  = await _client.PutAsync(uri, content, options.CancellationToken).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Failed to upsert design document {_bucketName}/{designDocument.Name} - {uri} - {json}");
                throw;
            }
        }
Пример #6
0
 public static Task UpsertAsync(this IViewManager viewManager, DesignDocument designDocument)
 {
     return(viewManager.UpsertAsync(designDocument, UpsertViewIndexOptions.Default));
 }
Пример #7
0
 public static Task CreateAsync(this IViewManager viewManager, DesignDocument designDocument)
 {
     return(viewManager.CreateAsync(designDocument, CreateViewIndexOptions.Default));
 }