示例#1
0
        public async Task Flush()
        {
            var indexQuery = $"select * from system:indexes where keyspace_id={CbHelper.Literal(bucket.Name)}";

            // Flush and verify that the primary index was created by default.

            couchbase.Clear();

            var indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Single(indexes);

            var index = (JObject)indexes.First().GetValue("indexes");

            Assert.True((bool)index.GetValue("is_primary"));
            Assert.Equal("idx_primary", (string)index.GetValue("name"));

            // Write some data, verify that it was written then flush
            // the bucket and verify that the data is gone.

            await bucket.UpsertSafeAsync("hello", "world!");

            Assert.Equal("world!", await bucket.GetSafeAsync <string>("hello"));

            couchbase.Clear();
            Assert.Null(await bucket.FindSafeAsync <string>("hello"));

            // Create a secondary index and verify.

            await bucket.QuerySafeAsync <dynamic>($"create index idx_foo on {CbHelper.LiteralName(bucket.Name)} ( {CbHelper.LiteralName("Test")} )");

            indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Equal(2, indexes.Count);     // Expecting the primary and new secondary index

            // Clear the database and then verify that only the
            // recreated primary index exists.

            couchbase.Clear();

            indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Single(indexes);

            index = (JObject)indexes.First().GetValue("indexes");

            Assert.True((bool)index.GetValue("is_primary"));
            Assert.Equal("idx_primary", (string)index.GetValue("name"));
        }
示例#2
0
        public async Task Query()
        {
            // Ensure that the database starts out empty.

            couchbase.Clear();

            await Assert.ThrowsAsync <CouchbaseKeyValueResponseException>(async() => await bucket.GetSafeAsync <string>("jack"));

            await Assert.ThrowsAsync <CouchbaseKeyValueResponseException>(async() => await bucket.GetSafeAsync <string>("jill"));

            // Write a couple documents and then query for them.  Note
            // that we're explicitly using [RequestPlus] consistency
            // and we're doing a synchronous query.

            await bucket.UpsertSafeAsync("jack", new TestDoc()
            {
                Name = "Jack", Age = 11
            });

            await bucket.UpsertSafeAsync("jill", new TestDoc()
            {
                Name = "Jill", Age = 12
            });

            var context = new BucketContext(bucket);

            // Note that we're explicitly using [RequestPlus] consistency
            // and we're doing a synchronous query.

            var query =
                from doc in context.Query <TestDoc>()
                .ScanConsistency(Couchbase.N1QL.ScanConsistency.RequestPlus)
                select doc;

            var results = query.ToList();

            Assert.Equal(2, results.Count);
            Assert.Single(results.Where(doc => doc.Name == "Jack"));
            Assert.Single(results.Where(doc => doc.Name == "Jill"));

            // Use Couchbase [IQueryable<T>.ExecuteAsync()] to execute the query
            // and enable result streaming from the server.

            query =
                from doc in context.Query <TestDoc>()
                .ScanConsistency(Couchbase.N1QL.ScanConsistency.RequestPlus)
                .UseStreaming(true)
                select doc;

            await query.ExecuteAsync();

            results = query.ToList();

            Assert.Equal(2, results.Count);
            Assert.Single(results.Where(doc => doc.Name == "Jack"));
            Assert.Single(results.Where(doc => doc.Name == "Jill"));

            // Do a string based query using the [QueryAsync()] extension
            // method which throws an exception on errors.  Note that this doesn't
            // appear to be compatible with streaming (Rows is NULL).

            var queryRequest = new QueryRequest($"select {bucket.Name}.* from {bucket.Name}")
                               .ScanConsistency(ScanConsistency.RequestPlus);

            var queryResult = await bucket.QueryAsync <TestDoc>(queryRequest);

            var rows = queryResult.Rows;

            Assert.Equal(2, rows.Count);
            Assert.Single(rows.Where(doc => doc.Name == "Jack"));
            Assert.Single(rows.Where(doc => doc.Name == "Jill"));

            // Do a string based query using the [QuerySafeAsync()] extension
            // method which throws an exception on errors.  Note that this doesn't
            // appear to be compatible with streaming (the result is NULL).

            queryRequest = new QueryRequest($"select {bucket.Name}.* from {bucket.Name}")
                           .ScanConsistency(ScanConsistency.RequestPlus);

            results = await bucket.QuerySafeAsync <TestDoc>(queryRequest);

            Assert.Equal(2, results.Count);
            Assert.Single(results.Where(doc => doc.Name == "Jack"));
            Assert.Single(results.Where(doc => doc.Name == "Jill"));
        }
