Beispiel #1
0
        public async Task <IBucket> GetBucketAsync(string bucketName)
        {
            await EnsureBucketAsync(bucketName);
            await CreateIndex(bucketName);

            return(await ClusterHelper.GetBucketAsync(bucketName));
        }
Beispiel #2
0
        private async Task InitConnection()
        {
            //Initialize bucket if it doesn't exist
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = _config.Uris
            }, new PasswordAuthenticator(_config.UserName, _config.Password));

            //Connects to bucket
            _bucket = await ClusterHelper.GetBucketAsync(_config.BucketName);
        }
Beispiel #3
0
        public async Task CreateIndex(string bucketName)
        {
            var bucket = await ClusterHelper.GetBucketAsync(bucketName);

            var result =
                await bucket.QueryAsync <dynamic>(
                    $"CREATE PRIMARY INDEX `{bucketName}-index` ON `{bucketName}` USING GSI;");

            if (!result.Success)
            {
                if (result.Errors.First().Message.Contains("already exists"))
                {
                    return;
                }

                throw new Exception("Error creating index for bucket");
            }
        }
Beispiel #4
0
 private Task <IBucket> GetDefaultBucket()
 {
     return(ClusterHelper.GetBucketAsync(_bucketName));
 }
        public async Task Initialise()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.BucketName))
                {
                    throw new ArgumentException("BucketName can not be null or empty");
                }

                if (this.ClientConfig == null)
                {
                    throw new ArgumentException("You must supply a configuration to connect to Couchbase");
                }

                if (this.ClientConfig.BucketConfigs.All(a => a.Key != this.BucketName))
                {
                    throw new BucketConfigMissingFromConfigurationException($"The requested bucket is named '{this.BucketName}' however the provided Couchbase configuration has no bucket configuration");
                }

                if (!ClusterHelper.Initialized)
                {
                    ClusterHelper.Initialize(this.ClientConfig);
                }
                else
                {
                    foreach (var conf in this.ClientConfig.BucketConfigs)
                    {
                        if (ClusterHelper.Get().Configuration.BucketConfigs.ContainsKey(conf.Key))
                        {
                            ClusterHelper.Get().Configuration.BucketConfigs.Remove(conf.Key);
                        }

                        ClusterHelper.Get().Configuration.BucketConfigs.Add(conf.Key, conf.Value);
                    }
                }

                var timeoutPolicy = Policy.TimeoutAsync(30, TimeoutStrategy.Pessimistic);
                this.bucket = await timeoutPolicy.ExecuteAsync(async() => this.bucket = await ClusterHelper.GetBucketAsync(this.BucketName));
            }
            catch (Exception e)
            {
                await Task.FromException(e);
            }
        }