10
10
using System . Threading . Tasks ;
11
11
12
12
// ----------------------------------------------------------------------------------------------------------
13
- // Prerequistes -
13
+ // Prerequisites -
14
14
//
15
15
// 1. An Azure DocumentDB account -
16
16
// https://azure.microsoft.com/en-us/documentation/articles/documentdb-create-account/
24
24
// 1.1 - Basic Create
25
25
// 1.2 - Create collection with custom IndexPolicy
26
26
//
27
- // 2. Get DocumentCollection performance tier
28
- // An Offer.OfferType represents the current performance tier of a Collection
27
+ // 2. Get DocumentCollection performance (reserved throughput)
28
+ // Read the Offer object for the collection and extract OfferThroughput
29
29
//
30
- // 3. Change performance tier
31
- // By changing the Offer.OfferType you scale the linked Collection up, or down, between performance tiers
30
+ // 3. Change performance (reserved throughput)
31
+ // By changing the Offer.OfferThroughput you can scale throughput up or down
32
32
//
33
33
// 4. Get a DocumentCollection by its Id property
34
34
//
49
49
50
50
public class Program
51
51
{
52
- //Read config
53
52
private static readonly string endpointUrl = ConfigurationManager . AppSettings [ "EndPointUrl" ] ;
54
53
private static readonly string authorizationKey = ConfigurationManager . AppSettings [ "AuthorizationKey" ] ;
55
- private static readonly string databaseId = ConfigurationManager . AppSettings [ "DatabaseId" ] ;
56
- private static readonly string collectionId = ConfigurationManager . AppSettings [ "CollectionId" ] ;
54
+ private static readonly string databaseName = ConfigurationManager . AppSettings [ "DatabaseId" ] ;
55
+ private static readonly string collectionName = ConfigurationManager . AppSettings [ "CollectionId" ] ;
57
56
private static readonly ConnectionPolicy connectionPolicy = new ConnectionPolicy { UserAgentSuffix = " samples-net/3" } ;
58
57
59
- //Reusable instance of DocumentClient which represents the connection to a DocumentDB endpoint
60
58
private static DocumentClient client ;
61
-
62
- //The instance of a Database which we will be using for all the Collection operations being demo'd
63
- private static Database database ;
64
-
65
59
public static void Main ( string [ ] args )
66
60
{
67
61
try
68
62
{
69
- //Instantiate a new DocumentClient instance
70
63
using ( client = new DocumentClient ( new Uri ( endpointUrl ) , authorizationKey , connectionPolicy ) )
71
64
{
72
- //Get, or Create, a reference to Database
73
- database = GetOrCreateDatabaseAsync ( databaseId ) . Result ;
74
-
75
- //Do operations on Collections
65
+ CreateDatabaseIfNotExistsAsync ( ) . Wait ( ) ;
76
66
RunCollectionDemo ( ) . Wait ( ) ;
77
67
}
78
68
}
@@ -93,115 +83,136 @@ public static void Main(string[] args)
93
83
}
94
84
}
95
85
86
+ /// <summary>
87
+ /// Create database if it does not exist
88
+ /// </summary>
89
+ /// <returns></returns>
90
+ private static async Task CreateDatabaseIfNotExistsAsync ( )
91
+ {
92
+ try
93
+ {
94
+ await client . ReadDatabaseAsync ( UriFactory . CreateDatabaseUri ( databaseName ) ) ;
95
+ return ;
96
+ }
97
+ catch ( DocumentClientException e )
98
+ {
99
+ // If we receive an error other than database not found, fail
100
+ if ( e . StatusCode != System . Net . HttpStatusCode . NotFound )
101
+ {
102
+ throw ;
103
+ }
104
+ }
105
+
106
+ await client . CreateDatabaseAsync ( new Database { Id = databaseName } ) ;
107
+ }
108
+
109
+ /// <summary>
110
+ /// Run through basic collection access methods as a console app demo.
111
+ /// </summary>
112
+ /// <returns></returns>
96
113
private static async Task RunCollectionDemo ( )
97
- {
98
- //************************************
99
- // 1.1 - Basic Create
100
- //************************************
101
- DocumentCollection c1 = await client . CreateDocumentCollectionAsync ( database . SelfLink , new DocumentCollection { Id = collectionId } ) ;
114
+ {
115
+ DocumentCollection simpleCollection = await CreateCollection ( ) ;
102
116
103
- Console . WriteLine ( " \n 1.1. Created Collection \n {0}" , c1 ) ;
117
+ await CreateCollectionWithCustomIndexingPolicy ( ) ;
104
118
105
- //*************************************************
106
- // 1.2 - Create collection with custom IndexPolicy
107
- //*************************************************
108
- //This is just a very simple example with custome index policies
109
- //We cover index policies in detail in IndexManagement sample project
110
- DocumentCollection collectionSpec = new DocumentCollection
111
- {
112
- Id = "SampleCollectionWithCustomIndexPolicy"
113
- } ;
119
+ await GetAndChangeCollectionPerformance ( simpleCollection ) ;
120
+
121
+ await ReadCollectionProperties ( ) ;
122
+
123
+ await ListCollectionsInDatabase ( ) ;
114
124
115
- collectionSpec . IndexingPolicy . Automatic = false ;
116
- collectionSpec . IndexingPolicy . IndexingMode = IndexingMode . Lazy ;
117
- DocumentCollection c2 = await client . CreateDocumentCollectionAsync ( database . SelfLink , collectionSpec ) ;
125
+ await DeleteCollection ( ) ;
118
126
119
- Console . WriteLine ( "1.2. Created Collection {0}, with custom index policy \n {1}" , c2 . Id , c2 . IndexingPolicy ) ;
127
+ await client . DeleteDatabaseAsync ( UriFactory . CreateDatabaseUri ( databaseName ) ) ;
128
+ }
129
+ private static async Task < DocumentCollection > CreateCollection ( )
130
+ {
131
+ // Set throughput to the minimum value of 400 RU/s
132
+ DocumentCollection simpleCollection = await client . CreateDocumentCollectionAsync (
133
+ UriFactory . CreateDatabaseUri ( databaseName ) ,
134
+ new DocumentCollection { Id = collectionName } ,
135
+ new RequestOptions { OfferThroughput = 400 } ) ;
136
+
137
+ Console . WriteLine ( "\n 1.1. Created Collection \n {0}" , simpleCollection ) ;
138
+ return simpleCollection ;
139
+ }
140
+ private static async Task CreateCollectionWithCustomIndexingPolicy ( )
141
+ {
142
+ // Create a collection with custom index policy (lazy indexing)
143
+ // We cover index policies in detail in IndexManagement sample project
144
+ DocumentCollection collectionDefinition = new DocumentCollection ( ) ;
145
+
146
+ collectionDefinition . Id = "SampleCollectionWithCustomIndexPolicy" ;
147
+ collectionDefinition . IndexingPolicy . IndexingMode = IndexingMode . Lazy ;
120
148
149
+ DocumentCollection collectionWithLazyIndexing = await client . CreateDocumentCollectionAsync (
150
+ UriFactory . CreateDatabaseUri ( databaseName ) ,
151
+ collectionDefinition ,
152
+ new RequestOptions { OfferThroughput = 400 } ) ;
153
+
154
+ Console . WriteLine ( "1.2. Created Collection {0}, with custom index policy \n {1}" , collectionWithLazyIndexing . Id , collectionWithLazyIndexing . IndexingPolicy ) ;
155
+ }
156
+ private static async Task GetAndChangeCollectionPerformance ( DocumentCollection simpleCollection )
157
+ {
121
158
//*********************************************************************************************
122
- // 2. Get performance tier of a DocumentCollection
159
+ // Get configured performance (reserved throughput) of a DocumentCollection
160
+ //
161
+ // DocumentCollections each have a corresponding Offer resource that represents the reserved throughput of the collection.
162
+ // Offers are "linked" to DocumentCollection through the collection's SelfLink (Offer.ResourceLink == Collection.SelfLink)
123
163
//
124
- // DocumentCollection have offers which are of type S1, S2, or S3.
125
- // Each of these determine the performance throughput of a collection.
126
- // DocumentCollection is loosely coupled to Offer through its ResourceId (or its SelfLink)
127
- // Offers are "linked" to DocumentCollection through the collection's SelfLink
128
- // Offer.ResourceLink == Collection.SelfLink
129
164
//**********************************************************************************************
130
- Offer offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == c1 . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
165
+ Offer offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == simpleCollection . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
131
166
132
- Console . WriteLine ( "\n 2. Found Offer \n {0}\n using collection's SelfLink \n {1}" , offer , c1 . SelfLink ) ;
167
+ Console . WriteLine ( "\n 2. Found Offer \n {0}\n using collection's SelfLink \n {1}" , offer , simpleCollection . SelfLink ) ;
133
168
134
169
//******************************************************************************************************************
135
- // 3. Change performance tier of DocumentCollection
136
- // So the Offer is S1 by default (we see that b/c we never set this @ creation and it is an S1 as shown above),
137
- // Now let's step this collection up to an S2
138
- // To do this, change the OfferType property of the Offer to S2
139
- //
140
- // NB! If you run this you will be billed for 1 hour @ S2 price until we delete the DocumentCollection
170
+ // Change performance (reserved throughput) of DocumentCollection
171
+ // Let's change the performance of the collection to 500 RU/s
141
172
//******************************************************************************************************************
142
- offer . OfferType = "S2" ;
173
+ offer . OfferThroughput = 500 ;
143
174
Offer replaced = await client . ReplaceOfferAsync ( offer ) ;
144
175
145
- Console . WriteLine ( "\n 3. Replaced Offer. OfferType is now {0}.\n " , replaced . OfferType ) ;
176
+ Console . WriteLine ( "\n 3. Replaced Offer. Throughput is now {0}.\n " , replaced . OfferThroughput ) ;
146
177
147
- //Get the offer again after replace
148
- offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == c1 . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
149
-
150
- Console . WriteLine ( "3. Found Offer \n {0}\n using collection's ResourceId {1}.\n " , offer , c1 . ResourceId ) ;
178
+ // Get the offer again after replace
179
+ offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == simpleCollection . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
151
180
181
+ Console . WriteLine ( "3. Found Offer \n {0}\n using collection's ResourceId {1}.\n " , offer , simpleCollection . ResourceId ) ;
182
+ }
183
+ private static async Task ReadCollectionProperties ( )
184
+ {
152
185
//*************************************************
153
- //4. Get a DocumentCollection by its Id property
186
+ // Get a DocumentCollection by its Id property
154
187
//*************************************************
155
- DocumentCollection collection = await client . ReadDocumentCollectionAsync ( UriFactory . CreateDocumentCollectionUri ( databaseId , collectionId ) ) ;
188
+ DocumentCollection collection = await client . ReadDocumentCollectionAsync ( UriFactory . CreateDocumentCollectionUri ( databaseName , collectionName ) ) ;
156
189
157
190
Console . WriteLine ( "\n 4. Found Collection \n {0}\n " , collection ) ;
191
+ }
158
192
159
- //********************************************************
160
- //5. List all DocumentCollection resources on a Database
161
- //********************************************************
162
- var colls = await client . ReadDocumentCollectionFeedAsync ( UriFactory . CreateDatabaseUri ( databaseId ) ) ;
193
+ /// <summary>
194
+ /// List the collections within a database by calling the ReadFeed (scan) API.
195
+ /// </summary>
196
+ /// <returns></returns>
197
+ private static async Task ListCollectionsInDatabase ( )
198
+ {
163
199
Console . WriteLine ( "\n 5. Reading all DocumentCollection resources for a database" ) ;
164
- foreach ( var coll in colls )
200
+
201
+ foreach ( var collection in await client . ReadDocumentCollectionFeedAsync ( UriFactory . CreateDatabaseUri ( databaseName ) ) )
165
202
{
166
- Console . WriteLine ( coll ) ;
203
+ Console . WriteLine ( collection ) ;
167
204
}
168
-
169
- //*******************************************************************************
170
- //6. Delete a DocumentCollection
171
- //
172
- // NB! Deleting a collection will delete everything linked to the collection.
173
- // This includes ALL documents, stored procedures, triggers, udfs
174
- //*******************************************************************************
175
- await client . DeleteDocumentCollectionAsync ( UriFactory . CreateDocumentCollectionUri ( databaseId , collectionId ) ) ;
176
-
177
- Console . WriteLine ( "\n 6. Deleted Collection {0}\n " , c1 . Id ) ;
178
-
179
- //Cleanup
180
- //Delete Database.
181
- // - will delete everything linked to the database,
182
- // - we didn't really need to explictly delete the collection above
183
- // - it was just done for demonstration purposes.
184
- await client . DeleteDatabaseAsync ( database . SelfLink ) ;
185
205
}
186
206
187
- private static async Task < Database > GetOrCreateDatabaseAsync ( string id )
207
+ /// <summary>
208
+ /// Delete a collection
209
+ /// </summary>
210
+ /// <param name="simpleCollection"></param>
211
+ /// <returns></returns>
212
+ private static async Task DeleteCollection ( )
188
213
{
189
- // Get the database by name, or create a new one if one with the name provided doesn't exist.
190
- // Create a query object for database, filter by name.
191
- IEnumerable < Database > query = from db in client . CreateDatabaseQuery ( )
192
- where db . Id == id
193
- select db ;
194
-
195
- // Run the query and get the database (there should be only one) or null if the query didn't return anything.
196
- // Note: this will run synchronously. If async exectution is preferred, use IDocumentServiceQuery<T>.ExecuteNextAsync.
197
- Database database = query . FirstOrDefault ( ) ;
198
- if ( database == null )
199
- {
200
- // Create the database.
201
- database = await client . CreateDatabaseAsync ( new Database { Id = id } ) ;
202
- }
203
-
204
- return database ;
214
+ await client . DeleteDocumentCollectionAsync ( UriFactory . CreateDocumentCollectionUri ( databaseName , collectionName ) ) ;
215
+ Console . WriteLine ( "\n 6. Deleted Collection\n " ) ;
205
216
}
206
217
}
207
218
}
0 commit comments