-
Notifications
You must be signed in to change notification settings - Fork 479
/
ContainerBuilder.cs
285 lines (253 loc) · 13 KB
/
ContainerBuilder.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Fluent
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// <see cref="Container"/> fluent definition for creation flows.
/// </summary>
public class ContainerBuilder : ContainerDefinition<ContainerBuilder>
{
private readonly Database database;
private readonly CosmosClientContext clientContext;
private readonly Uri containerUri;
private UniqueKeyPolicy uniqueKeyPolicy;
private ConflictResolutionPolicy conflictResolutionPolicy;
private ChangeFeedPolicy changeFeedPolicy;
private ClientEncryptionPolicy clientEncryptionPolicy;
private VectorEmbeddingPolicy vectorEmbeddingPolicy;
/// <summary>
/// Creates an instance for unit-testing
/// </summary>
protected ContainerBuilder()
{
}
/// <summary>
/// Creates an instance of ContainerBuilder .
/// </summary>
/// <param name="database"> The Microsoft.Azure.Cosmos.Database object.</param>
/// <param name="name"> Azure Cosmos container name to create. </param>
/// <param name="partitionKeyPath"> The path to the partition key. Example: /partitionKey </param>
public ContainerBuilder(
Database database,
string name,
string partitionKeyPath)
: base(
string.IsNullOrEmpty(name) ? throw new ArgumentNullException(nameof(name)) : name,
string.IsNullOrEmpty(partitionKeyPath) ? throw new ArgumentNullException(nameof(partitionKeyPath)) : partitionKeyPath)
{
this.database = database ?? throw new ArgumentNullException(nameof(database));
this.clientContext = database.Client.ClientContext;
this.containerUri = UriFactory.CreateDocumentCollectionUri(this.database.Id, name);
}
/// <summary>
/// Defines a Unique Key policy for this Azure Cosmos container.
/// </summary>
/// <returns>An instance of <see cref="UniqueKeyDefinition"/>.</returns>
public UniqueKeyDefinition WithUniqueKey()
{
return new UniqueKeyDefinition(
this,
(uniqueKey) => this.AddUniqueKey(uniqueKey));
}
/// <summary>
/// Defined the conflict resolution for Azure Cosmos container
/// </summary>
/// <returns>An instance of <see cref="ConflictResolutionDefinition"/>.</returns>
public ConflictResolutionDefinition WithConflictResolution()
{
return new ConflictResolutionDefinition(
this,
(conflictPolicy) => this.AddConflictResolution(conflictPolicy));
}
/// <summary>
/// Defined the change feed policy for this Azure Cosmos container
/// </summary>
/// <param name="retention"> Indicates for how long operation logs have to be retained. <see cref="ChangeFeedPolicy.FullFidelityRetention"/>.</param>
/// <returns>An instance of <see cref="ChangeFeedPolicyDefinition"/>.</returns>
#if PREVIEW
public
#else
internal
#endif
ChangeFeedPolicyDefinition WithChangeFeedPolicy(TimeSpan retention)
{
return new ChangeFeedPolicyDefinition(
this,
retention,
(changeFeedPolicy) => this.AddChangeFeedPolicy(changeFeedPolicy));
}
/// <summary>
/// Defines the <see cref="ClientEncryptionPolicy"/> for Azure Cosmos container.
/// The <see cref="ClientEncryptionPolicy.PolicyFormatVersion"/> will be set to 1.
/// Note: If you need to include partition key or id field paths as part of <see cref="ClientEncryptionPolicy"/>, please set <see cref="ClientEncryptionPolicy.PolicyFormatVersion"/> to 2.
/// </summary>
/// <returns>An instance of <see cref="ClientEncryptionPolicyDefinition"/>.</returns>
public ClientEncryptionPolicyDefinition WithClientEncryptionPolicy()
{
return new ClientEncryptionPolicyDefinition(
this,
(clientEncryptionPolicy) => this.AddClientEncryptionPolicy(clientEncryptionPolicy), 1);
}
/// <summary>
/// Defines the ClientEncryptionPolicy for Azure Cosmos container
/// Note: If you need to include partition key or id field paths as part of <see cref="ClientEncryptionPolicy"/>, please set <see cref="ClientEncryptionPolicy.PolicyFormatVersion"/> to 2.
/// </summary>
/// <param name="policyFormatVersion">Version of the client encryption policy definition. Current supported versions are 1 and 2.</param>
/// <returns>An instance of <see cref="ClientEncryptionPolicyDefinition"/>.</returns>
public ClientEncryptionPolicyDefinition WithClientEncryptionPolicy(int policyFormatVersion)
{
return new ClientEncryptionPolicyDefinition(
this,
(clientEncryptionPolicy) => this.AddClientEncryptionPolicy(clientEncryptionPolicy),
policyFormatVersion);
}
/// <summary>
/// Defined the vector embedding policy for this Azure Cosmos container
/// </summary>
/// <param name="embeddings">List of vector embeddings to include in the policy definition.</param>
/// <returns>An instance of <see cref="VectorEmbeddingPolicyDefinition"/>.</returns>
internal VectorEmbeddingPolicyDefinition WithVectorEmbeddingPolicy(
Collection<Embedding> embeddings)
{
return new VectorEmbeddingPolicyDefinition(
this,
embeddings,
(embeddingPolicy) => this.AddVectorEmbeddingPolicy(embeddingPolicy));
}
/// <summary>
/// Creates a container with the current fluent definition.
/// </summary>
/// <param name="throughputProperties">Desired throughput for the container expressed in Request Units per second.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>An asynchronous Task representing the creation of a <see cref="Container"/> based on the Fluent definition.</returns>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso>
public async Task<ContainerResponse> CreateAsync(
ThroughputProperties throughputProperties,
CancellationToken cancellationToken = default)
{
ContainerProperties containerProperties = this.Build();
return await this.database.CreateContainerAsync(
containerProperties: containerProperties,
throughputProperties: throughputProperties,
cancellationToken: cancellationToken);
}
/// <summary>
/// Creates a container if it does not exist with the current fluent definition.
/// </summary>
/// <param name="throughputProperties">Desired throughput for the container expressed in Request Units per second.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>An asynchronous Task representing the creation of a <see cref="Container"/> based on the Fluent definition.</returns>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso>
public async Task<ContainerResponse> CreateIfNotExistsAsync(
ThroughputProperties throughputProperties,
CancellationToken cancellationToken = default)
{
ContainerProperties containerProperties = this.Build();
return await this.database.CreateContainerIfNotExistsAsync(
containerProperties: containerProperties,
throughputProperties: throughputProperties,
cancellationToken: cancellationToken);
}
/// <summary>
/// Creates a container with the current fluent definition.
/// </summary>
/// <param name="throughput">Desired throughput for the container expressed in Request Units per second.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>An asynchronous Task representing the creation of a <see cref="Container"/> based on the Fluent definition.</returns>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso>
public async Task<ContainerResponse> CreateAsync(
int? throughput = null,
CancellationToken cancellationToken = default)
{
ContainerProperties containerProperties = this.Build();
return await this.database.CreateContainerAsync(
containerProperties: containerProperties,
throughput: throughput,
requestOptions: null,
cancellationToken: cancellationToken);
}
/// <summary>
/// Creates a container if it does not exist with the current fluent definition.
/// </summary>
/// <param name="throughput">Desired throughput for the container expressed in Request Units per second.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>An asynchronous Task representing the creation of a <see cref="Container"/> based on the Fluent definition.</returns>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso>
public async Task<ContainerResponse> CreateIfNotExistsAsync(
int? throughput = null,
CancellationToken cancellationToken = default)
{
ContainerProperties containerProperties = this.Build();
return await this.database.CreateContainerIfNotExistsAsync(
containerProperties: containerProperties,
throughput: throughput,
requestOptions: null,
cancellationToken: cancellationToken);
}
/// <summary>
/// Applies the current Fluent definition and creates a container configuration.
/// </summary>
/// <returns>Builds the current Fluent configuration into an instance of <see cref="ContainerProperties"/>.</returns>
public new ContainerProperties Build()
{
ContainerProperties containerProperties = base.Build();
if (this.uniqueKeyPolicy != null)
{
containerProperties.UniqueKeyPolicy = this.uniqueKeyPolicy;
}
if (this.conflictResolutionPolicy != null)
{
containerProperties.ConflictResolutionPolicy = this.conflictResolutionPolicy;
}
if (this.changeFeedPolicy != null)
{
containerProperties.ChangeFeedPolicy = this.changeFeedPolicy;
}
if (this.clientEncryptionPolicy != null)
{
containerProperties.ClientEncryptionPolicy = this.clientEncryptionPolicy;
}
if (this.vectorEmbeddingPolicy != null)
{
containerProperties.VectorEmbeddingPolicy = this.vectorEmbeddingPolicy;
}
return containerProperties;
}
private void AddUniqueKey(UniqueKey uniqueKey)
{
if (this.uniqueKeyPolicy == null)
{
this.uniqueKeyPolicy = new UniqueKeyPolicy();
}
this.uniqueKeyPolicy.UniqueKeys.Add(uniqueKey);
}
private void AddConflictResolution(ConflictResolutionPolicy conflictResolutionPolicy)
{
if (conflictResolutionPolicy.Mode == ConflictResolutionMode.Custom
&& !string.IsNullOrEmpty(conflictResolutionPolicy.ResolutionProcedure))
{
this.clientContext.ValidateResource(conflictResolutionPolicy.ResolutionProcedure);
conflictResolutionPolicy.ResolutionProcedure = UriFactory.CreateStoredProcedureUri(this.containerUri.ToString(), conflictResolutionPolicy.ResolutionProcedure).ToString();
}
this.conflictResolutionPolicy = conflictResolutionPolicy;
}
private void AddChangeFeedPolicy(ChangeFeedPolicy changeFeedPolicy)
{
this.changeFeedPolicy = changeFeedPolicy;
}
private void AddClientEncryptionPolicy(ClientEncryptionPolicy clientEncryptionPolicy)
{
this.clientEncryptionPolicy = clientEncryptionPolicy;
}
private void AddVectorEmbeddingPolicy(VectorEmbeddingPolicy embeddingPolicy)
{
this.vectorEmbeddingPolicy = embeddingPolicy;
}
}
}