/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bucketName">Name of the bucket that this manager should operate on.</param>
        /// <param name="clientConfig">Configuration object for the database client</param>
        public CouchBaseDataManager(string bucketName, Couchbase.Configuration.Client.ClientConfiguration clientConfig)
        {
            //Bucket name should not be empty
            //Keep in mind that you should create the buckets before being able to use them either
            //using the commandline tool or the web console
            if (string.IsNullOrWhiteSpace(bucketName))
            {
                throw new ArgumentException("bucketName can not be null or empty");
            }
            //config should not be null either
            if (clientConfig == null)
            {
                throw new ArgumentException("You should suply a configuration to connect to CouchBase");
            }

            this.bucketName = bucketName;
            if (!OrleansCouchBaseStorage.IsInitialized)
            {
                ClusterHelper.Initialize(clientConfig);
                OrleansCouchBaseStorage.IsInitialized = true;
            }
            else
            {
                foreach (var conf in 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);
                }
            }
            //cache the bucket.
            bucket = ClusterHelper.GetBucket(this.bucketName);
        }
        private static ICluster GetCluster(string configurationKey)
        {
            NotNullOrWhiteSpace(configurationKey, nameof(configurationKey));

            if (!_clusters.TryGetValue(configurationKey, out ICluster cluster))
            {
                var config = GetConfiguration(configurationKey);
                if (config != null)
                {
                    cluster = new Cluster(config);
                }
                else
                {
                    // fallback to ClusterHelper as that's also a way ppl can configure this stuff...
                    try
                    {
                        cluster = ClusterHelper.Get();
                    }
                    catch (InitializationException)
                    {
                        // last fallback has also not been initialized yet
                        // this will use the development settings on localhost without any auth (might not work and blow up later).
                        cluster = new Cluster();
                    }

                    // update our configuration cache just in case
                    AddConfiguration(configurationKey, cluster.Configuration);
                }

                _clusters.TryAdd(configurationKey, cluster);
            }

            return(cluster);
        }
Exemple #3
0
        public void Test_OpenBucket()
        {
            ClusterHelper.Initialize();
            var cluster = ClusterHelper.Get();
            var bucket  = cluster.OpenBucket();

            Assert.AreEqual("default", bucket.Name);
        }
Exemple #4
0
        public void When_Initialized_Get_Returns_Instance()
        {
            ClusterHelper.Initialize();
            var cluster = ClusterHelper.Get();

            Assert.IsNotNull(cluster);
            cluster.Dispose();
        }
        public void Test_OpenBucket()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            var cluster = ClusterHelper.Get();
            var bucket  = cluster.OpenBucket();

            Assert.AreEqual("default", bucket.Name);
        }
        public void When_Initialized_Get_Returns_Instance()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            var cluster = ClusterHelper.Get();

            Assert.IsNotNull(cluster);
            cluster.Dispose();
        }
Exemple #7
0
        protected void SetContractResolver(IContractResolver contractResolver)
        {
            _contractResolver = contractResolver;

            var cluster = ClusterHelper.Get();

            cluster.Configuration.DeserializationSettings.ContractResolver = contractResolver;
            cluster.Configuration.SerializationSettings.ContractResolver   = contractResolver;
        }
Exemple #8
0
        public void Cluster_Uses_Provided_Authenticator()
        {
            var config = new ClientConfiguration();

            config.SetAuthenticator(new PasswordAuthenticator("username", "password"));
            ClusterHelper.Initialize(config);

            Assert.AreEqual(config.Authenticator, ClusterHelper.Get().Configuration.Authenticator);
        }
Exemple #9
0
        public void When_OpenBucket_Is_Called_Multiple_Times_Same_Bucket_Object_IsReturned()
        {
            ClusterHelper.Initialize();
            _cluster = ClusterHelper.Get();

            var bucket1 = _cluster.OpenBucket("default");
            var bucket2 = _cluster.OpenBucket("default");

            Assert.AreSame(bucket1, bucket2);
        }
Exemple #10
0
        public void Cluster_Uses_Provided_Authenticator_When_Passed_To_Initialize()
        {
            var config = new ClientConfiguration();

            ClusterHelper.Initialize(config, new PasswordAuthenticator("username", "password"));

            var authenticator = ClusterHelper.Get().Configuration.Authenticator;

            Assert.AreEqual(config.Authenticator, authenticator);
        }
        public void When_GetBucket_Is_Called_Multiple_Times_Same_Bucket_Object_IsReturned()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            ClusterHelper.Get().SetupEnhancedAuth();

            var bucket1 = ClusterHelper.GetBucket("default");
            var bucket2 = ClusterHelper.GetBucket("default");

            Assert.AreEqual(bucket1, bucket2);
        }
        public void When_A_Bucket_Instance_Is_Nulled_Its_Reference_Still_Exists()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            ClusterHelper.Get().SetupEnhancedAuth();

            var bucket1 = ClusterHelper.GetBucket("default");

            bucket1 = null;
            bucket1 = ClusterHelper.GetBucket("default");
            Assert.IsNotNull(bucket1);
        }
