Bulk operations

Bulk operations allow you to Upsert or Get a set of keys in parallel!

The IBucket interface provides two methods (with many overloads) for updating and retrieving documents based upon a range of id's or key's. Internally the client processes these keys in parallel taking advantage of the computer architecture of the application server and fully utilizing the resources that are available.

These methods are configurable through the overloads which take ParallelOptions object and/or the range parameters. Through these parameters you can specify the MaxDegreeOfParallism an optional CancellationToken and/or the size of the partition you wish to be processed per iteration. The overload that does not supply these, uses the defaults handled internally by the .NET framework.

Upsert: inserting or replacing a set of documents


     using (var bucket = _cluster.OpenBucket())
     {
         var items = new Dictionary<string, dynamic>
         {
             {"CouchbaseBucketTests.Test_Multi_Upsert.String", "string"},
             {"CouchbaseBucketTests.Test_Multi_Upsert.Json", new {Foo = "Bar", Baz = 2}},
             {"CouchbaseBucketTests.Test_Multi_Upsert.Int", 2},
             {"CouchbaseBucketTests.Test_Multi_Upsert.Number", 5.8},
             {"CouchbaseBucketTests.Test_Multi_Upsert.Binary", new[] {0x00, 0x00}}
         };

         var multiUpsert = bucket.Upsert(items);
         foreach (var item in multiUpsert)
         {
             Assert.IsTrue(item.Content.Success);
         }
     }
   

Get: retrieving a set of documents in parallel:


    using (var bucket = _cluster.OpenBucket())
    {
        var items = new List<string>
        {
            "CouchbaseBucketTests.Test_Multi_Upsert.String",
            "CouchbaseBucketTests.Test_Multi_Upsert.Json",
            "CouchbaseBucketTests.Test_Multi_Upsert.Int",
            "CouchbaseBucketTests.Test_Multi_Upsert.Number",
            "CouchbaseBucketTests.Test_Multi_Upsert.Binary"
        };

        var multiGet = bucket.Get<dynamic>>(items);
        foreach (var item in multiGet)
        {
            Assert.IsTrue(item.Content.Success);
        }
    }
   

Overloading the ParallelOptions and Partition Size

Here is an example of using one of the overloads that takes a ParallelOptions object and specifies a range to partition the requests into two requests per iteration utilizing a maximum of 2 cores:


     using (var bucket = _cluster.OpenBucket("beer-sample"))
     {
         var query = bucket.CreateQuery("beer", "brewery_beers");
         var results = bucket.Query<dynamic>(query);

         var keys = results.
             Rows.
             ConvertAll(x => x.id.Content).
             Cast<string>();

         var multiget = bucket.Get<dynamic>(keys.ToList(), new ParallelOptions
         {
             MaxDegreeOfParallelism = 2
         },
         2);
         Assert.AreEqual(results.TotalRows, multiget.Count);
     }
   

Note that this is a contrived example and you should use benchmarking and trial and error to determine the optimal parallel setting for your particular environment and use case.