Skip to content

Commit fc16687

Browse files
committed
remove cleanup
1 parent c311432 commit fc16687

File tree

9 files changed

+59
-243
lines changed

9 files changed

+59
-243
lines changed

samples/code-samples/ChangeFeed/Program.cs

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace DocumentDB.Samples.Queries
22
{
3-
using Shared.Util;
43
using Microsoft.Azure.Documents;
54
using Microsoft.Azure.Documents.Client;
65
using Microsoft.Azure.Documents.Linq;
@@ -9,7 +8,6 @@
98
using System;
109
using System.Collections.Generic;
1110
using System.Configuration;
12-
using System.Linq;
1311
using System.Threading.Tasks;
1412
using Newtonsoft.Json.Converters;
1513

@@ -55,8 +53,17 @@ public static void Main(string[] args)
5553

5654
private static async Task RunDemoAsync(string databaseId, string collectionId)
5755
{
58-
Database database = await GetNewDatabaseAsync(databaseId);
59-
DocumentCollection collection = await GetOrCreateCollectionAsync(databaseId, collectionId);
56+
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });
57+
58+
DocumentCollection collectionDefinition = new DocumentCollection();
59+
collectionDefinition.Id = collectionId;
60+
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
61+
collectionDefinition.PartitionKey.Paths.Add("/deviceId");
62+
63+
await client.CreateDocumentCollectionIfNotExistsAsync(
64+
UriFactory.CreateDocumentCollectionUri(databaseId, collectionId),
65+
collectionDefinition,
66+
new RequestOptions { OfferThroughput = 2500 });
6067

6168
Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
6269

@@ -151,52 +158,6 @@ private static async Task<Dictionary<string, string>> GetChanges(
151158
return checkpoints;
152159
}
153160

154-
/// <summary>
155-
/// Get a Database for this id. Delete if it already exists.
156-
/// </summary>
157-
/// <param id="id">The id of the Database to create.</param>
158-
/// <returns>The created Database object</returns>
159-
private static async Task<Database> GetNewDatabaseAsync(string id)
160-
{
161-
Database database = client.CreateDatabaseQuery().Where(c => c.Id == id).ToArray().FirstOrDefault();
162-
if (database != null)
163-
{
164-
await client.DeleteDatabaseAsync(database.SelfLink);
165-
}
166-
167-
database = await client.CreateDatabaseAsync(new Database { Id = id });
168-
return database;
169-
}
170-
171-
/// <summary>
172-
/// Get a DocuemntCollection by id, or create a new one if one with the id provided doesn't exist.
173-
/// </summary>
174-
/// <param name="id">The id of the DocumentCollection to search for, or create.</param>
175-
/// <returns>The matched, or created, DocumentCollection object</returns>
176-
private static async Task<DocumentCollection> GetOrCreateCollectionAsync(string databaseId, string collectionId)
177-
{
178-
DocumentCollection collection = client.CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(databaseId))
179-
.Where(c => c.Id == collectionId)
180-
.ToArray()
181-
.SingleOrDefault();
182-
183-
if (collection == null)
184-
{
185-
DocumentCollection collectionDefinition = new DocumentCollection();
186-
collectionDefinition.Id = collectionId;
187-
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
188-
collectionDefinition.PartitionKey.Paths.Add("/deviceId");
189-
190-
collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(
191-
client,
192-
databaseId,
193-
collectionDefinition,
194-
10100);
195-
}
196-
197-
return collection;
198-
}
199-
200161
/// <summary>
201162
/// Log exception error message to the console
202163
/// </summary>

samples/code-samples/CollectionManagement/Program.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void Main(string[] args)
6262
{
6363
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey, connectionPolicy))
6464
{
65-
CreateNewDatabaseAsync().Wait();
65+
client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName }).Wait();
6666
RunCollectionDemo().Wait();
6767
}
6868
}
@@ -77,29 +77,6 @@ public static void Main(string[] args)
7777
}
7878
}
7979