Exemple #13
0
        public void Test_Basic_Query()
        {
            var db    = new BucketContext(ClusterHelper.Get(), "beer-sample");
            var query = from x in db.Query <Beer>()
                        where x.Type == "beer"
                        select x;

            foreach (var beer in query)
            {
                Console.WriteLine(beer.Name);
            }
        }
        public void Test_GetBucket_Using_HttpStreamingProvider()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            _cluster = ClusterHelper.Get();

            const string expected = "default";

            using (var bucket = _cluster.OpenBucket("default"))
            {
                Assert.AreEqual(expected, bucket.Name);
            }
        }
Exemple #15
0
        public void UsingExplicitBucketName()
        {
            ClusterHelper.Initialize();
            ClusterHelper.Get().Authenticate(new PasswordAuthenticator("admin", "password"));

            var cacheConfig = new ConfigurationBuilder()
                              .WithCouchbaseCacheHandle("keydoesnotmatter", "beer-sample") // passing in a bucket name which should be used
                              .Build();

            var cache = new BaseCacheManager <int>(cacheConfig);

            cache.AddOrUpdate("test", 1, (v) => v + 1);
        }
        public void When_Close_Called_Bucket_Count_Is_Zero()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            ClusterHelper.Get().SetupEnhancedAuth();

            Assert.AreEqual(0, ClusterHelper.Count());
            var bucket1 = ClusterHelper.GetBucket("default");
            var bucket2 = ClusterHelper.GetBucket("default");

            Assert.AreEqual(1, ClusterHelper.Count());
            ClusterHelper.Close();
            Assert.AreEqual(0, ClusterHelper.Count());
        }
        public void When_RemoveBucket_Is_Called_Bucket_Count_Is_Zero()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            ClusterHelper.Get().SetupEnhancedAuth();

            //open a bucket and get the reference
            var bucket1 = ClusterHelper.GetBucket("default");
            var bucket2 = ClusterHelper.GetBucket("default");

            Assert.AreEqual(1, ClusterHelper.Count());
            ClusterHelper.RemoveBucket("default");
            Assert.AreEqual(0, ClusterHelper.Count());
        }
        public void Test_GetBucket_Using_CarrierPublicationProvider()
        {
            ClusterHelper.Initialize(ClientConfigUtil.GetConfiguration());
            _cluster = ClusterHelper.Get();

            const string expected = "default";

            using (var bucket = _cluster.OpenBucket("default"))
            {
                Assert.IsNotNull(bucket);
                Assert.AreEqual(expected, bucket.Name);
            }
        }
        public void Register(CouchbaseConfiguration configuration)
        {
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = new List <Uri> {
                    new Uri(configuration.Host)
                }
            });
            var username = configuration.Username;
            var password = configuration.Password;

            // provide authentication to cluster
            ClusterHelper.Get().Authenticate(new PasswordAuthenticator(username, password));
        }
        public void When_Configuration_Is_Customized_Good_Things_Happens()
        {
            var config = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri(ConfigurationManager.AppSettings["bootstrapUrl"])
                },
                PoolConfiguration = new PoolConfiguration(10, 10)
            };

            ClusterHelper.Initialize(config);
            _cluster = ClusterHelper.Get();
        }
        public void Test_GetBucket_Using_CarrierPublicationProvider()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            var cluster = ClusterHelper.Get();

            cluster.SetupEnhancedAuth();

            const string expected = "default";

            using (var bucket = cluster.OpenBucket("default"))
            {
                Assert.IsNotNull(bucket);
                Assert.AreEqual(expected, bucket.Name);
            }
        }
        /// <summary>
        /// Returns a Couchbase bucket or create one if it does not exist
        /// </summary>
        /// <param name="name">Name of the section from the configuration file</param>
        /// <param name="config">Configuration section information from the config file</param>
        /// <returns>Instance of the couchbase bucket to use</returns>
        public IBucket GetBucket(
            string name,
            NameValueCollection config)
        {
            // Get the bucket name to use from the configuration file and use a specific bucket if specified
            var bucketName = ProviderHelper.GetAndRemove(config, "bucket", false);

            if (!string.IsNullOrEmpty(bucketName))
            {
                return(ClusterHelper.GetBucket(bucketName));
            }

            // If no bucket is specified, simply use the default bucket (which will be the first in the list)
            return(ClusterHelper.Get().OpenBucket());
        }
        public void When_Bucket_Is_Opened_On_Two_Seperate_Threads_And_RemoveBucket_Is_Called_Count_Is_Zero()
        {
            ClusterHelper.Initialize(TestConfiguration.GetCurrentConfiguration());
            ClusterHelper.Get().SetupEnhancedAuth();

            var t1 = new Thread(OpenBucket);
            var t2 = new Thread(OpenBucket);

            t1.Start();
            t2.Start();

            TwoThreadsCompleted.Wait();
            Assert.AreEqual(1, ClusterHelper.Count());
            ClusterHelper.RemoveBucket("default");
            Assert.AreEqual(0, ClusterHelper.Count());
        }
