Skip to content

Commit e61770f

Browse files
committed
Partitioned collections changes
1 parent 4fce494 commit e61770f

32 files changed

+2064
-842
lines changed

samples/code-samples/.vs/config/applicationhost.config

Lines changed: 1030 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
55
</startup>
66
<appSettings file="appSettings.config"/>
7-
</configuration>
7+
</configuration>

samples/code-samples/CollectionManagement/CollectionManagement.csproj

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>DocumentDB.Samples.CollectionManagement</RootNamespace>
1111
<AssemblyName>DocumentDB.Samples.CollectionManagement</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14-
<SccProjectName>SAK</SccProjectName>
15-
<SccLocalPath>SAK</SccLocalPath>
16-
<SccAuxPath>SAK</SccAuxPath>
17-
<SccProvider>SAK</SccProvider>
14+
<SccProjectName>
15+
</SccProjectName>
16+
<SccLocalPath>
17+
</SccLocalPath>
18+
<SccAuxPath>
19+
</SccAuxPath>
20+
<SccProvider>
21+
</SccProvider>
1822
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
23+
<TargetFrameworkProfile />
1924
</PropertyGroup>
2025
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2126
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -37,13 +42,12 @@
3742
<WarningLevel>4</WarningLevel>
3843
</PropertyGroup>
3944
<ItemGroup>
40-
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Microsoft.Azure.DocumentDB.1.5.1\lib\net40\Microsoft.Azure.Documents.Client.dll</HintPath>
42-
<Private>True</Private>
45+
<Reference Include="Microsoft.Azure.Documents.Client">
46+
<HintPath>..\..\..\..\..\Downloads\DocumentDBTrial\DocumentDBTrial\DocumentDBTrial\bin\Debug\Microsoft.Azure.Documents.Client.dll</HintPath>
4347
</Reference>
4448
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
45-
<HintPath>..\packages\Newtonsoft.Json.5.0.7\lib\net45\Newtonsoft.Json.dll</HintPath>
46-
<Private>True</Private>
49+
<SpecificVersion>False</SpecificVersion>
50+
<HintPath>..\..\..\..\..\Downloads\DocumentDBTrial\DocumentDBTrial\DocumentDBTrial\bin\Debug\Newtonsoft.Json.dll</HintPath>
4751
</Reference>
4852
<Reference Include="System" />
4953
<Reference Include="System.Configuration" />
@@ -56,16 +60,19 @@
5660
</ItemGroup>
5761
<ItemGroup>
5862
<Compile Include="Program.cs" />
59-
<Compile Include="Properties\AssemblyInfo.cs" />
6063
</ItemGroup>
6164
<ItemGroup>
6265
<None Include="..\appSettings.config">
6366
<Link>appSettings.config</Link>
6467
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
68+
<SubType>Designer</SubType>
6569
</None>
6670
<None Include="App.config" />
6771
<None Include="packages.config" />
6872
</ItemGroup>
73+
<ItemGroup>
74+
<Folder Include="Properties\" />
75+
</ItemGroup>
6976
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7077
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7178
Other similar extension points exist, see Microsoft.Common.targets.

samples/code-samples/CollectionManagement/Program.cs

Lines changed: 110 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using System.Threading.Tasks;
1111

1212
// ----------------------------------------------------------------------------------------------------------
13-
// Prerequistes -
13+
// Prerequisites -
1414
//
1515
// 1. An Azure DocumentDB account -
1616
// https://azure.microsoft.com/en-us/documentation/articles/documentdb-create-account/
@@ -24,11 +24,11 @@
2424
// 1.1 - Basic Create
2525
// 1.2 - Create collection with custom IndexPolicy
2626
//
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
2929
//
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
3232
//
3333
// 4. Get a DocumentCollection by its Id property
3434
//
@@ -49,30 +49,20 @@
4949

5050
public class Program
5151
{
52-
//Read config
5352
private static readonly string endpointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
5453
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"];
5756
private static readonly ConnectionPolicy connectionPolicy = new ConnectionPolicy { UserAgentSuffix = " samples-net/3" };
5857

59-
//Reusable instance of DocumentClient which represents the connection to a DocumentDB endpoint
6058
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-
6559
public static void Main(string[] args)
6660
{
6761
try
6862
{
69-
//Instantiate a new DocumentClient instance
7063
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey, connectionPolicy))
7164
{
72-
//Get, or Create, a reference to Database
73-
database = GetOrCreateDatabaseAsync(databaseId).Result;
74-
75-
//Do operations on Collections
65+
CreateDatabaseIfNotExistsAsync().Wait();
7666
RunCollectionDemo().Wait();
7767
}
7868
}
@@ -93,115 +83,136 @@ public static void Main(string[] args)
9383
}
9484
}
9585

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>
96113
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();
102116

103-
Console.WriteLine("\n1.1. Created Collection \n{0}", c1);
117+
await CreateCollectionWithCustomIndexingPolicy();
104118

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();
114124

115-
collectionSpec.IndexingPolicy.Automatic = false;
116-
collectionSpec.IndexingPolicy.IndexingMode = IndexingMode.Lazy;
117-
DocumentCollection c2 = await client.CreateDocumentCollectionAsync(database.SelfLink, collectionSpec );
125+
await DeleteCollection();
118126

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("\n1.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;
120148

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+
{
121158
//*********************************************************************************************
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)
123163
//
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
129164
//**********************************************************************************************
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();
131166

132-
Console.WriteLine("\n2. Found Offer \n{0}\nusing collection's SelfLink \n{1}", offer, c1.SelfLink);
167+
Console.WriteLine("\n2. Found Offer \n{0}\nusing collection's SelfLink \n{1}", offer, simpleCollection.SelfLink);
133168

134169
//******************************************************************************************************************
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
141172
//******************************************************************************************************************
142-
offer.OfferType = "S2";
173+
offer.OfferThroughput = 500;
143174
Offer replaced = await client.ReplaceOfferAsync(offer);
144175

145-
Console.WriteLine("\n3. Replaced Offer. OfferType is now {0}.\n", replaced.OfferType);
176+
Console.WriteLine("\n3. Replaced Offer. Throughput is now {0}.\n", replaced.OfferThroughput);
146177

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}\nusing 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();
151180

181+
Console.WriteLine("3. Found Offer \n{0}\nusing collection's ResourceId {1}.\n", offer, simpleCollection.ResourceId);
182+
}
183+
private static async Task ReadCollectionProperties()
184+
{
152185
//*************************************************
153-
//4. Get a DocumentCollection by its Id property
186+
// Get a DocumentCollection by its Id property
154187
//*************************************************
155-
DocumentCollection collection = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId));
188+
DocumentCollection collection = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
156189

157190
Console.WriteLine("\n4. Found Collection \n{0}\n", collection);
191+
}
158192

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+
{
163199
Console.WriteLine("\n5. 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)))
165202
{
166-
Console.WriteLine(coll);
203+
Console.WriteLine(collection);
167204
}
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("\n6. 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);
185205
}
186206

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()
188213
{
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("\n6. Deleted Collection\n");
205216
}
206217
}
207218
}

0 commit comments

Comments
 (0)