Skip to content

Commit b0b75c1

Browse files
author
ryancrawcour
committed
enhanced comments
1 parent 914b1c6 commit b0b75c1

File tree

2 files changed

+61
-86
lines changed

2 files changed

+61
-86
lines changed

samples/code-samples/CollectionManagement/Program.cs

Lines changed: 59 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,23 @@
2424
// 1.1 - Basic Create
2525
// 1.2 - Create collection with custom IndexPolicy
2626
//
27-
// 2. Get Offer
27+
// 2. Get DocumentCollection performance tier
2828
// An Offer.OfferType represents the current performance tier of a Collection
2929
//
30-
// 3. Replace Offer
30+
// 3. Change performance tier
3131
// By changing the Offer.OfferType you scale the linked Collection up, or down, between performance tiers
3232
//
33-
// 4. Delete Collection
33+
// 4. Get a DocumentCollection by its Id property
3434
//
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.
3544
// ----------------------------------------------------------------------------------------------------------
3645
// See Also -
3746
//
@@ -89,14 +98,13 @@ private static async Task RunCollectionDemo()
8998
//************************************
9099
// 1.1 - Basic Create
91100
//************************************
92-
93101
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("\n1.1. Created Collection \n{0}", c1);
95104

96105
//*************************************************
97106
// 1.2 - Create collection with custom IndexPolicy
98107
//*************************************************
99-
100108
//This is just a very simple example with custome index policies
101109
//We cover index policies in detail in IndexManagement sample project
102110
DocumentCollection collectionSpec = new DocumentCollection
@@ -106,69 +114,68 @@ private static async Task RunCollectionDemo()
106114

107115
collectionSpec.IndexingPolicy.Automatic = false;
108116
collectionSpec.IndexingPolicy.IndexingMode = IndexingMode.Lazy;
109-
110117
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)
115118

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+
//**********************************************************************************************
122130
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);
124131

125-
//*****************
126-
// 3. Replace Offer
127-
//*****************
132+
Console.WriteLine("\n2. Found Offer \n{0}\nusing collection's SelfLink \n{1}", offer, c1.SelfLink);
128133

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+
//******************************************************************************************************************
133142
offer.OfferType = "S2";
134143
Offer replaced = await client.ReplaceOfferAsync(offer);
135-
Console.WriteLine("3 Replaced Offer. OfferType is now {0}.\n", replaced.OfferType);
144+
145+
Console.WriteLine("\n3. Replaced Offer. OfferType is now {0}.\n", replaced.OfferType);
136146

137147
//Get the offer again after replace
138148
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}\nusing collection's ResourceId {1}.\n", offer, c1.ResourceId);
144151

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));
150156

151-
//*********************************
152-
//3.2 Query for DocumentCollection
153-
//*********************************
157+
Console.WriteLine("\n4. Found Collection \n{0}\n", collection);
154158

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("\n5. Reading all DocumentCollection resources for a database");
164+
foreach (var coll in colls)
159165
{
160-
Console.WriteLine("3.2 Found Collection {0}\n", col.Id);
166+
Console.WriteLine(coll);
161167
}
162168

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+
//*******************************************************************************
169175
await client.DeleteDocumentCollectionAsync(c1.SelfLink);
170-
Console.WriteLine("4 Deleted Collection {0}\n", c1.Id);
171-
176+
177+
Console.WriteLine("\n6. Deleted Collection {0}\n", c1.Id);
178+
172179
//Cleanup
173180
//Delete Database.
174181
// - will delete everything linked to the database,
@@ -177,38 +184,6 @@ private static async Task RunCollectionDemo()
177184
await client.DeleteDatabaseAsync(database.SelfLink);
178185
}
179186

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-
212187
private static async Task<Database> GetOrCreateDatabaseAsync(string id)
213188
{
214189
// Get the database by name, or create a new one if one with the name provided doesn't exist.

samples/code-samples/DatabaseManagement/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void Main(string[] args)
4949
//Setup a single instance of DocumentClient that is reused throughout the application
5050
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
5151
{
52-
DatabaseManagementAsync().Wait();
52+
RunDatabaseDemo().Wait();
5353
}
5454
}
5555
catch (DocumentClientException de)
@@ -69,7 +69,7 @@ public static void Main(string[] args)
6969
}
7070
}
7171

72-
private static async Task DatabaseManagementAsync()
72+
private static async Task RunDatabaseDemo()
7373
{
7474
//********************************************************************************************************
7575
// 1 - Query for a Database

0 commit comments

Comments
 (0)