Exemple #24
0
 protected override void InitialiseInternal()
 {
     if (_clientConfig == null)
     {
         _clientConfig = _clientConfig ?? new ClientConfiguration((CouchbaseClientSection)ConfigurationManager.GetSection("couchbaseClients/couchbase"));
         BucketConfig  = _clientConfig.BucketConfigs.Values.FirstOrDefault();
     }
     try
     {
         ClusterHelper.Get();
     }
     catch (InitializationException)
     {
         ClusterHelper.Initialize(_clientConfig);
     }
 }
Exemple #25
0
        private static void CreateCluster()
        {
            var cbConfig = new CouchbaseConfiguration();
            var port     = cbConfig.GetPort();

            var hosts  = cbConfig.GetHosts().Select(x => new Uri(string.Format("{0}:{1}/pools", x, port))).ToList();
            var config = new ClientConfiguration()
            {
                Servers = hosts,
            };

            ClusterHelper.Initialize(config);

            cluster = ClusterHelper.Get(); // Single connection, do not use using (do not dispose a recreate)

            //var cm = cluster.CreateManager("Administrator", "123456");
        }
Exemple #26
0
        public void When_Configuration_Is_Customized_Good_Things_Happens()
        {
            var config = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://192.168.56.101:8091/pools")
                },
                PoolConfiguration = new PoolConfiguration
                {
                    MaxSize = 10,
                    MinSize = 10
                }
            };

            ClusterHelper.Initialize(config);
            _cluster = ClusterHelper.Get();
        }
Exemple #27
0
        public void UsingAppConfigWithAuthentication()
        {
            // see couchbaseClients/couchbase section in app.config of this project
            // Note: even though we pass in "cb", CacheManager will fall back to the
            // default couchbase section at couchbaseClients/couchbase!
            // We could also pass in the section name explicitly instead of "cb".

            ClusterHelper.Initialize("couchbaseClients/couchbase");
            ClusterHelper.Get().Authenticate(new PasswordAuthenticator("admin", "password"));

            var cacheConfig = new ConfigurationBuilder()
                              .WithCouchbaseCacheHandle("keydoesnotmatter")
                              .Build();

            var cache = new BaseCacheManager <int>(cacheConfig);

            cache.AddOrUpdate("test", 1, (v) => v + 1);
        }
        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);
            }
        }
Exemple #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EyWebRegistry" /> class.
        /// </summary>
        /// <param name="binFolder">An instance of <see cref="System.String" />.</param>
        public EyWebRegistry()
        {
            Scan(assemblyScanner =>
            {
                assemblyScanner.WithDefaultConventions();
                assemblyScanner.AssembliesFromApplicationBaseDirectory();
                assemblyScanner.TheCallingAssembly();
                assemblyScanner.LookForRegistries();
            });
            ClusterHelper.Initialize("couchbaseClients/couchbase");

            this.For <HttpContextBase>().Use(x => GetContext());

            // this.For<SecurityData>().Use(x => GetSecurityData()).Transient();
            //this.For<ISessionStorage>().Use<CouchBaseSessionStore>();
            //this.For<ILockable>().Use<CouchBaseLock>();
            this.For <Cluster>().Use(ClusterHelper.Get()).Singleton();
            this.For <CacheManager>().Use(new CacheManager(new InCouchBaseCache())).Singleton();
            // this.For<ISessionPesistanceStore>().Use<RedisSessionStorage>();
        }
Exemple #30
0
        public static void Register()
        {
            var couchbaseServer = ConfigurationManager.AppSettings.Get("CouchbaseServer");

            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = new List <Uri> {
                    new Uri(couchbaseServer)
                }
            });

            var bucketName = ConfigurationManager.AppSettings.Get("CouchbaseCRMBucket");
            var username   = ConfigurationManager.AppSettings.Get("CouchbaseUser");
            var password   = ConfigurationManager.AppSettings.Get("CouchbasePassword");

            // provide authentication to cluster
            ClusterHelper.Get().Authenticate(new PasswordAuthenticator(username, password));

            EnsureIndexes(bucketName);
        }