80-
/// <summary>
81-
/// Create database if it does not exist
82-
/// </summary>
83-
/// <returns></returns>
84-
private static async Task CreateNewDatabaseAsync()
85-
{
86-
try
87-
{
88-
await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
89-
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
90-
}
91-
catch (DocumentClientException e)
92-
{
93-
// If we receive an error other than database not found, fail
94-
if (e.StatusCode != System.Net.HttpStatusCode.NotFound)
95-
{
96-
throw;
97-
}
98-
}
99-
100-
await client.CreateDatabaseAsync(new Database { Id = databaseName });
101-
}
102-
10380
/// <summary>
10481
/// Run through basic collection access methods as a console app demo.
10582
/// </summary>
@@ -127,7 +104,7 @@ private static async Task RunCollectionDemo()
127104
private static async Task<DocumentCollection> CreateCollection()
128105
{
129106
// Set throughput to the minimum value of 400 RU/s
130-
DocumentCollection simpleCollection = await client.CreateDocumentCollectionAsync(
107+
DocumentCollection simpleCollection = await client.CreateDocumentCollectionIfNotExistsAsync(
131108
UriFactory.CreateDatabaseUri(databaseName),
132109
new DocumentCollection { Id = collectionName },
133110
new RequestOptions { OfferThroughput = 400 });
@@ -145,7 +122,7 @@ private static async Task<DocumentCollection> CreatePartitionedCollection()
145122
collectionDefinition.Id = "PartitionedCollection";
146123
collectionDefinition.PartitionKey.Paths.Add("/deviceId");
147124

148-
DocumentCollection partitionedCollection = await client.CreateDocumentCollectionAsync(
125+
DocumentCollection partitionedCollection = await client.CreateDocumentCollectionIfNotExistsAsync(
149126
UriFactory.CreateDatabaseUri(databaseName),
150127
collectionDefinition,
151128
new RequestOptions { OfferThroughput = 10100 });
@@ -163,7 +140,7 @@ private static async Task CreateCollectionWithCustomIndexingPolicy()
163140
collectionDefinition.Id = "SampleCollectionWithCustomIndexPolicy";
164141
collectionDefinition.IndexingPolicy.IndexingMode = IndexingMode.Lazy;
165142

166-
DocumentCollection collectionWithLazyIndexing = await client.CreateDocumentCollectionAsync(
143+
DocumentCollection collectionWithLazyIndexing = await client.CreateDocumentCollectionIfNotExistsAsync(
167144
UriFactory.CreateDatabaseUri(databaseName),
168145
collectionDefinition,
169146
new RequestOptions { OfferThroughput = 400 });
@@ -177,7 +154,7 @@ private static async Task CreateCollectionWithTtlExpiration()
177154
collectionDefinition.Id = "TtlExpiryCollection";
178155
collectionDefinition.DefaultTimeToLive = 60 * 60 * 24; //expire in 1 day
179156

180-
DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionAsync(
157+
DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionIfNotExistsAsync(
181158
UriFactory.CreateDatabaseUri(databaseName),
182159
collectionDefinition,
183160
new RequestOptions { OfferThroughput = 400 });

samples/code-samples/DatabaseManagement/Program.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void Main(string[] args)
7373
/// <returns></returns>
7474
private static async Task RunDatabaseDemo()
7575
{
76-
await CreateDatabaseIfNotExists();
76+
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName });
7777

7878
Database database = await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
7979
Console.WriteLine("\n3. Read a database resource: {0}", database);
@@ -84,25 +84,9 @@ private static async Task RunDatabaseDemo()
8484
Console.WriteLine(db);
8585
}
8686

87-
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
88-
Console.WriteLine("\n5. Database {0} deleted.", database.Id);
89-
}
90-
91-
/// <summary>
92-
/// Create a database if it doesn't exist.
93-
/// </summary>
94-
/// <returns></returns>
95-
private static async Task CreateDatabaseIfNotExists()
96-
{
97-
Database database = client.CreateDatabaseQuery().Where(db => db.Id == databaseName).AsEnumerable().FirstOrDefault();
98-
Console.WriteLine("1. Query for a database returned: {0}", database == null ? "no results" : database.Id);
99-
100-
//check if a database was returned
101-
if (database == null)
102-
{
103-
database = await client.CreateDatabaseAsync(new Database { Id = databaseName });
104-
Console.WriteLine("\n2. Created Database: id - {0}", database.Id);
105-
}
87+
// Uncomment to delete database!
88+
// await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
89+
// Console.WriteLine("\n5. Database {0} deleted.", database.Id);
10690
}
10791
}
10892
}

samples/code-samples/DocumentManagement/Program.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public static void Main(string[] args)
7979

8080
RunDocumentsDemo().Wait();
8181

82-
Cleanup();
82+
// Uncomment to delete database!
83+
// Cleanup();
8384
}
8485
}
8586
#if !DEBUG
@@ -497,10 +498,7 @@ private static void Cleanup()
497498

498499
private static async Task Initialize()
499500
{
500-
await DeleteDatabaseIfExists(databaseName);
501-
502-
await client.CreateDatabaseAsync(new Database { Id = databaseName });
503-
501+
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName });
504502

505503
// We create a partitioned collection here which needs a partition key. Partitioned collections
506504
// can be created with very high values of provisioned throughput (up to OfferThroughput = 250,000)
@@ -518,27 +516,10 @@ private static async Task Initialize()
518516
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
519517

520518
// Create with a throughput of 1000 RU/s
521-
await client.CreateDocumentCollectionAsync(
519+
await client.CreateDocumentCollectionIfNotExistsAsync(
522520
UriFactory.CreateDatabaseUri(databaseName),
523521
collectionDefinition,
524522
new RequestOptions { OfferThroughput = 1000 });
525523
}
526-
527-
private static async Task<Database> DeleteDatabaseIfExists(string databaseId)
528-
{
529-
var databaseUri = UriFactory.CreateDatabaseUri(databaseId);
530-
531-
Database database = client.CreateDatabaseQuery()
532-
.Where(db => db.Id == databaseId)
533-
.ToArray()
534-
.FirstOrDefault();
535-
536-
if (database != null)
537-
{
538-
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
539-
}
540-
541-
return database;
542-
}
543524
}
544525
}

samples/code-samples/Geospatial/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private static async Task RunDemoAsync()
9898
// Create a new collection, or modify an existing one to enable spatial indexing.
9999
DocumentCollection collection = await GetCollectionWithSpatialIndexingAsync();
100100

101-
await Cleanup();
101+
// Uncomment to delete database
102+
// await Cleanup();
102103

103104
// NOTE: In GeoJSON, longitude comes before latitude.
104105
// DocumentDB uses the WGS-84 coordinate reference standard. Longitudes are between -180 and 180 degrees, and latitudes between -90 and 90 degrees.
@@ -365,7 +366,7 @@ private static async Task<DocumentCollection> GetCollectionWithSpatialIndexingAs
365366
collection = await client.CreateDocumentCollectionAsync(
366367
UriFactory.CreateDatabaseUri(DatabaseName),
367368
collectionDefinition,
368-
new RequestOptions { OfferThroughput = 10000 });
369+
new RequestOptions { OfferThroughput = 1000 });
369370
}
370371
else
371372
{

samples/code-samples/IndexManagement/Program.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void Main(string[] args)
8282
private static async Task RunIndexDemo()
8383
{
8484
// Init
85-
var database = await GetNewDatabaseAsync(databaseId);
85+
var database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });
8686

8787
// 1. Exclude a document from the index
8888
await ExplicitlyExcludeFromIndex();
@@ -105,8 +105,8 @@ private static async Task RunIndexDemo()
105105
// 7. Perform an index transform
106106
await PerformIndexTransformations();
107107

