public async Task Test_GetAllScopes()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName1 = "test_scopex1", collectionName = "test_collection1", scopeName2 = "test_scopex2", scopeName3 = "test_scopex3", scopeName4 = "test_scopex4";
            var          scopeSpec1     = new ScopeSpec(scopeName1);
            var          scopeSpec2     = new ScopeSpec(scopeName2);
            var          scopeSpec3     = new ScopeSpec(scopeName3);
            var          scopeSpec4     = new ScopeSpec(scopeName4);
            var          collectionSpec = new CollectionSpec(scopeName1, collectionName);
            // create scope
            await collectionManager.CreateScopeAsync(scopeSpec1).ConfigureAwait(false);

            await collectionManager.CreateScopeAsync(scopeSpec2).ConfigureAwait(false);

            await collectionManager.CreateScopeAsync(scopeSpec3).ConfigureAwait(false);

            await collectionManager.CreateScopeAsync(scopeSpec4).ConfigureAwait(false);

            // get all scopes
            var getAllScopesResult = await collectionManager.GetAllScopesAsync().ConfigureAwait(false);

            Assert.NotNull(getAllScopesResult);
        }
        public async Task Test_RemoveOps()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope13", collectionName = "test_collection13";
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);

            try
            {
                // create scope and collection
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                var scope = await bucket.ScopeAsync(scopeName);

                var collection = await scope.CollectionAsync(collectionName);

                var insertTasks = Enumerable.Range(0, 20).Select(x => collection.InsertAsync($"mykey-{x}", new { }));
                await Task.WhenAll(insertTasks);

                var removeTasks = Enumerable.Range(0, 20).Select(x => collection.RemoveAsync($"mykey-{x}"));
                await Task.WhenAll(removeTasks);
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
        public async Task Test_DropNonExistentCollection_Throws_CollectionNotFoundException()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName          = "scope_only";
            var          collectionSpecNone = new CollectionSpec(scopeName, "collection_null");

            await DropScopeAndCollectionIfExists(scopeName, collectionSpecNone, collectionManager);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                await collectionManager.DropCollectionAsync(collectionSpecNone).ConfigureAwait(false);
            }
            catch (CollectionNotFoundException e)
            {
                Assert.Equal("Collection with name collection_null not found in scope scope_only", e.Message);
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpecNone, collectionManager);
            }
        }
        public async Task Test_DropNonExistentScope_Throws_ScopeNotFoundException()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope8", collectionName = "test_collection8";
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                await collectionManager.DropScopeAsync("scope_none").ConfigureAwait(false);
            }
            catch (ScopeNotFoundException e)
            {
                Assert.Equal("Scope with name scope_none not found", e.Message);
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
        public async Task Test_SingleScopeMaxNumberOfCollections()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var    collectionManager = (CollectionManager)bucket.Collections;
            string scopeName         = "singlescope1";
            var    scopeSpec         = new ScopeSpec(scopeName);

            try
            {
                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

                for (int i = 0; i < 1000; i++)
                {
                    var collectionSpec = new CollectionSpec(scopeName, (1000 + i).ToString());
                    await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                    var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                    Assert.True(collectionExistsResult);
                }
            }
            finally
            {
                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
        public async Task Test_ScopeNotFound()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope3", collectionName = "test_collection3";
            var          collectionSpec        = new CollectionSpec(scopeName, collectionName);
            var          collectionSpecInvalid = new CollectionSpec("noscope", "emptycollection");

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // create collection
                await Assert.ThrowsAsync <ScopeNotFoundException>(async() => await collectionManager.CreateCollectionAsync(collectionSpecInvalid).ConfigureAwait(false));
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
Example #7
0
        public async Task Test_Collection_Exists()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default");

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "my_scope", collectionName = "my_collection";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                // create scope
                //await collectionManager.CreateScopeAsync(scopeSpec);

                // create collection
                // await collectionManager.CreateCollectionAsync(collectionSpec);

                var collection = bucket.Scope(scopeName).Collection(collectionName);
                var result     = await collection.UpsertAsync("key3", new { });

                var result2 = await collection.UpsertAsync("key3", new { boo = "bee" }, new UpsertOptions().Expiry(TimeSpan.FromMilliseconds(100000)));
            }
            catch
            {
                // ???
            }
            finally
            {
                // drop collection
                //await collectionManager.DropCollectionAsync(collectionSpec);
                //await collectionManager.DropScopeAsync(scopeName);
            }
        }
        public async Task Test_CollectionExists()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope3", collectionName = "test_collection3";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                // create scope
                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                // collection exists
                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);
            }
            finally
            {
                // drop collection
                await collectionManager.DropCollectionAsync(collectionSpec).ConfigureAwait(false);

                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
 private async Task DropCollectionIfExists(CollectionSpec collectionSpec, CollectionManager collectionManager)
 {
     if (await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false))
     {
         // drop collection
         await collectionManager.DropCollectionAsync(collectionSpec).ConfigureAwait(false);
     }
 }
        public async Task InitializeAsync()
        {
            var opts = GetClusterOptions();

            Cluster = await Couchbase.Cluster.ConnectAsync(
                _settings.ConnectionString,
                opts)
                      .ConfigureAwait(false);

            var bucketSettings = new BucketSettings()
            {
                BucketType  = BucketType.Couchbase,
                Name        = BucketName,
                RamQuotaMB  = 100,
                NumReplicas = 0
            };

            try
            {
                await Cluster.Buckets.CreateBucketAsync(bucketSettings).ConfigureAwait(false);
            }
            catch (BucketExistsException)
            {
            }
            catch (System.Net.Http.HttpRequestException)
            {
                // why did it fail?
            }

            try
            {
                var bucket = await Cluster.BucketAsync(BucketName);

                try
                {
                    await bucket.Collections.CreateScopeAsync(CustomScopeName);

                    await Task.Delay(5_000);
                }
                catch (ScopeExistsException)
                {}

                try
                {
                    var collectionSpec = new CollectionSpec(scopeName: CustomScopeName, CustomCollectionName);
                    await bucket.Collections.CreateCollectionAsync(collectionSpec);

                    await Task.Delay(5_000);
                }
                catch (CollectionExistsException)
                {}
            }
            catch
            {
                throw;
            }
        }
        public async Task Test_CollectionManager_With_MinExpiry()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope", collectionName = "test_collection";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName)
            {
                MaxExpiry = TimeSpan.FromMinutes(10)
            };

            try
            {
                // create scope
                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

                // scope exists
                var scopeExistsResult = await collectionManager.ScopeExistsAsync(scopeName).ConfigureAwait(false);

                Assert.True(scopeExistsResult);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // get all scopes
                var getAllScopesResult = await collectionManager.GetAllScopesAsync().ConfigureAwait(false);

                var scope = getAllScopesResult.SingleOrDefault(x => x.Name == scopeName);
                Assert.NotNull(scope);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                // collection exists
                scope = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(TimeSpan.FromMinutes(10), scope.Collections.First(x => x.Name == collectionName).MaxExpiry);
            }
            finally
            {
                // drop collection
                await collectionManager.DropCollectionAsync(collectionSpec).ConfigureAwait(false);

                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
        public async Task Test_CollectionExistsException()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope5", collectionName = "test_collection5";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                // collection exists
                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);

                // scope exists
                var scopeExistsResult = await collectionManager.ScopeExistsAsync(scopeName).ConfigureAwait(false);

                Assert.True(scopeExistsResult);

                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);
            }
            catch (CollectionExistsException e)
            {
                Assert.Equal("Collection with name test_collection5 already exists in scope test_scope5", e.Message);
            }

            finally
            {
                // drop collection
                await collectionManager.DropCollectionAsync(collectionSpec).ConfigureAwait(false);

                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
        public async Task Test_CollectionManager()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default");

            var collectionManager = bucket.Collections;

            const string scopeName = "test_scope", collectionName = "test_collection";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                // create scope
                await collectionManager.CreateScopeAsync(scopeSpec);

                // scope exists
                var scopeExistsResult = await collectionManager.ScopeExistsAsync(scopeName);

                Assert.True(scopeExistsResult);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName);

                Assert.Equal(scopeName, getScopeResult.Name);

                // get all scopes
                var getAllScopesResult = await collectionManager.GetAllScopesAsync();

                var scope = getAllScopesResult.SingleOrDefault(x => x.Name == scopeName);
                Assert.NotNull(scope);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec);

                // collection exists
                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec);

                Assert.True(collectionExistsResult);
            }
            finally
            {
                // drop collection
                await collectionManager.DropCollectionAsync(collectionSpec);

                // drop scope
                await collectionManager.DropScopeAsync(scopeName);
            }
        }
        public async Task Test_CollectionManager()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope2", collectionName = "test_collection2";
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                // create scope
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

                // scope exists
                var scopeExistsResult = await collectionManager.ScopeExistsAsync(scopeName).ConfigureAwait(false);

                Assert.True(scopeExistsResult);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // get all scopes
                var getAllScopesResult = await collectionManager.GetAllScopesAsync().ConfigureAwait(false);

                var scope = getAllScopesResult.SingleOrDefault(x => x.Name == scopeName);
                Assert.NotNull(scope);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

                // collection exists
                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
        public async System.Threading.Tasks.Task Test_Collections_QueryOps()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;
            var scopeName         = "query_test_scope2";
            var collectionName    = "query_test_collection2";
            var docId             = "mydoc2";
            var collectionSpec    = new CollectionSpec(scopeName, collectionName);

            await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                var scope = await bucket.ScopeAsync(scopeName);

                var collection = await scope.CollectionAsync(collectionName);

                var task = await collection.InsertAsync(docId, new { }).ConfigureAwait(false);

                var options =
                    new QueryOptions("select * from `" + collectionName + "` where meta().id=\"" + docId + "\"")
                {
                    QueryContext = "namespace:bucket:scope:collection"
                };
                var args = options.GetFormValues();
                Assert.Equal("namespace:bucket:scope:collection", args["query_context"]);
                options = new QueryOptions("SELECT * FROM `$bucket` WHERE collectionName=$name")
                          .Parameter("bucket", "default").Parameter("collectionName", "query_test_collection2");

                var values = options.GetFormValues();
                Assert.Equal("default", values["$bucket"]);
                Assert.Equal("query_test_collection2", values["$collectionName"]);
            }
            catch (CouchbaseException e)
            {
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
Example #16
0
        /// <summary>
        /// Re-requests light information and updates all properties
        /// </summary>
        public async Task Refresh()
        {
            Light light = await GetRefreshed();

            Id               = light.Id;
            UUID             = light.UUID;
            Label            = light.Label;
            IsConnected      = light.IsConnected;
            PowerState       = light.PowerState;
            Color            = light.Color;
            Brightness       = light.Brightness;
            group            = light.group;
            location         = light.location;
            LastSeen         = light.LastSeen;
            SecondsSinceSeen = light.SecondsSinceSeen;
            ProductName      = light.ProductName;
        }
        public async Task Test_ScopeNotFound()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope1", collectionName = "test_collection1";
            var          scopeSpec             = new ScopeSpec(scopeName);
            var          collectionSpec        = new CollectionSpec(scopeName, collectionName);
            var          collectionSpecInvalid = new CollectionSpec("noscope", "emptycollection");

            try
            {
                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);


                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpecInvalid).ConfigureAwait(false);

                var scopeExistsResult = await collectionManager.ScopeExistsAsync("noscope").ConfigureAwait(false);

                Assert.False(scopeExistsResult);

                // collection exists
                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpecInvalid).ConfigureAwait(false);

                Assert.False(collectionExistsResult);
            }
            finally
            {
                // drop collection
                await collectionManager.DropCollectionAsync(collectionSpec).ConfigureAwait(false);

                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
        public async Task Test_Collections_DataverseCollectionQuery()
        {
            var cluster = await _fixture.GetCluster().ConfigureAwait(false);

            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            string dataverseName     = bucket.Name + "." + scopeName;
            var    collectionManager = (CollectionManager)bucket.Collections;
            var    scopeSpec         = new ScopeSpec(scopeName);
            var    analytics         = cluster.AnalyticsIndexes;
            await analytics.CreateDataverseAsync(dataverseName);

            string statement = "CREATE ANALYTICS COLLECTION `" + dataverseName + "`.`" + collectionName + "` ON `" + bucket.Name + "`.`" + scopeName + "`.`" + collectionName + "`";

            try
            {
                var analyticsResult = await cluster.AnalyticsQueryAsync <TestRequest>(statement).ConfigureAwait(false);

                var result = await analyticsResult.ToListAsync().ConfigureAwait(false);

                await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

                var collectionSpec = new CollectionSpec(scopeName, collectionName);
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);
                var scope = bucket.Scope(scopeName);
                statement       = "SELECT * FROM `" + collectionName + "` where `" + collectionName + "`.foo= \"bar\"";
                analyticsResult = await cluster.AnalyticsQueryAsync <TestRequest>(statement).ConfigureAwait(false);

                result = await analyticsResult.ToListAsync().ConfigureAwait(false);

                Assert.True(result.Any());
            }
            finally
            {
                // drop scope
                await collectionManager.DropScopeAsync(scopeName).ConfigureAwait(false);
            }
        }
        public async Task Test_CollectionExistsException()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "test_scope7", collectionName = "test_collection7";
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                // get scope
                var getScopeResult = await collectionManager.GetScopeAsync(scopeName).ConfigureAwait(false);

                Assert.Equal(scopeName, getScopeResult.Name);

                // create collection
                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                // collection exists
                var collectionExistsResult =
                    await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);

                // scope exists
                var scopeExistsResult = await collectionManager.ScopeExistsAsync(scopeName).ConfigureAwait(false);

                Assert.True(scopeExistsResult);

                await Assert
                .ThrowsAsync <CollectionExistsException>(async() =>
                                                         await collectionManager.CreateCollectionAsync(collectionSpec)).ConfigureAwait(false);
            }
            finally
            {
                await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);
            }
        }
        public async System.Threading.Tasks.Task Test_Collections_QueryOptionsAsync()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;
            var scopeName         = "query_test_scope1";
            var collectionName    = "query_test_collection1";
            var docId             = "mydoc1";
            var collectionSpec    = new CollectionSpec(scopeName, collectionName);

            await DropScopeAndCollectionIfExists(scopeName, collectionSpec, collectionManager);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName).ConfigureAwait(false);

                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                var collectionExistsResult = await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);

                var scope = await bucket.ScopeAsync(scopeName);

                var collection = await scope.CollectionAsync(collectionName);

                var task = await collection.InsertAsync(docId, new { }).ConfigureAwait(false);

                var options = new QueryOptions("select * from `" + collectionName + "` where meta().id=\"" + docId + "\"")
                {
                    QueryContext = "namespace:bucket:scope:collection"
                };
                var args = options.GetFormValues();
                Assert.Equal("namespace:bucket:scope:collection", args["query_context"]);
            }
            finally
            {
                await DropScopeIfExists(scopeName, collectionManager);
            }
        }
