-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
137 lines (108 loc) · 4.55 KB
/
Program.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
namespace TestConsole
{
public class Program
{
private readonly CosmosClient _client;
private readonly LoggingRequestHandler _logger = new LoggingRequestHandler();
// Entry point
static void Main()
{
var program = new Program();
program.RunTestAsync().Wait();
}
public Program()
{
var accountEndpoint = "https://localhost:8081";
var authKeyOrResourceToken = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
var clientOptions = new CosmosClientOptions();
clientOptions.CustomHandlers.Add(_logger);
_client = new CosmosClient(accountEndpoint, authKeyOrResourceToken, clientOptions);
}
public async Task RunTestAsync()
{
var container = await GetContainerAsync();
await CreateItemsAsync(container);
_logger.Enabled = true; // Start logging now
Console.WriteLine();
Console.WriteLine("---- There should only be one call after this line ----");
Console.WriteLine();
var query = new QueryDefinition($"select r from root r where r.{nameof(Example.Partition)} = @partitionKey order by r._ts")
.WithParameter("@partitionKey", Example.PartitionValue);
var feed1 = container.GetItemQueryIterator<Example>(query, requestOptions: new QueryRequestOptions
{
PartitionKey = Example.PartitionKey,
MaxItemCount = 5,
MaxBufferedItemCount = 0,
MaxConcurrency = 0
});
var firstPage = await feed1.ReadNextAsync();
Console.WriteLine($"Retrieved first page, {firstPage.Count} items.");
Console.WriteLine();
Console.WriteLine("---- There should only be one call after this line ----");
Console.WriteLine();
var feed2 = container.GetItemQueryIterator<Example>(query, continuationToken: firstPage.ContinuationToken, requestOptions: new QueryRequestOptions
{
PartitionKey = Example.PartitionKey,
MaxItemCount = 5,
MaxBufferedItemCount = 0,
MaxConcurrency = 0
});
var secondPage = await feed2.ReadNextAsync();
Console.WriteLine($"Retrieved second page, {secondPage.Count} items.");
}
private async Task<Container> GetContainerAsync()
{
const string databaseId = "FeedTest";
const string containerId = "Test";
var database = _client.GetDatabase(databaseId);
try { await database.DeleteAsync(); } catch { }
var databaseResponse = await _client.CreateDatabaseIfNotExistsAsync(databaseId);
database = databaseResponse.Database;
var containerResponse = await database.CreateContainerAsync(new ContainerProperties
{
Id = containerId,
PartitionKeyPath = $"/{nameof(Example.Partition)}"
});
return containerResponse.Container;
}
private async Task CreateItemsAsync(Container container)
{
for (var i = 0; i < 100; i++)
{
await container.CreateItemAsync(new Example { Index = i }, Example.PartitionKey);
}
}
}
public class Example
{
public const string PartitionValue = "test";
public static readonly PartitionKey PartitionKey = new PartitionKey(PartitionValue);
public string id => Guid.NewGuid().ToString();
public int Index { get; set; }
public string Partition => PartitionValue;
}
public class LoggingRequestHandler : RequestHandler
{
public bool Enabled { get; set; }
public override async Task<ResponseMessage> SendAsync(RequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
if (Enabled)
{
Console.WriteLine($"{request.Method} {request.RequestUri}");
using (var reader = new StreamReader(response.Content, Encoding.UTF8, true, 1024, true))
{
Console.WriteLine(reader.ReadToEnd());
}
Console.WriteLine();
}
return response;
}
}
}