示例#3
0
        public async Task Clear()
        {
            var indexQuery = $"select * from system:indexes where keyspace_id={CouchbaseHelper.Literal(bucket.Name)}";

            // Flush and verify that the primary index was created by default.

            couchbase.Clear();

            var indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Single(indexes);

            var index = (JObject)indexes.First().GetValue("indexes");

            Assert.True((bool)index.GetValue("is_primary"));
            Assert.Equal("#primary", (string)index.GetValue("name"));

            // Write some data, verify that it was written then flush
            // the bucket and verify that the data is gone.

            await bucket.UpsertSafeAsync("hello", "world!");

            Assert.Equal("world!", await bucket.GetSafeAsync <string>("hello"));

            couchbase.Clear();
            Assert.Null(await bucket.FindSafeAsync <string>("hello"));

            // Create a secondary index and verify.

            await bucket.QuerySafeAsync <dynamic>($"create index idx_foo on {CouchbaseHelper.LiteralName(bucket.Name)} ( {CouchbaseHelper.LiteralName("Test")} )");

            indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Equal(2, indexes.Count);     // Expecting the primary and new secondary index

            // Clear the database and then verify that only the
            // recreated primary index exists.

            couchbase.Clear();

            indexes = await bucket.QuerySafeAsync <JObject>(indexQuery);

            Assert.Single(indexes);

            index = (JObject)indexes.First().GetValue("indexes");

            Assert.True((bool)index.GetValue("is_primary"));
            Assert.Equal("#primary", (string)index.GetValue("name"));

            // Create a fts index and verify.

            var ftsIndex = new Dictionary <string, object>();

            ftsIndex.Add("type", "fulltext-index");
            ftsIndex.Add("name", "test");
            ftsIndex.Add("sourceType", "couchbase");
            ftsIndex.Add("sourceName", "test");

            await jsonClient.PutAsync("/api/index/test", JsonConvert.SerializeObject(ftsIndex));

            ftsIndex = new Dictionary <string, object>();

            ftsIndex.Add("type", "fulltext-index");
            ftsIndex.Add("name", "test123");
            ftsIndex.Add("sourceType", "couchbase");
            ftsIndex.Add("sourceName", "test");

            await jsonClient.PutAsync("/api/index/test123", JsonConvert.SerializeObject(ftsIndex));

            var ftsIndexes = jsonClient.GetAsync <dynamic>("/api/index").Result.indexDefs;

            Assert.True(((JObject)ftsIndexes.indexDefs).Count == 2);

            // Clear the database and then verify that only the
            // recreated primary index exists.

            couchbase.Clear();

            ftsIndexes = jsonClient.GetAsync <dynamic>("/api/index").Result.indexDefs;

            Assert.True(((JObject)ftsIndexes.indexDefs).Count == 0);
        }
示例#4
0
        public async Task LinesUuid()
        {
            // Verify that we can import a file with one JSON object per line,
            // generating a UUID key for each.

            using (var folder = new TempFolder())
            {
                var jsonFile = Path.Combine(folder.Path, "lines.json");

                File.WriteAllText(jsonFile,
                                  @"{ ""name"": ""jack"", ""age"": 10 }
{ ""name"": ""jill"", ""age"": 11 }
{ ""name"": ""spot"", ""age"": 2 }
");
                var playbook =
                    $@"
- name: test
  hosts: localhost
  tasks:
    - name: import
      neon_couchbase_import:
        servers:
          - {couchbase.Settings.Servers.First().Host}
        bucket: {bucket.Name}
        username: {couchbase.Username}
        password: {couchbase.Password}
        source: {Path.GetFileName(jsonFile)}
        format: json-lines
";
                var results    = AnsiblePlayer.PlayInFolderNoGather(folder.Path, playbook);
                var taskResult = results.GetTaskResult("import");

                Assert.True(taskResult.Success);
                Assert.True(taskResult.Changed);

                // Verify

                var query = new QueryRequest($"select meta({bucket.Name}).id, {bucket.Name}.* from {bucket.Name}")
                            .ScanConsistency(ScanConsistency.RequestPlus);

                var items = await bucket.QuerySafeAsync <dynamic>(query);

                // Verify that we have all of documents.

                Assert.Equal(3, items.Count);
                Assert.Single(items.Where(doc => doc.name == "jack"));
                Assert.Single(items.Where(doc => doc.name == "jill"));
                Assert.Single(items.Where(doc => doc.name == "spot"));

                // Verify that all of the IDs look like UUIDs.  We're
                // going to assume that all entity UUIDs have the same
                // length for this test.

                var keyLength = EntityHelper.CreateUuid().Length;

                Assert.Equal(3, items.Count(doc => ((string)doc.id).Length == keyLength));
            }
        }