Example #21
0
        private async Task CreateCollectionsAsync()
        {
            _logger.LogInformation("Creating collections...");
            var tables = (await _sqlConnection.QueryAsync(@"
                select TABLE_SCHEMA, TABLE_NAME
                from INFORMATION_SCHEMA.TABLES
                WHERE TABLE_TYPE = 'BASE TABLE'")).ToList();

            foreach (var table in tables)
            {
                string collectionName = GetCollectionName(table.TABLE_SCHEMA, table.TABLE_NAME);
                string scopeName      = GetScopeName(table.TABLE_SCHEMA);

                _logger.LogInformation($"Creating collection `{collectionName}`...");

                await CreateScopeIfNecessaryAsync(scopeName);

                if (await CollectionExistsAsync(collectionName, scopeName))
                {
                    _logger.LogInformation("already exists.");
                    continue;
                }

                try
                {
                    var spec = new CollectionSpec(scopeName, collectionName);
                    await _collManager.CreateCollectionAsync(spec);
                }
                catch
                {
                    _logger.LogError($"Unable to create collection `{collectionName}` in scope `{scopeName}`");
                    throw;
                }
                _logger.LogInformation("Done");
            }
            _logger.LogInformation("Collection creation complete.");
        }
        public async Task Test_RemoveOps()
        {
            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var collectionManager = (CollectionManager)bucket.Collections;

            const string scopeName = "my_scope1", collectionName = "my_collection1";
            var          scopeSpec      = new ScopeSpec(scopeName);
            var          collectionSpec = new CollectionSpec(scopeName, collectionName);

            // create scope
            await collectionManager.CreateScopeAsync(scopeSpec).ConfigureAwait(false);

            // create collection
            await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

            var collection = bucket.Scope(scopeName).Collection(collectionName);
            var tasks      = new List <Task>();

            for (var i = 0; i < 20; i++)
            {
                await collection.RemoveAsync($"mykey-{i}").ConfigureAwait(false);
            }
        }
        public async Task ExecuteAsync()
        {
            ICluster cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");

            IUserManager users = cluster.Users;

            Console.WriteLine("bucketAdmin");
            // tag::bucketAdmin[]
            {
                var user = new User("bucketAdmin");
                user.Password    = "******";
                user.DisplayName = "Bucket Admin [travel-sample]";
                user.Roles       = new List <Role>()
                {
                    new Role("bucket_admin", "travel-sample")
                };
                await users.UpsertUserAsync(user);
            }
            // end::bucketAdmin[]

            {
                var collectionMgr = await getCollectionManager("bucketAdmin", "password");

                // tag::create-scope[]
                try {
                    await collectionMgr.CreateScopeAsync("example-scope");
                }
                catch (ScopeExistsException) {
                    Console.WriteLine("The scope already exists");
                }
                // end::create-scope[]
            }

            Console.WriteLine("scopeAdmin");
            // tag::scopeAdmin[]
            {
                var user = new User("scopeAdmin");
                user.Password    = "******";
                user.DisplayName = "Manage Collections in Scope [travel-sample:*]";
                user.Roles       = new List <Role>()
                {
                    new Role("scope_admin", "travel-sample"),
                    new Role("data_reader", "travel-sample")
                };
                await users.UpsertUserAsync(user);
            }
            // end::scopeAdmin[]

            {
                Console.WriteLine("create-collection");
                var collectionMgr = await getCollectionManager("scopeAdmin", "password");

                // tag::create-collection[]
                var spec = new CollectionSpec("example-scope", "example-collection");

                try {
                    await collectionMgr.CreateCollectionAsync(spec);
                }
                catch (CollectionExistsException) {
                    Console.WriteLine("Collection already exists");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::create-collection[]

                Console.WriteLine("drop-collection");
                // tag::drop-collection[]
                try {
                    await collectionMgr.DropCollectionAsync(spec);
                }
                catch (CollectionNotFoundException) {
                    Console.WriteLine("The specified collection doesn't exist");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::drop-collection[]
            }
            {
                Console.WriteLine("drop-scope");
                var collectionMgr = await getCollectionManager("bucketAdmin", "password");

                // tag::drop-scope[]
                try {
                    await collectionMgr.DropScopeAsync("example-scope");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified scope doesn't exist");
                }
                // end::drop-scope[]
            }
        }
Example #24
0
            public override bool Equals(object obj)
            {
                CollectionSpec spec = obj as CollectionSpec;

                return(spec != null && spec.id == id && spec.name == name);
            }
Example #25
0
            public override bool Equals(object obj)
            {
                CollectionSpec spec = obj as CollectionSpec;

                return(spec != null && spec.Id == Id && spec.Name == Name);
            }
        public async Task ExecuteAsync()
        {
            Console.WriteLine("scopeAdmin");
            {
                // tag::scopeAdmin[]
                ICluster clusterAdmin = await Cluster.ConnectAsync(
                    "couchbase://localhost", "Administrator", "password");

                IUserManager users = clusterAdmin.Users;

                var user = new User("scopeAdmin")
                {
                    Password    = "******",
                    DisplayName = "Manage Scopes [travel-sample:*]",
                    Roles       = new List <Role>()
                    {
                        new Role("scope_admin", "travel-sample"),
                        new Role("data_reader", "travel-sample")
                    }
                };

                await users.UpsertUserAsync(user);

                // end::scopeAdmin[]
            }

            ICluster cluster = await Cluster.ConnectAsync("couchbase://localhost", "scopeAdmin", "password");

            IBucket bucket = await cluster.BucketAsync("travel-sample");

            // tag::create-collection-manager[]
            ICouchbaseCollectionManager collectionMgr = bucket.Collections;
            // end::create-collection-manager[]
            {
                Console.WriteLine("create-scope");
                // tag::create-scope[]
                try {
                    await collectionMgr.CreateScopeAsync("example-scope");
                }
                catch (ScopeExistsException) {
                    Console.WriteLine("The scope already exists");
                }
                // end::create-scope[]
            }
            {
                Console.WriteLine("create-collection");
                // tag::create-collection[]
                var spec = new CollectionSpec("example-scope", "example-collection");

                try {
                    await collectionMgr.CreateCollectionAsync(spec);
                }
                catch (CollectionExistsException) {
                    Console.WriteLine("Collection already exists");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::create-collection[]

                Console.WriteLine("listing-scope-collection");
                // tag::listing-scope-collection[]
                var scopes = await collectionMgr.GetAllScopesAsync();

                foreach (ScopeSpec scopeSpec in scopes)
                {
                    Console.WriteLine($"Scope: {scopeSpec.Name}");

                    foreach (CollectionSpec collectionSpec in scopeSpec.Collections)
                    {
                        Console.WriteLine($" - {collectionSpec.Name}");
                    }
                }
                // end::listing-scope-collection[]

                Console.WriteLine("drop-collection");
                // tag::drop-collection[]
                try {
                    await collectionMgr.DropCollectionAsync(spec);
                }
                catch (CollectionNotFoundException) {
                    Console.WriteLine("The specified collection doesn't exist");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::drop-collection[]
            }
            {
                Console.WriteLine("drop-scope");
                // tag::drop-scope[]
                try {
                    await collectionMgr.DropScopeAsync("example-scope");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified scope doesn't exist");
                }
                // end::drop-scope[]
            }
        }
Example #27
0
        public async Task CreateAndDropCollectionIndex()
        {
            var cluster = await _fixture.GetCluster().ConfigureAwait(false);

            var bucketName        = _fixture.GetDefaultBucket().Result.Name;
            var collectionManager = _fixture.GetDefaultBucket().Result.Collections;

            var scopeName      = Guid.NewGuid().ToString();
            var collectionName = Guid.NewGuid().ToString();
            var collectionSpec = new CollectionSpec(scopeName, collectionName);

            try
            {
                await collectionManager.CreateScopeAsync(scopeName);

                await collectionManager.CreateCollectionAsync(collectionSpec);

                const string indexName = "indexmgr_test_collection";
                try
                {
                    await cluster.QueryIndexes.CreateIndexAsync(bucketName, indexName, new[] { "type" }, options =>
                    {
                        options.ScopeNameValue      = scopeName;
                        options.CollectionNameValue = collectionName;
                    }).ConfigureAwait(false);
                }
                catch (IndexExistsException)
                {
                    _outputHelper.WriteLine("IndexExistsException.  Maybe from a previous run.  Skipping.");
                }

                var failedCleanup = false;
                try
                {
                    await cluster.QueryIndexes.BuildDeferredIndexesAsync(bucketName, options =>
                    {
                        options.ScopeNameValue      = scopeName;
                        options.CollectionNameValue = collectionName;
                    }).ConfigureAwait(false);

                    using var cts = new CancellationTokenSource(10000);

                    await cluster.QueryIndexes.WatchIndexesAsync(bucketName, new[] { indexName },
                                                                 options =>
                    {
                        options.CancellationToken(cts.Token);
                        options.ScopeNameValue      = scopeName;
                        options.CollectionNameValue = collectionName;
                    }).ConfigureAwait(false);

                    var getIndexes = await cluster.QueryIndexes.GetAllIndexesAsync(bucketName, options =>
                    {
                        options.ScopeNameValue      = scopeName;
                        options.CollectionNameValue = collectionName;
                    });

                    Assert.Contains(indexName, getIndexes.Select(idx => idx.Name));
                }
                finally
                {
                    try
                    {
                        await cluster.QueryIndexes.DropIndexAsync(bucketName, indexName, options =>
                        {
                            options.ScopeNameValue      = scopeName;
                            options.CollectionNameValue = collectionName;
                        }).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        _outputHelper.WriteLine($"Failure during cleanup: {e}");
                        failedCleanup = true;
                    }
                }

                Assert.False(failedCleanup);
            }
            finally
            {
                await collectionManager.DropScopeAsync(scopeName);
            }
        }
 private async Task DropScopeAndCollectionIfExists(string scopeName, CollectionSpec collectionSpec,
                                                   CollectionManager collectionManager)
 {
     await DropCollectionIfExists(collectionSpec, collectionManager);
     await DropScopeIfExists(scopeName, collectionManager);
 }
Example #29
0
        public async Task Test_Collections_DataverseCollectionQuery()
        {
            var cluster = await _fixture.GetCluster().ConfigureAwait(false);

            var bucket = await _fixture.Cluster.BucketAsync("default").ConfigureAwait(false);

            var dataverseName     = bucket.Name + "." + ScopeName;
            var collectionManager = (CollectionManager)bucket.Collections;
            var analytics         = cluster.AnalyticsIndexes;

            try
            {
                await using var dataverseDisposer = DisposeCleaner.DropDataverseOnDispose(analytics, dataverseName, _output);
                await collectionManager.CreateScopeAsync(ScopeName).ConfigureAwait(false);

                await using var scopeDispose = DisposeCleaner.DropScopeOnDispose(collectionManager, ScopeName, _output);
                var collectionSpec = new CollectionSpec(ScopeName, CollectionName);

                await Task.Delay(TimeSpan.FromSeconds(1));

                await collectionManager.CreateCollectionAsync(collectionSpec).ConfigureAwait(false);

                await Task.Delay(TimeSpan.FromSeconds(1));

                var collectionExistsResult =
                    await collectionManager.CollectionExistsAsync(collectionSpec).ConfigureAwait(false);

                Assert.True(collectionExistsResult);

                await bucket.Scope(ScopeName).Collection(CollectionName).UpsertAsync("KEY1", new { bar = "foo" });

                await analytics.CreateDataverseAsync(dataverseName).ConfigureAwait(false);

                await Task.Delay(TimeSpan.FromSeconds(5));

                var statement = $"CREATE ANALYTICS COLLECTION {FilteredCollection} ON {bucket.Name}.{ScopeName}.{CollectionName}";
                await cluster.AnalyticsQueryAsync <TestRequest>(statement).ConfigureAwait(false);

                await using var analyticsCollectionDisposer = new DisposeCleanerAsync(() =>
                                                                                      cluster.AnalyticsQueryAsync <dynamic>($"DROP ANALYTICS COLLECTION {FilteredCollection}"),
                                                                                      _output
                                                                                      );

                await Task.Delay(TimeSpan.FromSeconds(5));

                var selectStatement  = $"SELECT * FROM `{FilteredCollection}`";
                var analyticsResult2 = await cluster.AnalyticsQueryAsync <TestRequest>(selectStatement).ConfigureAwait(false);

                var result = await analyticsResult2.ToListAsync().ConfigureAwait(false);

                Assert.True(result.Any());
            }
            catch (Exception e)
            {
                _output.WriteLine("oops{0}", e);
            }
            finally
            {
                // give some time befor the cleanups happen.
                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }