24
24
// 1.1 - Basic Create
25
25
// 1.2 - Create collection with custom IndexPolicy
26
26
//
27
- // 2. Get Offer
27
+ // 2. Get DocumentCollection performance tier
28
28
// An Offer.OfferType represents the current performance tier of a Collection
29
29
//
30
- // 3. Replace Offer
30
+ // 3. Change performance tier
31
31
// By changing the Offer.OfferType you scale the linked Collection up, or down, between performance tiers
32
32
//
33
- // 4. Delete Collection
33
+ // 4. Get a DocumentCollection by its Id property
34
34
//
35
+ // 5. List all DocumentCollection resources in a Database
36
+ //
37
+ // 6. Delete DocumentCollection
38
+ // ----------------------------------------------------------------------------------------------------------
39
+ // Note -
40
+ //
41
+ // Running this sample will create (and delete) multiple DocumentCollections on your account.
42
+ // Each time a DocumentCollection is created the account will be billed for 1 hour of usage based on
43
+ // the performance tier of that account.
35
44
// ----------------------------------------------------------------------------------------------------------
36
45
// See Also -
37
46
//
@@ -89,14 +98,13 @@ private static async Task RunCollectionDemo()
89
98
//************************************
90
99
// 1.1 - Basic Create
91
100
//************************************
92
-
93
101
DocumentCollection c1 = await client . CreateDocumentCollectionAsync ( database . SelfLink , new DocumentCollection { Id = collectionId } ) ;
94
- Console . WriteLine ( "1.1 Created Collection {0}.\n " , c1 ) ;
102
+
103
+ Console . WriteLine ( "\n 1.1. Created Collection \n {0}" , c1 ) ;
95
104
96
105
//*************************************************
97
106
// 1.2 - Create collection with custom IndexPolicy
98
107
//*************************************************
99
-
100
108
//This is just a very simple example with custome index policies
101
109
//We cover index policies in detail in IndexManagement sample project
102
110
DocumentCollection collectionSpec = new DocumentCollection
@@ -106,69 +114,68 @@ private static async Task RunCollectionDemo()
106
114
107
115
collectionSpec . IndexingPolicy . Automatic = false ;
108
116
collectionSpec . IndexingPolicy . IndexingMode = IndexingMode . Lazy ;
109
-
110
117
DocumentCollection c2 = await client . CreateDocumentCollectionAsync ( database . SelfLink , collectionSpec ) ;
111
- Console . WriteLine ( "1.2 Created Collection {0}, with custom index policy {1}.\n " , c2 . Id , c2 . IndexingPolicy ) ;
112
-
113
- //DocumentCollection have offers which are of type S1, S2, or S3. Each of these determine the performance throughput of a collection.
114
- //DocumentCollection is loosely coupled to Offer through its ResourceId (or its SelfLink)
115
118
116
- //**************
117
- // 2. Get Offer
118
- //**************
119
-
120
- //Offers are "linked" to DocumentCollection through the collection's SelfLink
121
- //Offer.ResourceLink == Collection.SelfLink
119
+ Console . WriteLine ( "1.2. Created Collection {0}, with custom index policy \n {1}" , c2 . Id , c2 . IndexingPolicy ) ;
120
+
121
+ //*********************************************************************************************
122
+ // 2. Get performance tier of a DocumentCollection
123
+ //
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
+ //**********************************************************************************************
122
130
Offer offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == c1 . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
123
- Console . WriteLine ( "2 Found Offer {0} using collection's SelfLink {1}.\n " , offer , c1 . SelfLink ) ;
124
131
125
- //*****************
126
- // 3. Replace Offer
127
- //*****************
132
+ Console . WriteLine ( "\n 2. Found Offer \n {0}\n using collection's SelfLink \n {1}" , offer , c1 . SelfLink ) ;
128
133
129
- //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),
130
- //Now let's step this collection up to an S2
131
- //To do this, change the OfferType property of the Offer to S2
132
- //NB! If you run this you will be billed for at least 1 hour @ S2 price
134
+ //******************************************************************************************************************
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
141
+ //******************************************************************************************************************
133
142
offer . OfferType = "S2" ;
134
143
Offer replaced = await client . ReplaceOfferAsync ( offer ) ;
135
- Console . WriteLine ( "3 Replaced Offer. OfferType is now {0}.\n " , replaced . OfferType ) ;
144
+
145
+ Console . WriteLine ( "\n 3. Replaced Offer. OfferType is now {0}.\n " , replaced . OfferType ) ;
136
146
137
147
//Get the offer again after replace
138
148
offer = client . CreateOfferQuery ( ) . Where ( o => o . ResourceLink == c1 . SelfLink ) . AsEnumerable ( ) . Single ( ) ;
139
- Console . WriteLine ( "2 Found Offer {0} using collection's ResourceId {1}.\n " , offer , c1 . ResourceId ) ;
140
-
141
- //**************************************
142
- //3.1 Read a feed of DocumentCollection
143
- //***************************************
149
+
150
+ Console . WriteLine ( "3. Found Offer \n {0}\n using collection's ResourceId {1}.\n " , offer , c1 . ResourceId ) ;
144
151
145
- List < DocumentCollection > cols = await ReadCollectionsFeedAsync ( database . SelfLink ) ;
146
- foreach ( var col in cols )
147
- {
148
- Console . WriteLine ( "3.1 Found Collection {0}\n " , col . Id ) ;
149
- }
152
+ //********************************************************
153
+ //4. List all DocumentCollection resources in a Database
154
+ //********************************************************
155
+ DocumentCollection collection = await client . ReadDocumentCollectionAsync ( UriFactory . CreateDocumentCollectionUri ( databaseId , collectionId ) ) ;
150
156
151
- //*********************************
152
- //3.2 Query for DocumentCollection
153
- //*********************************
157
+ Console . WriteLine ( "\n 4. Found Collection \n {0}\n " , collection ) ;
154
158
155
- //You can also query a Database for DocumentCollections.
156
- //This is useful when you're looking for a specific matching criteria. E.g. id == "SampleCollection"
157
- cols = client . CreateDocumentCollectionQuery ( database . CollectionsLink ) . Where ( coll => coll . Id == collectionId ) . ToList ( ) ;
158
- foreach ( var col in cols )
159
+ //********************************************************
160
+ //5. List all DocumentCollection resources on a Database
161
+ //********************************************************
162
+ var colls = await client . ReadDocumentCollectionFeedAsync ( UriFactory . CreateDatabaseUri ( databaseId ) ) ;
163
+ Console . WriteLine ( "\n 5. Reading all DocumentCollection resources for a database" ) ;
164
+ foreach ( var coll in colls )
159
165
{
160
- Console . WriteLine ( "3.2 Found Collection {0} \n " , col . Id ) ;
166
+ Console . WriteLine ( coll ) ;
161
167
}
162
168
163
- //********************************
164
- //4 . Delete a DocumentCollection
165
- //********************************
166
-
167
- //NB: Deleting a collection will delete everything linked to the collection.
168
- // This includes ALL documents, stored procedures, triggers, udfs
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
+ //*******************************************************************************
169
175
await client . DeleteDocumentCollectionAsync ( c1 . SelfLink ) ;
170
- Console . WriteLine ( "4 Deleted Collection {0}\n " , c1 . Id ) ;
171
-
176
+
177
+ Console . WriteLine ( "\n 6. Deleted Collection {0}\n " , c1 . Id ) ;
178
+
172
179
//Cleanup
173
180
//Delete Database.
174
181
// - will delete everything linked to the database,
@@ -177,38 +184,6 @@ private static async Task RunCollectionDemo()
177
184
await client . DeleteDatabaseAsync ( database . SelfLink ) ;
178
185
}
179
186
180
- private static async Task < List < DocumentCollection > > ReadCollectionsFeedAsync ( string databaseSelfLink )
181
- {
182
- // This method uses a ReadCollectionsFeedAsync method to read a list of all collections on a database.
183
- // It demonstrates a pattern for how to control paging and deal with continuations
184
- // This should not be needed for reading a list of collections as there are unlikely to be many hundred,
185
- // but this same pattern is introduced here and can be used on other ReadFeed methods.
186
-
187
- string continuation = null ;
188
- List < DocumentCollection > collections = new List < DocumentCollection > ( ) ;
189
-
190
- do
191
- {
192
- FeedOptions options = new FeedOptions
193
- {
194
- RequestContinuation = continuation ,
195
- MaxItemCount = 50
196
- } ;
197
-
198
- FeedResponse < DocumentCollection > response = ( FeedResponse < DocumentCollection > ) await client . ReadDocumentCollectionFeedAsync ( databaseSelfLink , options ) ;
199
-
200
- foreach ( DocumentCollection col in response )
201
- {
202
- collections . Add ( col ) ;
203
- }
204
-
205
- continuation = response . ResponseContinuation ;
206
-
207
- } while ( ! String . IsNullOrEmpty ( continuation ) ) ;
208
-
209
- return collections ;
210
- }
211
-
212
187
private static async Task < Database > GetOrCreateDatabaseAsync ( string id )
213
188
{
214
189
// Get the database by name, or create a new one if one with the name provided doesn't exist.
0 commit comments