示例#1
0
        public async Task UpdateBucketAsync(BucketSettings settings, UpdateBucketOptions?options = null)
        {
            options ??= new UpdateBucketOptions();
            var uri = GetUri(settings.Name);

            _logger.LogInformation($"Attempting to upsert bucket with name {settings.Name} - {uri}");

            try
            {
                // upsert bucket
                var content = new FormUrlEncodedContent(GetBucketSettingAsFormValues(settings));
                var result  = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Failed to upsert bucket with name {settings.Name} - {uri}");
                throw;
            }
        }
        public async Task CreateBucketAsync(BucketSettings settings, CreateBucketOptions?options = null)
        {
            options ??= new CreateBucketOptions();
            var uri = GetUri();

            _logger.LogInformation("Attempting to create bucket with name {settings.Name} - {uri}",
                                   _redactor.MetaData(settings.Name), _redactor.SystemData(uri));

            try
            {
                // create bucket
                var content = new FormUrlEncodedContent(GetBucketSettingAsFormValues(settings));
                var result  = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                if (result.StatusCode == HttpStatusCode.BadRequest)
                {
                    var json = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (json.IndexOf("Bucket with given name already exists", StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        throw new BucketExistsException(settings.Name);
                    }
                }

                result.EnsureSuccessStatusCode();
            }
            catch (BucketExistsException)
            {
                _logger.LogError("Failed to create bucket with name {settings.Name} because it already exists",
                                 _redactor.MetaData(settings.Name));
                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to create bucket with name {settings.Name} - {uri}",
                                 _redactor.MetaData(settings.Name), _redactor.SystemData(uri));
                throw;
            }
        }
示例#3
0
        private IEnumerable <KeyValuePair <string, string> > GetBucketSettingAsFormValues(BucketSettings settings)
        {
            var values = new Dictionary <string, string>();

            values.Add("name", settings.Name);
            values.Add("bucketType", settings.BucketType.GetDescription());
            values.Add("ramQuotaMB", settings.RamQuotaMB.ToString());
            values.Add("replicaIndex", settings.ReplicaIndexes ? "1" : "0");
            values.Add("replicaNumber", settings.NumReplicas.ToString());
            values.Add("flushEnabled", settings.FlushEnabled ? "1" : "0");

            if (settings.ConflictResolutionType.HasValue)
            {
                values.Add("conflictResolutionType", settings.ConflictResolutionType.GetDescription());
            }

            if (settings.EjectionMethod.HasValue)
            {
                values.Add("evictionPolicy", settings.EjectionMethod.GetDescription());
            }

            if (settings.MaxTtl > 0)
            {
                values.Add("maxTTL", settings.MaxTtl.ToString());
            }

            if (settings.CompressionMode.HasValue)
            {
                values.Add("compressionMode", settings.CompressionMode.GetDescription());
            }

            return(values);
        }
