forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBulkGet.cs
51 lines (40 loc) · 1.94 KB
/
BulkGet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Couchbase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DevGuide
{
public class BulkGet : ConnectionBase
{
public override async Task ExecuteAsync()
{
// Call BulkInsert to generate some data
await new BulkInsert().ExecuteAsync();
// Generate the keys to retrieve
var keys = Enumerable.Range(1, 100).Select(i => "dotnetDevguideExample-" + i).ToList();
// Option 1a: use Get with a IList<string> of document keys to retrieve
// Note: There is no GetAsync overload that takes multiple keys (yet)
// which is why this example wraps the synchronized method in a new Task.
// If your code is fully synchronous, you can simply call _bucket.Get(...)
var bulkResult = await Task.Run(() => _bucket.Get<Data>(keys));
var successCount = bulkResult.Where(r => r.Value.Success).Count();
Console.WriteLine("Got {0} values", bulkResult.Count);
// Option 1b: Specify ParallelOptions to customize how the client parallelizes the gets
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 32 };
var bulkResult2 = await Task.Run(() => _bucket.Get<Data>(keys, parallelOptions));
successCount = bulkResult2.Where(r => r.Value.Success).Count();
Console.WriteLine("Got {0} values", successCount);
// Option 2: Spawn multiple Upsert tasks and wait on for all to complete
var bulkTasks = keys.Select(key => _bucket.GetAsync<Data>(key));
var bulkResults = await Task.WhenAll(bulkTasks);
successCount = bulkResults.Where(r => r.Success).Count();
Console.WriteLine("Got {0} values", successCount);
}
static void Main(string[] args)
{
new BulkGet().ExecuteAsync().Wait();
}
}
}