108-
// Cleanup
109-
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
108+
// Uncomment to Cleanup
109+
// await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
110110
}
111111

112112
/// <summary>
@@ -480,23 +480,6 @@ private static bool ShowQueryIsNotAllowed(DocumentCollection collection, string
480480
return false;
481481
}
482482

483-
/// <summary>
484-
/// Get a Database for this id. Delete if it already exists.
485-
/// </summary>
486-
/// <param id="id">The id of the Database to create.</param>
487-
/// <returns>The created Database object</returns>
488-
private static async Task<Database> GetNewDatabaseAsync(string id)
489-
{
490-
Database database = client.CreateDatabaseQuery().Where(c => c.Id == id).ToArray().SingleOrDefault();
491-
if (database != null)
492-
{
493-
await client.DeleteDatabaseAsync(database.SelfLink);
494-
}
495-
496-
database = await client.CreateDatabaseAsync(new Database { Id = id });
497-
return database;
498-
}
499-
500483
/// <summary>
501484
/// Log exception error message to the console
502485
/// </summary>

samples/code-samples/Queries/Program.cs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void Main(string[] args)
6363

6464
private static async Task RunDemoAsync(string databaseId, string collectionId)
6565
{
66-
Database database = await GetNewDatabaseAsync(databaseId);
66+
Database database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });
6767
DocumentCollection collection = await GetOrCreateCollectionAsync(databaseId, collectionId);
6868

6969
Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
@@ -124,8 +124,8 @@ private static async Task RunDemoAsync(string databaseId, string collectionId)
124124
// Query using order by across multiple partitions
125125
await QueryWithOrderByForPartitionedCollectionAsync(collectionUri);
126126

127-
// Cleanup
128-
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
127+
// Uncomment to Cleanup
128+
// await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
129129
}
130130

131131
private static void QueryAllDocuments(Uri collectionUri)
@@ -961,43 +961,15 @@ private static async Task CreateDocuments(Uri collectionUri)
961961
/// <returns>The matched, or created, DocumentCollection object</returns>
962962
private static async Task<DocumentCollection> GetOrCreateCollectionAsync(string databaseId, string collectionId)
963963
{
964-
DocumentCollection collection = client.CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(databaseId))
965-
.Where(c => c.Id == collectionId)
966-
.ToArray()
967-
.SingleOrDefault();
968-
969-
if (collection == null)
970-
{
971-
DocumentCollection collectionDefinition = new DocumentCollection();
972-
collectionDefinition.Id = collectionId;
973-
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
974-
collectionDefinition.PartitionKey.Paths.Add("/LastName");
975-
976-
collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(
977-
client,
978-
databaseId,
979-
collectionDefinition,
980-
400);
981-
}
982-
983-
return collection;
984-
}
985-
986-
/// <summary>
987-
/// Get a Database for this id. Delete if it already exists.
988-
/// </summary>
989-
/// <param id="id">The id of the Database to create.</param>
990-
/// <returns>The created Database object</returns>
991-
private static async Task<Database> GetNewDatabaseAsync(string id)
992-
{
993-
Database database = client.CreateDatabaseQuery().Where(c => c.Id == id).ToArray().FirstOrDefault();
994-
if (database != null)
995-
{
996-
await client.DeleteDatabaseAsync(database.SelfLink);
997-
}
998-
999-
database = await client.CreateDatabaseAsync(new Database { Id = id });
1000-
return database;
964+
DocumentCollection collectionDefinition = new DocumentCollection();
965+
collectionDefinition.Id = collectionId;
966+
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
967+
collectionDefinition.PartitionKey.Paths.Add("/LastName");
968+
969+
return await client.CreateDocumentCollectionIfNotExistsAsync(
970+
UriFactory.CreateDatabaseUri(databaseId),
971+
collectionDefinition,
972+
new RequestOptions { OfferThroughput = 400 });
1001973
}
1002974

1003975
/// <summary>

0 commit comments

Comments
 (0)