示例#4
0
 public static Task UpdateBucketAsync(this IBucketManager bucketManager, BucketSettings settings)
 {
     return(bucketManager.UpdateBucketAsync(settings, UpdateBucketOptions.Default));
 }
        private BucketSettings GetBucketSettings(JToken json)
        {
            var settings = new BucketSettings
            {
                Name         = json.GetTokenValue <string>("name"),
                MaxTtl       = json.GetTokenValue <int>("maxTTL"),
                RamQuotaMB   = json.GetTokenValue <int>("quota.rawRAM"),
                FlushEnabled = json.SelectToken("controllers.flush") != null
            };

            var bucketTypeToken = json.SelectToken("bucketType");

            if (bucketTypeToken != null &&
                EnumExtensions.TryGetFromDescription(bucketTypeToken.Value <string>(), out BucketType bucketType))
            {
                settings.BucketType = bucketType;
            }

            if (settings.BucketType != BucketType.Memcached)
            {
                settings.NumReplicas = json.GetTokenValue <int>("replicaNumber");
            }

            if (settings.BucketType == BucketType.Couchbase)
            {
                settings.ReplicaIndexes = json.GetTokenValue <bool>("replicaIndex");
            }

            var conflictResolutionToken = json.SelectToken("conflictResolutionType");

            if (conflictResolutionToken != null &&
                EnumExtensions.TryGetFromDescription(conflictResolutionToken.Value <string>(), out ConflictResolutionType conflictResolutionType))
            {
                settings.ConflictResolutionType = conflictResolutionType;
            }

            var compressionModeToken = json.SelectToken("compressionMode");

            if (compressionModeToken != null &&
                EnumExtensions.TryGetFromDescription(compressionModeToken.Value <string>(), out CompressionMode compressionMode))
            {
                settings.CompressionMode = compressionMode;
            }

            var evictionPolicyToken = json.SelectToken("evictionPolicy");

            if (evictionPolicyToken != null &&
                EnumExtensions.TryGetFromDescription(evictionPolicyToken.Value <string>(), out EvictionPolicyType evictionPolicyType))
            {
                settings.EvictionPolicy = evictionPolicyType;
            }

            var durabilityMinLevelToken = json.SelectToken("durabilityMinLevel");

            if (durabilityMinLevelToken != null &&
                EnumExtensions.TryGetFromDescription(durabilityMinLevelToken.Value <string>(),
                                                     out DurabilityLevel durabilityMinLevel))
            {
                settings.DurabilityMinimumLevel = durabilityMinLevel;
            }

            return(settings);
        }
        private IEnumerable <KeyValuePair <string, string> > GetBucketSettingAsFormValues(BucketSettings settings)
        {
            var values = new Dictionary <string, string>
            {
                { "name", settings.Name },
                { "bucketType", settings.BucketType.GetDescription() },
                { "ramQuotaMB", settings.RamQuotaMB.ToString() },
                { "flushEnabled", settings.FlushEnabled ? "1" : "0" }
            };

            if (settings.BucketType != BucketType.Memcached)
            {
                values.Add("replicaNumber", settings.NumReplicas.ToString());
            }

            if (settings.BucketType == BucketType.Couchbase)
            {
                values.Add("replicaIndex", settings.ReplicaIndexes ? "1" : "0");
            }

            if (settings.ConflictResolutionType.HasValue)
            {
                values.Add("conflictResolutionType", settings.ConflictResolutionType.GetDescription());
            }

            /*Policy-assignment depends on bucket type. For a Couchbase bucket, the policy can be valueOnly (which is the default)
             *  or fullEviction. For an Ephemeral bucket, the policy can be noEviction (which is the default) or nruEviction. No policy
             *  can be assigned to a Memcached bucket.*/

            if (settings.EvictionPolicy.HasValue)
            {
                if (settings.BucketType == BucketType.Couchbase)
                {
                    if (settings.EvictionPolicy == EvictionPolicyType.NoEviction ||
                        settings.EvictionPolicy == EvictionPolicyType.NotRecentlyUsed)
                    {
                        throw new InvalidArgumentException(
                                  "For a Couchbase bucket, the eviction policy can be valueOnly (which is the default) or fullEviction.");
                    }
                }

                if (settings.BucketType == BucketType.Ephemeral)
                {
                    if (settings.EvictionPolicy == EvictionPolicyType.ValueOnly ||
                        settings.EvictionPolicy == EvictionPolicyType.FullEviction)
                    {
                        throw new InvalidArgumentException(
                                  "For an Ephemeral bucket, the eviction policy can be noEviction (which is the default) or nruEviction.");
                    }
                }

                if (settings.BucketType == BucketType.Memcached)
                {
                    throw new InvalidArgumentException("No eviction policy can be assigned to a Memcached bucket.");
                }

                values.Add("evictionPolicy", settings.EvictionPolicy.GetDescription());
            }

            if (settings.MaxTtl > 0)
            {
                values.Add("maxTTL", settings.MaxTtl.ToString());
            }

            if (settings.CompressionMode.HasValue)
            {
                values.Add("compressionMode", settings.CompressionMode.GetDescription());
            }

            if (settings.DurabilityMinimumLevel != DurabilityLevel.None)
            {
                values.Add("durabilityMinLevel", settings.DurabilityMinimumLevel.GetDescription());
            }

            return(values);
        }